View Javadoc
1   /**
2    * Copyright 2005-2016 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.rice.krms.impl.util;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.uif.RemotableAttributeField;
20  import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
21  import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
22  import org.kuali.rice.krms.framework.type.ActionTypeService;
23  import org.kuali.rice.krms.framework.type.AgendaTypeService;
24  import org.kuali.rice.krms.framework.type.RuleTypeService;
25  import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
26  import org.kuali.rice.krms.impl.type.ActionTypeServiceBase;
27  import org.kuali.rice.krms.impl.type.AgendaTypeServiceBase;
28  import org.kuali.rice.krms.impl.type.RuleTypeServiceBase;
29  import org.kuali.rice.krms.impl.ui.AgendaEditor;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  /**
35   * KRMS utilility class for retrieving KRMS objects for {@link org.kuali.rice.krms.impl.ui.AgendaEditorMaintainable} and
36   * {@link org.kuali.rice.krms.impl.repository.AgendaInquiryHelperServiceImpl}
37   *
38   * @author Kuali Rice Team (rice.collab@kuali.org)
39   */
40  public class KrmsRetriever {
41  
42      private static final long serialVersionUID = 1L;
43  
44      /**
45       * This only supports a single action within a rule.
46       * @param agendaEditor
47       * @return List<RemotableAttributeField>
48       */
49      public List<RemotableAttributeField> retrieveRuleActionCustomAttributes(AgendaEditor agendaEditor) {
50          List<RemotableAttributeField> results = new ArrayList<RemotableAttributeField>();
51          // if we have an rule action w/ a typeId set on it
52          if (agendaEditor != null && agendaEditor.getAgendaItemLineRuleAction() != null
53                  &&!StringUtils.isBlank(agendaEditor.getAgendaItemLineRuleAction().getTypeId())) {
54              ActionTypeService actionTypeService = getActionTypeService(agendaEditor.getAgendaItemLineRuleAction().getTypeId());
55              results.addAll(actionTypeService.getAttributeFields(agendaEditor.getAgendaItemLineRuleAction().getTypeId()));
56          }
57  
58          return results;
59      }
60  
61      /**
62       * Get the AgendaEditor out of the MaintenanceDocumentForm's newMaintainableObject
63       * @param model the MaintenanceDocumentForm
64       * @return the AgendaEditor
65       */
66      private AgendaEditor getAgendaEditor(Object model) {
67          MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm)model;
68          return (AgendaEditor)maintenanceForm.getDocument().getNewMaintainableObject().getDataObject();
69      }
70  
71      private ActionTypeService getActionTypeService(String krmsTypeId) {
72          //
73          // Get the ActionTypeService by hook or by crook
74          //
75  
76          KrmsTypeDefinition krmsType = KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService().getTypeById(krmsTypeId);
77  
78          ActionTypeService actionTypeService = null;
79  
80          if (!StringUtils.isBlank(krmsTypeId)) {
81              String serviceName = krmsType.getServiceName();
82  
83              if (!StringUtils.isBlank(serviceName)) {
84                  actionTypeService = KrmsRepositoryServiceLocator.getService(serviceName);
85              }
86          }
87          if (actionTypeService == null) {
88              actionTypeService = ActionTypeServiceBase.defaultActionTypeService;
89          }
90  
91          //        if (actionTypeService == null) { actionTypeService = AgendaTypeServiceBase.defaultAgendaTypeService; }
92  
93          return actionTypeService;
94      }
95  
96      /**
97       *
98       * @param agendaEditor
99       * @return List<RemotableAttributeField>
100      */
101     public List<RemotableAttributeField> retrieveAgendaCustomAttributes(AgendaEditor agendaEditor) {
102         List<RemotableAttributeField> results = new ArrayList<RemotableAttributeField>();
103         // if we have an agenda w/ a typeId set on it
104         if (agendaEditor != null && agendaEditor.getAgenda() != null
105                 && !StringUtils.isBlank(agendaEditor.getAgenda().getTypeId())) {
106 
107             String krmsTypeId = agendaEditor.getAgenda().getTypeId();
108 
109             AgendaTypeService agendaTypeService = getAgendaTypeService(krmsTypeId);
110             results.addAll(agendaTypeService.getAttributeFields(krmsTypeId));
111         }
112 
113         return results;
114     }
115 
116     private AgendaTypeService getAgendaTypeService(String krmsTypeId) {
117         //
118         // Get the AgendaTypeService by hook or by crook
119         //
120 
121         KrmsTypeDefinition krmsType =
122                 KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService().
123                         getTypeById(krmsTypeId);
124 
125         AgendaTypeService agendaTypeService = null;
126 
127         if (!StringUtils.isBlank(krmsTypeId)) {
128             String serviceName = krmsType.getServiceName();
129 
130             if (!StringUtils.isBlank(serviceName)) {
131                 agendaTypeService = KrmsRepositoryServiceLocator.getService(serviceName);
132             }
133         }
134 
135         if (agendaTypeService == null) { agendaTypeService = AgendaTypeServiceBase.defaultAgendaTypeService; }
136 
137         return agendaTypeService;
138     }
139 
140     public List<RemotableAttributeField> retrieveRuleCustomAttributes(AgendaEditor agendaEditor) {
141         List<RemotableAttributeField> results = new ArrayList<RemotableAttributeField>();
142         // if we have an rule w/ a typeId set on it
143         if (agendaEditor != null && agendaEditor.getAgendaItemLine() != null && agendaEditor.getAgendaItemLine().getRule() != null
144                 && !StringUtils.isBlank(agendaEditor.getAgendaItemLine().getRule().getTypeId())) {
145 
146             String krmsTypeId = agendaEditor.getAgendaItemLine().getRule().getTypeId();
147 
148             RuleTypeService ruleTypeService = getRuleTypeService(krmsTypeId);
149             results.addAll(ruleTypeService.getAttributeFields(krmsTypeId));
150         }
151 
152         return results;
153     }
154 
155 
156     private RuleTypeService getRuleTypeService(String krmsTypeId) {
157         RuleTypeService ruleTypeService = null;
158         String serviceName = getRuleTypeServiceName(krmsTypeId);
159 
160         if (!StringUtils.isBlank(serviceName)) {
161             ruleTypeService = KrmsRepositoryServiceLocator.getService(serviceName);
162         }
163         if (ruleTypeService == null) {
164             ruleTypeService = RuleTypeServiceBase.defaultRuleTypeService;
165         }
166         return ruleTypeService;
167     }
168 
169     private String getRuleTypeServiceName(String krmsTypeId) {
170         String serviceName = null;
171         KrmsTypeDefinition krmsType =
172                 KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService().
173                         getTypeById(krmsTypeId);
174 
175         if (!StringUtils.isBlank(krmsTypeId)) {
176             serviceName = krmsType.getServiceName();
177         }
178         return serviceName;
179     }
180 }