|
@@ -11,6 +11,7 @@ import org.springframework.context.ApplicationContext;
|
|
import org.springframework.context.ApplicationContextAware;
|
|
import org.springframework.context.ApplicationContextAware;
|
|
import org.springframework.util.Assert;
|
|
import org.springframework.util.Assert;
|
|
|
|
|
|
|
|
+import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
@@ -26,31 +27,31 @@ public abstract class AbstractStorageService implements StorageService, Applicat
|
|
|
|
|
|
private Map<String, Strategy> map;
|
|
private Map<String, Strategy> map;
|
|
|
|
|
|
- public abstract List<Map> select(String collectionId, Query query) throws IOException;
|
|
|
|
|
|
+ public abstract List<Map> select(Query query) throws IOException;
|
|
|
|
|
|
- public abstract void insert(String collectionId, Map params) throws IOException;
|
|
|
|
|
|
+ public abstract void insert(StorageEnum type, String collection, Map params) throws IOException;
|
|
|
|
|
|
- public abstract void update(String collectionId, Map params) throws IOException;
|
|
|
|
|
|
+ public abstract void update(StorageEnum type, String collection, Map params) throws IOException;
|
|
|
|
|
|
- public abstract void delete(String collectionId, String id) throws IOException;
|
|
|
|
|
|
+ public abstract void delete(StorageEnum type, String collection, String id) throws IOException;
|
|
|
|
|
|
- public abstract void deleteAll(String collectionId) throws IOException;
|
|
|
|
|
|
+ public abstract void deleteAll(StorageEnum type, String collection) throws IOException;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 记录日志
|
|
* 记录日志
|
|
*
|
|
*
|
|
- * @param collectionId
|
|
|
|
|
|
+ * @param collection
|
|
* @param params
|
|
* @param params
|
|
*/
|
|
*/
|
|
- public abstract void insertLog(String collectionId, Map<String, Object> params) throws IOException;
|
|
|
|
|
|
+ public abstract void insertLog(StorageEnum type, String collection, Map<String, Object> params) throws IOException;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 记录错误数据
|
|
* 记录错误数据
|
|
*
|
|
*
|
|
- * @param collectionId
|
|
|
|
|
|
+ * @param collection
|
|
* @param list
|
|
* @param list
|
|
*/
|
|
*/
|
|
- public abstract void insertData(String collectionId, List<Map> list) throws IOException;
|
|
|
|
|
|
+ public abstract void insertData(StorageEnum type, String collection, List<Map> list) throws IOException;
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
|
@@ -58,17 +59,13 @@ public abstract class AbstractStorageService implements StorageService, Applicat
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public List<Map> query(StorageEnum type, Query query) {
|
|
|
|
- return query(type, query, null);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public List<Map> query(StorageEnum type, Query query, String collectionId) {
|
|
|
|
|
|
+ public List<Map> query(Query query) {
|
|
try {
|
|
try {
|
|
- collectionId = getCollectionId(type, collectionId);
|
|
|
|
- return select(collectionId, query);
|
|
|
|
|
|
+ String collection = getCollection(query.getType(), query.getCollection());
|
|
|
|
+ query.setCollection(collection);
|
|
|
|
+ return select(query);
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
- logger.error("query collectionId:{}, query:{}, failed:{}", collectionId, query, e.getMessage());
|
|
|
|
|
|
+ logger.error("query collectionId:{}, params:{}, failed:{}", query.getCollection(), query.getParams(), e.getMessage());
|
|
throw new StorageException(e);
|
|
throw new StorageException(e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -83,7 +80,8 @@ public abstract class AbstractStorageService implements StorageService, Applicat
|
|
Assert.notNull(params, "Params can not be null.");
|
|
Assert.notNull(params, "Params can not be null.");
|
|
logger.debug("collectionId:{}, params:{}", collectionId, params);
|
|
logger.debug("collectionId:{}, params:{}", collectionId, params);
|
|
try {
|
|
try {
|
|
- insert(getCollectionId(type, collectionId), params);
|
|
|
|
|
|
+ String collection = getCollection(type, collectionId);
|
|
|
|
+ insert(type, collection, params);
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
logger.error("add collectionId:{}, params:{}, failed:{}", collectionId, params, e.getMessage());
|
|
logger.error("add collectionId:{}, params:{}, failed:{}", collectionId, params, e.getMessage());
|
|
throw new StorageException(e);
|
|
throw new StorageException(e);
|
|
@@ -100,7 +98,8 @@ public abstract class AbstractStorageService implements StorageService, Applicat
|
|
Assert.notNull(params, "Params can not be null.");
|
|
Assert.notNull(params, "Params can not be null.");
|
|
logger.debug("collectionId:{}, params:{}", collectionId, params);
|
|
logger.debug("collectionId:{}, params:{}", collectionId, params);
|
|
try {
|
|
try {
|
|
- update(getCollectionId(type, collectionId), params);
|
|
|
|
|
|
+ String collection = getCollection(type, collectionId);
|
|
|
|
+ update(type, collection, params);
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
logger.error("edit collectionId:{}, params:{}, failed:{}", collectionId, params, e.getMessage());
|
|
logger.error("edit collectionId:{}, params:{}, failed:{}", collectionId, params, e.getMessage());
|
|
throw new StorageException(e);
|
|
throw new StorageException(e);
|
|
@@ -117,7 +116,8 @@ public abstract class AbstractStorageService implements StorageService, Applicat
|
|
Assert.hasText(id, "ID can not be null.");
|
|
Assert.hasText(id, "ID can not be null.");
|
|
logger.debug("collectionId:{}, id:{}", collectionId, id);
|
|
logger.debug("collectionId:{}, id:{}", collectionId, id);
|
|
try {
|
|
try {
|
|
- delete(getCollectionId(type, collectionId), id);
|
|
|
|
|
|
+ String collection = getCollection(type, collectionId);
|
|
|
|
+ delete(type, collection, id);
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
logger.error("remove collectionId:{}, id:{}, failed:{}", collectionId, id, e.getMessage());
|
|
logger.error("remove collectionId:{}, id:{}, failed:{}", collectionId, id, e.getMessage());
|
|
throw new StorageException(e);
|
|
throw new StorageException(e);
|
|
@@ -127,7 +127,8 @@ public abstract class AbstractStorageService implements StorageService, Applicat
|
|
@Override
|
|
@Override
|
|
public void addLog(StorageEnum type, Map<String, Object> params) {
|
|
public void addLog(StorageEnum type, Map<String, Object> params) {
|
|
try {
|
|
try {
|
|
- insertLog(getCollectionId(type, null), params);
|
|
|
|
|
|
+ String collection = getCollection(type, null);
|
|
|
|
+ insertLog(type, collection, params);
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
logger.error(e.getMessage());
|
|
logger.error(e.getMessage());
|
|
throw new StorageException(e);
|
|
throw new StorageException(e);
|
|
@@ -137,7 +138,8 @@ public abstract class AbstractStorageService implements StorageService, Applicat
|
|
@Override
|
|
@Override
|
|
public void addData(StorageEnum type, String collectionId, List<Map> list) {
|
|
public void addData(StorageEnum type, String collectionId, List<Map> list) {
|
|
try {
|
|
try {
|
|
- insertData(getCollectionId(type, collectionId), list);
|
|
|
|
|
|
+ String collection = getCollection(type, collectionId);
|
|
|
|
+ insertData(type, collection, list);
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
logger.error(e.getMessage());
|
|
logger.error(e.getMessage());
|
|
throw new StorageException(e);
|
|
throw new StorageException(e);
|
|
@@ -147,18 +149,26 @@ public abstract class AbstractStorageService implements StorageService, Applicat
|
|
@Override
|
|
@Override
|
|
public void clear(StorageEnum type, String collectionId) {
|
|
public void clear(StorageEnum type, String collectionId) {
|
|
try {
|
|
try {
|
|
- deleteAll(getCollectionId(type, collectionId));
|
|
|
|
|
|
+ String collection = getCollection(type, collectionId);
|
|
|
|
+ deleteAll(type, collection);
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
logger.error("clear collectionId:{}, failed:{}", collectionId, e.getMessage());
|
|
logger.error("clear collectionId:{}, failed:{}", collectionId, e.getMessage());
|
|
throw new StorageException(e);
|
|
throw new StorageException(e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- private String getCollectionId(StorageEnum type, String collectionId) {
|
|
|
|
- Assert.notNull(type, "StorageEnum can not be null.");
|
|
|
|
- Strategy strategy = map.get(type.getType().concat("Strategy"));
|
|
|
|
|
|
+ protected String getSeparator(){
|
|
|
|
+ return File.separator;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private String getCollection(StorageEnum type, String collection) {
|
|
|
|
+ Assert.notNull(type, "StorageEnum type can not be null.");
|
|
|
|
+ return getCollection(type.getType(), collection);
|
|
|
|
+ }
|
|
|
|
+ private String getCollection(String type, String collection) {
|
|
|
|
+ Strategy strategy = map.get(type.concat("Strategy"));
|
|
Assert.notNull(strategy, "Strategy does not exist.");
|
|
Assert.notNull(strategy, "Strategy does not exist.");
|
|
- return strategy.createCollectionId(collectionId);
|
|
|
|
|
|
+ return strategy.createCollectionId(getSeparator(), collection);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|