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