|
|
@@ -22,211 +22,222 @@ import java.util.Set;
|
|
|
|
|
|
/**
|
|
|
* 模版
|
|
|
- *
|
|
|
+ *
|
|
|
* @author sunyj
|
|
|
* @since 2017年9月2日 下午6:54:58
|
|
|
*/
|
|
|
@Service
|
|
|
public class TemplateServiceImpl extends BaseService<Template> implements TemplateService {
|
|
|
|
|
|
- @Autowired
|
|
|
- private TemplateDao templateDao;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private DataSourceDao dataSourceDao;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private GlobalParameterDao globalParameterDao;
|
|
|
-
|
|
|
- @Autowired
|
|
|
- private ResourcePointDao resourePointDao;
|
|
|
-
|
|
|
- @Override
|
|
|
- public Template save(@NotEmpty("json") String json) {
|
|
|
- Template template = templateDao.parse(json);
|
|
|
- checkDataSource(template.getDataSourceCode());
|
|
|
- checkGlobalParameters(template.getGlobalParameterCodes());
|
|
|
- // 保存时,不允许指定参数的 code
|
|
|
- List<TemplateParameter> parameters = template.getParameters();
|
|
|
- if (!CollectionUtils.isEmpty(parameters)) {
|
|
|
- for (TemplateParameter parameter : parameters) {
|
|
|
- parameter.init();
|
|
|
- }
|
|
|
- }
|
|
|
- return templateDao.save(template);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Template savePart(String json) {
|
|
|
- Template template = templateDao.parse(json);
|
|
|
- // 可以不指定数据源
|
|
|
- String dataSourceCode = template.getDataSourceCode();
|
|
|
- if (!StringUtils.isEmpty(dataSourceCode)) {
|
|
|
- checkDataSource(template.getDataSourceCode());
|
|
|
- }
|
|
|
- checkGlobalParameters(template.getGlobalParameterCodes());
|
|
|
- // 保存时,不允许指定参数的 code
|
|
|
- List<TemplateParameter> parameters = template.getParameters();
|
|
|
- if (!CollectionUtils.isEmpty(parameters)) {
|
|
|
- for (TemplateParameter parameter : parameters) {
|
|
|
- parameter.init();
|
|
|
- }
|
|
|
- }
|
|
|
- return templateDao.savePart(template);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public int update(@NotEmpty("json") String json) throws IllegalArgumentException, OperationException {
|
|
|
- Template template = templateDao.parse(json);
|
|
|
- String code = template.codeNotEmpty();
|
|
|
- checkDataSource(template.getDataSourceCode());
|
|
|
- checkGlobalParameters(template.getGlobalParameterCodes());
|
|
|
- List<TemplateParameter> parameters = template.getParameters();
|
|
|
- if (!CollectionUtils.isEmpty(parameters)) {
|
|
|
- throw new OperationException("请单独更新模版参数");
|
|
|
- }
|
|
|
- Set<String> ignoreFields = new HashSet<>();
|
|
|
- // 不更新模版参数
|
|
|
- ignoreFields.add("parameters");
|
|
|
- UpdateOperations<Template> operations = templateDao.createUpdateOperations(template, ignoreFields);
|
|
|
- return templateDao.update(code, operations);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public int updatePart(@NotEmpty("json") String json) throws IllegalArgumentException, OperationException {
|
|
|
- Template template = templateDao.parse(json);
|
|
|
- // 可以不指定数据源
|
|
|
- String dataSourceCode = template.getDataSourceCode();
|
|
|
- if (!StringUtils.isEmpty(dataSourceCode)) {
|
|
|
- checkDataSource(template.getDataSourceCode());
|
|
|
- }
|
|
|
- checkGlobalParameters(template.getGlobalParameterCodes());
|
|
|
- List<TemplateParameter> parameters = template.getParameters();
|
|
|
- if (!CollectionUtils.isEmpty(parameters)) {
|
|
|
- throw new OperationException("请单独更新模版参数");
|
|
|
- }
|
|
|
- return templateDao.updatePart(json);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 检查数据源是否存在
|
|
|
- *
|
|
|
- * @param dataSourceCode
|
|
|
- * 数据源的 code
|
|
|
- * @throws IllegalArgumentException
|
|
|
- * 数据源不存在
|
|
|
- */
|
|
|
- private void checkDataSource(@NotEmpty("dataSourceCode") String dataSourceCode) throws IllegalArgumentException {
|
|
|
- DataSource dataSource = dataSourceDao.findOne(dataSourceCode);
|
|
|
- if (dataSource == null) {
|
|
|
- throw new IllegalArgumentException("数据源不存在:" + dataSourceCode);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 检查公共参数是否存在
|
|
|
- *
|
|
|
- * @param globalParameterCodes
|
|
|
- * 公共参数的 code
|
|
|
- * @throws IllegalArgumentException
|
|
|
- * 公共参数不存在
|
|
|
- */
|
|
|
- private void checkGlobalParameters(List<String> globalParameterCodes) throws IllegalArgumentException {
|
|
|
- if (!CollectionUtils.isEmpty(globalParameterCodes)) {
|
|
|
- for (String globalParameterCode : globalParameterCodes) {
|
|
|
- GlobalParameter globalParameter = globalParameterDao.findOne(globalParameterCode);
|
|
|
- if (globalParameter == null) {
|
|
|
- throw new IllegalArgumentException("公共参数不存在:" + globalParameterCode);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public List<Template> getByResourcePointCode(@NotEmpty("resourcePointCode") String resourcePointCode) {
|
|
|
- ResourcePoint resourcePoint = resourePointDao.findOne(resourcePointCode);
|
|
|
- if (resourcePoint == null) {
|
|
|
- throw new IllegalStateException("资源点不存在:" + resourcePointCode);
|
|
|
- }
|
|
|
- // 资源点所能查看的模版
|
|
|
- List<String> resourcePointCodes = resourcePoint.getTemplateCodes();
|
|
|
- if(CollectionUtils.isEmpty(resourcePointCodes)){
|
|
|
- return null;
|
|
|
- }
|
|
|
- return templateDao.findListBy("code", resourcePointCodes);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public int addParameter(@NotEmpty("code") String code, @NotEmpty("parameter") TemplateParameter parameter) {
|
|
|
- parameter.init();
|
|
|
- templateDao.checkFields(parameter);
|
|
|
- UpdateOperations<Template> operations = templateDao.createUpdateOperations();
|
|
|
- operations.addToSet("parameters", parameter);
|
|
|
- return templateDao.update(code, operations);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public int deleteParameter(@NotEmpty("code") String code, @NotEmpty("parameterCode") String parameterCode)
|
|
|
- throws IllegalStateException, IllegalArgumentException, OperationException {
|
|
|
- // 先获取模版
|
|
|
- Template template = getTemplate(code, parameterCode);
|
|
|
- if (template == null) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- List<TemplateParameter> parameters = template.getParameters();
|
|
|
- // 删除获取到的模版的指定参数,再更新到数据库
|
|
|
- for (int i = parameters.size() - 1; i >= 0; i--) {
|
|
|
- TemplateParameter parameter = parameters.get(i);
|
|
|
- if (parameterCode.equals(parameter.getCode())) {
|
|
|
- parameters.remove(i);
|
|
|
- }
|
|
|
- }
|
|
|
- return templateDao.update(template);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 根据模版的 code 和模版参数的 code 获取模版
|
|
|
- *
|
|
|
- * @param code
|
|
|
- * 模版的 code
|
|
|
- * @param parameterCode
|
|
|
- * 模版参数的 code
|
|
|
- * @return 模版
|
|
|
- * @throws IllegalStateException
|
|
|
- */
|
|
|
- private Template getTemplate(@NotEmpty("code") String code, @NotEmpty("parameterCode") String parameterCode)
|
|
|
- throws IllegalStateException {
|
|
|
- Query<Template> query = templateDao.createQuery();
|
|
|
- query.field("code").equal(code);
|
|
|
- query.field("parameters.code").equal(parameterCode);
|
|
|
- List<Template> templates = templateDao.find(query);
|
|
|
- if (CollectionUtils.isEmpty(templates)) {
|
|
|
- return null;
|
|
|
- } else if (templates.size() > 1) {
|
|
|
- throw new IllegalStateException("存在不止一个模版");
|
|
|
- }
|
|
|
- return templates.get(0);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public int updateParameter(@NotEmpty("code") String code, @NotEmpty("parameter") TemplateParameter parameter)
|
|
|
- throws IllegalStateException, IllegalArgumentException, OperationException {
|
|
|
- String parameterCode = parameter.getCode();
|
|
|
- templateDao.checkFields(parameter);
|
|
|
- // 先获取模版
|
|
|
- Template template = getTemplate(code, parameterCode);
|
|
|
- if (template == null) {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- List<TemplateParameter> parameters = template.getParameters();
|
|
|
- // 删除获取到的模版的指定参数,再更新到数据库
|
|
|
- for (int i = parameters.size() - 1; i >= 0; i--) {
|
|
|
- TemplateParameter templateParameter = parameters.get(i);
|
|
|
- if (parameterCode.equals(templateParameter.getCode())) {
|
|
|
- parameters.set(i, parameter);
|
|
|
- }
|
|
|
- }
|
|
|
- return templateDao.update(template);
|
|
|
- }
|
|
|
+ @Autowired
|
|
|
+ private TemplateDao templateDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DataSourceDao dataSourceDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private GlobalParameterDao globalParameterDao;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ResourcePointDao resourePointDao;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Template save(@NotEmpty("json") String json) {
|
|
|
+ Template template = templateDao.parse(json);
|
|
|
+ checkDataSource(template.getDataSourceCode());
|
|
|
+ checkGlobalParameters(template.getGlobalParameterCodes());
|
|
|
+ // 保存时,不允许指定参数的 code
|
|
|
+ List<TemplateParameter> parameters = template.getParameters();
|
|
|
+ if (!CollectionUtils.isEmpty(parameters)) {
|
|
|
+ for (TemplateParameter parameter : parameters) {
|
|
|
+ parameter.init();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return templateDao.save(template);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Template savePart(String json) {
|
|
|
+ Template template = templateDao.parse(json);
|
|
|
+ // 可以不指定数据源
|
|
|
+ String dataSourceCode = template.getDataSourceCode();
|
|
|
+ if (!StringUtils.isEmpty(dataSourceCode)) {
|
|
|
+ checkDataSource(template.getDataSourceCode());
|
|
|
+ }
|
|
|
+ checkGlobalParameters(template.getGlobalParameterCodes());
|
|
|
+ // 保存时,不允许指定参数的 code
|
|
|
+ List<TemplateParameter> parameters = template.getParameters();
|
|
|
+ if (!CollectionUtils.isEmpty(parameters)) {
|
|
|
+ for (TemplateParameter parameter : parameters) {
|
|
|
+ parameter.init();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return templateDao.savePart(template);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int update(@NotEmpty("json") String json) throws IllegalArgumentException, OperationException {
|
|
|
+ Template template = templateDao.parse(json);
|
|
|
+ String code = template.codeNotEmpty();
|
|
|
+ checkDataSource(template.getDataSourceCode());
|
|
|
+ checkGlobalParameters(template.getGlobalParameterCodes());
|
|
|
+ List<TemplateParameter> parameters = template.getParameters();
|
|
|
+ if (!CollectionUtils.isEmpty(parameters)) {
|
|
|
+ throw new OperationException("请单独更新模版参数");
|
|
|
+ }
|
|
|
+ Set<String> ignoreFields = new HashSet<>();
|
|
|
+ // 不更新模版参数
|
|
|
+ ignoreFields.add("parameters");
|
|
|
+ UpdateOperations<Template> operations = templateDao.createUpdateOperations(template, ignoreFields);
|
|
|
+ return templateDao.update(code, operations);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int updatePart(@NotEmpty("json") String json) throws IllegalArgumentException, OperationException {
|
|
|
+ Template template = templateDao.parse(json);
|
|
|
+ // 可以不指定数据源
|
|
|
+ String dataSourceCode = template.getDataSourceCode();
|
|
|
+ if (!StringUtils.isEmpty(dataSourceCode)) {
|
|
|
+ checkDataSource(template.getDataSourceCode());
|
|
|
+ }
|
|
|
+ checkGlobalParameters(template.getGlobalParameterCodes());
|
|
|
+ List<TemplateParameter> parameters = template.getParameters();
|
|
|
+ if (!CollectionUtils.isEmpty(parameters)) {
|
|
|
+ throw new OperationException("请单独更新模版参数");
|
|
|
+ }
|
|
|
+ return templateDao.updatePart(json);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查数据源是否存在
|
|
|
+ *
|
|
|
+ * @param dataSourceCode 数据源的 code
|
|
|
+ * @throws IllegalArgumentException 数据源不存在
|
|
|
+ */
|
|
|
+ private void checkDataSource(@NotEmpty("dataSourceCode") String dataSourceCode) throws IllegalArgumentException {
|
|
|
+ DataSource dataSource = dataSourceDao.findOne(dataSourceCode);
|
|
|
+ if (dataSource == null) {
|
|
|
+ throw new IllegalArgumentException("数据源不存在:" + dataSourceCode);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查公共参数是否存在
|
|
|
+ *
|
|
|
+ * @param globalParameterCodes 公共参数的 code
|
|
|
+ * @throws IllegalArgumentException 公共参数不存在
|
|
|
+ */
|
|
|
+ private void checkGlobalParameters(List<String> globalParameterCodes) throws IllegalArgumentException {
|
|
|
+ if (!CollectionUtils.isEmpty(globalParameterCodes)) {
|
|
|
+ for (String globalParameterCode : globalParameterCodes) {
|
|
|
+ GlobalParameter globalParameter = globalParameterDao.findOne(globalParameterCode);
|
|
|
+ if (globalParameter == null) {
|
|
|
+ throw new IllegalArgumentException("公共参数不存在:" + globalParameterCode);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Template> getByResourcePointCode(@NotEmpty("resourcePointCode") String resourcePointCode) {
|
|
|
+ ResourcePoint resourcePoint = resourePointDao.findOne(resourcePointCode);
|
|
|
+ if (resourcePoint == null) {
|
|
|
+ throw new IllegalStateException("资源点不存在:" + resourcePointCode);
|
|
|
+ }
|
|
|
+ // 资源点所能查看的模版
|
|
|
+ List<String> resourcePointCodes = resourcePoint.getTemplateCodes();
|
|
|
+ if (CollectionUtils.isEmpty(resourcePointCodes)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return templateDao.findListBy("code", resourcePointCodes);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Template getByResourcePointCode(@NotEmpty("code") String code, @NotEmpty("resourcePointCode") String resourcePointCode) {
|
|
|
+ ResourcePoint resourcePoint = resourePointDao.findOne(resourcePointCode);
|
|
|
+ if (resourcePoint == null) {
|
|
|
+ throw new IllegalStateException("资源点不存在:" + resourcePointCode);
|
|
|
+ }
|
|
|
+ // 资源点所能查看的模版
|
|
|
+ List<String> resourcePointCodes = resourcePoint.getTemplateCodes();
|
|
|
+ if (CollectionUtils.isEmpty(resourcePointCodes)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (!resourcePointCodes.contains(code)) {
|
|
|
+ throw new IllegalArgumentException("该资源点 " + resourcePoint.getName() + " 未分配该模版:" + code);
|
|
|
+ }
|
|
|
+ return templateDao.findOne(code);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int addParameter(@NotEmpty("code") String code, @NotEmpty("parameter") TemplateParameter parameter) {
|
|
|
+ parameter.init();
|
|
|
+ templateDao.checkFields(parameter);
|
|
|
+ UpdateOperations<Template> operations = templateDao.createUpdateOperations();
|
|
|
+ operations.addToSet("parameters", parameter);
|
|
|
+ return templateDao.update(code, operations);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int deleteParameter(@NotEmpty("code") String code, @NotEmpty("parameterCode") String parameterCode)
|
|
|
+ throws IllegalStateException, IllegalArgumentException, OperationException {
|
|
|
+ // 先获取模版
|
|
|
+ Template template = getTemplate(code, parameterCode);
|
|
|
+ if (template == null) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ List<TemplateParameter> parameters = template.getParameters();
|
|
|
+ // 删除获取到的模版的指定参数,再更新到数据库
|
|
|
+ for (int i = parameters.size() - 1; i >= 0; i--) {
|
|
|
+ TemplateParameter parameter = parameters.get(i);
|
|
|
+ if (parameterCode.equals(parameter.getCode())) {
|
|
|
+ parameters.remove(i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return templateDao.update(template);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据模版的 code 和模版参数的 code 获取模版
|
|
|
+ *
|
|
|
+ * @param code 模版的 code
|
|
|
+ * @param parameterCode 模版参数的 code
|
|
|
+ * @return 模版
|
|
|
+ * @throws IllegalStateException
|
|
|
+ */
|
|
|
+ private Template getTemplate(@NotEmpty("code") String code, @NotEmpty("parameterCode") String parameterCode)
|
|
|
+ throws IllegalStateException {
|
|
|
+ Query<Template> query = templateDao.createQuery();
|
|
|
+ query.field("code").equal(code);
|
|
|
+ query.field("parameters.code").equal(parameterCode);
|
|
|
+ List<Template> templates = templateDao.find(query);
|
|
|
+ if (CollectionUtils.isEmpty(templates)) {
|
|
|
+ return null;
|
|
|
+ } else if (templates.size() > 1) {
|
|
|
+ throw new IllegalStateException("存在不止一个模版");
|
|
|
+ }
|
|
|
+ return templates.get(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int updateParameter(@NotEmpty("code") String code, @NotEmpty("parameter") TemplateParameter parameter)
|
|
|
+ throws IllegalStateException, IllegalArgumentException, OperationException {
|
|
|
+ String parameterCode = parameter.getCode();
|
|
|
+ templateDao.checkFields(parameter);
|
|
|
+ // 先获取模版
|
|
|
+ Template template = getTemplate(code, parameterCode);
|
|
|
+ if (template == null) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ List<TemplateParameter> parameters = template.getParameters();
|
|
|
+ // 删除获取到的模版的指定参数,再更新到数据库
|
|
|
+ for (int i = parameters.size() - 1; i >= 0; i--) {
|
|
|
+ TemplateParameter templateParameter = parameters.get(i);
|
|
|
+ if (parameterCode.equals(templateParameter.getCode())) {
|
|
|
+ parameters.set(i, parameter);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return templateDao.update(template);
|
|
|
+ }
|
|
|
|
|
|
}
|