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