View Javadoc

1   /**
2    * Copyright 2011 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  package org.kuali.student.enrollment.class2.population.keyvalue;
16  
17  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
18  import org.kuali.rice.core.api.util.ConcreteKeyValue;
19  import org.kuali.rice.core.api.util.KeyValue;
20  import org.kuali.rice.krad.uif.control.UifKeyValuesFinderBase;
21  import org.kuali.rice.krad.uif.view.ViewModel;
22  import org.kuali.student.r2.common.util.ContextUtils;
23  import org.kuali.student.r2.core.constants.PopulationServiceConstants;
24  import org.kuali.student.r2.core.population.dto.PopulationRuleInfo;
25  import org.kuali.student.r2.core.population.service.PopulationService;
26  
27  import javax.xml.namespace.QName;
28  import java.io.Serializable;
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  /**
33   * This class holds the list of rule agenda ids to their rule names
34   *
35   * @author Kuali Student
36   */
37  public class PopulationRuleAgendaKeyValues extends UifKeyValuesFinderBase implements Serializable {
38      private static final long serialVersionUID = 1L;
39  
40      private transient PopulationService populationService = null;
41  
42  
43      /**
44       * Creates a list of KeyValues maping the agenda id to the rule name
45       *
46       * @param model - The calling view model
47       * @return List of all population rule KeyValues
48       */
49      @Override
50      public List<KeyValue> getKeyValues(ViewModel model) {
51  
52          List<KeyValue> keyValues = new ArrayList<KeyValue>();
53  
54          // The complete list of Rule Types
55          String ruleTypes[] = PopulationServiceConstants.POPULATION_RULE_TYPE_KEYS;
56  
57          // Grab Rule entries for each Rule Type
58          for (int j = 0; j < ruleTypes.length; j++) {
59              try {
60  
61                  // Create a keyValue entry for each Rule found for the type
62                  ArrayList<String> populationRules = (ArrayList<String>) getPopulationService().getPopulationRuleIdsByType(ruleTypes[j], ContextUtils.createDefaultContextInfo());
63                  for (int i = 0; i < populationRules.size(); i++) {
64                      PopulationRuleInfo ruleInfo = getPopulationService().getPopulationRule(populationRules.get(i), ContextUtils.createDefaultContextInfo());
65                      if (ruleInfo.getAgendaIds() != null && ruleInfo.getAgendaIds().size() > 0)
66                          keyValues.add(new ConcreteKeyValue(ruleInfo.getAgendaIds().get(0), ruleInfo.getName()));
67                  }
68  
69              } catch (Exception e) {
70                  throw new RuntimeException("Error getting PopulationRuleInfo", e);
71              }
72          }
73  
74          return keyValues;
75      }
76  
77      /**
78       * Stores and retrieves the PopulationService for the page
79       *
80       * @return The population service
81       */
82      private PopulationService getPopulationService() {
83          if (populationService == null) {
84              populationService = (PopulationService) GlobalResourceLoader.getService(new QName(PopulationServiceConstants.NAMESPACE, "PopulationService"));
85          }
86          return this.populationService;
87      }
88  }