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