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