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.MaintenanceForm;
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 MaintenanceForm maintenanceForm = (MaintenanceForm) 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
124
125 keyValues.add(new ConcreteKeyValue(KrmsImplConstants.PARAMETERIZED_TERM_PREFIX
126 + output.getId(), output.getName()
127
128 + "(" + StringUtils.repeat("_", ",", termResolver.getParameterSpecifications().size()) +")"));
129 }
130 }
131 }
132 }
133
134 return keyValues;
135 }
136
137
138
139
140 private boolean isTermSpecificationInCategory(TermSpecificationBo termSpec, String categoryId) {
141 if (termSpec.getCategories() != null) {
142 for (CategoryBo category : termSpec.getCategories()) {
143 if (categoryId.equals(category.getId())) {
144 return true;
145 }
146 }
147 }
148 return false;
149 }
150
151
152
153
154 private PropositionBo findPropositionUnderEdit(PropositionBo currentProposition) {
155 PropositionBo result = null;
156 if (currentProposition.getEditMode()) {
157 result = currentProposition;
158 } else {
159 if (currentProposition.getCompoundComponents() != null) {
160 for (PropositionBo child : currentProposition.getCompoundComponents()) {
161 result = findPropositionUnderEdit(child);
162 if (result != null) break;
163 }
164 }
165 }
166 return result;
167 }
168
169 }