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  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.criteria.QueryByCriteria;
20  import org.kuali.rice.core.api.criteria.QueryResults;
21  import org.kuali.rice.core.api.util.ConcreteKeyValue;
22  import org.kuali.rice.core.api.util.KeyValue;
23  import org.kuali.rice.kns.service.KNSServiceLocator;
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.impl.repository.CategoryBo;
29  
30  import java.util.ArrayList;
31  import java.util.Collection;
32  import java.util.Collections;
33  import java.util.List;
34  import java.util.Map;
35  
36  /**
37   * Helper class that returns all category types that are valid for the given context.
38   */
39  public class CategoryValuesFinder extends UifKeyValuesFinderBase {
40  
41      private boolean blankOption;
42  
43      /**
44       * @return the blankOption
45       */
46      public boolean isBlankOption() {
47          return this.blankOption;
48      }
49  
50      /**
51       * @param blankOption the blankOption to set
52       */
53      public void setBlankOption(boolean blankOption) {
54          this.blankOption = blankOption;
55      }
56  
57      @Override
58      public List<KeyValue> getKeyValues(ViewModel model) {
59          List<KeyValue> keyValues = new ArrayList<KeyValue>();
60  
61          MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) model;
62          AgendaEditor agendaEditor = ((AgendaEditor) maintenanceForm.getDocument().getNewMaintainableObject().getDataObject());
63  
64          // if we have an agenda w/ a selected context
65          if (agendaEditor.getAgenda() != null && StringUtils.isNotBlank(agendaEditor.getAgenda().getContextId())) {
66  
67              // key off the namespace
68  
69              String namespace = agendaEditor.getNamespace();
70  
71              if(blankOption){
72                  keyValues.add(new ConcreteKeyValue("", ""));
73              }
74  
75              QueryByCriteria.Builder criteriaBuilder = QueryByCriteria.Builder.forAttribute("namespace", namespace);
76              criteriaBuilder.setOrderByAscending("name");
77  
78              QueryResults<CategoryBo> categoryResults =
79                      KRADServiceLocator.getDataObjectService().findMatching(CategoryBo.class, criteriaBuilder.build());
80              List<CategoryBo> categories = categoryResults.getResults();
81  
82              for (CategoryBo category : categories) {
83                  keyValues.add(new ConcreteKeyValue(category.getId(), category.getName()));
84              }
85          }
86  
87          return keyValues;
88      }
89  }