WorkflowInstanceView.java
4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* 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);
}
}