001/**
002 * Copyright 2005-2015 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.rule.web;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.exception.RiceRuntimeException;
020import org.kuali.rice.kew.rule.RuleBaseValues;
021import org.kuali.rice.kew.rule.RuleResponsibilityBo;
022import org.kuali.rice.kew.service.KEWServiceLocator;
023import org.kuali.rice.kew.api.KewApiConstants;
024import org.kuali.rice.kim.api.group.Group;
025import org.kuali.rice.kim.api.identity.principal.Principal;
026import org.kuali.rice.kim.api.services.KimApiServiceLocator;
027import org.kuali.rice.kns.web.struts.form.KualiForm;
028
029import javax.servlet.http.HttpServletRequest;
030import java.util.ArrayList;
031import java.util.List;
032
033/**
034 * Struts ActionForm for {@link DelegateRuleAction}.
035 *
036 * @author Kuali Rice Team (rice.collab@kuali.org)
037 */
038public class DelegateRuleForm extends KualiForm {
039
040        private static final long serialVersionUID = 5412969516727713859L;
041
042        private String parentRuleId;
043        private String parentResponsibilityId;
044
045        private RuleBaseValues parentRule;
046        private RuleResponsibilityBo parentResponsibility;
047                
048        private List<String> reviewers = new ArrayList<String>();
049        private List<String> responsibilityTypes = new ArrayList<String>();
050        private List<String> actionRequestCodes = new ArrayList<String>();
051        
052        public String getParentRuleId() {
053                return this.parentRuleId;
054        }
055
056        public void setParentRuleId(String parentRuleId) {
057                this.parentRuleId = parentRuleId;
058        }
059
060        public String getParentResponsibilityId() {
061                return this.parentResponsibilityId;
062        }
063
064        public void setParentResponsibilityId(String parentResponsibilityId) {
065                this.parentResponsibilityId = parentResponsibilityId;
066        }
067
068        public RuleBaseValues getParentRule() {
069                return this.parentRule;
070        }
071
072        public void setParentRule(RuleBaseValues parentRule) {
073            if (this.parentRule != null 
074                    && parentRule != null
075                    && this.parentResponsibility != null) {
076                if (!StringUtils.equals(this.parentRule.getId(), parentRule.getId())) {
077                    this.parentResponsibility = null;
078                    this.parentResponsibilityId = null;
079                }
080            }
081                this.parentRule = parentRule;
082        }
083
084        public RuleResponsibilityBo getParentResponsibility() {
085                return this.parentResponsibility;
086        }
087
088        public void setParentResponsibility(RuleResponsibilityBo parentResponsibility) {
089                this.parentResponsibility = parentResponsibility;
090        }
091
092        public List<String> getReviewers() {
093                return this.reviewers;
094        }
095
096        public void setReviewers(List<String> reviewers) {
097                this.reviewers = reviewers;
098        }
099
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}