use of org.kuali.kfs.kew.engine.node.NodeState in project cu-kfs by CU-CommunityApps.
the class DocumentOperationAction method save.
public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
DocumentOperationForm docForm = (DocumentOperationForm) form;
boolean change = false;
String routeHeaderOp = docForm.getRouteHeaderOp();
if (!KewApiConstants.UPDATE.equals(routeHeaderOp) && !KewApiConstants.NOOP.equals(routeHeaderOp)) {
throw new WorkflowServiceErrorException("Document operation not defined", new WorkflowServiceErrorImpl("Document operation not defined", "docoperation.operation.invalid"));
}
if (KewApiConstants.UPDATE.equals(routeHeaderOp)) {
setRouteHeaderTimestamps(docForm);
DocumentRouteHeaderValue dHeader = docForm.getRouteHeader();
String initials = docForm.getInitialNodeInstances();
List<RouteNodeInstance> lInitials = new ArrayList<RouteNodeInstance>();
if (StringUtils.isNotEmpty(initials)) {
StringTokenizer tokenInitials = new StringTokenizer(initials, ",");
while (tokenInitials.hasMoreTokens()) {
String instanceId = tokenInitials.nextToken().trim();
LOG.debug(instanceId);
RouteNodeInstance instance = getRouteNodeService().findRouteNodeInstanceById(instanceId);
lInitials.add(instance);
}
}
dHeader.setInitialRouteNodeInstances(lInitials);
/*
* FINP-7381 changes from KualiCo patch release 2021-02-26 applied to
* original KEW-to-KFS KualiCo patch release 2021-01-28 version of the file.
*/
getRouteHeaderService().validateRouteHeader(dHeader);
getRouteHeaderService().saveRouteHeader(dHeader);
change = true;
}
for (Iterator actionRequestIter = docForm.getActionRequestOps().iterator(); actionRequestIter.hasNext(); ) {
DocOperationIndexedParameter actionRequestOp = (DocOperationIndexedParameter) actionRequestIter.next();
int index = actionRequestOp.getIndex();
String opValue = actionRequestOp.getValue();
ActionRequest actionRequest = docForm.getActionRequests().get(index);
String createDateParamName = "actionRequests[" + index + "].createDateString";
if (!KewApiConstants.UPDATE.equals(opValue) && !KewApiConstants.DELETE.equals(opValue) && !KewApiConstants.NOOP.equals(opValue)) {
throw new WorkflowServiceErrorException("Action request operation not defined", new WorkflowServiceErrorImpl("Action request operation not defined", "docoperation.actionrequest.operation.invalid"));
}
if (KewApiConstants.UPDATE.equals(opValue)) {
try {
actionRequest.setCreateDate(new Timestamp(KFSConstants.getDefaultDateFormat().parse(request.getParameter(createDateParamName)).getTime()));
actionRequest.setCreateDateString(KFSConstants.getDefaultDateFormat().format(actionRequest.getCreateDate()));
actionRequest.setDocumentId(docForm.getRouteHeader().getDocumentId());
if (StringUtils.isNotBlank(actionRequest.getParentActionRequestId())) {
actionRequest.setParentActionRequest(getActionRequestService().findByActionRequestId(actionRequest.getParentActionRequestId()));
}
if (StringUtils.isNotBlank(actionRequest.getActionTakenId())) {
actionRequest.setActionTaken(getActionTakenService().findByActionTakenId(actionRequest.getActionTakenId()));
}
if (actionRequest.getNodeInstance() != null && actionRequest.getNodeInstance().getRouteNodeInstanceId() == null) {
actionRequest.setNodeInstance(null);
} else if (actionRequest.getNodeInstance() != null && actionRequest.getNodeInstance().getRouteNodeInstanceId() != null) {
actionRequest.setNodeInstance(KEWServiceLocator.getRouteNodeService().findRouteNodeInstanceById(actionRequest.getNodeInstance().getRouteNodeInstanceId()));
}
getActionRequestService().saveActionRequest(actionRequest);
change = true;
} catch (ParseException pe) {
throw new WorkflowServiceErrorException("Action request create date parsing error", new WorkflowServiceErrorImpl("Action request create date parsing error", "docoperation.actionrequests.dateparsing.error", actionRequest.getActionRequestId()));
}
}
if (KewApiConstants.DELETE.equals(opValue)) {
getActionRequestService().deleteActionRequestGraph(actionRequest);
change = true;
}
}
for (Iterator actionTakenIter = docForm.getActionTakenOps().iterator(); actionTakenIter.hasNext(); ) {
DocOperationIndexedParameter actionTakenOp = (DocOperationIndexedParameter) actionTakenIter.next();
int index = actionTakenOp.getIndex();
String opValue = actionTakenOp.getValue();
String actionDateParamName = "actionsTaken[" + index + "].actionDateString";
ActionTaken actionTaken = docForm.getActionsTaken().get(index);
if (!KewApiConstants.UPDATE.equals(opValue) && !KewApiConstants.DELETE.equals(opValue) && !KewApiConstants.NOOP.equals(opValue)) {
throw new WorkflowServiceErrorException("Action taken operation not defined", new WorkflowServiceErrorImpl("Action taken operation not defined", "docoperation.actiontaken.operation.invalid"));
}
if (KewApiConstants.UPDATE.equals(opValue)) {
try {
actionTaken.setActionDate(new Timestamp(KFSConstants.getDefaultDateFormat().parse(request.getParameter(actionDateParamName)).getTime()));
actionTaken.setActionDateString(KFSConstants.getDefaultDateFormat().format(actionTaken.getActionDate()));
getActionTakenService().saveActionTaken(actionTaken);
change = true;
} catch (ParseException pe) {
throw new WorkflowServiceErrorException("Action taken action date parsing error", new WorkflowServiceErrorImpl("Action taken action date parse error", "docoperation.actionstaken.dateparsing.error", actionTaken.getActionTakenId()));
}
}
if (KewApiConstants.DELETE.equals(opValue)) {
getActionTakenService().delete(actionTaken);
change = true;
}
}
for (Iterator actionItemIter = docForm.getActionItemOps().iterator(); actionItemIter.hasNext(); ) {
DocOperationIndexedParameter actionItemOp = (DocOperationIndexedParameter) actionItemIter.next();
int index = actionItemOp.getIndex();
String opValue = actionItemOp.getValue();
String dateAssignedParamName = "actionItems[" + index + "].dateAssignedStringValue";
ActionItem actionItem = docForm.getActionItems().get(index);
if (!KewApiConstants.UPDATE.equals(opValue) && !KewApiConstants.DELETE.equals(opValue) && !KewApiConstants.NOOP.equals(opValue)) {
throw new WorkflowServiceErrorException("Action Item operation not defined", new WorkflowServiceErrorImpl("Action Item operation not defined", "docoperation.operation.invalid"));
}
if (KewApiConstants.UPDATE.equals(opValue)) {
try {
actionItem.setDateAssigned(new Timestamp(KFSConstants.getDefaultDateFormat().parse(request.getParameter(dateAssignedParamName)).getTime()));
actionItem.setDateAssignedStringValue(KFSConstants.getDefaultDateFormat().format(actionItem.getDateAssigned()));
actionItem.setDocumentId(docForm.getRouteHeader().getDocumentId());
getActionListService().saveActionItem(actionItem);
change = true;
} catch (ParseException pe) {
throw new WorkflowServiceErrorException("Action item date assigned parsing error", new WorkflowServiceErrorImpl("Action item date assigned parse error", "docoperation.actionitem.dateassignedparsing.error", actionItem.getId()));
}
}
if (KewApiConstants.DELETE.equals(opValue)) {
try {
actionItem.setDateAssigned(new Timestamp(KFSConstants.getDefaultDateFormat().parse(request.getParameter(dateAssignedParamName)).getTime()));
actionItem.setDateAssignedStringValue(KFSConstants.getDefaultDateFormat().format(actionItem.getDateAssigned()));
actionItem.setDocumentId(docForm.getRouteHeader().getDocumentId());
getActionListService().deleteActionItem(actionItem);
change = true;
} catch (ParseException pe) {
throw new WorkflowServiceErrorException("Action item date assigned parsing error", new WorkflowServiceErrorImpl("Action item date assigned parse error", "docoperation.actionitem.dateassignedparsing.error", actionItem.getId()));
}
}
}
List routeNodeInstances = (List) (request.getSession().getAttribute("routeNodeInstances"));
String ids = (docForm.getNodeStatesDelete() != null) ? docForm.getNodeStatesDelete().trim() : null;
List statesToBeDeleted = new ArrayList();
if (ids != null && !"".equals(ids)) {
StringTokenizer idSets = new StringTokenizer(ids);
while (idSets.hasMoreTokens()) {
String id = idSets.nextToken().trim();
statesToBeDeleted.add(Long.valueOf(id));
}
}
for (Iterator routeNodeInstanceIter = docForm.getRouteNodeInstanceOps().iterator(); routeNodeInstanceIter.hasNext(); ) {
DocOperationIndexedParameter routeNodeInstanceOp = (DocOperationIndexedParameter) routeNodeInstanceIter.next();
int index = routeNodeInstanceOp.getIndex();
String opValue = routeNodeInstanceOp.getValue();
LOG.debug(opValue);
RouteNodeInstance routeNodeInstance = (RouteNodeInstance) (routeNodeInstances.get(index));
RouteNodeInstance routeNodeInstanceNew = docForm.getRouteNodeInstance(index);
if (!KewApiConstants.UPDATE.equals(opValue) && !KewApiConstants.DELETE.equals(opValue) && !KewApiConstants.NOOP.equals(opValue)) {
throw new WorkflowServiceErrorException("Route Node Instance Operation not defined", new WorkflowServiceErrorImpl("Route Node Instance Operation not defined", "docoperation.routenodeinstance.operation.invalid"));
}
if (KewApiConstants.UPDATE.equals(opValue)) {
routeNodeInstance.setActive(routeNodeInstanceNew.isActive());
LOG.debug(Boolean.toString(routeNodeInstanceNew.isActive()));
routeNodeInstance.setComplete(routeNodeInstanceNew.isComplete());
routeNodeInstance.setInitial(routeNodeInstanceNew.isInitial());
List<NodeState> nodeStates = routeNodeInstance.getState();
List<NodeState> nodeStatesNew = routeNodeInstanceNew.getState();
if (nodeStates != null) {
for (int i = 0; i < nodeStates.size(); i++) {
NodeState nodeState = nodeStates.get(i);
NodeState nodeStateNew = nodeStatesNew.get(i);
if (nodeStateNew.getKey() != null && !nodeStateNew.getKey().trim().equals("")) {
nodeState.setKey(nodeStateNew.getKey());
LOG.debug(nodeState.getKey());
nodeState.setValue(nodeStateNew.getValue());
LOG.debug(nodeState.getValue());
}
}
}
getRouteNodeService().save(routeNodeInstance);
LOG.debug("saved");
change = true;
}
if (KewApiConstants.DELETE.equals(opValue)) {
List<NodeState> nodeStates = routeNodeInstance.getState();
List<NodeState> nodeStatesNew = routeNodeInstanceNew.getState();
if (nodeStates != null) {
for (int i = 0; i < nodeStates.size(); i++) {
NodeState nodeState = nodeStates.get(i);
NodeState nodeStateNew = nodeStatesNew.get(i);
if (nodeStateNew.getKey() == null || nodeStateNew.getKey().trim().equals("")) {
statesToBeDeleted.remove(nodeState.getNodeStateId());
}
}
}
getRouteNodeService().deleteByRouteNodeInstance(routeNodeInstance);
LOG.debug(routeNodeInstance.getRouteNodeInstanceId() + " is deleted");
change = true;
break;
}
if (KewApiConstants.NOOP.equals(opValue)) {
routeNodeInstanceNew.setActive(routeNodeInstance.isActive());
routeNodeInstanceNew.setComplete(routeNodeInstance.isComplete());
routeNodeInstanceNew.setInitial(routeNodeInstance.isInitial());
List<NodeState> nodeStates = routeNodeInstance.getState();
List<NodeState> nodeStatesNew = routeNodeInstanceNew.getState();
if (nodeStates != null) {
for (int i = 0; i < nodeStates.size(); i++) {
NodeState nodeState = nodeStates.get(i);
NodeState nodeStateNew = nodeStatesNew.get(i);
if (nodeStateNew.getKey() == null || nodeStateNew.getKey().trim().equals("")) {
statesToBeDeleted.remove(nodeState.getNodeStateId());
}
nodeStateNew.setKey(nodeState.getKey());
nodeStateNew.setValue(nodeState.getValue());
}
}
}
}
if (statesToBeDeleted != null && statesToBeDeleted.size() > 0) {
getRouteNodeService().deleteNodeStates(statesToBeDeleted);
}
List branches = (List) request.getSession().getAttribute("branches");
String branchStateIds = (docForm.getBranchStatesDelete() != null) ? docForm.getBranchStatesDelete().trim() : null;
List<Long> branchStatesToBeDeleted = new ArrayList<>();
if (branchStateIds != null && !"".equals(branchStateIds)) {
StringTokenizer idSets = new StringTokenizer(branchStateIds);
while (idSets.hasMoreTokens()) {
String id = idSets.nextToken().trim();
branchStatesToBeDeleted.add(Long.valueOf(id));
}
}
for (Iterator branchesOpIter = docForm.getBranchOps().iterator(); branchesOpIter.hasNext(); ) {
DocOperationIndexedParameter branchesOp = (DocOperationIndexedParameter) branchesOpIter.next();
int index = branchesOp.getIndex();
String opValue = branchesOp.getValue();
LOG.debug(opValue);
Branch branch = (Branch) (branches.get(index));
Branch branchNew = docForm.getBranche(index);
if (!KewApiConstants.UPDATE.equals(opValue) && !KewApiConstants.NOOP.equals(opValue)) {
throw new WorkflowServiceErrorException("Route Node Instance Operation not defined", new WorkflowServiceErrorImpl("Route Node Instance Operation not defined", "docoperation.routenodeinstance.operation.invalid"));
}
if (KewApiConstants.UPDATE.equals(opValue)) {
branch.setName(branchNew.getName());
List<BranchState> branchStates = branch.getBranchState();
List<BranchState> branchStatesNew = branchNew.getBranchState();
if (branchStates != null) {
for (int i = 0; i < branchStates.size(); i++) {
BranchState branchState = branchStates.get(i);
if (i < branchStatesNew.size()) {
BranchState branchStateNew = branchStatesNew.get(i);
if (branchStateNew.getKey() != null && !branchStateNew.getKey().trim().equals("")) {
branchState.setKey(branchStateNew.getKey());
LOG.debug(branchState.getKey());
branchState.setValue(branchStateNew.getValue());
LOG.debug(branchState.getValue());
}
}
}
}
getBranchService().save(branch);
LOG.debug("branch saved");
change = true;
}
if (KewApiConstants.NOOP.equals(opValue)) {
branchNew.setName(branch.getName());
List<BranchState> branchStates = branch.getBranchState();
List<BranchState> branchStatesNew = branchNew.getBranchState();
if (branchStates != null) {
for (int i = 0; i < branchStates.size(); i++) {
BranchState branchState = branchStates.get(i);
BranchState branchStateNew = branchStatesNew.get(i);
if (branchStateNew.getKey() == null || branchStateNew.getKey().trim().equals("")) {
branchStatesToBeDeleted.remove(branchState.getBranchStateId());
}
branchStateNew.setKey(branchState.getKey());
LOG.debug(branchState.getKey());
branchStateNew.setValue(branchState.getValue());
LOG.debug(branchState.getValue());
}
}
}
}
if (branchStatesToBeDeleted != null && branchStatesToBeDeleted.size() > 0) {
List<BranchState> branchStatesToDelete = new ArrayList<>();
List<String> branchStateIdsToBeDeleted = new ArrayList<>(branchStatesToBeDeleted.size());
// Converting a list of Long values to list of String values
for (Long branchStateToBeDeleted : branchStatesToBeDeleted) {
branchStateIdsToBeDeleted.add(String.valueOf(branchStateToBeDeleted));
}
for (String branchStateId : branchStateIdsToBeDeleted) {
BranchState branchState = getBusinessObjectService().findBySinglePrimaryKey(BranchState.class, branchStateId);
branchStatesToDelete.add(branchState);
}
getBranchService().deleteBranchStates(branchStatesToDelete);
}
WorkflowDocument workflowDocument = WorkflowDocumentFactory.loadDocument(GlobalVariables.getUserSession().getPrincipalId(), docForm.getDocumentId());
String annotation = docForm.getAnnotation();
if (StringUtils.isEmpty(annotation)) {
annotation = DEFAULT_LOG_MSG;
}
workflowDocument.logAnnotation(annotation);
ActionMessages messages = new ActionMessages();
String forward = null;
if (change) {
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("docoperation.operation.saved"));
docForm.setRouteHeader(getRouteHeaderService().getRouteHeader(docForm.getRouteHeader().getDocumentId()));
forward = "summary";
} else {
messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("docoperation.operation.noop"));
forward = "basic";
}
saveMessages(request, messages);
return mapping.findForward(forward);
}
Aggregations