1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krms.impl.ui;
17
18
19 import org.apache.commons.collections.CollectionUtils;
20 import org.apache.commons.lang.StringUtils;
21 import org.kuali.rice.core.api.util.ConcreteKeyValue;
22 import org.kuali.rice.core.api.util.KeyValue;
23 import org.kuali.rice.krad.service.KRADServiceLocator;
24 import org.kuali.rice.krad.uif.control.UifKeyValuesFinderBase;
25 import org.kuali.rice.krad.uif.view.ViewModel;
26 import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
27 import org.kuali.rice.krms.impl.repository.*;
28 import org.kuali.rice.krms.impl.util.KrmsImplConstants;
29
30 import java.util.*;
31
32
33
34
35
36
37
38 public class ValidTermsForPropositionValuesFinder extends UifKeyValuesFinderBase {
39
40
41
42
43
44
45 @Override
46 public List<KeyValue> getKeyValues(ViewModel model) {
47 List<KeyValue> keyValues = new ArrayList<KeyValue>();
48
49 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) model;
50 PropositionBo rootProposition = ((PropositionBo) maintenanceForm.getDocument().getNewMaintainableObject().getDataObject());
51
52 PropositionBo editModeProposition = findPropositionUnderEdit(rootProposition);
53 String selectedCategoryId = (editModeProposition != null) ? editModeProposition.getCategoryId() : null;
54
55
56
57 Collection<ContextValidTermBo> contextValidTerms = null;
58 contextValidTerms = KRADServiceLocator.getBusinessObjectService()
59 .findMatching(ContextValidTermBo.class, Collections.singletonMap("contextId", "20000"));
60
61 List<String> termSpecIds = new ArrayList();
62 for (ContextValidTermBo validTerm : contextValidTerms) {
63 termSpecIds.add(validTerm.getTermSpecificationId());
64 }
65
66 if (termSpecIds.size() > 0) {
67 Collection<TermBo> terms = null;
68 Map<String,Object> criteria = new HashMap<String,Object>();
69 criteria.put("specificationId", termSpecIds);
70 terms = KRADServiceLocator.getBusinessObjectService().findMatchingOrderBy(TermBo.class, criteria, "description", true);
71
72
73 for (TermBo term : terms) {
74 String selectName = term.getDescription();
75
76 if (StringUtils.isBlank(selectName) || "null".equals(selectName)) {
77 selectName = term.getSpecification().getName();
78 }
79
80 if (!StringUtils.isBlank(selectedCategoryId)) {
81
82 if (isTermSpecificationInCategory(term.getSpecification(), selectedCategoryId)) {
83 keyValues.add(new ConcreteKeyValue(term.getId(), selectName));
84 }
85 } else {
86 keyValues.add(new ConcreteKeyValue(term.getId(), selectName));
87 }
88 }
89
90
91
92
93
94
95 Collection<TermResolverBo> termResolvers =
96 KRADServiceLocator.getBusinessObjectService().findMatchingOrderBy(
97 TermResolverBo.class, Collections.singletonMap("outputId", termSpecIds), "name", true
98 );
99
100
101
102 if (termResolvers != null) for (TermResolverBo termResolver : termResolvers) {
103 if (!CollectionUtils.isEmpty(termResolver.getParameterSpecifications())) {
104 TermSpecificationBo output = termResolver.getOutput();
105
106
107 if (StringUtils.isBlank(selectedCategoryId) ||
108 isTermSpecificationInCategory(output, selectedCategoryId)) {
109
110
111 keyValues.add(new ConcreteKeyValue(KrmsImplConstants.PARAMETERIZED_TERM_PREFIX
112 + output.getId(), output.getName()
113
114 + "(" + StringUtils.repeat("_", ",", termResolver.getParameterSpecifications().size()) +")"));
115 }
116 }
117 }
118 }
119
120 return keyValues;
121 }
122
123
124
125
126 private boolean isTermSpecificationInCategory(TermSpecificationBo termSpec, String categoryId) {
127 if (termSpec.getCategories() != null) {
128 for (CategoryBo category : termSpec.getCategories()) {
129 if (categoryId.equals(category.getId())) {
130 return true;
131 }
132 }
133 }
134 return false;
135 }
136
137
138
139
140 private PropositionBo findPropositionUnderEdit(PropositionBo currentProposition) {
141 PropositionBo result = null;
142 if (currentProposition.getEditMode()) {
143 result = currentProposition;
144 } else {
145 if (currentProposition.getCompoundComponents() != null) {
146 for (PropositionBo child : currentProposition.getCompoundComponents()) {
147 result = findPropositionUnderEdit(child);
148 if (result != null) break;
149 }
150 }
151 }
152 return result;
153 }
154
155 }