| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kew.impl.peopleflow; |
| 17 | |
|
| 18 | |
import org.apache.commons.lang.StringUtils; |
| 19 | |
import org.kuali.rice.core.api.config.ConfigurationException; |
| 20 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
| 21 | |
import org.kuali.rice.core.api.util.xml.XmlJotter; |
| 22 | |
import org.kuali.rice.kew.actionrequest.ActionRequestValue; |
| 23 | |
import org.kuali.rice.kew.api.action.ActionRequestType; |
| 24 | |
import org.kuali.rice.kew.api.peopleflow.PeopleFlowDefinition; |
| 25 | |
import org.kuali.rice.kew.api.peopleflow.PeopleFlowService; |
| 26 | |
import org.kuali.rice.kew.engine.RouteContext; |
| 27 | |
import org.kuali.rice.kew.engine.node.NodeState; |
| 28 | |
import org.kuali.rice.kew.engine.node.RouteNodeUtils; |
| 29 | |
import org.kuali.rice.kew.api.exception.WorkflowException; |
| 30 | |
import org.kuali.rice.kew.routemodule.RouteModule; |
| 31 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
| 32 | |
import org.kuali.rice.kew.util.ResponsibleParty; |
| 33 | |
import org.w3c.dom.Element; |
| 34 | |
|
| 35 | |
import javax.xml.bind.JAXBContext; |
| 36 | |
import javax.xml.bind.JAXBException; |
| 37 | |
import javax.xml.bind.annotation.XmlAttribute; |
| 38 | |
import javax.xml.bind.annotation.XmlElement; |
| 39 | |
import javax.xml.bind.annotation.XmlElements; |
| 40 | |
import javax.xml.bind.annotation.XmlRootElement; |
| 41 | |
import javax.xml.bind.annotation.XmlValue; |
| 42 | |
import java.util.ArrayList; |
| 43 | |
import java.util.List; |
| 44 | |
|
| 45 | 0 | public class PeopleFlowRouteModule implements RouteModule { |
| 46 | |
|
| 47 | |
private static final String PEOPLE_FLOW_PROPERTY = "peopleFlow"; |
| 48 | |
|
| 49 | |
private static final String PEOPLE_FLOW_ITERATION_KEY = "peopleFlowIteration"; |
| 50 | |
public static final String PEOPLE_FLOW_SEQUENCE = "peopleFlowSequence"; |
| 51 | |
|
| 52 | |
private static final JAXBContext jaxbContext; |
| 53 | |
static { |
| 54 | |
try { |
| 55 | 0 | jaxbContext = JAXBContext.newInstance(PeopleFlowConfig.class); |
| 56 | 0 | } catch (JAXBException e) { |
| 57 | 0 | throw new RiceRuntimeException("Failed to initialize JAXB!", e); |
| 58 | 0 | } |
| 59 | 0 | } |
| 60 | |
|
| 61 | |
private PeopleFlowService peopleFlowService; |
| 62 | |
private PeopleFlowRequestGenerator peopleFlowRequestGenerator; |
| 63 | |
|
| 64 | |
@Override |
| 65 | |
public List<ActionRequestValue> findActionRequests(RouteContext context) throws Exception { |
| 66 | 0 | int iteration = incrementIteration(context); |
| 67 | 0 | List<PeopleFlowConfig> configurations = parsePeopleFlowConfiguration(context); |
| 68 | 0 | List<ActionRequestValue> actionRequests = new ArrayList<ActionRequestValue>(); |
| 69 | 0 | int index = 0; |
| 70 | 0 | for (PeopleFlowConfig configuration : configurations) { |
| 71 | 0 | if (index++ == iteration) { |
| 72 | 0 | PeopleFlowDefinition peopleFlow = loadPeopleFlow(configuration); |
| 73 | 0 | actionRequests.addAll(getPeopleFlowRequestGenerator().generateRequests(context, peopleFlow, configuration.actionRequested)); |
| 74 | 0 | break; |
| 75 | |
} |
| 76 | |
} |
| 77 | 0 | return actionRequests; |
| 78 | |
} |
| 79 | |
|
| 80 | |
@Override |
| 81 | |
public boolean isMoreRequestsAvailable(RouteContext context) { |
| 82 | 0 | Integer currentIteration = getCurrentIteration(context); |
| 83 | 0 | if (currentIteration == null) { |
| 84 | 0 | return false; |
| 85 | |
} |
| 86 | 0 | List<PeopleFlowConfig> configurations = parsePeopleFlowConfiguration(context); |
| 87 | 0 | return currentIteration.intValue() < configurations.size(); |
| 88 | |
} |
| 89 | |
|
| 90 | |
@Override |
| 91 | |
public ResponsibleParty resolveResponsibilityId(String responsibilityId) throws WorkflowException { |
| 92 | 0 | return null; |
| 93 | |
} |
| 94 | |
|
| 95 | |
protected List<PeopleFlowConfig> parsePeopleFlowConfiguration(RouteContext context) { |
| 96 | 0 | List<PeopleFlowConfig> configs = new ArrayList<PeopleFlowConfig>(); |
| 97 | 0 | NodeState peopleFlowSequenceNodeState = context.getNodeInstance().getNodeState(PEOPLE_FLOW_SEQUENCE); |
| 98 | 0 | if (peopleFlowSequenceNodeState != null) { |
| 99 | 0 | String peopleFlowSequence = peopleFlowSequenceNodeState.getValue(); |
| 100 | 0 | if (StringUtils.isNotBlank(peopleFlowSequence)) { |
| 101 | 0 | String[] peopleFlowValues = peopleFlowSequence.split(","); |
| 102 | 0 | for (String peopleFlowValue : peopleFlowValues) { |
| 103 | 0 | String[] peopleFlowProperties = peopleFlowValue.split(":"); |
| 104 | 0 | PeopleFlowConfig config = new PeopleFlowConfig(); |
| 105 | 0 | config.actionRequested = ActionRequestType.fromCode(peopleFlowProperties[0]); |
| 106 | 0 | config.peopleFlowIdentifier = peopleFlowProperties[1]; |
| 107 | 0 | configs.add(config); |
| 108 | |
} |
| 109 | |
} |
| 110 | 0 | } else { |
| 111 | 0 | List<Element> peopleFlowElements = RouteNodeUtils.getCustomRouteNodeElements( |
| 112 | |
context.getNodeInstance().getRouteNode(), PEOPLE_FLOW_PROPERTY); |
| 113 | |
|
| 114 | 0 | for (Element peopleFlowElement : peopleFlowElements) { |
| 115 | |
try { |
| 116 | 0 | PeopleFlowConfig config = (PeopleFlowConfig)jaxbContext.createUnmarshaller().unmarshal(peopleFlowElement); |
| 117 | 0 | if (config.actionRequested == null) { |
| 118 | |
|
| 119 | 0 | config.actionRequested = ActionRequestType.APPROVE; |
| 120 | |
} |
| 121 | 0 | if (config == null) { |
| 122 | 0 | throw new IllegalStateException("People flow configuration element did not properly unmarshall from XML: " + XmlJotter.jotNode(peopleFlowElement)); |
| 123 | |
} |
| 124 | 0 | configs.add(config); |
| 125 | 0 | } catch (JAXBException e) { |
| 126 | 0 | throw new RiceRuntimeException("Failed to unmarshall people flow configuration from route node.", e); |
| 127 | 0 | } |
| 128 | |
} |
| 129 | |
} |
| 130 | 0 | return configs; |
| 131 | |
} |
| 132 | |
|
| 133 | |
protected PeopleFlowDefinition loadPeopleFlow(PeopleFlowConfig configuration) { |
| 134 | 0 | if (configuration.isId()) { |
| 135 | 0 | PeopleFlowDefinition peopleFlow = getPeopleFlowService().getPeopleFlow(configuration.getId()); |
| 136 | 0 | if (peopleFlow == null) { |
| 137 | 0 | throw new ConfigurationException("Failed to locate a people flow with the given id of '" + configuration.getId() + "'"); |
| 138 | |
} |
| 139 | 0 | return peopleFlow; |
| 140 | |
} else { |
| 141 | 0 | String namespaceCode = configuration.getName().namespaceCode; |
| 142 | 0 | String name = configuration.getName().name; |
| 143 | 0 | PeopleFlowDefinition peopleFlow = getPeopleFlowService().getPeopleFlowByName(namespaceCode, name); |
| 144 | 0 | if (peopleFlow == null) { |
| 145 | 0 | throw new ConfigurationException("Failed to locate a people flow with the given namespaceCode of '" + namespaceCode + "' and name of '" + name + "'"); |
| 146 | |
} |
| 147 | 0 | return peopleFlow; |
| 148 | |
} |
| 149 | |
} |
| 150 | |
|
| 151 | |
protected int incrementIteration(RouteContext context) { |
| 152 | 0 | int nextIteration = 0; |
| 153 | 0 | NodeState nodeState = context.getNodeInstance().getNodeState(PEOPLE_FLOW_ITERATION_KEY); |
| 154 | 0 | if (nodeState == null) { |
| 155 | 0 | nodeState = new NodeState(); |
| 156 | 0 | nodeState.setNodeInstance(context.getNodeInstance()); |
| 157 | 0 | nodeState.setKey(PEOPLE_FLOW_ITERATION_KEY); |
| 158 | 0 | context.getNodeInstance().addNodeState(nodeState); |
| 159 | |
} else { |
| 160 | 0 | int currentIteration = Integer.parseInt(nodeState.getValue()); |
| 161 | 0 | nextIteration = currentIteration + 1; |
| 162 | |
} |
| 163 | 0 | nodeState.setValue(Integer.toString(nextIteration)); |
| 164 | 0 | if (!context.isSimulation()) { |
| 165 | 0 | KEWServiceLocator.getRouteNodeService().save(nodeState); |
| 166 | |
} |
| 167 | 0 | return nextIteration; |
| 168 | |
} |
| 169 | |
|
| 170 | |
protected Integer getCurrentIteration(RouteContext context) { |
| 171 | 0 | NodeState nodeState = context.getNodeInstance().getNodeState(PEOPLE_FLOW_ITERATION_KEY); |
| 172 | 0 | if (nodeState == null) { |
| 173 | 0 | return null; |
| 174 | |
} |
| 175 | 0 | return Integer.valueOf(nodeState.getValue()); |
| 176 | |
} |
| 177 | |
|
| 178 | |
public PeopleFlowService getPeopleFlowService() { |
| 179 | 0 | return peopleFlowService; |
| 180 | |
} |
| 181 | |
|
| 182 | |
public void setPeopleFlowService(PeopleFlowService peopleFlowService) { |
| 183 | 0 | this.peopleFlowService = peopleFlowService; |
| 184 | 0 | } |
| 185 | |
|
| 186 | |
public PeopleFlowRequestGenerator getPeopleFlowRequestGenerator() { |
| 187 | 0 | return peopleFlowRequestGenerator; |
| 188 | |
} |
| 189 | |
|
| 190 | |
public void setPeopleFlowRequestGenerator(PeopleFlowRequestGenerator peopleFlowRequestGenerator) { |
| 191 | 0 | this.peopleFlowRequestGenerator = peopleFlowRequestGenerator; |
| 192 | 0 | } |
| 193 | |
|
| 194 | |
@XmlRootElement(name = "peopleFlow") |
| 195 | 0 | private static class PeopleFlowConfig { |
| 196 | |
|
| 197 | |
@XmlElement(name = "actionRequested") |
| 198 | |
ActionRequestType actionRequested; |
| 199 | |
|
| 200 | |
@XmlElements(value = { |
| 201 | |
@XmlElement(name = "name", type = NameConfig.class), |
| 202 | |
@XmlElement(name = "id", type = String.class) |
| 203 | |
}) |
| 204 | |
Object peopleFlowIdentifier; |
| 205 | |
|
| 206 | |
boolean isId() { |
| 207 | 0 | return peopleFlowIdentifier instanceof String; |
| 208 | |
} |
| 209 | |
boolean isName() { |
| 210 | 0 | return peopleFlowIdentifier instanceof NameConfig; |
| 211 | |
} |
| 212 | |
|
| 213 | |
NameConfig getName() { |
| 214 | 0 | return (NameConfig)peopleFlowIdentifier; |
| 215 | |
} |
| 216 | |
|
| 217 | |
String getId() { |
| 218 | 0 | return (String)peopleFlowIdentifier; |
| 219 | |
} |
| 220 | |
|
| 221 | |
} |
| 222 | |
|
| 223 | 0 | private static class NameConfig { |
| 224 | |
|
| 225 | |
@XmlAttribute(name = "namespace") |
| 226 | |
String namespaceCode; |
| 227 | |
|
| 228 | |
@XmlValue |
| 229 | |
String name; |
| 230 | |
|
| 231 | |
} |
| 232 | |
|
| 233 | |
} |