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.CategoryBo;
28 import org.kuali.rice.krms.impl.repository.ContextValidTermBo;
29 import org.kuali.rice.krms.impl.repository.PropositionBo;
30 import org.kuali.rice.krms.impl.repository.TermBo;
31 import org.kuali.rice.krms.impl.repository.TermResolverBo;
32 import org.kuali.rice.krms.impl.repository.TermSpecificationBo;
33 import org.kuali.rice.krms.impl.util.KrmsImplConstants;
34
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.Map;
40 import java.util.List;
41
42
43
44
45
46
47
48 public class ValidTermsValuesFinder extends UifKeyValuesFinderBase {
49
50
51
52
53
54
55 @Override
56 public List<KeyValue> getKeyValues(ViewModel model) {
57 List<KeyValue> keyValues = new ArrayList<KeyValue>();
58
59 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) model;
60 AgendaEditor agendaEditor = ((AgendaEditor) maintenanceForm.getDocument().getNewMaintainableObject().getDataObject());
61 String contextId = agendaEditor.getAgenda().getContextId();
62
63 String selectedPropId = agendaEditor.getSelectedPropositionId();
64
65 PropositionBo rootProposition = agendaEditor.getAgendaItemLine().getRule().getProposition();
66 PropositionBo editModeProposition = findPropositionUnderEdit(rootProposition);
67 String selectedCategoryId = (editModeProposition != null) ? editModeProposition.getCategoryId() : null;
68
69
70
71 Collection<ContextValidTermBo> contextValidTerms = null;
72 contextValidTerms = KRADServiceLocator.getBusinessObjectService()
73 .findMatching(ContextValidTermBo.class, Collections.singletonMap("contextId", contextId));
74
75 List<String> termSpecIds = new ArrayList();
76 for (ContextValidTermBo validTerm : contextValidTerms) {
77 termSpecIds.add(validTerm.getTermSpecificationId());
78 }
79
80 if (termSpecIds.size() > 0) {
81 Collection<TermBo> terms = null;
82 Map<String,Object> criteria = new HashMap<String,Object>();
83 criteria.put("specificationId", termSpecIds);
84 terms = KRADServiceLocator.getBusinessObjectService().findMatchingOrderBy(TermBo.class, criteria, "description", true);
85
86
87 for (TermBo term : terms) {
88 String selectName = term.getDescription();
89
90 if (StringUtils.isBlank(selectName) || "null".equals(selectName)) {
91 selectName = term.getSpecification().getName();
92 }
93
94 if (!StringUtils.isBlank(selectedCategoryId)) {
95
96 if (isTermSpecificationInCategory(term.getSpecification(), selectedCategoryId)) {
97 keyValues.add(new ConcreteKeyValue(term.getId(), selectName));
98 }
99 } else {
100 keyValues.add(new ConcreteKeyValue(term.getId(), selectName));
101 }
102 }
103
104
105
106
107
108
109 Collection<TermResolverBo> termResolvers =
110 KRADServiceLocator.getBusinessObjectService().findMatchingOrderBy(
111 TermResolverBo.class, Collections.singletonMap("outputId", termSpecIds), "name", true
112 );
113
114
115
116 if (termResolvers != null) for (TermResolverBo termResolver : termResolvers) {
117 if (!CollectionUtils.isEmpty(termResolver.getParameterSpecifications())) {
118 TermSpecificationBo output = termResolver.getOutput();
119
120
121 if (StringUtils.isBlank(selectedCategoryId) ||
122 isTermSpecificationInCategory(output, selectedCategoryId)) {
123 String outputDescription = StringUtils.isBlank(output.getDescription())?output.getName():
124 output.getDescription();
125
126 keyValues.add(new ConcreteKeyValue(KrmsImplConstants.PARAMETERIZED_TERM_PREFIX
127 + output.getId(), outputDescription
128
129 + "(" + StringUtils.repeat("_", ",", termResolver.getParameterSpecifications().size()) +")"));
130 }
131 }
132 }
133 }
134
135 return keyValues;
136 }
137
138
139
140
141 private boolean isTermSpecificationInCategory(TermSpecificationBo termSpec, String categoryId) {
142 if (termSpec.getCategories() != null) {
143 for (CategoryBo category : termSpec.getCategories()) {
144 if (categoryId.equals(category.getId())) {
145 return true;
146 }
147 }
148 }
149 return false;
150 }
151
152
153
154
155 private PropositionBo findPropositionUnderEdit(PropositionBo currentProposition) {
156 PropositionBo result = null;
157 if (currentProposition.getEditMode()) {
158 result = currentProposition;
159 } else {
160 if (currentProposition.getCompoundComponents() != null) {
161 for (PropositionBo child : currentProposition.getCompoundComponents()) {
162 result = findPropositionUnderEdit(child);
163 if (result != null) break;
164 }
165 }
166 }
167 return result;
168 }
169
170 }