View Javadoc

1   /**
2    * Copyright 2005-2011 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.lang.StringUtils;
20  import org.kuali.rice.core.api.util.ConcreteKeyValue;
21  import org.kuali.rice.core.api.util.KeyValue;
22  import org.kuali.rice.krad.service.KRADServiceLocator;
23  import org.kuali.rice.krad.uif.control.UifKeyValuesFinderBase;
24  import org.kuali.rice.krad.uif.view.ViewModel;
25  import org.kuali.rice.krad.web.form.MaintenanceForm;
26  import org.kuali.rice.krms.impl.repository.CategoryBo;
27  import org.kuali.rice.krms.impl.repository.ContextValidTermBo;
28  import org.kuali.rice.krms.impl.repository.PropositionBo;
29  import org.kuali.rice.krms.impl.repository.TermBo;
30  
31  import java.util.ArrayList;
32  import java.util.Collection;
33  import java.util.Collections;
34  import java.util.HashMap;
35  import java.util.Map;
36  import java.util.List;
37  
38  /**
39   * 
40   * @author Kuali Rice Team (rice.collab@kuali.org)
41   *
42   */
43  public class ValidTermsValuesFinder extends UifKeyValuesFinderBase {
44  
45      @Override
46      public List<KeyValue> getKeyValues(ViewModel model) {
47          List<KeyValue> keyValues = new ArrayList<KeyValue>();
48  
49          MaintenanceForm maintenanceForm = (MaintenanceForm) model;
50          AgendaEditor agendaEditor = ((AgendaEditor) maintenanceForm.getDocument().getNewMaintainableObject().getDataObject());
51          String contextId = agendaEditor.getAgenda().getContextId();
52  
53          String selectedPropId = agendaEditor.getSelectedPropositionId();
54  
55          PropositionBo rootProposition = agendaEditor.getAgendaItemLine().getRule().getProposition();
56          PropositionBo editModeProposition = findPropositionUnderEdit(rootProposition);
57          String selectedCategoryId = (editModeProposition != null) ? editModeProposition.getCategoryId() : null;
58  
59          // Get all valid terms
60  
61          Collection<ContextValidTermBo> contextValidTerms = null;
62          contextValidTerms = KRADServiceLocator.getBusinessObjectService()
63                  .findMatching(ContextValidTermBo.class, Collections.singletonMap("contextId", contextId));
64  
65          List<String> termSpecIds = new ArrayList();
66          for (ContextValidTermBo validTerm : contextValidTerms) {
67              termSpecIds.add(validTerm.getTermSpecificationId());
68          }
69  
70          if (termSpecIds.size() > 0) { // if we don't have any valid terms, skip it
71              Collection<TermBo> terms = null;
72              Map<String,Object> criteria = new HashMap<String,Object>();
73              criteria.put("specificationId", termSpecIds);
74              terms = KRADServiceLocator.getBusinessObjectService().findMatchingOrderBy(TermBo.class, criteria, "description", true);
75  
76              // add all terms that are in the selected category (or else add 'em all if no category is selected)
77              for (TermBo term : terms) {
78                  String selectName = term.getDescription();
79  
80                  if (StringUtils.isBlank(selectName) || "null".equals(selectName)) {
81                      selectName = term.getSpecification().getName();
82                  }
83  
84                  if (!StringUtils.isBlank(selectedCategoryId)) {
85                      // only add if the term has the selected category
86                      if (term.getSpecification().getCategories() != null) {
87                          for (CategoryBo category : term.getSpecification().getCategories()) {
88                              if (selectedCategoryId.equals(category.getId())) {
89                                  keyValues.add(new ConcreteKeyValue(term.getId(), selectName));
90                                  break;
91                              }
92                          }
93                      }
94                  } else {
95                      keyValues.add(new ConcreteKeyValue(term.getId(), selectName));
96                  }
97              }
98          }
99  
100         return keyValues;
101     }
102 
103     /**
104      * helper method to find the proposition under edit
105      */
106     private PropositionBo findPropositionUnderEdit(PropositionBo currentProposition) {
107         PropositionBo result = null;
108         if (currentProposition.getEditMode()) {
109             result = currentProposition;
110         } else {
111             if (currentProposition.getCompoundComponents() != null) {
112                 for (PropositionBo child : currentProposition.getCompoundComponents()) {
113                     result = findPropositionUnderEdit(child);
114                     if (result != null) break;
115                 }
116             }
117         }
118         return result;
119     }
120 
121 }