001    /**
002     * Copyright 2005-2011 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     */
016    package org.kuali.rice.krms.impl.repository;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.rice.core.api.uif.RemotableAttributeField;
020    import org.kuali.rice.kns.inquiry.KualiInquirableImpl;
021    import org.kuali.rice.krad.uif.container.Container;
022    import org.kuali.rice.krad.uif.view.View;
023    import org.kuali.rice.krad.web.form.InquiryForm;
024    import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
025    import org.kuali.rice.krms.framework.type.ActionTypeService;
026    import org.kuali.rice.krms.framework.type.AgendaTypeService;
027    import org.kuali.rice.krms.framework.type.RuleTypeService;
028    import org.kuali.rice.krms.impl.type.ActionTypeServiceBase;
029    import org.kuali.rice.krms.impl.type.AgendaTypeServiceBase;
030    import org.kuali.rice.krms.impl.type.RuleTypeServiceBase;
031    import org.kuali.rice.krms.impl.ui.AgendaEditor;
032    
033    import java.util.ArrayList;
034    import java.util.List;
035    import java.util.Map;
036    
037    public class AgendaInquiryHelperServiceImpl extends KualiInquirableImpl {
038    
039        @Override
040        public AgendaEditor retrieveDataObject(Map fieldValues) {
041            AgendaEditor agendaEditor = null;
042    
043            String agendaId = (String) fieldValues.get("id");
044            AgendaBo agenda = getBusinessObjectService().findBySinglePrimaryKey(AgendaBo.class, agendaId);
045            if (agenda != null) {
046                agendaEditor = new AgendaEditor();
047                agendaEditor.setAgenda(agenda);
048                agendaEditor.setNamespace(agenda.getContext().getNamespace());
049                agendaEditor.setContextName(agenda.getContext().getName());
050                agendaEditor.setCustomAttributesMap(agenda.getAttributes());
051            }
052    
053            return agendaEditor;
054        }
055    
056        public List<RemotableAttributeField> retrieveCustomAttributes(View view, Object model, Container container) {
057            List<RemotableAttributeField> results = new ArrayList<RemotableAttributeField>();
058    
059            InquiryForm inquiryForm = (InquiryForm)model;
060            AgendaEditor agendaEditor = (AgendaEditor)inquiryForm.getDataObject();
061    
062            // if we have an agenda w/ a typeId set on it
063            if (agendaEditor.getAgenda() != null && !StringUtils.isBlank(agendaEditor.getAgenda().getTypeId())) {
064    
065                String krmsTypeId = agendaEditor.getAgenda().getTypeId();
066    
067                AgendaTypeService agendaTypeService = getAgendaTypeService(krmsTypeId);
068                results.addAll(agendaTypeService.getAttributeFields(krmsTypeId));
069            }
070    
071            return results;
072        }
073    
074        private AgendaTypeService getAgendaTypeService(String krmsTypeId) {
075            //
076            // Get the AgendaTypeService by hook or by crook
077            //
078    
079            KrmsTypeDefinition krmsType =
080                        KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService().
081                                getTypeById(krmsTypeId);
082    
083            AgendaTypeService agendaTypeService = null;
084    
085            if (!StringUtils.isBlank(krmsTypeId)) {
086                String serviceName = krmsType.getServiceName();
087    
088                if (!StringUtils.isBlank(serviceName)) {
089                    agendaTypeService = KrmsRepositoryServiceLocator.getService(serviceName);
090                }
091            }
092    
093            if (agendaTypeService == null) { agendaTypeService = AgendaTypeServiceBase.defaultAgendaTypeService; }
094    
095            return agendaTypeService;
096        }
097    
098        /**
099         *  This only supports a single action within a rule.
100         */
101        public List<RemotableAttributeField> retrieveRuleActionCustomAttributes(View view, Object model, Container container) {
102            List<RemotableAttributeField> results = new ArrayList<RemotableAttributeField>();
103    
104            InquiryForm inquiryForm = (InquiryForm)model;
105            AgendaEditor agendaEditor = (AgendaEditor)inquiryForm.getDataObject();
106    
107            // if we have an rule action w/ a typeId set on it
108            if (!StringUtils.isBlank(agendaEditor.getAgendaItemLineRuleAction().getTypeId())) {
109                ActionTypeService actionTypeService = getActionTypeService(agendaEditor.getAgendaItemLineRuleAction().getTypeId());
110                results.addAll(actionTypeService.getAttributeFields(agendaEditor.getAgendaItemLineRuleAction().getTypeId()));
111            }
112    
113            return results;
114        }
115    
116        public List<RemotableAttributeField> retrieveRuleCustomAttributes(View view, Object model, Container container) {
117            List<RemotableAttributeField> results = new ArrayList<RemotableAttributeField>();
118    
119            InquiryForm inquiryForm = (InquiryForm)model;
120            AgendaEditor agendaEditor = (AgendaEditor)inquiryForm.getDataObject();
121    
122            // if we have an rule w/ a typeId set on it
123            if (agendaEditor.getAgendaItemLine() != null && agendaEditor.getAgendaItemLine().getRule() != null
124                    && !StringUtils.isBlank(agendaEditor.getAgendaItemLine().getRule().getTypeId())) {
125    
126                String krmsTypeId = agendaEditor.getAgendaItemLine().getRule().getTypeId();
127    
128                RuleTypeService ruleTypeService = getRuleTypeService(krmsTypeId);
129                results.addAll(ruleTypeService.getAttributeFields(krmsTypeId));
130            }
131    
132            return results;
133        }
134    
135        private ActionTypeService getActionTypeService(String krmsTypeId) {
136            //
137            // Get the ActionTypeService by hook or by crook
138            //
139    
140            KrmsTypeDefinition krmsType =
141                        KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService().
142                                getTypeById(krmsTypeId);
143    
144            ActionTypeService actionTypeService = null;
145    
146            if (!StringUtils.isBlank(krmsTypeId)) {
147                String serviceName = krmsType.getServiceName();
148    
149                if (!StringUtils.isBlank(serviceName)) {
150                    actionTypeService = KrmsRepositoryServiceLocator.getService(serviceName);
151                }
152            }
153            if (actionTypeService == null) {
154                actionTypeService = ActionTypeServiceBase.defaultActionTypeService;
155            }
156    
157    //        if (actionTypeService == null) { actionTypeService = AgendaTypeServiceBase.defaultAgendaTypeService; }
158    
159            return actionTypeService;
160        }
161    
162        private RuleTypeService getRuleTypeService(String krmsTypeId) {
163            RuleTypeService ruleTypeService = null;
164            String serviceName = getRuleTypeServiceName(krmsTypeId);
165    
166            if (!StringUtils.isBlank(serviceName)) {
167                ruleTypeService = KrmsRepositoryServiceLocator.getService(serviceName);
168            }
169            if (ruleTypeService == null) {
170                ruleTypeService = RuleTypeServiceBase.defaultRuleTypeService;
171            }
172            return ruleTypeService;
173        }
174    
175        private String getRuleTypeServiceName(String krmsTypeId) {
176            String serviceName = null;
177            KrmsTypeDefinition krmsType =
178                    KrmsRepositoryServiceLocator.getKrmsTypeRepositoryService().
179                            getTypeById(krmsTypeId);
180    
181            if (!StringUtils.isBlank(krmsTypeId)) {
182                serviceName = krmsType.getServiceName();
183            }
184            return serviceName;
185        }
186    
187    }