001/**
002 * Copyright 2005-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.krms.impl.util;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.uif.RemotableAttributeField;
020import org.kuali.rice.krad.web.form.MaintenanceDocumentForm;
021import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
022import org.kuali.rice.krms.framework.type.ActionTypeService;
023import org.kuali.rice.krms.framework.type.AgendaTypeService;
024import org.kuali.rice.krms.framework.type.RuleTypeService;
025import org.kuali.rice.krms.impl.repository.KrmsRepositoryServiceLocator;
026import org.kuali.rice.krms.impl.type.ActionTypeServiceBase;
027import org.kuali.rice.krms.impl.type.AgendaTypeServiceBase;
028import org.kuali.rice.krms.impl.type.RuleTypeServiceBase;
029import org.kuali.rice.krms.impl.ui.AgendaEditor;
030
031import java.util.ArrayList;
032import java.util.List;
033
034/**
035 * KRMS utilility class for retrieving KRMS objects for {@link org.kuali.rice.krms.impl.ui.AgendaEditorMaintainable} and
036 * {@link org.kuali.rice.krms.impl.repository.AgendaInquiryHelperServiceImpl}
037 *
038 * @author Kuali Rice Team (rice.collab@kuali.org)
039 */
040public class KrmsRetriever {
041
042    private static final long serialVersionUID = 1L;
043
044    /**
045     * This only supports a single action within a rule.
046     * @param agendaEditor
047     * @return List<RemotableAttributeField>
048     */
049    public List<RemotableAttributeField> retrieveRuleActionCustomAttributes(AgendaEditor agendaEditor) {
050        List<RemotableAttributeField> results = new ArrayList<RemotableAttributeField>();
051        // if we have an rule action w/ a typeId set on it
052        if (agendaEditor != null && agendaEditor.getAgendaItemLineRuleAction() != null
053                &&!StringUtils.isBlank(agendaEditor.getAgendaItemLineRuleAction().getTypeId())) {
054            ActionTypeService actionTypeService = getActionTypeService(agendaEditor.getAgendaItemLineRuleAction().getTypeId());
055            results.addAll(actionTypeService.getAttributeFields(agendaEditor.getAgendaItemLineRuleAction().getTypeId()));
056        }
057
058        return results;
059    }
060
061    /**
062     * Get the AgendaEditor out of the MaintenanceDocumentForm's newMaintainableObject
063     * @param model the MaintenanceDocumentForm
064     * @return the AgendaEditor
065     */
066    private AgendaEditor getAgendaEditor(Object model) {
067        MaintenanceDocumentForm maintenanceForm = (MaintenanceDocumentForm)model;
068        return (AgendaEditor)maintenanceForm.getDocument().getNewMaintainableObject().getDataObject();
069    }
070
071    private ActionTypeService getActionTypeService(String krmsTypeId) {
072        //
073        // Get the ActionTypeService by hook or by crook
074        //
075
076        KrmsTypeDefinition krmsType = KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService().getTypeById(krmsTypeId);
077
078        ActionTypeService actionTypeService = null;
079
080        if (!StringUtils.isBlank(krmsTypeId)) {
081            String serviceName = krmsType.getServiceName();
082
083            if (!StringUtils.isBlank(serviceName)) {
084                actionTypeService = KrmsRepositoryServiceLocator.getService(serviceName);
085            }
086        }
087        if (actionTypeService == null) {
088            actionTypeService = ActionTypeServiceBase.defaultActionTypeService;
089        }
090
091        //        if (actionTypeService == null) { actionTypeService = AgendaTypeServiceBase.defaultAgendaTypeService; }
092
093        return actionTypeService;
094    }
095
096    /**
097     *
098     * @param agendaEditor
099     * @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}