View Javadoc
1   /**
2    * Copyright 2005-2013 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.student.lum.lu.ui.course.keyvalues;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.Comparator;
21  import java.util.List;
22  
23  import javax.xml.namespace.QName;
24  
25  import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
26  import org.kuali.rice.core.api.util.ConcreteKeyValue;
27  import org.kuali.rice.core.api.util.KeyValue;
28  import org.kuali.rice.krad.uif.control.UifKeyValuesFinderBase;
29  import org.kuali.rice.krad.uif.view.ViewModel;
30  import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
31  import org.kuali.rice.krms.dto.PropositionEditor;
32  import org.kuali.rice.krms.dto.RuleEditor;
33  import org.kuali.rice.krms.util.AgendaUtilities;
34  import org.kuali.rice.krms.util.PropositionTreeUtil;
35  import org.kuali.student.common.util.ContextBuilder;
36  import org.kuali.student.lum.lu.ui.krms.dto.LUPropositionEditor;
37  import org.kuali.student.r2.common.dto.ContextInfo;
38  import org.kuali.student.r2.lum.lrc.dto.ResultValueInfo;
39  import org.kuali.student.r2.lum.lrc.dto.ResultValuesGroupInfo;
40  import org.kuali.student.r2.lum.lrc.service.LRCService;
41  import org.kuali.student.r2.lum.util.constants.LrcServiceConstants;
42  
43  /**
44   * KeyFinder for Grade Values based on the Grade Scale.
45   *
46   * @author Kuali Student Team
47   */
48  public class CMGradeValuesKeyFinder extends UifKeyValuesFinderBase {
49  
50      private LRCService lrcService;
51  
52      @Override
53      public List<KeyValue> getKeyValues(ViewModel model) {
54          List<KeyValue> keyValues = new ArrayList<KeyValue>();
55          String resultValuesGroupKey = "";
56  
57          MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) model;
58          RuleEditor ruleEditor = AgendaUtilities.getRuleWrapper(maintenanceForm).getRuleEditor();
59  
60          PropositionEditor propositionEditor = PropositionTreeUtil.getProposition(ruleEditor);
61          if ((propositionEditor != null) && (propositionEditor instanceof LUPropositionEditor)) {
62                  resultValuesGroupKey = ((LUPropositionEditor) propositionEditor).getGradeScale();
63          }
64          
65          try {
66  
67              if (resultValuesGroupKey != null) {
68                  ResultValuesGroupInfo rvgInfo = this.getLRCService().getResultValuesGroup(resultValuesGroupKey, this.getContextInfo());
69                  List<ResultValueInfo> rvInfos = this.getLRCService().getResultValuesByKeys(rvgInfo.getResultValueKeys(), this.getContextInfo());
70  
71                  Collections.sort(rvInfos, new Comparator<ResultValueInfo>() {
72  
73                      @Override
74                      public int compare(ResultValueInfo o1, ResultValueInfo o2) {
75                          Integer first = Integer.valueOf(o1.getNumericValue());
76                          Integer second = Integer.valueOf(o2.getNumericValue());
77                          return (second < first) ? -1 : ((second.equals(first)) ? 0 : 1);
78                      }
79                  });
80  
81                  for (ResultValueInfo info : rvInfos) {
82                      keyValues.add(new ConcreteKeyValue(info.getKey(), info.getName()));
83                  }
84              }
85  
86          } catch (Exception e) {
87              throw new RuntimeException("Unable to retrieve result values", e);
88          }
89  
90          return keyValues;
91      }
92  
93      private LRCService getLRCService() {
94          if (lrcService == null) {
95              QName qname = new QName(LrcServiceConstants.NAMESPACE, LrcServiceConstants.SERVICE_NAME_LOCAL_PART);
96              lrcService = (LRCService) GlobalResourceLoader.getService(qname);
97          }
98          return lrcService;
99      }
100 
101     private ContextInfo getContextInfo() {
102         return ContextBuilder.loadContextInfo();
103     }
104 }