View Javadoc

1   /**
2    * Copyright 2012 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   */
16  package org.kuali.student.enrollment.class2.courseoffering.util;
17  
18  import org.kuali.rice.core.api.util.ConcreteKeyValue;
19  import org.kuali.rice.core.api.util.KeyValue;
20  import org.kuali.rice.kim.api.identity.Person;
21  import org.kuali.rice.kim.api.identity.PersonService;
22  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
23  import org.kuali.rice.kim.impl.KIMPropertyConstants;
24  import org.kuali.student.enrollment.common.util.ContextBuilder;
25  import org.kuali.student.enrollment.courseoffering.dto.CourseOfferingInfo;
26  import org.kuali.student.enrollment.courseoffering.dto.OfferingInstructorInfo;
27  import org.kuali.student.r2.common.util.ContextUtils;
28  import org.kuali.student.r2.core.class1.type.dto.TypeInfo;
29  import org.kuali.student.r2.lum.course.dto.ActivityInfo;
30  import org.kuali.student.r2.lum.course.dto.CourseInfo;
31  import org.kuali.student.r2.lum.course.dto.FormatInfo;
32  import org.kuali.student.r2.lum.course.service.CourseService;
33  import org.kuali.student.r2.lum.course.service.assembler.CourseAssemblerConstants;
34  import org.kuali.student.r2.common.dto.ContextInfo;
35  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
36  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
37  import org.kuali.student.r2.common.exceptions.MissingParameterException;
38  import org.kuali.student.r2.common.exceptions.OperationFailedException;
39  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
40  import org.kuali.student.r2.lum.util.constants.LrcServiceConstants;
41  import org.kuali.student.r2.core.class1.type.service.TypeService;
42  import org.kuali.student.r2.lum.lrc.dto.ResultValueInfo;
43  import org.kuali.student.r2.lum.lrc.dto.ResultValuesGroupInfo;
44  import org.kuali.student.r2.lum.lrc.service.LRCService;
45  
46  import java.util.*;
47  
48  /**
49   * This class //TODO ...
50   *
51   * @author Kuali Student Team
52   */
53  public class ViewHelperUtil {
54      private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ViewHelperUtil.class);
55      public static List<Person> getInstructorByPersonId(String personId){
56          Map<String, String> searchCriteria = new HashMap<String, String>();
57          searchCriteria.put(KIMPropertyConstants.Person.ENTITY_ID, personId);
58          List<Person> lstPerson = getPersonService().findPeople(searchCriteria);
59          return lstPerson;
60      }
61  
62      public static PersonService getPersonService() {
63          return KimApiServiceLocator.getPersonService();
64      }
65  
66      public static String buildDerivedFormatName(TypeService typeService, ContextInfo contextInfo, FormatInfo formatInfo) throws DoesNotExistException, InvalidParameterException, MissingParameterException, OperationFailedException, PermissionDeniedException {
67          StringBuilder formatNameBuilder = new StringBuilder();
68  
69          // Create a derived name based on the activities, until https://jira.kuali.org/browse/KSENROLL-1518 is finished
70          List<ActivityInfo> activities = formatInfo.getActivities();
71          for (ActivityInfo activity : activities) {
72              if(formatNameBuilder.length() != 0) {
73                  formatNameBuilder.append(" / ");
74              }
75              TypeInfo type = typeService.getType(activity.getTypeKey(), contextInfo);
76              formatNameBuilder.append(type.getName());
77          }
78  
79          if(formatInfo.getActivities().size() == 1) {
80              formatNameBuilder.append(" Only");
81          }
82  
83          return formatNameBuilder.toString();
84      }
85  
86      public static List<KeyValue> collectActivityTypeKeyValues(CourseInfo course, TypeService typeService, ContextInfo contextInfo) {
87          List<KeyValue> results = new ArrayList<KeyValue>();
88  
89          Set<String> activityTypes = new HashSet<String>();
90          for(FormatInfo format : course.getFormats()) {
91              for (ActivityInfo activity : format.getActivities()) {
92                  // if we haven't added a value for this activity type yet
93                  if(activityTypes.add(activity.getTypeKey())) {
94                      try {
95                          TypeInfo type = typeService.getType(activity.getTypeKey(), contextInfo);
96                          results.add(new ConcreteKeyValue(type.getKey(), type.getName()));
97                      } catch (Exception e) {
98                          throw new RuntimeException(e);
99                      }
100                 }
101             }
102         }
103 
104         return results;
105     }
106 
107 
108     public static String trimTrailing0(String creditValue){
109         if (creditValue.indexOf(".0") > 0) {
110             return creditValue.substring(0, creditValue.length( )- 2);
111         } else {
112             return creditValue;
113         }
114     }
115 
116 
117     //get credit count from persisted COInfo or from CourseInfo
118     public static String getCreditCount(CourseOfferingInfo coInfo, CourseInfo courseInfo) throws Exception{
119         String creditOptionId = coInfo.getCreditOptionId();
120         String creditCount = "";
121         if (creditOptionId != null) { //Lookup persisted values
122             ResultValuesGroupInfo resultValuesGroupInfo = getLrcService().getResultValuesGroup(creditOptionId, ContextBuilder.loadContextInfo());
123             String typeKey = resultValuesGroupInfo.getTypeKey();
124             //Get the actual values
125             List<ResultValueInfo> resultValueInfos = getLrcService().getResultValuesByKeys(resultValuesGroupInfo.getResultValueKeys(), ContextBuilder.loadContextInfo());
126             if (typeKey.equals(LrcServiceConstants.RESULT_VALUES_GROUP_TYPE_KEY_FIXED)) {                                 //fixed
127                 if (!resultValueInfos.isEmpty()) {
128                     creditCount = trimTrailing0(resultValueInfos.get(0).getValue());
129                 }
130             } else if (typeKey.equals(LrcServiceConstants.RESULT_VALUES_GROUP_TYPE_KEY_RANGE)) {                          //range
131                 creditCount = trimTrailing0(resultValuesGroupInfo.getResultValueRange().getMinValue()) + " - " +
132                         trimTrailing0(resultValuesGroupInfo.getResultValueRange().getMaxValue());
133             } else if (typeKey.equals(LrcServiceConstants.RESULT_VALUES_GROUP_TYPE_KEY_MULTIPLE)) {                       //range
134                 if (!resultValueInfos.isEmpty()) {
135                     List<Float> creditValuesF = new ArrayList();
136                     for (ResultValueInfo resultValueInfo : resultValueInfos ) {  //convert String to Float for sorting
137                         creditValuesF.add(Float.valueOf(resultValueInfo.getValue()));
138                     }
139                     Collections.sort(creditValuesF);
140                     for (Float creditF : creditValuesF ){
141                         creditCount = creditCount + ", " + trimTrailing0(String.valueOf(creditF));
142                     }
143                 creditCount =  creditCount.substring(2);  //trim leading ", "
144                 }
145             }
146         } else { //Lookup original course values
147             if (courseInfo == null) {
148                 courseInfo = (CourseInfo) getCourseService().getCourse(coInfo.getCourseId(), ContextUtils.getContextInfo());
149             }
150             String creditOpt = courseInfo.getCreditOptions().get(0).getType();
151             if (creditOpt.equalsIgnoreCase(CourseAssemblerConstants.COURSE_RESULT_COMP_TYPE_CREDIT_FIXED) ){              //fixed
152                 creditCount = trimTrailing0(getLrcService().getResultValue(courseInfo.getCreditOptions().get(0).getResultValueKeys().get(0), ContextUtils.getContextInfo()).getValue());
153             } else if (creditOpt.equalsIgnoreCase(CourseAssemblerConstants.COURSE_RESULT_COMP_TYPE_CREDIT_VARIABLE) ){    //range
154                 //minCreditValue - maxCreditValue
155                 creditCount = trimTrailing0(courseInfo.getCreditOptions().get(0).getAttributeValue(LrcServiceConstants.R1_DYN_ATTR_CREDIT_OPTION_MIN_CREDITS))
156                         +" - "+trimTrailing0(courseInfo.getCreditOptions().get(0).getAttributeValue(LrcServiceConstants.R1_DYN_ATTR_CREDIT_OPTION_MAX_CREDITS));
157             } else if (creditOpt.equalsIgnoreCase(CourseAssemblerConstants.COURSE_RESULT_COMP_TYPE_CREDIT_MULTIPLE) ){    //multiple
158                 List<ResultValueInfo> creditValuesRVI = getLrcService().getResultValuesByKeys(courseInfo.getCreditOptions().get(0).getResultValueKeys(), ContextUtils.getContextInfo());
159                 List<Float> creditValuesF = new ArrayList();
160                 for (ResultValueInfo creditRVI : creditValuesRVI ) {  //convert String to Float for sorting
161                     creditValuesF.add(Float.valueOf(creditRVI.getValue()));
162                 }
163                 Collections.sort(creditValuesF);
164                 for (Float creditF : creditValuesF ){
165                     creditCount = creditCount + ", " + trimTrailing0(String.valueOf(creditF));
166                 }
167                 creditCount =  creditCount.substring(2);  //trim leading ", "
168             } else {
169                 //no credit option
170                 LOG.info("Credit is missing for subject course " + coInfo.getCourseCode());
171                 return "N/A";
172             }
173         }
174         return creditCount;
175     }
176 
177     public static LRCService getLrcService() {
178         return CourseOfferingResourceLoader.loadLrcService();
179     }
180 
181     public static CourseService getCourseService() {
182         return CourseOfferingResourceLoader.loadCourseService();
183     }
184 
185     public static OfferingInstructorInfo findDisplayInstructor(List<OfferingInstructorInfo> instructors) {
186         OfferingInstructorInfo result = null;
187 
188         if(instructors != null && !instructors.isEmpty()) {
189 
190             // Build the display name for the Instructor
191             Collection<OfferingInstructorInfo> highestInstEffortInstructors = new ArrayList<OfferingInstructorInfo>();
192             float highestInstEffortComparison = 0f;
193 
194             for (OfferingInstructorInfo instructor : instructors) {
195                 // if this instructor has a higher percent effort than any previous instructors,
196                 // clear the list we are keeping track of and set the new comparison number to this instructor's percentage effort
197                 if(instructor.getPercentageEffort() > highestInstEffortComparison) {
198                     highestInstEffortInstructors.clear();
199                     highestInstEffortComparison = instructor.getPercentageEffort();
200                     highestInstEffortInstructors.add(instructor);
201                 }
202                 // if this instructor's percent effort is tied with the comparison number,
203                 // add this instructor to the list of highest effort instructors
204                 else if (instructor.getPercentageEffort() == highestInstEffortComparison) {
205                     highestInstEffortInstructors.add(instructor);
206                 }
207             }
208 
209             if(highestInstEffortInstructors.size() == 1) {
210                 result = highestInstEffortInstructors.iterator().next();
211             }
212             else {
213                 List<String> names = new ArrayList<String>(highestInstEffortInstructors.size());
214                 Map<String, OfferingInstructorInfo> nameMap = new HashMap<String, OfferingInstructorInfo>(highestInstEffortInstructors.size());
215                 for(OfferingInstructorInfo oiInfo : highestInstEffortInstructors) {
216                     names.add(oiInfo.getPersonName());
217                     nameMap.put(oiInfo.getPersonName(), oiInfo);
218                 }
219 
220                 Collections.sort(names);
221 
222                 result = nameMap.get(names.get(0));
223             }
224         }
225 
226         return result;
227     }
228 }