View Javadoc

1   /**
2    * Copyright 2005-2012 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.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   * ValuesFinder used to populate the list of available Terms when creating/editing a proposition in a
44   * KRMS Rule.
45   *
46   * @author Kuali Rice Team (rice.collab@kuali.org)
47   */
48  public class ValidTermsValuesFinder extends UifKeyValuesFinderBase {
49  
50      /**
51       * get the value list for the Term dropdown in the KRMS rule editing UI
52       * @param model
53       * @return
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          // Get all valid terms
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) { // if we don't have any valid terms, skip it
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              // add all terms that are in the selected category (or else add 'em all if no category is selected)
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                      // only add if the term has the selected category
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             // Add Parameterized Term Specs
106             //
107 
108             // get term resolvers for the given term specs
109             Collection<TermResolverBo> termResolvers =
110                     KRADServiceLocator.getBusinessObjectService().findMatchingOrderBy(
111                             TermResolverBo.class, Collections.singletonMap("outputId", termSpecIds), "name", true
112                     );
113 
114             // TODO: what if there is more than one resolver for a given term specification?
115 
116             if (termResolvers != null) for (TermResolverBo termResolver : termResolvers) {
117                 if (!CollectionUtils.isEmpty(termResolver.getParameterSpecifications())) {
118                     TermSpecificationBo output = termResolver.getOutput();
119 
120                     // filter by category
121                     if (StringUtils.isBlank(selectedCategoryId) ||
122                             isTermSpecificationInCategory(output, selectedCategoryId)) {
123 
124                         // we use a special prefix to differentiate these, as they are term spec ids instead of term ids.
125                         keyValues.add(new ConcreteKeyValue(KrmsImplConstants.PARAMETERIZED_TERM_PREFIX
126                                 + output.getId(), output.getName()
127                                 // build a string that indicates the number of parameters
128                                 + "(" + StringUtils.repeat("_", ",", termResolver.getParameterSpecifications().size()) +")"));
129                     }
130                 }
131             }
132         }
133 
134         return keyValues;
135     }
136 
137     /**
138      * @return true if the term specification is in the given category
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      * helper method to find the proposition under edit
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 }