View Javadoc

1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * ValuesFinder used to populate the list of available Terms when creating/editing a proposition in a
34   * KRMS Rule.
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  public class ValidTermsForPropositionValuesFinder extends UifKeyValuesFinderBase {
39  
40      /**
41       * get the value list for the Term dropdown in the KRMS rule editing UI
42       * @param model
43       * @return
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          // Get all valid terms
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) { // if we don't have any valid terms, skip it
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              // add all terms that are in the selected category (or else add 'em all if no category is selected)
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                      // only add if the term has the selected category
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              // Add Parameterized Term Specs
92              //
93  
94              // get term resolvers for the given term specs
95              Collection<TermResolverBo> termResolvers =
96                      KRADServiceLocator.getBusinessObjectService().findMatchingOrderBy(
97                              TermResolverBo.class, Collections.singletonMap("outputId", termSpecIds), "name", true
98                      );
99  
100             // TODO: what if there is more than one resolver for a given term specification?
101 
102             if (termResolvers != null) for (TermResolverBo termResolver : termResolvers) {
103                 if (!CollectionUtils.isEmpty(termResolver.getParameterSpecifications())) {
104                     TermSpecificationBo output = termResolver.getOutput();
105 
106                     // filter by category
107                     if (StringUtils.isBlank(selectedCategoryId) ||
108                             isTermSpecificationInCategory(output, selectedCategoryId)) {
109 
110                         // we use a special prefix to differentiate these, as they are term spec ids instead of term ids.
111                         keyValues.add(new ConcreteKeyValue(KrmsImplConstants.PARAMETERIZED_TERM_PREFIX
112                                 + output.getId(), output.getName()
113                                 // build a string that indicates the number of parameters
114                                 + "(" + StringUtils.repeat("_", ",", termResolver.getParameterSpecifications().size()) +")"));
115                     }
116                 }
117             }
118         }
119 
120         return keyValues;
121     }
122 
123     /**
124      * @return true if the term specification is in the given category
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      * helper method to find the proposition under edit
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 }