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 import org.apache.commons.collections.CollectionUtils;
19 import org.apache.commons.lang.StringUtils;
20 import org.kuali.rice.core.api.criteria.QueryByCriteria;
21 import org.kuali.rice.core.api.criteria.QueryResults;
22 import org.kuali.rice.core.api.util.ConcreteKeyValue;
23 import org.kuali.rice.core.api.util.KeyValue;
24 import org.kuali.rice.krad.service.KRADServiceLocator;
25 import org.kuali.rice.krad.uif.control.UifKeyValuesFinderBase;
26 import org.kuali.rice.krad.uif.view.ViewModel;
27 import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
28 import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
29 import org.kuali.rice.krms.api.repository.rule.RuleDefinition;
30 import org.kuali.rice.krms.impl.repository.AgendaItemBo;
31 import org.kuali.rice.krms.impl.repository.CategoryBo;
32 import org.kuali.rice.krms.impl.repository.ContextValidTermBo;
33 import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
34 import org.kuali.rice.krms.impl.repository.PropositionBo;
35 import org.kuali.rice.krms.impl.repository.TermBo;
36 import org.kuali.rice.krms.impl.repository.TermResolverBo;
37 import org.kuali.rice.krms.impl.repository.TermSpecificationBo;
38 import org.kuali.rice.krms.impl.util.KrmsImplConstants;
39
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.HashSet;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Set;
46
47
48
49
50
51
52
53 public class ValidTermsForPropositionValuesFinder extends UifKeyValuesFinderBase {
54
55
56
57
58
59
60 @Override
61 public List<KeyValue> getKeyValues(ViewModel model) {
62 List<KeyValue> keyValues = new ArrayList<KeyValue>();
63
64 MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) model;
65 PropositionBo rootProposition = ((PropositionBo) maintenanceForm.getDocument().getNewMaintainableObject().getDataObject());
66
67 PropositionBo editModeProposition = findPropositionUnderEdit(rootProposition);
68 String selectedCategoryId = (editModeProposition != null) ? editModeProposition.getCategoryId() : null;
69
70
71
72 List<ContextValidTermBo> contextValidTerms = getContextValidTerms(rootProposition.getRuleId());
73
74 List<String> termSpecIds = new ArrayList();
75
76 for (ContextValidTermBo validTerm : contextValidTerms) {
77 termSpecIds.add(validTerm.getTermSpecificationId());
78 }
79
80 if (termSpecIds.size() > 0) {
81 QueryResults<TermBo> terms = null;
82 Map<String,Object> critMap = new HashMap<String,Object>();
83 critMap.put("specificationId", termSpecIds);
84
85 QueryByCriteria criteria =
86 QueryByCriteria.Builder.forAttribute("specificationId", termSpecIds).setOrderByAscending("description").build();
87
88 terms = KRADServiceLocator.getDataObjectService().findMatching(TermBo.class, criteria);
89
90
91 if (!CollectionUtils.isEmpty(terms.getResults())) for (TermBo term : terms.getResults()) {
92 String selectName = term.getDescription();
93
94 if (StringUtils.isBlank(selectName) || "null".equals(selectName)) {
95 selectName = term.getSpecification().getName();
96 }
97
98 if (!StringUtils.isBlank(selectedCategoryId)) {
99
100 if (isTermSpecificationInCategory(term.getSpecification(), selectedCategoryId)) {
101 keyValues.add(new ConcreteKeyValue(term.getId(), selectName));
102 }
103 } else {
104 keyValues.add(new ConcreteKeyValue(term.getId(), selectName));
105 }
106 }
107
108
109
110
111
112
113 QueryByCriteria.Builder termResolverCritBuilder = QueryByCriteria.Builder.forAttribute("outputId", termSpecIds);
114 termResolverCritBuilder.setOrderByAscending("name");
115 QueryResults<TermResolverBo> termResolvers =
116 KRADServiceLocator.getDataObjectService().findMatching(TermResolverBo.class, termResolverCritBuilder.build());
117
118
119
120 if (termResolvers.getResults() != null) for (TermResolverBo termResolver : termResolvers.getResults()) {
121 if (!CollectionUtils.isEmpty(termResolver.getParameterSpecifications())) {
122 TermSpecificationBo output = termResolver.getOutput();
123
124
125 if (StringUtils.isBlank(selectedCategoryId) ||
126 isTermSpecificationInCategory(output, selectedCategoryId)) {
127
128
129 keyValues.add(new ConcreteKeyValue(KrmsImplConstants.PARAMETERIZED_TERM_PREFIX
130 + output.getId(), output.getName()
131
132 + "(" + StringUtils.repeat("_", ",", termResolver.getParameterSpecifications().size()) +")"));
133 }
134 }
135 }
136 }
137
138 return keyValues;
139 }
140
141
142
143
144
145
146
147
148 private List<ContextValidTermBo> getContextValidTerms(String ruleId) {
149 RuleDefinition rule = KrmsRepositoryServiceLocator
150 .getRuleBoService().getRuleByRuleId(ruleId);
151
152 QueryByCriteria agendaItemCriteria = QueryByCriteria.Builder.forAttribute("ruleId", rule.getId()).build();
153 QueryResults<AgendaItemBo> agendaItems =
154 KRADServiceLocator.getDataObjectService().findMatching(AgendaItemBo.class, agendaItemCriteria);
155
156 Set<String> agendaIds = new HashSet<String>();
157 if (!CollectionUtils.isEmpty(agendaItems.getResults())) for (AgendaItemBo agendaItem : agendaItems.getResults()) {
158 agendaIds.add(agendaItem.getAgendaId());
159 }
160
161 Set<String> contextIds = new HashSet<String>();
162 for (String agendaId : agendaIds) {
163 AgendaDefinition agenda = KrmsRepositoryServiceLocator.getAgendaBoService().getAgendaByAgendaId(agendaId);
164
165 if (agenda != null) {
166 contextIds.add(agenda.getContextId());
167 }
168 }
169
170 List<ContextValidTermBo> contextValidTerms = new ArrayList<ContextValidTermBo>();
171
172 for (String contextId : contextIds) {
173 QueryResults<ContextValidTermBo> queryResults =
174 KRADServiceLocator.getDataObjectService().findMatching(ContextValidTermBo.class,
175 QueryByCriteria.Builder.forAttribute("contextId", contextId).build());
176
177 if (!CollectionUtils.isEmpty(queryResults.getResults())) {
178 contextValidTerms.addAll(queryResults.getResults());
179 }
180 }
181 return contextValidTerms;
182 }
183
184
185
186
187 private boolean isTermSpecificationInCategory(TermSpecificationBo termSpec, String categoryId) {
188 if (termSpec.getCategories() != null) {
189 for (CategoryBo category : termSpec.getCategories()) {
190 if (categoryId.equals(category.getId())) {
191 return true;
192 }
193 }
194 }
195 return false;
196 }
197
198
199
200
201 private PropositionBo findPropositionUnderEdit(PropositionBo currentProposition) {
202 PropositionBo result = null;
203 if (currentProposition.getEditMode()) {
204 result = currentProposition;
205 } else {
206 if (currentProposition.getCompoundComponents() != null) {
207 for (PropositionBo child : currentProposition.getCompoundComponents()) {
208 result = findPropositionUnderEdit(child);
209 if (result != null) break;
210 }
211 }
212 }
213 return result;
214 }
215
216 }