WorkflowInstanceView.java 4.8 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.workflow.exec.HistoryItem
 *  com.day.cq.workflow.exec.WorkItem
 *  com.day.cq.workflow.exec.Workflow
 *  com.day.cq.workflow.model.WorkflowModel
 *  com.day.cq.workflow.model.WorkflowNode
 *  com.day.crx.statistics.PathBuilder
 *  javax.jcr.Node
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 */
package com.day.cq.workflow.impl.statistics;

import com.day.cq.workflow.exec.HistoryItem;
import com.day.cq.workflow.exec.WorkItem;
import com.day.cq.workflow.exec.Workflow;
import com.day.cq.workflow.impl.statistics.AbstractWorkflowView;
import com.day.cq.workflow.impl.statistics.WorkflowInstancePathViewBuilder;
import com.day.cq.workflow.model.WorkflowModel;
import com.day.cq.workflow.model.WorkflowNode;
import com.day.crx.statistics.PathBuilder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

public class WorkflowInstanceView
extends AbstractWorkflowView {
    public static final String TIME_POSTFIX = "_totalTime";
    public static final String ABS_COUNT_POSTFIX = "_totalCount";
    public static final String COUNT_POSTFIX = "_currentCount";
    private WorkItem currentItem;
    private WorkItem previousItem;
    private WorkflowModel model;

    public WorkflowInstanceView(String pathPrefix, WorkflowModel model, List<HistoryItem> historyItems, WorkItem currentItem) {
        super(pathPrefix);
        this.currentItem = currentItem;
        this.previousItem = historyItems != null ? historyItems.get(historyItems.size() - 1).getWorkItem() : null;
        this.model = model;
    }

    protected PathBuilder getPathBuilder() {
        return new WorkflowInstancePathViewBuilder(this.model.getId());
    }

    public void write(Node node) throws RepositoryException {
        if (this.previousItem != null) {
            this.update(node, this.previousItem.getNode().getId(), false);
        }
        if (this.currentItem != null && this.currentItem.getWorkflow().getState().equals("ABORTED")) {
            this.update(node, this.currentItem.getNode().getId(), false);
        } else {
            if (this.currentItem != null) {
                this.update(node, this.currentItem.getNode().getId(), true);
            }
            this.updateExecutionTime(node);
        }
    }

    public Map<String, Map<String, Long>> getInstanceStats(Session session) {
        HashMap<String, Map<String, Long>> instanceStatsMap = new HashMap<String, Map<String, Long>>();
        Node node = this.getNode(session);
        if (node != null) {
            List wfNodes = this.model.getNodes();
            for (WorkflowNode wfNode : wfNodes) {
                try {
                    String id = wfNode.getId();
                    long totalCnt = node.getProperty(id + "_totalCount").getLong();
                    long totalTime = node.getProperty(id + "_totalTime").getLong();
                    long currentCount = node.getProperty(id + "_currentCount").getLong();
                    HashMap<String, Long> map = new HashMap<String, Long>();
                    map.put("Total Count", totalCnt);
                    map.put("Total Time", totalTime);
                    map.put("Current Count", currentCount);
                    instanceStatsMap.put(id, map);
                }
                catch (RepositoryException re) {}
            }
        }
        return instanceStatsMap;
    }

    private void updateExecutionTime(Node node) throws RepositoryException {
        if (this.previousItem != null) {
            String id = this.previousItem.getNode().getId() + "_totalTime";
            String absCountId = this.previousItem.getNode().getId() + "_totalCount";
            long totalTime = 0;
            if (node.hasProperty(id)) {
                totalTime = node.getProperty(id).getLong();
            }
            long absCount = 0;
            if (node.hasProperty(absCountId)) {
                absCount = node.getProperty(absCountId).getLong();
            }
            long endTime = this.currentItem != null ? this.currentItem.getTimeEnded().getTime() : this.previousItem.getWorkflow().getTimeEnded().getTime();
            long time = endTime - this.previousItem.getTimeStarted().getTime();
            node.setProperty(id, time + totalTime);
            node.setProperty(absCountId, ++absCount);
        }
    }

    private void update(Node node, String mid, boolean doIncrement) throws RepositoryException {
        String id = mid + "_currentCount";
        long count = 0;
        if (node.hasProperty(id)) {
            count = node.getProperty(id).getLong();
        }
        count = doIncrement ? ++count : --count;
        node.setProperty(id, count > 0 ? count : 0);
    }
}