View Javadoc

1   /**
2    * Copyright 2005-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Manages document state in relation to users seeing future requests for a particular document.
33   * Construct the object with a document and a user and ask it questions in relation to future requests.
34   *
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   *
37   */
38  public class FutureRequestDocumentStateManager {
39  
40      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      {
52          if (document.getRootBranch() != null) {
53          	for (BranchState state : document.getRootBranchState()) {
54          	    if (isStateForUser(state, principalId)) {
55              		if (isReceiveFutureRequests(state)) {
56              		    this.receiveFutureRequests = true;
57              		} else if (isDoNotReceiveFutureRequests(state)) {
58              		    this.doNotReceiveFutureRequests = true;
59              		} else if (isClearFutureRequests(state)) {
60              		    this.clearFutureRequestState = true;
61              		    this.receiveFutureRequests = false;
62              		    this.doNotReceiveFutureRequests = false;
63              		    break;
64              		}
65          	    }
66          	}
67          }
68      	if (this.isClearFutureRequestState()) {
69      	    this.clearStateFromDocument(document);
70      	}
71      }
72  
73      public FutureRequestDocumentStateManager (DocumentRouteHeaderValue document, Group kimGroup)
74      {
75          GroupService ims = KimApiServiceLocator.getGroupService();
76          List<String> principalIds =
77              ims.getMemberPrincipalIds(kimGroup.getId());
78  
79          for (String id : principalIds)
80  		{
81  		    FutureRequestDocumentStateManager requestStateMngr =
82  		        new FutureRequestDocumentStateManager(document, id);
83  		    if (requestStateMngr.isReceiveFutureRequests()) {
84  			this.receiveFutureRequests = true;
85  		    } else if (requestStateMngr.isDoNotReceiveFutureRequests()) {
86  			this.doNotReceiveFutureRequests = true;
87  		    }
88  		}
89      }
90  
91      protected void clearStateFromDocument(DocumentRouteHeaderValue document) {
92          if (document.getRootBranchState() != null) {
93          	for (BranchState state : document.getRootBranchState()) {
94          	    if (state.getKey().contains(FUTURE_REQUESTS_VAR_KEY)) {
95          		String values[] = state.getKey().split(",");
96          		state.setKey(DEACTIVATED_REQUESTS_VARY_KEY + "," + values[1] + "," + new Date().toString());
97          	    }
98          	}
99          	KEWServiceLocator.getRouteNodeService().save(document.getRootBranch());
100         }
101     }
102 
103     protected boolean isStateForUser(BranchState state, String principalId)
104     {
105         String[] values = state.getKey().split(",");
106         if (values.length != 4 || ! values[0].contains(FUTURE_REQUESTS_VAR_KEY)) {
107             return false;
108         }
109         String statePrincipalId = values[1];
110         return principalId.equals(statePrincipalId);
111     }
112 
113     protected boolean isReceiveFutureRequests(BranchState state) {
114         return state.getValue().equals(KewApiConstants.RECEIVE_FUTURE_REQUESTS_BRANCH_STATE_VALUE);
115     }
116 
117 
118     protected boolean isDoNotReceiveFutureRequests(BranchState state) {
119         return state.getValue().equals(KewApiConstants.DONT_RECEIVE_FUTURE_REQUESTS_BRANCH_STATE_VALUE);
120     }
121 
122     protected boolean isClearFutureRequests(BranchState state) {
123         return state.getValue().equals(KewApiConstants.CLEAR_FUTURE_REQUESTS_BRANCH_STATE_VALUE);
124     }
125 
126     public boolean isClearFutureRequestState() {
127         return this.clearFutureRequestState;
128     }
129 
130     public boolean isDoNotReceiveFutureRequests() {
131         return this.doNotReceiveFutureRequests;
132     }
133 
134     public boolean isReceiveFutureRequests() {
135         return this.receiveFutureRequests;
136     }
137 
138 }