1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.impl.rule;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
20 import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
21 import org.kuali.rice.krms.api.repository.term.TermSpecificationDefinition;
22 import org.kuali.rice.krms.impl.repository.CategoryBo;
23 import org.kuali.rice.krms.impl.repository.ContextBo;
24 import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
25 import org.kuali.rice.krms.impl.repository.TermBoService;
26 import org.kuali.rice.krms.impl.repository.TermSpecificationBo;
27 import org.kuali.rice.krms.impl.util.KRMSPropertyConstants;
28
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.LinkedList;
32 import java.util.List;
33 import java.util.Map;
34
35 public class TermSpecBusRule extends MaintenanceDocumentRuleBase {
36
37 @Override
38 protected boolean processCustomSaveDocumentBusinessRules(MaintenanceDocument document) {
39 boolean isValid = true;
40
41 TermSpecificationBo termSpec = (TermSpecificationBo) document.getNewMaintainableObject().getDataObject();
42 isValid &= validateId(termSpec);
43 isValid &= validateCategory(termSpec);
44 isValid &= validateContext(termSpec);
45 isValid &= validateNameNamespace(termSpec);
46
47 return isValid;
48 }
49
50 private boolean validateCategory(TermSpecificationBo termSpecificationBo) {
51 List<CategoryBo> categories = termSpecificationBo.getCategories();
52 List<String> categoryIds = new ArrayList<String>();
53
54 boolean valid = true;
55 for (CategoryBo category: categories) {
56 if (categoryIds.contains(category.getId())) {
57 this.putFieldError(KRMSPropertyConstants.TermSpecification.CATEGORY, "error.termSpecification.duplicateCategory");
58 valid = false;
59 } else {
60 categoryIds.add(category.getId());
61 }
62 }
63 return valid;
64 }
65
66 private boolean validateContext(TermSpecificationBo termSpec) {
67 List<ContextBo> termSpecContexts = termSpec.getContexts();
68 List<String> contextIds = new ArrayList<String>();
69 boolean valid = true;
70 for (ContextBo context: termSpecContexts) {
71 if (contextIds.contains(context.getId())) {
72 this.putFieldError(KRMSPropertyConstants.TermSpecification.CONTEXT, "error.termSpecification.duplicateContext");
73 valid = false;
74 } else {
75 contextIds.add(context.getId());
76 }
77 }
78 return valid;
79 }
80
81 private boolean validateId(TermSpecificationBo termSpec) {
82 if (StringUtils.isNotBlank(termSpec.getId())) {
83 TermSpecificationDefinition termSpecInDatabase = getTermBoService().getTermSpecificationById(termSpec.getId());
84 if ((termSpecInDatabase != null) && (!StringUtils.equals(termSpecInDatabase.getId(), termSpec.getId()))) {
85 this.putFieldError(KRMSPropertyConstants.TermSpecification.TERM_SPECIFICATION_ID, "error.termSpecification.duplicateId");
86 return false;
87 }
88 }
89
90 return true;
91 }
92
93
94
95
96
97
98 private boolean validateNameNamespace(TermSpecificationBo termSpec) {
99 if (StringUtils.isNotBlank(termSpec.getName()) && StringUtils.isNotBlank(
100 termSpec.getNamespace())) {
101
102 Map<String, String> criteria = new HashMap<String, String>();
103
104 criteria.put("name", termSpec.getName());
105 criteria.put("namespace", termSpec.getNamespace());
106
107 TermSpecificationBo termSpecInDatabase = getBoService().findByPrimaryKey(TermSpecificationBo.class, criteria);
108
109 if((termSpecInDatabase != null) && (!StringUtils.equals(termSpecInDatabase.getId(), termSpec.getId()))) {
110 this.putFieldError(KRMSPropertyConstants.TermSpecification.NAME, "error.term.duplicateNameNamespace");
111 return false;
112 }
113 }
114
115 return true;
116 }
117
118 public TermBoService getTermBoService() {
119 return KrmsRepositoryServiceLocator.getTermBoService();
120 }
121
122 }