001    /**
002     * Copyright 2005-2012 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.rule.RuleBaseValues;
024    import org.kuali.rice.kew.rule.RuleDelegationBo;
025    import org.kuali.rice.kew.rule.web.WebRuleUtils;
026    import org.kuali.rice.kew.service.KEWServiceLocator;
027    import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
028    import org.kuali.rice.kns.document.MaintenanceDocument;
029    import org.kuali.rice.kns.web.ui.Section;
030    import org.kuali.rice.krad.maintenance.MaintenanceLock;
031    import org.kuali.rice.kns.maintenance.Maintainable;
032    
033    /**
034     * This class is the maintainable implementation for Routing Rules 
035     * in KEW (represented by the {@link RuleBaseValues} business object). 
036     * 
037     * @author Kuali Rice Team (rice.collab@kuali.org)
038     *
039     */
040    public class RoutingRuleDelegationMaintainable extends KualiMaintainableImpl {
041            
042            /**
043             * Override the getSections method on this maintainable so that the Section Containing the various Rule Attributes
044             * can be dynamically generated based on the RuleTemplate which is selected.
045             */
046            @Override
047            public List getSections(MaintenanceDocument document, Maintainable oldMaintainable) {
048                    List<Section> sections = super.getSections(document, oldMaintainable);
049                    return WebRuleUtils.customizeSections(getThisRule(), sections, true);
050            }
051            
052            /**
053             * On creation of a new rule document, we must validate that a rule template and document type are set. 
054             */
055            @Override
056            public void processAfterNew(MaintenanceDocument document,
057                            Map<String, String[]> parameters) {
058                    initializeBusinessObjects(document);
059                    WebRuleUtils.validateRuleAndResponsibility(getOldRuleDelegation(document), getNewRuleDelegation(document), parameters);
060                    WebRuleUtils.validateRuleTemplateAndDocumentType(getOldRule(document), getNewRule(document), parameters);
061                    WebRuleUtils.establishDefaultRuleValues(getNewRule(document));
062                    getNewRule(document).setDocumentId(document.getDocumentHeader().getDocumentNumber());
063            }
064                    
065            /**
066             * Creates the initial structure of the new business object so that it can be properly
067             * populated with non-null object references.
068             */
069            private void initializeBusinessObjects(MaintenanceDocument document) {
070                    RuleDelegationBo oldRuleDelegation = getOldRuleDelegation(document);
071                    RuleDelegationBo newRuleDelegation = getNewRuleDelegation(document);
072            
073                    if (oldRuleDelegation.getDelegationRule() == null) {
074                            oldRuleDelegation.setDelegationRule(new RuleBaseValues());
075                    }
076                    if (newRuleDelegation.getDelegationRule() == null) {
077                            newRuleDelegation.setDelegationRule(new RuleBaseValues());
078                    }
079            }
080            
081            /**
082             * This is a hack to get around the fact that when a document is first created, this value is
083             * true which causes issues if you want to be able to initialize fields on  the document using
084             * request parameters.  See SectionBridge.toSection for the "if" block where it populates
085             * Field.propertyValue to see why this causes problems
086             */
087            @Override
088            public void setGenerateDefaultValues(String docTypeName) {              
089                    
090            }
091            
092            /**
093         * A complete override of the implementation for saving a Rule
094         */
095        @Override
096        public void saveBusinessObject() {
097            WebRuleUtils.clearKeysForSave(getThisRuleDelegation());
098            WebRuleUtils.translateResponsibilitiesForSave(getThisRule());
099            WebRuleUtils.translateFieldValuesForSave(getThisRule());
100            WebRuleUtils.processRuleForDelegationSave(getThisRuleDelegation());
101            KEWServiceLocator.getRuleService().makeCurrent(getThisRuleDelegation(), true);
102        }
103    
104        @Override
105        public void processAfterCopy(MaintenanceDocument document, Map<String, String[]> parameters) {
106            WebRuleUtils.processRuleForCopy(document.getDocumentNumber(), getOldRule(document), getNewRule(document));
107            super.processAfterCopy(document, parameters);
108        }
109        
110            @Override
111            public void processAfterEdit(MaintenanceDocument document,
112                            Map<String, String[]> parameters) {
113                    if (!getOldRule(document).getCurrentInd()) {
114                            throw new RiceRuntimeException("Cannot edit a non-current version of a rule.");
115                    }
116                    WebRuleUtils.populateForCopyOrEdit(getOldRule(document), getNewRule(document));
117                    getNewRule(document).setPreviousRuleId(getOldRule(document).getId());
118                    getNewRule(document).setDocumentId(document.getDocumentHeader().getDocumentNumber());
119                    super.processAfterEdit(document, parameters);
120            }
121    
122        
123            @Override
124            public List<MaintenanceLock> generateMaintenanceLocks() {
125                    if (getThisRule().getId() == null) {
126                            return Collections.emptyList();
127                    }
128                    return super.generateMaintenanceLocks();
129            }
130        
131        @Override
132            public String getDocumentTitle(MaintenanceDocument document) {
133                    StringBuffer title = new StringBuffer();
134            RuleBaseValues rule = getThisRule();
135            if (rule.getPreviousRuleId() != null) {
136                title.append("Editing Rule Delegation '").append(rule.getDescription()).append("'");
137            } else {
138                title.append("Adding Rule Delegation '").append(rule.getDescription()).append("'");
139            }
140            return title.toString();
141            }
142            
143            protected RuleDelegationBo getNewRuleDelegation(MaintenanceDocument document) {
144                    return (RuleDelegationBo)document.getNewMaintainableObject().getDataObject();
145            }
146            
147            protected RuleDelegationBo getOldRuleDelegation(MaintenanceDocument document) {
148                    return (RuleDelegationBo)document.getOldMaintainableObject().getDataObject();
149            }
150    
151            protected RuleDelegationBo getThisRuleDelegation() {
152                    return (RuleDelegationBo)getDataObject();
153            }
154    
155            protected RuleBaseValues getNewRule(MaintenanceDocument document) {
156                    return getNewRuleDelegation(document).getDelegationRule();
157            }
158    
159            protected RuleBaseValues getOldRule(MaintenanceDocument document) {
160                    return getOldRuleDelegation(document).getDelegationRule();
161            }
162    
163            protected RuleBaseValues getThisRule() {
164                    return getThisRuleDelegation().getDelegationRule();
165            }
166            
167            /**
168             * This overridden method ...
169             *
170             * @see org.kuali.rice.krad.maintenance.KualiMaintainableImpl#prepareForSave()
171             */
172            @Override
173            public void prepareForSave() {
174                    super.prepareForSave();
175                    WebRuleUtils.translateResponsibilitiesForSave(getThisRule());
176            }
177            
178    
179            
180            
181    }