1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kew.util; |
17 | |
|
18 | |
import org.apache.log4j.Logger; |
19 | |
import org.kuali.rice.kew.api.KewApiConstants; |
20 | |
import org.kuali.rice.kew.engine.node.BranchState; |
21 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
22 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
23 | |
import org.kuali.rice.kim.api.group.Group; |
24 | |
import org.kuali.rice.kim.api.group.GroupService; |
25 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
26 | |
|
27 | |
import java.util.Date; |
28 | |
import java.util.List; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class FutureRequestDocumentStateManager { |
39 | |
|
40 | 0 | private static final Logger LOG = Logger.getLogger(FutureRequestDocumentStateManager.class); |
41 | |
|
42 | |
private boolean receiveFutureRequests; |
43 | |
private boolean doNotReceiveFutureRequests; |
44 | |
private boolean clearFutureRequestState; |
45 | |
|
46 | |
|
47 | |
public static final String FUTURE_REQUESTS_VAR_KEY = BranchState.VARIABLE_PREFIX + KewApiConstants.RECEIVE_FUTURE_REQUESTS_BRANCH_STATE_KEY; |
48 | |
public static final String DEACTIVATED_REQUESTS_VARY_KEY = BranchState.VARIABLE_PREFIX + KewApiConstants.DEACTIVATED_FUTURE_REQUESTS_BRANCH_STATE_KEY; |
49 | |
|
50 | |
public FutureRequestDocumentStateManager (DocumentRouteHeaderValue document, String principalId) |
51 | 0 | { |
52 | 0 | if (document.getRootBranch() != null) { |
53 | 0 | for (BranchState state : document.getRootBranchState()) { |
54 | 0 | if (isStateForUser(state, principalId)) { |
55 | 0 | if (isReceiveFutureRequests(state)) { |
56 | 0 | this.receiveFutureRequests = true; |
57 | 0 | } else if (isDoNotReceiveFutureRequests(state)) { |
58 | 0 | this.doNotReceiveFutureRequests = true; |
59 | 0 | } else if (isClearFutureRequests(state)) { |
60 | 0 | this.clearFutureRequestState = true; |
61 | 0 | this.receiveFutureRequests = false; |
62 | 0 | this.doNotReceiveFutureRequests = false; |
63 | 0 | break; |
64 | |
} |
65 | |
} |
66 | |
} |
67 | |
} |
68 | 0 | if (this.isClearFutureRequestState()) { |
69 | 0 | this.clearStateFromDocument(document); |
70 | |
} |
71 | 0 | } |
72 | |
|
73 | |
public FutureRequestDocumentStateManager (DocumentRouteHeaderValue document, Group kimGroup) |
74 | 0 | { |
75 | 0 | GroupService ims = KimApiServiceLocator.getGroupService(); |
76 | 0 | List<String> principalIds = |
77 | |
ims.getMemberPrincipalIds(kimGroup.getId()); |
78 | |
|
79 | 0 | for (String id : principalIds) |
80 | |
{ |
81 | 0 | FutureRequestDocumentStateManager requestStateMngr = |
82 | |
new FutureRequestDocumentStateManager(document, id); |
83 | 0 | if (requestStateMngr.isReceiveFutureRequests()) { |
84 | 0 | this.receiveFutureRequests = true; |
85 | 0 | } else if (requestStateMngr.isDoNotReceiveFutureRequests()) { |
86 | 0 | this.doNotReceiveFutureRequests = true; |
87 | |
} |
88 | 0 | } |
89 | 0 | } |
90 | |
|
91 | |
protected void clearStateFromDocument(DocumentRouteHeaderValue document) { |
92 | 0 | if (document.getRootBranchState() != null) { |
93 | 0 | for (BranchState state : document.getRootBranchState()) { |
94 | 0 | if (state.getKey().contains(FUTURE_REQUESTS_VAR_KEY)) { |
95 | 0 | String values[] = state.getKey().split(","); |
96 | 0 | state.setKey(DEACTIVATED_REQUESTS_VARY_KEY + "," + values[1] + "," + new Date().toString()); |
97 | 0 | } |
98 | |
} |
99 | 0 | KEWServiceLocator.getRouteNodeService().save(document.getRootBranch()); |
100 | |
} |
101 | 0 | } |
102 | |
|
103 | |
protected boolean isStateForUser(BranchState state, String principalId) |
104 | |
{ |
105 | 0 | String[] values = state.getKey().split(","); |
106 | 0 | if (values.length != 4 || ! values[0].contains(FUTURE_REQUESTS_VAR_KEY)) { |
107 | 0 | return false; |
108 | |
} |
109 | 0 | String statePrincipalId = values[1]; |
110 | 0 | return principalId.equals(statePrincipalId); |
111 | |
} |
112 | |
|
113 | |
protected boolean isReceiveFutureRequests(BranchState state) { |
114 | 0 | return state.getValue().equals(KewApiConstants.RECEIVE_FUTURE_REQUESTS_BRANCH_STATE_VALUE); |
115 | |
} |
116 | |
|
117 | |
|
118 | |
protected boolean isDoNotReceiveFutureRequests(BranchState state) { |
119 | 0 | return state.getValue().equals(KewApiConstants.DONT_RECEIVE_FUTURE_REQUESTS_BRANCH_STATE_VALUE); |
120 | |
} |
121 | |
|
122 | |
protected boolean isClearFutureRequests(BranchState state) { |
123 | 0 | return state.getValue().equals(KewApiConstants.CLEAR_FUTURE_REQUESTS_BRANCH_STATE_VALUE); |
124 | |
} |
125 | |
|
126 | |
public boolean isClearFutureRequestState() { |
127 | 0 | return this.clearFutureRequestState; |
128 | |
} |
129 | |
|
130 | |
public boolean isDoNotReceiveFutureRequests() { |
131 | 0 | return this.doNotReceiveFutureRequests; |
132 | |
} |
133 | |
|
134 | |
public boolean isReceiveFutureRequests() { |
135 | 0 | return this.receiveFutureRequests; |
136 | |
} |
137 | |
|
138 | |
} |