001/**
002 * Copyright 2005-2016 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.kew.document;
017
018import java.util.Collections;
019import java.util.List;
020import java.util.Map;
021
022import org.kuali.rice.core.api.exception.RiceRuntimeException;
023import org.kuali.rice.kew.api.KEWPropertyConstants;
024import org.kuali.rice.kew.rule.RuleBaseValues;
025import org.kuali.rice.kew.rule.RuleResponsibilityBo;
026import org.kuali.rice.kew.rule.bo.RuleTemplateBo;
027import org.kuali.rice.kew.rule.web.WebRuleUtils;
028import org.kuali.rice.kew.service.KEWServiceLocator;
029import org.kuali.rice.kns.document.MaintenanceDocument;
030import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
031import org.kuali.rice.kns.maintenance.Maintainable;
032import org.kuali.rice.kns.web.ui.Section;
033import org.kuali.rice.krad.bo.PersistableBusinessObject;
034import org.kuali.rice.krad.data.KradDataServiceLocator;
035import org.kuali.rice.krad.maintenance.MaintenanceLock;
036import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
037
038/**
039 * This class is the maintainable implementation for Routing Rules
040 * in KEW (represented by the {@link RuleBaseValues} business object).
041 *
042 * @author Kuali Rice Team (rice.collab@kuali.org)
043 *
044 */
045public class RoutingRuleMaintainable extends KualiMaintainableImpl {
046
047        private static final long serialVersionUID = -5920808902137192662L;
048
049        /**
050         * Override the getSections method on this maintainable so that the Section Containing the various Rule Attributes
051         * can be dynamically generated based on the RuleTemplate which is selected.
052         */
053        @Override
054        public List getSections(MaintenanceDocument document, Maintainable oldMaintainable) {
055            // since the child lists are not repopulated upon loading the objects, we need to re-load them manually so the section
056            // generation functions.
057        if ( getNewRule(document) != null ) {   
058            getNewRule(document).setRuleTemplate( getDataObjectService().find(RuleTemplateBo.class, getNewRule(document).getRuleTemplateId() ) );
059        }
060        
061        if ( getOldRule(document) != null ) {   
062            getOldRule(document).setRuleTemplate( getDataObjectService().find(RuleTemplateBo.class, getOldRule(document).getRuleTemplateId() ) );
063        }
064
065            List<Section> sections = super.getSections(document, oldMaintainable);
066                return WebRuleUtils.customizeSections(getThisRule(), sections, false);
067        }
068
069        /**
070         * On creation of a new rule document, we must validate that a rule template and document type are set.
071         */
072        @Override
073        public void processAfterNew(MaintenanceDocument document,
074                        Map<String, String[]> parameters) {
075                WebRuleUtils.validateRuleTemplateAndDocumentType(getOldRule(document), getNewRule(document), parameters);
076                WebRuleUtils.establishDefaultRuleValues(getNewRule(document));
077                getNewRule(document).setDocumentId(document.getDocumentHeader().getDocumentNumber());
078        }
079
080        /**
081         * This is a hack to get around the fact that when a document is first created, this value is
082         * true which causes issues if you want to be able to initialize fields on  the document using
083         * request parameters.  See SectionBridge.toSection for the "if" block where it populates
084         * Field.propertyValue to see why this causes problems
085         */
086        @Override
087        public void setGenerateBlankRequiredValues(String docTypeName) {
088
089        }
090
091    /**
092     * A complete override of the implementation for saving a Rule
093     */
094    @Override
095    public void saveBusinessObject() {
096        WebRuleUtils.clearKeysForSave(getThisRule());
097        WebRuleUtils.translateResponsibilitiesForSave(getThisRule());
098        WebRuleUtils.translateFieldValuesForSave(getThisRule());
099        KEWServiceLocator.getRuleService().makeCurrent(getThisRule(), true);
100    }
101    
102    @Override
103    public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> parameters) {
104        WebRuleUtils.processRuleForCopy(document.getDocumentNumber(), getOldRule(document), getNewRule(document));
105        super.processAfterCopy(document, parameters);
106    }
107
108        @Override
109        public void processAfterEdit(MaintenanceDocument document,
110                        Map<String, String[]> parameters) {
111                if (!getOldRule(document).getCurrentInd()) {
112                        throw new RiceRuntimeException("Cannot edit a non-current version of a rule.");
113                }
114                WebRuleUtils.populateForCopyOrEdit(getOldRule(document), getNewRule(document));
115
116                getNewRule(document).setPreviousRuleId(getOldRule(document).getId());
117
118                getNewRule(document).setDocumentId(document.getDocumentHeader().getDocumentNumber());
119                super.processAfterEdit(document, parameters);
120        }
121
122        /**
123         * Returns the new RuleBaseValues business object.
124         */
125        protected RuleBaseValues getNewRule(MaintenanceDocument document) {
126                return (RuleBaseValues)document.getNewMaintainableObject().getBusinessObject();
127        }
128
129        /**
130         * Returns the old RuleBaseValues business object.
131         */
132        protected RuleBaseValues getOldRule(MaintenanceDocument document) {
133                return (RuleBaseValues)document.getOldMaintainableObject().getBusinessObject();
134        }
135
136        /**
137         * Returns the RuleBaseValues business object associated with this Maintainable.
138         */
139        protected RuleBaseValues getThisRule() {
140                return (RuleBaseValues)getBusinessObject();
141        }
142
143        /**
144         * Overridden implementation of maintenance locks.  The default locking for Routing Rules
145         * is based on previous version (can't route more than one rule based off the same
146         * previous verison).  However, for the first version of a rule, the previous version id
147         * will be null.
148         *
149         * So for a new Route Rule maintenance document we don't want any locks generated.
150         *
151         * TODO can we just let the locking key be the primary key? (ruleBaseValuesId)
152         */
153        @Override
154        public List<MaintenanceLock> generateMaintenanceLocks() {
155                if (getThisRule().getPreviousRuleId() == null) {
156                        return Collections.emptyList();
157                }
158                return super.generateMaintenanceLocks();
159        }
160
161        @Override
162        public String getDocumentTitle(MaintenanceDocument document) {
163                StringBuffer title = new StringBuffer();
164        RuleBaseValues rule = getThisRule();
165        if (rule.getPreviousRuleId() != null) {
166            title.append("Editing Rule '").append(rule.getDescription()).append("'");
167        } else {
168            title.append("Adding Rule '").append(rule.getDescription()).append("'");
169        }
170        return title.toString();
171        }
172
173//      /**
174//       * This overridden method ...
175//       *
176//       * @see org.kuali.rice.krad.maintenance.KualiMaintainableImpl#prepareForSave()
177//       */
178//      @Override
179//      public void prepareForSave() {
180//              super.prepareForSave();
181//              WebRuleUtils.translateResponsibilitiesForSave(getThisRule());
182//      }
183
184    /**
185         * @see org.kuali.rice.krad.maintenance.KualiMaintainableImpl#setNewCollectionLineDefaultValues(java.lang.String, org.kuali.rice.krad.bo.PersistableBusinessObject)
186         */
187        @Override
188        protected void setNewCollectionLineDefaultValues(String collectionName,
189                        PersistableBusinessObject addLine) {
190                super.setNewCollectionLineDefaultValues(collectionName, addLine);
191                if (KEWPropertyConstants.RESP_SECTION_NAME_SET.contains(collectionName)) {
192                        RuleTemplateBo ruleTemplate = getThisRule().getRuleTemplate();
193                        if(ruleTemplate.getDefaultActionRequestValue() != null && ruleTemplate.getDefaultActionRequestValue().getValue() != null){
194                                ((RuleResponsibilityBo) addLine).setActionRequestedCd(ruleTemplate.getDefaultActionRequestValue().getValue());
195                }
196                }
197        }
198
199}