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  import org.apache.cxf.common.util.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.MaintenanceForm;
25  import org.kuali.rice.krms.impl.repository.CategoryBo;
26  import org.kuali.rice.krms.impl.ui.AgendaEditor;
27  
28  import java.util.ArrayList;
29  import java.util.Collection;
30  import java.util.Collections;
31  import java.util.List;
32  import java.util.Map;
33  
34  /**
35   * Helper class that returns all category types that are valid for the given context.
36   */
37  public class CategoryValuesFinder extends UifKeyValuesFinderBase {
38  
39      private boolean blankOption;
40  
41      /**
42       * @return the blankOption
43       */
44      public boolean isBlankOption() {
45          return this.blankOption;
46      }
47  
48      /**
49       * @param blankOption the blankOption to set
50       */
51      public void setBlankOption(boolean blankOption) {
52          this.blankOption = blankOption;
53      }
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  
62          // if we have an agenda w/ a selected context
63          if (agendaEditor.getAgenda() != null && !StringUtils.isEmpty(agendaEditor.getAgenda().getContextId())) {
64  
65              // key off the namespace
66  
67              String namespace = agendaEditor.getNamespace();
68  
69              if(blankOption){
70                  keyValues.add(new ConcreteKeyValue("", ""));
71              }
72  
73              Map<String, String> criteria = Collections.singletonMap("namespace", namespace);
74              Collection<CategoryBo> categories =
75                      KRADServiceLocator.getBusinessObjectService().findMatchingOrderBy(
76                              CategoryBo.class, criteria, "name", true
77                      );
78  
79              for (CategoryBo category : categories) {
80                  keyValues.add(new ConcreteKeyValue(category.getId(), category.getName()));
81              }
82          }
83  
84          return keyValues;
85      }
86  }