Search in sources :

Example 1 with Branch

use of org.kuali.kfs.kew.engine.node.Branch in project cu-kfs by CU-CommunityApps.

the class DocumentOperationAction method getDocument.

public ActionForward getDocument(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    DocumentOperationForm docForm = (DocumentOperationForm) form;
    String docId = null;
    // check if we have a plausible docId first
    if (StringUtils.isEmpty(docForm.getDocumentId())) {
        GlobalVariables.getMessageMap().putError("documentId", KFSKeyConstants.ERROR_REQUIRED, "Document ID");
    } else {
        try {
            docId = docForm.getDocumentId().trim();
        } catch (NumberFormatException nfe) {
            GlobalVariables.getMessageMap().putError("documentId", KFSKeyConstants.ERROR_NUMERIC, "Document ID");
        }
    }
    if (docId != null) {
        // to clear Document Field first;
        docForm.resetOps();
        DocumentRouteHeaderValue routeHeader = getRouteHeaderService().getRouteHeader(docId);
        List routeNodeInstances = getRouteNodeService().findRouteNodeInstances(docId);
        Map branches1 = new HashMap();
        List branches = new ArrayList();
        if (routeHeader == null) {
            GlobalVariables.getMessageMap().putError("documentId", KFSKeyConstants.ERROR_EXISTENCE, "document");
        } else {
            docForm.setRouteHeader(routeHeader);
            setRouteHeaderTimestampsToString(docForm);
            docForm.setRouteHeaderOp(KewApiConstants.NOOP);
            docForm.setDocumentId(docForm.getDocumentId().trim());
            String initials = "";
            for (Iterator lInitials = routeHeader.getInitialRouteNodeInstances().iterator(); lInitials.hasNext(); ) {
                String initial = ((RouteNodeInstance) lInitials.next()).getRouteNodeInstanceId();
                LOG.debug(initial);
                initials = initials + initial + ", ";
            }
            if (initials.trim().length() > 1) {
                initials = initials.substring(0, initials.lastIndexOf(","));
            }
            docForm.setInitialNodeInstances(initials);
            request.getSession().setAttribute("routeNodeInstances", routeNodeInstances);
            docForm.setRouteNodeInstances(routeNodeInstances);
            if (routeNodeInstances != null) {
                Iterator routeNodeInstanceIter = routeNodeInstances.iterator();
                while (routeNodeInstanceIter.hasNext()) {
                    RouteNodeInstance routeNodeInstance = (RouteNodeInstance) routeNodeInstanceIter.next();
                    Branch branch = routeNodeInstance.getBranch();
                    if (!branches1.containsKey(branch.getName())) {
                        branches1.put(branch.getName(), branch);
                        branches.add(branch);
                        LOG.debug(branch.getName() + "; " + branch.getBranchState());
                    }
                }
                if (branches.size() < 1) {
                    branches = null;
                }
            }
            branches1.clear();
            request.getSession().setAttribute("branches", branches);
            docForm.setBranches(branches);
        }
    }
    return mapping.findForward("basic");
}
Also used : HashMap(java.util.HashMap) Branch(org.kuali.kfs.kew.engine.node.Branch) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) RouteNodeInstance(org.kuali.kfs.kew.engine.node.RouteNodeInstance) Map(java.util.Map) HashMap(java.util.HashMap) DocumentRouteHeaderValue(org.kuali.kfs.kew.routeheader.DocumentRouteHeaderValue)

Example 2 with Branch

use of org.kuali.kfs.kew.engine.node.Branch 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);
}
Also used : NodeState(org.kuali.kfs.kew.engine.node.NodeState) WorkflowDocument(org.kuali.kfs.kew.api.WorkflowDocument) WorkflowServiceErrorException(org.kuali.kfs.kew.exception.WorkflowServiceErrorException) ArrayList(java.util.ArrayList) RouteNodeInstance(org.kuali.kfs.kew.engine.node.RouteNodeInstance) Timestamp(java.sql.Timestamp) BranchState(org.kuali.kfs.kew.engine.node.BranchState) DocumentRouteHeaderValue(org.kuali.kfs.kew.routeheader.DocumentRouteHeaderValue) ActionMessages(org.apache.struts.action.ActionMessages) Branch(org.kuali.kfs.kew.engine.node.Branch) Iterator(java.util.Iterator) ActionMessage(org.apache.struts.action.ActionMessage) List(java.util.List) ArrayList(java.util.ArrayList) WorkflowServiceErrorImpl(org.kuali.kfs.kew.exception.WorkflowServiceErrorImpl) ActionItem(org.kuali.kfs.kew.actionitem.ActionItem) StringTokenizer(java.util.StringTokenizer) ActionRequest(org.kuali.kfs.kew.actionrequest.ActionRequest) ParseException(java.text.ParseException) ActionTaken(org.kuali.kfs.kew.actiontaken.ActionTaken)

Aggregations

ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Branch (org.kuali.kfs.kew.engine.node.Branch)2 RouteNodeInstance (org.kuali.kfs.kew.engine.node.RouteNodeInstance)2 DocumentRouteHeaderValue (org.kuali.kfs.kew.routeheader.DocumentRouteHeaderValue)2 Timestamp (java.sql.Timestamp)1 ParseException (java.text.ParseException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 StringTokenizer (java.util.StringTokenizer)1 ActionMessage (org.apache.struts.action.ActionMessage)1 ActionMessages (org.apache.struts.action.ActionMessages)1 ActionItem (org.kuali.kfs.kew.actionitem.ActionItem)1 ActionRequest (org.kuali.kfs.kew.actionrequest.ActionRequest)1 ActionTaken (org.kuali.kfs.kew.actiontaken.ActionTaken)1 WorkflowDocument (org.kuali.kfs.kew.api.WorkflowDocument)1 BranchState (org.kuali.kfs.kew.engine.node.BranchState)1 NodeState (org.kuali.kfs.kew.engine.node.NodeState)1 WorkflowServiceErrorException (org.kuali.kfs.kew.exception.WorkflowServiceErrorException)1