View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kew.document;
17  
18  import java.util.Collections;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.kuali.rice.core.api.exception.RiceRuntimeException;
23  import org.kuali.rice.kew.rule.RuleBaseValues;
24  import org.kuali.rice.kew.rule.RuleDelegationBo;
25  import org.kuali.rice.kew.rule.web.WebRuleUtils;
26  import org.kuali.rice.kew.service.KEWServiceLocator;
27  import org.kuali.rice.kns.maintenance.KualiMaintainableImpl;
28  import org.kuali.rice.kns.document.MaintenanceDocument;
29  import org.kuali.rice.kns.web.ui.Section;
30  import org.kuali.rice.krad.maintenance.MaintenanceLock;
31  import org.kuali.rice.kns.maintenance.Maintainable;
32  
33  /**
34   * This class is the maintainable implementation for Routing Rules 
35   * in KEW (represented by the {@link RuleBaseValues} business object). 
36   * 
37   * @author Kuali Rice Team (rice.collab@kuali.org)
38   *
39   */
40  public class RoutingRuleDelegationMaintainable extends KualiMaintainableImpl {
41  	
42  	/**
43  	 * Override the getSections method on this maintainable so that the Section Containing the various Rule Attributes
44  	 * can be dynamically generated based on the RuleTemplate which is selected.
45  	 */
46  	@Override
47  	public List getSections(MaintenanceDocument document, Maintainable oldMaintainable) {
48  		List<Section> sections = super.getSections(document, oldMaintainable);
49  		return WebRuleUtils.customizeSections(getThisRule(), sections, true);
50  	}
51  	
52  	/**
53  	 * On creation of a new rule document, we must validate that a rule template and document type are set. 
54  	 */
55  	@Override
56  	public void processAfterNew(MaintenanceDocument document,
57  			Map<String, String[]> parameters) {
58  		initializeBusinessObjects(document);
59  		WebRuleUtils.validateRuleAndResponsibility(getOldRuleDelegation(document), getNewRuleDelegation(document), parameters);
60  		WebRuleUtils.validateRuleTemplateAndDocumentType(getOldRule(document), getNewRule(document), parameters);
61  		WebRuleUtils.establishDefaultRuleValues(getNewRule(document));
62  		getNewRule(document).setDocumentId(document.getDocumentHeader().getDocumentNumber());
63  	}
64  		
65  	/**
66  	 * Creates the initial structure of the new business object so that it can be properly
67  	 * populated with non-null object references.
68  	 */
69  	private void initializeBusinessObjects(MaintenanceDocument document) {
70  		RuleDelegationBo oldRuleDelegation = getOldRuleDelegation(document);
71  		RuleDelegationBo newRuleDelegation = getNewRuleDelegation(document);
72          
73  		if (oldRuleDelegation.getDelegationRule() == null) {
74  			oldRuleDelegation.setDelegationRule(new RuleBaseValues());
75  		}
76  		if (newRuleDelegation.getDelegationRule() == null) {
77  			newRuleDelegation.setDelegationRule(new RuleBaseValues());
78  		}
79  	}
80  	
81  	/**
82  	 * This is a hack to get around the fact that when a document is first created, this value is
83   	 * true which causes issues if you want to be able to initialize fields on  the document using
84   	 * request parameters.  See SectionBridge.toSection for the "if" block where it populates
85   	 * Field.propertyValue to see why this causes problems
86  	 */
87  	@Override
88  	public void setGenerateDefaultValues(String docTypeName) {		
89  		
90  	}
91  	
92  	/**
93       * A complete override of the implementation for saving a Rule
94       */
95      @Override
96      public void saveBusinessObject() {
97      	WebRuleUtils.clearKeysForSave(getThisRuleDelegation());
98      	WebRuleUtils.translateResponsibilitiesForSave(getThisRule());
99      	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 }