View Javadoc

1   /**
2    * Copyright 2005-2012 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.rule.web;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.core.api.exception.RiceRuntimeException;
20  import org.kuali.rice.kew.rule.RuleBaseValues;
21  import org.kuali.rice.kew.rule.RuleResponsibilityBo;
22  import org.kuali.rice.kew.service.KEWServiceLocator;
23  import org.kuali.rice.kew.api.KewApiConstants;
24  import org.kuali.rice.kim.api.group.Group;
25  import org.kuali.rice.kim.api.identity.principal.Principal;
26  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
27  import org.kuali.rice.kns.web.struts.form.KualiForm;
28  
29  import javax.servlet.http.HttpServletRequest;
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  /**
34   * Struts ActionForm for {@link DelegateRuleAction}.
35   *
36   * @author Kuali Rice Team (rice.collab@kuali.org)
37   */
38  public class DelegateRuleForm extends KualiForm {
39  
40  	private static final long serialVersionUID = 5412969516727713859L;
41  
42  	private String parentRuleId;
43  	private String parentResponsibilityId;
44  
45  	private RuleBaseValues parentRule;
46  	private RuleResponsibilityBo parentResponsibility;
47  		
48  	private List<String> reviewers = new ArrayList<String>();
49  	private List<String> responsibilityTypes = new ArrayList<String>();
50  	private List<String> actionRequestCodes = new ArrayList<String>();
51  	
52  	public String getParentRuleId() {
53  		return this.parentRuleId;
54  	}
55  
56  	public void setParentRuleId(String parentRuleId) {
57  		this.parentRuleId = parentRuleId;
58  	}
59  
60  	public String getParentResponsibilityId() {
61  		return this.parentResponsibilityId;
62  	}
63  
64  	public void setParentResponsibilityId(String parentResponsibilityId) {
65  		this.parentResponsibilityId = parentResponsibilityId;
66  	}
67  
68  	public RuleBaseValues getParentRule() {
69  		return this.parentRule;
70  	}
71  
72  	public void setParentRule(RuleBaseValues parentRule) {
73  	    if (this.parentRule != null 
74  	            && parentRule != null
75  	            && this.parentResponsibility != null) {
76  	    	if (!StringUtils.equals(this.parentRule.getId(), parentRule.getId())) {
77  	            this.parentResponsibility = null;
78  	            this.parentResponsibilityId = null;
79  	        }
80  	    }
81  		this.parentRule = parentRule;
82  	}
83  
84  	public RuleResponsibilityBo getParentResponsibility() {
85  		return this.parentResponsibility;
86  	}
87  
88  	public void setParentResponsibility(RuleResponsibilityBo parentResponsibility) {
89  		this.parentResponsibility = parentResponsibility;
90  	}
91  
92  	public List<String> getReviewers() {
93  		return this.reviewers;
94  	}
95  
96  	public void setReviewers(List<String> reviewers) {
97  		this.reviewers = reviewers;
98  	}
99  
100 	public List<String> getResponsibilityTypes() {
101 		return this.responsibilityTypes;
102 	}
103 
104 	public void setResponsibilityTypes(List<String> responsibilityTypes) {
105 		this.responsibilityTypes = responsibilityTypes;
106 	}
107 
108 	public List<String> getActionRequestCodes() {
109 		return this.actionRequestCodes;
110 	}
111 
112 	public void setActionRequestCodes(List<String> actionRequestCodes) {
113 		this.actionRequestCodes = actionRequestCodes;
114 	}
115 
116 	public String getRuleDescription() {
117 		if (getParentRule() == null) {
118 			return "";
119 		}
120 		return getParentRule().getDescription();
121 	}
122 
123 	@Override
124 	public void populate(HttpServletRequest request) {
125 				
126 		super.populate(request);
127 
128 		reviewers.clear();
129 		responsibilityTypes.clear();
130 		actionRequestCodes.clear();
131 		
132 		if (getParentRuleId() != null) {
133 			setParentRule(KEWServiceLocator.getRuleService().findRuleBaseValuesById(getParentRuleId()));
134 		}
135 		if (getParentResponsibilityId() != null && getParentRule() != null) {
136 			for (RuleResponsibilityBo responsibility : getParentRule().getRuleResponsibilities()) {
137 				if (responsibility.getResponsibilityId().equals(getParentResponsibilityId())) {
138 					setParentResponsibility(responsibility);
139 					break;
140 				}
141 			}
142 		}
143 		
144 		if (getParentRule() != null) {
145 			for (RuleResponsibilityBo responsibility : getParentRule().getRuleResponsibilities()) {
146 				if (KewApiConstants.RULE_RESPONSIBILITY_WORKFLOW_ID.equals(responsibility.getRuleResponsibilityType())) {
147 					Principal principal = KEWServiceLocator.getIdentityHelperService().getPrincipal(responsibility.getRuleResponsibilityName());
148 					if (principal != null) {
149 					    reviewers.add(principal.getPrincipalName());
150 					}
151 					responsibilityTypes.add(KewApiConstants.RULE_RESPONSIBILITY_WORKFLOW_ID_LABEL);
152 				} else if (KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID.equals(responsibility.getRuleResponsibilityType())) {
153 					Group group = KimApiServiceLocator.getGroupService().getGroup(responsibility.getRuleResponsibilityName());
154 					if (group != null) {
155 					    reviewers.add(group.getNamespaceCode() + " " + group.getName());
156 					}
157 					responsibilityTypes.add(KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID_LABEL);
158 				} else if (KewApiConstants.RULE_RESPONSIBILITY_ROLE_ID.equals(responsibility.getRuleResponsibilityType())) {
159 					reviewers.add(responsibility.getResolvedRoleName());
160 					responsibilityTypes.add(KewApiConstants.RULE_RESPONSIBILITY_ROLE_ID_LABEL);
161 				} else {
162 					throw new RiceRuntimeException("Encountered a responsibility with an invalid type, type value was " + responsibility.getRuleResponsibilityType());
163 				}
164 				actionRequestCodes.add(KewApiConstants.ACTION_REQUEST_CODES.get(responsibility.getActionRequestedCd()));
165 			}
166 		}
167 		
168 	}
169 
170 	
171 
172 }