View Javadoc

1   /**
2    * Copyright 2005-2012 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 (!StringUtils.isBlank(agendaEditor.getAgendaItemLineRuleAction().getTypeId())) {
56              ActionTypeService actionTypeService = getActionTypeService(agendaEditor.getAgendaItemLineRuleAction().getTypeId());
57              results.addAll(actionTypeService.getAttributeFields(agendaEditor.getAgendaItemLineRuleAction().getTypeId()));
58          }
59  
60          return results;
61      }
62  
63      /**
64       * Get the AgendaEditor out of the MaintenanceForm's newMaintainableObject
65       * @param model the MaintenanceForm
66       * @return the AgendaEditor
67       */
68      private AgendaEditor getAgendaEditor(Object model) {
69          MaintenanceForm maintenanceForm = (MaintenanceForm)model;
70          return (AgendaEditor)maintenanceForm.getDocument().getNewMaintainableObject().getDataObject();
71      }
72  
73      private ActionTypeService getActionTypeService(String krmsTypeId) {
74          //
75          // Get the ActionTypeService by hook or by crook
76          //
77  
78          KrmsTypeDefinition krmsType = KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService().getTypeById(krmsTypeId);
79  
80          ActionTypeService actionTypeService = null;
81  
82          if (!StringUtils.isBlank(krmsTypeId)) {
83              String serviceName = krmsType.getServiceName();
84  
85              if (!StringUtils.isBlank(serviceName)) {
86                  actionTypeService = KrmsRepositoryServiceLocator.getService(serviceName);
87              }
88          }
89          if (actionTypeService == null) {
90              actionTypeService = ActionTypeServiceBase.defaultActionTypeService;
91          }
92  
93          //        if (actionTypeService == null) { actionTypeService = AgendaTypeServiceBase.defaultAgendaTypeService; }
94  
95          return actionTypeService;
96      }
97  
98      /**
99       *
100      * @param agendaEditor
101      * @return List<RemotableAttributeField>
102      */
103     public List<RemotableAttributeField> retrieveAgendaCustomAttributes(AgendaEditor agendaEditor) {
104         List<RemotableAttributeField> results = new ArrayList<RemotableAttributeField>();
105         // if we have an agenda w/ a typeId set on it
106         if (agendaEditor.getAgenda() != null && !StringUtils.isBlank(agendaEditor.getAgenda().getTypeId())) {
107 
108             String krmsTypeId = agendaEditor.getAgenda().getTypeId();
109 
110             AgendaTypeService agendaTypeService = getAgendaTypeService(krmsTypeId);
111             results.addAll(agendaTypeService.getAttributeFields(krmsTypeId));
112         }
113 
114         return results;
115     }
116 
117     private AgendaTypeService getAgendaTypeService(String krmsTypeId) {
118         //
119         // Get the AgendaTypeService by hook or by crook
120         //
121 
122         KrmsTypeDefinition krmsType =
123                 KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService().
124                         getTypeById(krmsTypeId);
125 
126         AgendaTypeService agendaTypeService = null;
127 
128         if (!StringUtils.isBlank(krmsTypeId)) {
129             String serviceName = krmsType.getServiceName();
130 
131             if (!StringUtils.isBlank(serviceName)) {
132                 agendaTypeService = KrmsRepositoryServiceLocator.getService(serviceName);
133             }
134         }
135 
136         if (agendaTypeService == null) { agendaTypeService = AgendaTypeServiceBase.defaultAgendaTypeService; }
137 
138         return agendaTypeService;
139     }
140 
141     public List<RemotableAttributeField> retrieveRuleCustomAttributes(AgendaEditor agendaEditor) {
142         List<RemotableAttributeField> results = new ArrayList<RemotableAttributeField>();
143         // if we have an rule w/ a typeId set on it
144         if (agendaEditor.getAgendaItemLine() != null && agendaEditor.getAgendaItemLine().getRule() != null
145                 && !StringUtils.isBlank(agendaEditor.getAgendaItemLine().getRule().getTypeId())) {
146 
147             String krmsTypeId = agendaEditor.getAgendaItemLine().getRule().getTypeId();
148 
149             RuleTypeService ruleTypeService = getRuleTypeService(krmsTypeId);
150             results.addAll(ruleTypeService.getAttributeFields(krmsTypeId));
151         }
152 
153         return results;
154     }
155 
156 
157     private RuleTypeService getRuleTypeService(String krmsTypeId) {
158         RuleTypeService ruleTypeService = null;
159         String serviceName = getRuleTypeServiceName(krmsTypeId);
160 
161         if (!StringUtils.isBlank(serviceName)) {
162             ruleTypeService = KrmsRepositoryServiceLocator.getService(serviceName);
163         }
164         if (ruleTypeService == null) {
165             ruleTypeService = RuleTypeServiceBase.defaultRuleTypeService;
166         }
167         return ruleTypeService;
168     }
169 
170     private String getRuleTypeServiceName(String krmsTypeId) {
171         String serviceName = null;
172         KrmsTypeDefinition krmsType =
173                 KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService().
174                         getTypeById(krmsTypeId);
175 
176         if (!StringUtils.isBlank(krmsTypeId)) {
177             serviceName = krmsType.getServiceName();
178         }
179         return serviceName;
180     }
181 }