All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.cq.testing.client.workflow.WorkflowInstance Maven / Gradle / Ivy

There is a newer version: 1.2.8
Show newest version
/*
 * Copyright 2017 Adobe Systems Incorporated
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.adobe.cq.testing.client.workflow;

import com.adobe.cq.testing.client.WorkflowClient;
import org.codehaus.jackson.JsonNode;

import java.util.*;


/**
 * Wraps a Workflow Instance info returned from server as JSON.
 */

public class WorkflowInstance {

    private JsonNode rootNode = null;

    /**
     * The only constructor
     *
     * @param rootNode the JSON node containing all infos
     */
    public WorkflowInstance(JsonNode rootNode){
        this.rootNode = rootNode;
    }

    /**
     * returns the id of this workflow instance
     * @return the id
     */
    public String getId(){
        if (rootNode.get("id") == null)return null;
        return rootNode.get("id").getValueAsText();
    }

    /**
     * returns the current state of this workflow instance, see {@link WorkflowClient.Status }
     * @return the current status.
     */
    public WorkflowClient.Status getStatus(){
        if (rootNode.get("state") == null)return null;
        return WorkflowClient.Status.valueOf(rootNode.get("state").getValueAsText());
    }

    /**
     * The model used for this workflow instance
     * @return  the model id
     */
    public String getModelId(){
        if (rootNode.get("model") == null)return null;
        return rootNode.get("model").getValueAsText();
    }

    /**
     * The type of payload reference ,either URL or JCR_PATH
     * @return the payload type
     */
    public String getPayloadType(){
        if (rootNode.get("payloadType") == null)return null;
        return rootNode.get("payloadType").getValueAsText();
    }

    /**
     * the payload reference
     * @return the url or jcr path
     */
    public String getPayload(){
        if (rootNode.get("payload") == null)return null;
        return rootNode.get("payload").getValueAsText();
    }

    /**
     * returns who started this workflow instance
     * @return the initiator id
     */
    public String getInitiator(){
        if (rootNode.get("initiator") == null)return null;
        return rootNode.get("initiator").getValueAsText();
    }

    /**
     * time when the workflow instance was started
     * @return the start time
     */
    public Date getStartTime() {
        if (rootNode.get("startTime") == null)return null;
        return WorkflowClient.parseJSONDate(rootNode.get("startTime").getValueAsText());
    }

    /**
     * time when this instance was completed/aborted
     * @return the end time
     */
    public Date getEndTime()  {
        if (rootNode.get("endTime") == null)return null;
        return WorkflowClient.parseJSONDate(rootNode.get("endTime").getValueAsText());
    }

    public List getWorkItemIds(){
        if (rootNode.get("workItems") == null)return null;
        List list = new ArrayList<>();

        JsonNode itemsNode = rootNode.get("workItems");
        for (int i = 0; i < itemsNode.size(); i++){
            list.add(itemsNode.get(i).get("id").getValueAsText());
        }
        return list;
    }


    /**
     * A map of all currently active work items belonging to this instance
     * @return  a map of all work items, where the key ist the work item id, and the value is
     * the node id from the corresponding model.
     */
    public Map getWorkItems() {
        if (rootNode.get("workItems") == null)return null;
        Map map = new HashMap<>();

        JsonNode itemsNode = rootNode.get("workItems");
        for (int i = 0; i < itemsNode.size(); i++){
            JsonNode workItem = itemsNode.get(i);
            map.put(workItem.get("id").getValueAsText(), workItem.get("node").getValueAsText());
        }
        return map;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy