Forum Stats

  • 3,876,125 Users
  • 2,267,065 Discussions
  • 7,912,441 Comments

Discussions

Permanently change variable passed from process to ADF

abdiel
abdiel Member Posts: 33
edited Feb 2, 2017 8:42AM in JDeveloper and ADF

I need to change the variables passed from process to the ADF and doing the change permanently, even if the task is not finish. Let's say an idPerson coming from the process, and when a button action will be called, that id be updated. Then if the user cancel the activity or close the browser and the activity is reopened, the value must be the new one. Any help will be appreciated.

Thanks in advance.

Best Answer

  • abdiel
    abdiel Member Posts: 33
    edited Feb 2, 2017 8:42AM Answer ✓

    Finally I found a way to do it through the java API. Althougth still some problems remain saving the task, I think that the code can be posted. Hope this help some one.

        /**

         * Set a value to current activity payload variable

         * @param name Name of variable. Can be like BoProcess.idPerson

         * @param value Value to set

         * @throws Exception

         */

        public static void setPayloadValue(String name, Object value) throws Exception {

            FacesContext context = FacesContext.getCurrentInstance();

            String ctx = (String) context.getApplication().evaluateExpressionGet(context, "#{pageFlowScope.bpmWorklistContext}", String.class);

            String tskId = (String)context.getApplication().evaluateExpressionGet(context, "#{pageFlowScope.bpmWorklistTaskId}", String.class);

            //IWorkflowServiceClient workflowSvcClient = WorkflowService.getWorkflowServiceClient(); //seems to be for previous version

            IWorkflowServiceClient workflowSvcClient =

                WorkflowServiceClientFactory.getWorkflowServiceClient(WorkflowServiceClientFactory.REMOTE_CLIENT);

            ITaskService taskSvc = workflowSvcClient.getTaskService();

            ITaskQueryService wfQueryService = workflowSvcClient.getTaskQueryService();

            IWorkflowContext wfContext = wfQueryService.getWorkflowContext(ctx);

            Task myTask = wfQueryService.getTaskDetailsById(wfContext, tskId);

            Element payloadElement = myTask.getPayloadAsElement();

            Node node = getDomNode(name, payloadElement);

            node.setTextContent(value.toString());

          

            myTask.setPayloadAsElement(payloadElement);

            taskSvc.updateTask(wfContext, myTask);

        }

      

        /**

         * Get the value of a variable in the payload of the current activity

         * @param name Name of variable

         * @return     The value of the variable

         * @throws Exception

         */

        public static String getPayloadValue(String name) throws Exception {

            FacesContext context = FacesContext.getCurrentInstance();

            String ctx = (String) context.getApplication().evaluateExpressionGet(context, "#{pageFlowScope.bpmWorklistContext}", String.class);

            String tskId = (String)context.getApplication().evaluateExpressionGet(context, "#{pageFlowScope.bpmWorklistTaskId}", String.class);

            //IWorkflowServiceClient workflowSvcClient = WorkflowService.getWorkflowServiceClient(); //seems to be for previous version

            IWorkflowServiceClient workflowSvcClient =

                WorkflowServiceClientFactory.getWorkflowServiceClient(WorkflowServiceClientFactory.REMOTE_CLIENT);

            ITaskService taskSvc = workflowSvcClient.getTaskService();

            ITaskQueryService wfQueryService = workflowSvcClient.getTaskQueryService();

            IWorkflowContext wfContext = wfQueryService.getWorkflowContext(ctx);

            Task myTask = wfQueryService.getTaskDetailsById(wfContext, tskId);

            Element payloadElement = myTask.getPayloadAsElement();      

            Node node = getDomNode(name, payloadElement);

            return node.getTextContent();

        }

        /**

         * Get a node recursive from a Dom Node

         *

         * @param name Name of node, can be like BoProcess.idPerson

         * @param node Node to search

         * @return     Node found, null if not found.

         */

        public static Node getDomNode(String name, Node node) {

            String[] vars = name.split("\\.");

            if (vars.length == 1) {

                return getFirstLevelDomNode(name, node);

            } else if (vars.length > 1) {

                String var = vars[0];

                String splitName = name.substring(name.indexOf(".")+1);

                Node childNode = getFirstLevelDomNode(var, node);

                return getDomNode(splitName,childNode);

            }

            return null;

        }

      

        private static Node getFirstLevelDomNode(String name, Node node) {

            NodeList childNodes = node.getChildNodes();

            for (int i = 0; i < childNodes.getLength(); i++) {

                Node currentNode = childNodes.item(i);

                if (currentNode.getNodeType() == Node.ELEMENT_NODE && name.equals(currentNode.getNodeName())) {

                    return currentNode;

                }

            }

            return null;

        }

Answers

  • abdiel
    abdiel Member Posts: 33
    edited Feb 2, 2017 8:42AM Answer ✓

    Finally I found a way to do it through the java API. Althougth still some problems remain saving the task, I think that the code can be posted. Hope this help some one.

        /**

         * Set a value to current activity payload variable

         * @param name Name of variable. Can be like BoProcess.idPerson

         * @param value Value to set

         * @throws Exception

         */

        public static void setPayloadValue(String name, Object value) throws Exception {

            FacesContext context = FacesContext.getCurrentInstance();

            String ctx = (String) context.getApplication().evaluateExpressionGet(context, "#{pageFlowScope.bpmWorklistContext}", String.class);

            String tskId = (String)context.getApplication().evaluateExpressionGet(context, "#{pageFlowScope.bpmWorklistTaskId}", String.class);

            //IWorkflowServiceClient workflowSvcClient = WorkflowService.getWorkflowServiceClient(); //seems to be for previous version

            IWorkflowServiceClient workflowSvcClient =

                WorkflowServiceClientFactory.getWorkflowServiceClient(WorkflowServiceClientFactory.REMOTE_CLIENT);

            ITaskService taskSvc = workflowSvcClient.getTaskService();

            ITaskQueryService wfQueryService = workflowSvcClient.getTaskQueryService();

            IWorkflowContext wfContext = wfQueryService.getWorkflowContext(ctx);

            Task myTask = wfQueryService.getTaskDetailsById(wfContext, tskId);

            Element payloadElement = myTask.getPayloadAsElement();

            Node node = getDomNode(name, payloadElement);

            node.setTextContent(value.toString());

          

            myTask.setPayloadAsElement(payloadElement);

            taskSvc.updateTask(wfContext, myTask);

        }

      

        /**

         * Get the value of a variable in the payload of the current activity

         * @param name Name of variable

         * @return     The value of the variable

         * @throws Exception

         */

        public static String getPayloadValue(String name) throws Exception {

            FacesContext context = FacesContext.getCurrentInstance();

            String ctx = (String) context.getApplication().evaluateExpressionGet(context, "#{pageFlowScope.bpmWorklistContext}", String.class);

            String tskId = (String)context.getApplication().evaluateExpressionGet(context, "#{pageFlowScope.bpmWorklistTaskId}", String.class);

            //IWorkflowServiceClient workflowSvcClient = WorkflowService.getWorkflowServiceClient(); //seems to be for previous version

            IWorkflowServiceClient workflowSvcClient =

                WorkflowServiceClientFactory.getWorkflowServiceClient(WorkflowServiceClientFactory.REMOTE_CLIENT);

            ITaskService taskSvc = workflowSvcClient.getTaskService();

            ITaskQueryService wfQueryService = workflowSvcClient.getTaskQueryService();

            IWorkflowContext wfContext = wfQueryService.getWorkflowContext(ctx);

            Task myTask = wfQueryService.getTaskDetailsById(wfContext, tskId);

            Element payloadElement = myTask.getPayloadAsElement();      

            Node node = getDomNode(name, payloadElement);

            return node.getTextContent();

        }

        /**

         * Get a node recursive from a Dom Node

         *

         * @param name Name of node, can be like BoProcess.idPerson

         * @param node Node to search

         * @return     Node found, null if not found.

         */

        public static Node getDomNode(String name, Node node) {

            String[] vars = name.split("\\.");

            if (vars.length == 1) {

                return getFirstLevelDomNode(name, node);

            } else if (vars.length > 1) {

                String var = vars[0];

                String splitName = name.substring(name.indexOf(".")+1);

                Node childNode = getFirstLevelDomNode(var, node);

                return getDomNode(splitName,childNode);

            }

            return null;

        }

      

        private static Node getFirstLevelDomNode(String name, Node node) {

            NodeList childNodes = node.getChildNodes();

            for (int i = 0; i < childNodes.getLength(); i++) {

                Node currentNode = childNodes.item(i);

                if (currentNode.getNodeType() == Node.ELEMENT_NODE && name.equals(currentNode.getNodeName())) {

                    return currentNode;

                }

            }

            return null;

        }

This discussion has been closed.