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.enrollment.class1.krms.keyvalue;
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.api.KrmsConstants;
25  import org.kuali.rice.krms.api.repository.RuleManagementService;
26  import org.kuali.rice.krms.api.repository.agenda.AgendaDefinition;
27  import org.kuali.rice.krms.api.repository.reference.ReferenceObjectBinding;
28  import org.kuali.student.enrollment.class1.krms.dto.FERuleManagementWrapper;
29  import org.kuali.student.r2.common.exceptions.DoesNotExistException;
30  import org.kuali.student.r2.common.exceptions.InvalidParameterException;
31  import org.kuali.student.r2.common.exceptions.MissingParameterException;
32  import org.kuali.student.r2.common.exceptions.OperationFailedException;
33  import org.kuali.student.r2.common.exceptions.PermissionDeniedException;
34  import org.kuali.student.common.util.security.ContextUtils;
35  import org.kuali.student.r2.core.class1.type.dto.TypeInfo;
36  import org.kuali.student.r2.core.class1.type.service.TypeService;
37  import org.kuali.student.r2.core.constants.AtpServiceConstants;
38  import org.kuali.student.r2.core.constants.KSKRMSServiceConstants;
39  import org.kuali.student.r2.core.constants.TypeServiceConstants;
40  
41  import javax.xml.namespace.QName;
42  import java.io.Serializable;
43  import java.util.ArrayList;
44  import java.util.List;
45  
46  /**
47   * This class created values for the term droplist in krms ui.
48   *
49   * @author Kuali Student Team
50   */
51  public class ExistingMatrixKeyValueFinder extends UifKeyValuesFinderBase implements Serializable {
52  
53      private static final long serialVersionUID = 1L;
54  
55      private TypeService typeService;
56      private RuleManagementService ruleManagementService;
57  
58      @Override
59      public List<KeyValue> getKeyValues(ViewModel model) {
60  
61          String typeKey = null;
62          MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm) model;
63          Object dataObject = maintenanceForm.getDocument().getNewMaintainableObject().getDataObject();
64          if (dataObject instanceof FERuleManagementWrapper) {
65              typeKey = ((FERuleManagementWrapper) dataObject).getType().getKey();
66          }
67  
68          List<KeyValue> keyValues = new ArrayList<KeyValue>();
69          try {
70  
71              keyValues.add(new ConcreteKeyValue("na", "Select Term Type"));
72  
73              for (TypeInfo type : getAcalTermTypes()) {
74  
75                  //Ignore current type key.
76                  if (type.getKey().equals(typeKey)) {
77                      continue;
78                  }
79                  if (ownsAgenda(type)) {
80                      ConcreteKeyValue keyValue = new ConcreteKeyValue();
81                      keyValue.setKey(type.getKey());
82                      keyValue.setValue(type.getName());
83                      keyValues.add(keyValue);
84                  }
85              }
86  
87          } catch (Exception e) {
88              throw new RuntimeException("Error getting term key values.", e);
89          }
90  
91          return keyValues;
92      }
93  
94      private boolean ownsAgenda(TypeInfo type) {
95          //Only add term types that already has a matrix.
96          List<ReferenceObjectBinding> refObjects = this.getRuleManagementService().findReferenceObjectBindingsByReferenceObject(TypeServiceConstants.REF_OBJECT_URI_TYPE, type.getKey());
97          if (refObjects.size() > 0) {
98              for (ReferenceObjectBinding refObject : refObjects) {
99                  AgendaDefinition agenda = this.getRuleManagementService().getAgenda(refObject.getKrmsObjectId());
100                 if (agenda.getAttributes().containsKey(KSKRMSServiceConstants.AGENDA_ATTRIBUTE_FINAL_EXAM_OWNER_TERM_TYPE)) {
101                     String ownerTermType = agenda.getAttributes().get(KSKRMSServiceConstants.AGENDA_ATTRIBUTE_FINAL_EXAM_OWNER_TERM_TYPE);
102                     if (type.getKey().equals(ownerTermType)) {
103                         return true;
104                     }
105                 }
106             }
107 
108         }
109 
110         return false;
111     }
112 
113     private List<TypeInfo> getAcalTermTypes() throws InvalidParameterException, MissingParameterException, DoesNotExistException, PermissionDeniedException, OperationFailedException {
114         return getTypeService().getTypesForGroupType(AtpServiceConstants.ATP_TERM_GROUPING_TYPE_KEY, ContextUtils.createDefaultContextInfo());
115     }
116 
117     public TypeService getTypeService() {
118         if (typeService == null) {
119             typeService = (TypeService) GlobalResourceLoader.getService(new QName(TypeServiceConstants.NAMESPACE, TypeServiceConstants.SERVICE_NAME_LOCAL_PART));
120         }
121         return this.typeService;
122     }
123 
124     public RuleManagementService getRuleManagementService() {
125         if (ruleManagementService == null) {
126             ruleManagementService = (RuleManagementService) GlobalResourceLoader.getService(new QName(KrmsConstants.Namespaces.KRMS_NAMESPACE_2_0, "ruleManagementService"));
127         }
128         return ruleManagementService;
129     }
130 
131 }
132