View Javadoc

1   /**
2    * Copyright 2013 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   * Created by David Yin on 3/25/13
16   */
17  package org.kuali.student.enrollment.class2.courseoffering.keyvalue;
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.uif.control.UifKeyValuesFinderBase;
23  import org.kuali.rice.krad.uif.view.ViewModel;
24  import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingManagementUtil;
25  import org.kuali.student.enrollment.class2.courseoffering.form.CourseOfferingManagementForm;
26  import org.kuali.student.enrollment.class2.courseoffering.dto.ActivityOfferingClusterWrapper;
27  import org.kuali.student.enrollment.class2.courseoffering.util.CourseOfferingResourceLoader;
28  import org.kuali.student.enrollment.courseoffering.service.CourseOfferingService;
29  import org.kuali.student.r2.common.util.ContextUtils;
30  import org.kuali.student.r2.core.class1.type.service.TypeService;
31  import org.kuali.student.r2.lum.course.service.CourseService;
32  
33  import java.io.Serializable;
34  import java.util.ArrayList;
35  import java.util.List;
36  
37  /**
38   * This class provides a key value finder for Activity Offering Clusters for the Create Activity Offering ui
39   *
40   * @author Kuali Student Team
41   */
42  public class ClustersForCreateAOKeyValues extends UifKeyValuesFinderBase implements Serializable {
43      @Override
44      public List<KeyValue> getKeyValues(ViewModel model) {
45          List<KeyValue> keyValues = new ArrayList<KeyValue>();
46          FormatsForCreateAOKeyValues formatsForCreateAOKeyValues = new FormatsForCreateAOKeyValues();
47          CourseOfferingManagementForm coForm = (CourseOfferingManagementForm) model;
48          String formatOfferingId = coForm.getFormatOfferingIdForNewAO();
49          coForm.setHasAOCluster(false);
50          //If there is no selected format and the CO has no formats, return an  empty list
51          if (StringUtils.isEmpty(formatOfferingId)) {
52              //  Just return if the CO has no formats (e.g. it was just created).
53              if (coForm.getFoId2aoTypeMap().isEmpty()) {
54                  return keyValues;
55              }
56              formatOfferingId = formatsForCreateAOKeyValues.getFirstKey(model);
57          }
58  
59          if (!StringUtils.isEmpty(formatOfferingId)) {
60              try {
61                  //Grab the clusters corresponding to the selected format id (or all if no selection was made)
62                  List<ActivityOfferingClusterWrapper> clusters = new ArrayList<ActivityOfferingClusterWrapper>();
63                  for (ActivityOfferingClusterWrapper cluster : coForm.getClusterResultList()) {
64                      if (StringUtils.isEmpty(formatOfferingId) || formatOfferingId.equals(cluster.getFormatOfferingId())) {
65                          clusters.add(cluster);
66                      }
67                  }
68  
69                  //if there are clusters for the given FO, display them in the dropdown, otherwise create a default cluster to display
70                  if (!clusters.isEmpty()) {
71                      for (ActivityOfferingClusterWrapper cluster : clusters) {
72                          keyValues.add(new ConcreteKeyValue(cluster.getAoCluster().getId()==null?"":cluster.getAoCluster().getId(), cluster.getAoCluster().getPrivateName()));
73                          coForm.setHasAOCluster(true);
74                      }
75                  } else {
76                      keyValues.add(new ConcreteKeyValue("", CourseOfferingManagementUtil.getCourseOfferingServiceFacade().getDefaultClusterNamePerCO(coForm.getCurrentCourseOfferingWrapper().getCourseOfferingId(), ContextUtils.getContextInfo())));
77                  }
78              } catch (Exception e) {
79                  throw new RuntimeException(e);
80              }
81          }
82  
83          return keyValues;
84      }
85  
86      protected CourseService getCourseService() {
87          return CourseOfferingResourceLoader.loadCourseService();
88      }
89  
90      protected TypeService getTypeService() {
91          return CourseOfferingResourceLoader.loadTypeService();
92      }
93  
94      protected CourseOfferingService getCourseOfferingService() {
95          return CourseOfferingResourceLoader.loadCourseOfferingService();
96      }
97  
98  
99  }