001/**
002 * Copyright 2005-2017 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.engine.simulation;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.core.api.exception.RiceRuntimeException;
020import org.kuali.rice.kew.actionrequest.KimPrincipalRecipient;
021import org.kuali.rice.kew.actionrequest.Recipient;
022import org.kuali.rice.kew.api.action.RoutingReportActionToTake;
023import org.kuali.rice.kew.api.action.RoutingReportCriteria;
024import org.kuali.rice.kew.service.KEWServiceLocator;
025import org.kuali.rice.kim.api.identity.Person;
026import org.kuali.rice.kim.api.identity.principal.Principal;
027import org.kuali.rice.kim.api.services.KimApiServiceLocator;
028
029import java.util.ArrayList;
030import java.util.List;
031
032/**
033 * Criteria which acts as input to the {@link SimulationEngine}.
034 *
035 * @author Kuali Rice Team (rice.collab@kuali.org)
036 * @see SimulationEngine
037 */
038public class SimulationCriteria {
039
040    // fields related to document simulation
041    private String documentId;
042    private String destinationNodeName;
043    private List<Recipient> destinationRecipients = new ArrayList<Recipient>();
044
045    // fields related to document type simulation
046    private String documentTypeName;
047    private String xmlContent;
048    private List<String> nodeNames = new ArrayList<String>();
049    private List<String> ruleTemplateNames = new ArrayList<String>();
050
051    // fields related to both simulation types
052    private Boolean activateRequests;
053    private Person routingUser;
054    private List<SimulationActionToTake> actionsToTake = new ArrayList<SimulationActionToTake>();
055    private boolean flattenNodes;
056
057    public SimulationCriteria() {
058        this.activateRequests = null;
059        this.flattenNodes = false;
060    }
061
062    public static SimulationCriteria createSimulationCritUsingDocumentId(String documentId) {
063        return new SimulationCriteria(null, documentId);
064    }
065
066    public static SimulationCriteria createSimulationCritUsingDocTypeName(String documentTypeName) {
067        return new SimulationCriteria(documentTypeName, null);
068    }
069
070    private SimulationCriteria(String documentTypeName, String documentId) {
071        if (StringUtils.isNotBlank(documentId)) {
072            this.documentId = documentId;
073        } else if (StringUtils.isNotBlank(documentTypeName)) {
074            this.documentTypeName = documentTypeName;
075        }
076    }
077
078    public Boolean isActivateRequests() {
079        return this.activateRequests;
080    }
081
082    public void setActivateRequests(Boolean activateRequests) {
083        this.activateRequests = activateRequests;
084    }
085
086    public String getDocumentId() {
087        return documentId;
088    }
089
090    public void setDocumentId(String documentId) {
091        this.documentId = documentId;
092    }
093
094    public String getDestinationNodeName() {
095        return destinationNodeName;
096    }
097
098    public void setDestinationNodeName(String destinationNodeName) {
099        this.destinationNodeName = destinationNodeName;
100    }
101
102    public List<Recipient> getDestinationRecipients() {
103        return destinationRecipients;
104    }
105
106    public void setDestinationRecipients(List<Recipient> destinationRecipients) {
107        this.destinationRecipients = destinationRecipients;
108    }
109
110    public String getDocumentTypeName() {
111        return documentTypeName;
112    }
113
114    public void setDocumentTypeName(String documentTypeName) {
115        this.documentTypeName = documentTypeName;
116    }
117
118    public List<String> getRuleTemplateNames() {
119        return ruleTemplateNames;
120    }
121
122    public void setRuleTemplateNames(List<String> ruleTemplateNames) {
123        this.ruleTemplateNames = ruleTemplateNames;
124    }
125
126    public String getXmlContent() {
127        return xmlContent;
128    }
129
130    public void setXmlContent(String xmlContent) {
131        this.xmlContent = xmlContent;
132    }
133
134    public List<String> getNodeNames() {
135        return nodeNames;
136    }
137
138    public void setNodeNames(List<String> nodeNames) {
139        this.nodeNames = nodeNames;
140    }
141
142    public boolean isDocumentSimulation() {
143        return documentId != null;
144    }
145
146    public boolean isDocumentTypeSimulation() {
147        return !org.apache.commons.lang.StringUtils.isEmpty(documentTypeName);
148    }
149
150    public List<SimulationActionToTake> getActionsToTake() {
151        return actionsToTake;
152    }
153
154    public void setActionsToTake(List<SimulationActionToTake> actionsToTake) {
155        this.actionsToTake = actionsToTake;
156    }
157
158    public Person getRoutingUser() {
159        return routingUser;
160    }
161
162    public void setRoutingUser(Person routingUser) {
163        this.routingUser = routingUser;
164    }
165
166    public boolean isFlattenNodes() {
167        return this.flattenNodes;
168    }
169
170    public void setFlattenNodes(boolean flattenNodes) {
171        this.flattenNodes = flattenNodes;
172    }
173
174    public static SimulationCriteria from(RoutingReportCriteria criteriaVO) {
175        if (criteriaVO == null) {
176            return null;
177        }
178        SimulationCriteria criteria = new SimulationCriteria();
179        criteria.setDestinationNodeName(criteriaVO.getTargetNodeName());
180        criteria.setDocumentId(criteriaVO.getDocumentId());
181        criteria.setDocumentTypeName(criteriaVO.getDocumentTypeName());
182        criteria.setXmlContent(criteriaVO.getXmlContent());
183        criteria.setActivateRequests(criteriaVO.isActivateRequests());
184        criteria.setFlattenNodes(criteriaVO.isFlattenNodes());
185        if (criteriaVO.getRoutingPrincipalId() != null) {
186            Principal kPrinc = KEWServiceLocator.getIdentityHelperService().
187                    getPrincipal(criteriaVO.getRoutingPrincipalId());
188            Person user = KimApiServiceLocator.getPersonService().getPerson(kPrinc.getPrincipalId());
189            if (user == null) {
190                throw new RiceRuntimeException(
191                        "Could not locate user for the given id: " + criteriaVO.getRoutingPrincipalId());
192            }
193            criteria.setRoutingUser(user);
194        }
195        if (criteriaVO.getRuleTemplateNames() != null) {
196            criteria.setRuleTemplateNames(criteriaVO.getRuleTemplateNames());
197        }
198        if (criteriaVO.getNodeNames() != null) {
199            criteria.setNodeNames(criteriaVO.getNodeNames());
200        }
201        if (criteriaVO.getTargetPrincipalIds() != null) {
202            for (String targetPrincipalId : criteriaVO.getTargetPrincipalIds()) {
203                Principal principal = KEWServiceLocator.getIdentityHelperService().getPrincipal(targetPrincipalId);
204                criteria.getDestinationRecipients().add(new KimPrincipalRecipient(principal));
205            }
206        }
207
208        if (criteriaVO.getActionsToTake() != null) {
209            for (RoutingReportActionToTake actionToTake : criteriaVO.getActionsToTake()) {
210                criteria.getActionsToTake().add(SimulationActionToTake.from(actionToTake));
211            }
212        }
213
214        return criteria;
215    }
216}