View Javadoc

1   /**
2    * Copyright 2005-2012 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.documentoperation.web;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.apache.struts.action.ActionForm;
20  import org.apache.struts.action.ActionForward;
21  import org.apache.struts.action.ActionMapping;
22  import org.apache.struts.action.ActionMessage;
23  import org.apache.struts.action.ActionMessages;
24  import org.kuali.rice.core.api.util.RiceConstants;
25  import org.kuali.rice.core.api.util.RiceKeyConstants;
26  import org.kuali.rice.kew.actionitem.ActionItem;
27  import org.kuali.rice.kew.actionlist.service.ActionListService;
28  import org.kuali.rice.kew.actionrequest.ActionRequestValue;
29  import org.kuali.rice.kew.actionrequest.service.ActionRequestService;
30  import org.kuali.rice.kew.api.document.DocumentOrchestrationQueue;
31  import org.kuali.rice.kew.api.document.DocumentProcessingOptions;
32  import org.kuali.rice.kew.api.document.DocumentRefreshQueue;
33  import org.kuali.rice.kew.api.action.ActionInvocation;
34  import org.kuali.rice.kew.api.action.ActionInvocationQueue;
35  import org.kuali.rice.kew.actiontaken.ActionTakenValue;
36  import org.kuali.rice.kew.actiontaken.service.ActionTakenService;
37  import org.kuali.rice.kew.api.KewApiServiceLocator;
38  import org.kuali.rice.kew.api.WorkflowDocument;
39  import org.kuali.rice.kew.api.WorkflowDocumentFactory;
40  import org.kuali.rice.kew.api.WorkflowRuntimeException;
41  import org.kuali.rice.kew.api.action.ActionType;
42  import org.kuali.rice.kew.api.document.DocumentProcessingQueue;
43  import org.kuali.rice.kew.api.document.OrchestrationConfig;
44  import org.kuali.rice.kew.api.document.attribute.DocumentAttributeIndexingQueue;
45  import org.kuali.rice.kew.doctype.bo.DocumentType;
46  import org.kuali.rice.kew.doctype.service.DocumentTypeService;
47  import org.kuali.rice.kew.engine.node.Branch;
48  import org.kuali.rice.kew.engine.node.BranchState;
49  import org.kuali.rice.kew.engine.node.NodeState;
50  import org.kuali.rice.kew.engine.node.RouteNodeInstance;
51  import org.kuali.rice.kew.engine.node.service.BranchService;
52  import org.kuali.rice.kew.engine.node.service.RouteNodeService;
53  import org.kuali.rice.kew.exception.WorkflowServiceErrorException;
54  import org.kuali.rice.kew.exception.WorkflowServiceErrorImpl;
55  import org.kuali.rice.kew.messaging.MessageServiceNames;
56  import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
57  import org.kuali.rice.kew.routeheader.service.RouteHeaderService;
58  import org.kuali.rice.kew.rule.bo.RuleTemplateBo;
59  import org.kuali.rice.kew.service.KEWServiceLocator;
60  import org.kuali.rice.kew.api.KewApiConstants;
61  import org.kuali.rice.kew.web.KewKualiAction;
62  import org.kuali.rice.kim.api.identity.principal.Principal;
63  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
64  import org.kuali.rice.krad.util.GlobalVariables;
65  
66  import javax.servlet.ServletException;
67  import javax.servlet.http.HttpServletRequest;
68  import javax.servlet.http.HttpServletResponse;
69  import java.io.IOException;
70  import java.sql.Timestamp;
71  import java.text.ParseException;
72  import java.util.ArrayList;
73  import java.util.HashMap;
74  import java.util.HashSet;
75  import java.util.Iterator;
76  import java.util.List;
77  import java.util.Map;
78  import java.util.Set;
79  import java.util.StringTokenizer;
80  
81  
82  /**
83   * Struts Action for doing editing of workflow documents.
84   *
85   * @author Kuali Rice Team (rice.collab@kuali.org)
86   */
87  public class DocumentOperationAction extends KewKualiAction {
88  	private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(DocumentOperationAction.class);
89  	private static final String DEFAULT_LOG_MSG = "Admin change via document operation";
90  
91  	public ActionForward start(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
92  		return mapping.findForward("basic");
93  	}
94  
95  	public ActionForward getDocument(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
96  		DocumentOperationForm docForm = (DocumentOperationForm) form;
97  		String docId = null;
98  		
99  		// check if we have a plausible docId first
100 		if (StringUtils.isEmpty(docForm.getDocumentId())) {
101 			GlobalVariables.getMessageMap().putError("documentId", RiceKeyConstants.ERROR_REQUIRED, "Document ID");
102 		} else {
103 			try {
104 				docId = docForm.getDocumentId().trim();
105 			} catch (NumberFormatException nfe) {
106 				GlobalVariables.getMessageMap().putError("documentId", RiceKeyConstants.ERROR_NUMERIC, "Document ID");
107 			}
108 		}
109 
110 		if (docId != null) {
111 			//to clear Document Field first;
112 			docForm.resetOps();
113 			DocumentRouteHeaderValue routeHeader = getRouteHeaderService().getRouteHeader(docId);
114 			List routeNodeInstances=getRouteNodeService().findRouteNodeInstances(docId);
115 			Map branches1=new HashMap();
116 			List branches=new ArrayList();
117 
118 			if (routeHeader == null) {
119 				GlobalVariables.getMessageMap().putError("documentId", RiceKeyConstants.ERROR_EXISTENCE, "document");
120 			} else {
121 				materializeDocument(routeHeader);
122 				docForm.setRouteHeader(routeHeader);
123 				setRouteHeaderTimestampsToString(docForm);
124 				docForm.setRouteHeaderOp(KewApiConstants.NOOP);
125 				docForm.setDocumentId(docForm.getDocumentId().trim());
126 				String initials="";
127 				for(Iterator lInitials=routeHeader.getInitialRouteNodeInstances().iterator();lInitials.hasNext();){
128 					String initial=((RouteNodeInstance)lInitials.next()).getRouteNodeInstanceId();
129 					LOG.debug(initial);
130 					initials=initials+initial+", ";
131 				}
132 				if(initials.trim().length()>1){
133 					initials=initials.substring(0,initials.lastIndexOf(","));
134 				}
135 				docForm.setInitialNodeInstances(initials);
136 				request.getSession().setAttribute("routeNodeInstances",routeNodeInstances);
137 				docForm.setRouteNodeInstances(routeNodeInstances);
138 				if(routeNodeInstances!=null){
139 					Iterator routeNodeInstanceIter=routeNodeInstances.iterator();
140 					while(routeNodeInstanceIter.hasNext()){
141 						RouteNodeInstance routeNodeInstance=(RouteNodeInstance) routeNodeInstanceIter.next();
142 						Branch branch=routeNodeInstance.getBranch();
143 						if (! branches1.containsKey(branch.getName())){
144 							branches1.put(branch.getName(),branch);
145 							branches.add(branch);
146 							LOG.debug(branch.getName()+"; "+branch.getBranchState());
147 						}
148 					}
149 					if(branches.size()<1){
150 						branches=null;
151 					}
152 				}
153 				branches1.clear();
154 				request.getSession().setAttribute("branches",branches);
155 				docForm.setBranches(branches);
156 			}
157 		}
158 			
159 		return mapping.findForward("basic");
160 	}
161 
162 	/**
163 	 * Sets up various objects on the document which are required for use inside of the Struts and JSP framework.
164 	 *
165 	 * Specifically, if a document has action requests with null RouteNodeInstances, it will create empty node instance
166 	 * objects.
167 	 */
168 	private void materializeDocument(DocumentRouteHeaderValue document) {
169 		for (Iterator iterator = document.getActionRequests().iterator(); iterator.hasNext();) {
170 			ActionRequestValue request = (ActionRequestValue) iterator.next();
171 			if (request.getNodeInstance() == null) {
172 				request.setNodeInstance(new RouteNodeInstance());
173 			}
174 		}
175 	}
176 
177 	public ActionForward clear(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
178 		DocumentOperationForm docForm = (DocumentOperationForm) form;
179 		docForm.setRouteHeader(new DocumentRouteHeaderValue());
180 		docForm.setDocumentId(null);
181 		return mapping.findForward("basic");
182 	}
183 
184 	public ActionMessages establishRequiredState(HttpServletRequest request, ActionForm form) throws Exception {
185 		return null;
186 	}
187 
188 	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
189 		DocumentOperationForm docForm = (DocumentOperationForm) form;
190 		boolean change = false;
191 
192 		String routeHeaderOp = docForm.getRouteHeaderOp();
193 		if (!KewApiConstants.UPDATE.equals(routeHeaderOp) && !KewApiConstants.NOOP.equals(routeHeaderOp)) {
194 			throw new WorkflowServiceErrorException("Document operation not defined", new WorkflowServiceErrorImpl("Document operation not defined", "docoperation.operation.invalid"));
195 		}
196 		if (KewApiConstants.UPDATE.equals(routeHeaderOp)) {
197 			setRouteHeaderTimestamps(docForm);
198 			DocumentRouteHeaderValue dHeader = docForm.getRouteHeader();
199 			String initials=docForm.getInitialNodeInstances();
200 			List<RouteNodeInstance> lInitials = new ArrayList<RouteNodeInstance>();
201 			if (StringUtils.isNotEmpty(initials)){ 
202 				StringTokenizer tokenInitials=new StringTokenizer(initials,",");
203 				while (tokenInitials.hasMoreTokens()) {
204 					String instanceId = tokenInitials.nextToken().trim();
205 					LOG.debug(instanceId);
206 					RouteNodeInstance instance = getRouteNodeService().findRouteNodeInstanceById(instanceId);
207 					lInitials.add(instance);
208 				}
209 			}
210 			dHeader.setInitialRouteNodeInstances(lInitials);
211 			getRouteHeaderService().validateRouteHeader(docForm.getRouteHeader());
212 			getRouteHeaderService().saveRouteHeader(docForm.getRouteHeader());
213 			change = true;
214 		}
215 
216 		for (Iterator actionRequestIter = docForm.getActionRequestOps().iterator(); actionRequestIter.hasNext();) {
217 			DocOperationIndexedParameter actionRequestOp = (DocOperationIndexedParameter) actionRequestIter.next();
218 			int index = actionRequestOp.getIndex().intValue();
219 			String opValue = actionRequestOp.getValue();
220 			ActionRequestValue actionRequest = docForm.getRouteHeader().getDocActionRequest(index);
221 			String createDateParamName = "actionRequestCreateDate" + index;
222 
223 			if (!KewApiConstants.UPDATE.equals(opValue) && !KewApiConstants.DELETE.equals(opValue) && !KewApiConstants.NOOP.equals(opValue)) {
224 				throw new WorkflowServiceErrorException("Action request operation not defined", new WorkflowServiceErrorImpl("Action request operation not defined", "docoperation.actionrequest.operation.invalid"));
225 			}
226 			if (KewApiConstants.UPDATE.equals(opValue)) {
227 				try {
228 					actionRequest.setCreateDate(new Timestamp(RiceConstants.getDefaultDateFormat().parse(request.getParameter(createDateParamName)).getTime()));
229 					actionRequest.setCreateDateString(RiceConstants.getDefaultDateFormat().format(actionRequest.getCreateDate()));
230 					actionRequest.setDocumentId(docForm.getRouteHeader().getDocumentId());
231 					actionRequest.setParentActionRequest(getActionRequestService().findByActionRequestId(actionRequest.getParentActionRequestId()));
232 					actionRequest.setActionTaken(getActionTakenService().findByActionTakenId(actionRequest.getActionTakenId()));
233 					if (actionRequest.getNodeInstance() != null && actionRequest.getNodeInstance().getRouteNodeInstanceId() == null) {
234 						actionRequest.setNodeInstance(null);
235 					} else if (actionRequest.getNodeInstance() != null && actionRequest.getNodeInstance().getRouteNodeInstanceId() != null) {
236 						actionRequest.setNodeInstance(KEWServiceLocator.getRouteNodeService().findRouteNodeInstanceById(actionRequest.getNodeInstance().getRouteNodeInstanceId()));
237 					}
238 					// getActionRequestService().validateActionRequest(actionRequest);
239 					getActionRequestService().saveActionRequest(actionRequest);
240 					change = true;
241 				} catch (ParseException pe) {
242 					throw new WorkflowServiceErrorException("Action request create date parsing error", new WorkflowServiceErrorImpl("Action request create date parsing error", "docoperation.actionrequests.dateparsing.error", actionRequest.getActionRequestId().toString()));
243 				}
244 
245 			}
246 			if (KewApiConstants.DELETE.equals(opValue)) {
247 			    getActionRequestService().deleteActionRequestGraph(actionRequest);
248 			    change = true;
249 			}
250 		}
251 
252 		for (Iterator actionTakenIter = docForm.getActionTakenOps().iterator(); actionTakenIter.hasNext();) {
253 			DocOperationIndexedParameter actionTakenOp = (DocOperationIndexedParameter) actionTakenIter.next();
254 			int index = actionTakenOp.getIndex().intValue();
255 			String opValue = actionTakenOp.getValue();
256 
257 			String actionDateParamName = "actionTakenActionDate" + index;
258 			ActionTakenValue actionTaken = docForm.getRouteHeader().getDocActionTaken(index);
259 			if (!KewApiConstants.UPDATE.equals(opValue) && !KewApiConstants.DELETE.equals(opValue) && !KewApiConstants.NOOP.equals(opValue)) {
260 				throw new WorkflowServiceErrorException("Action taken operation not defined", new WorkflowServiceErrorImpl("Action taken operation not defined", "docoperation.actiontaken.operation.invalid"));
261 			}
262 			if (KewApiConstants.UPDATE.equals(opValue)) {
263 				try {
264 					actionTaken.setActionDate(new Timestamp(RiceConstants.getDefaultDateFormat().parse(request.getParameter(actionDateParamName)).getTime()));
265 					actionTaken.setActionDateString(RiceConstants.getDefaultDateFormat().format(actionTaken.getActionDate()));
266 					// getActionTakenService().validateActionTaken(actionTaken);
267 					getActionTakenService().saveActionTaken(actionTaken);
268 					change = true;
269 				} catch (ParseException pe) {
270 					throw new WorkflowServiceErrorException("Action taken action date parsing error", new WorkflowServiceErrorImpl("Action taken action date parse error", "docoperation.actionstaken.dateparsing.error", actionTaken.getActionTakenId().toString()));
271 				}
272 			}
273 			if (KewApiConstants.DELETE.equals(opValue)) {
274 				getActionTakenService().delete(actionTaken);
275 				change = true;
276 			}
277 		}
278 
279 		for (Iterator actionItemIter = docForm.getActionItemOps().iterator(); actionItemIter.hasNext();) {
280 			DocOperationIndexedParameter actionItemOp = (DocOperationIndexedParameter) actionItemIter.next();
281 			int index = actionItemOp.getIndex().intValue();
282 			String opValue = actionItemOp.getValue();
283 
284 			String dateAssignedParamName = "actionItemDateAssigned" + index;
285 			ActionItem actionItem = docForm.getRouteHeader().getDocActionItem(index);
286 			if (!KewApiConstants.UPDATE.equals(opValue) && !KewApiConstants.DELETE.equals(opValue) && !KewApiConstants.NOOP.equals(opValue)) {
287 				throw new WorkflowServiceErrorException("Action Item operation not defined", new WorkflowServiceErrorImpl("Action Item operation not defined", "docoperation.operation.invalid"));
288 			}
289 			if (KewApiConstants.UPDATE.equals(opValue)) {
290 				try {
291 					actionItem.setDateAssigned(new Timestamp(RiceConstants.getDefaultDateFormat().parse(request.getParameter(dateAssignedParamName)).getTime()));
292 					actionItem.setDateAssignedStringValue(RiceConstants.getDefaultDateFormat().format(actionItem.getDateAssigned()));
293 					actionItem.setDocumentId(docForm.getRouteHeader().getDocumentId());
294 					// getActionItemService().validateActionItem(actionItem);
295 					getActionListService().saveActionItem(actionItem);
296 					change = true;
297 				} catch (ParseException pe) {
298 					throw new WorkflowServiceErrorException("Action item date assigned parsing error", new WorkflowServiceErrorImpl("Action item date assigned parse error", "docoperation.actionitem.dateassignedparsing.error", actionItem.getId().toString()));
299 				}
300 			}
301 			if (KewApiConstants.DELETE.equals(opValue)) {
302                 try {
303                     actionItem.setDateAssigned(new Timestamp(RiceConstants.getDefaultDateFormat().parse(request.getParameter(dateAssignedParamName)).getTime()));
304                     actionItem.setDateAssignedStringValue(RiceConstants.getDefaultDateFormat().format(actionItem.getDateAssigned()));
305                     actionItem.setDocumentId(docForm.getRouteHeader().getDocumentId());
306                     getActionListService().deleteActionItem(actionItem);
307                     change = true;
308                 } catch (ParseException pe) {
309                     throw new WorkflowServiceErrorException("Action item date assigned parsing error", new WorkflowServiceErrorImpl("Action item date assigned parse error", "docoperation.actionitem.dateassignedparsing.error", actionItem.getId().toString()));
310                 }
311 			}
312 		}
313 
314 		List routeNodeInstances=(List)(request.getSession().getAttribute("routeNodeInstances"));
315 		String ids = (docForm.getNodeStatesDelete() != null) ? docForm.getNodeStatesDelete().trim() : null;
316 		List statesToBeDeleted=new ArrayList();
317 		if(ids!=null && !ids.equals("")){
318 		    StringTokenizer idSets=new StringTokenizer(ids);
319 		    while (idSets.hasMoreTokens()) {
320 		    	String id=idSets.nextToken().trim();
321 		    	statesToBeDeleted.add(Long.valueOf(id));
322 		     }
323 		}
324 
325 		for (Iterator routeNodeInstanceIter = docForm.getRouteNodeInstanceOps().iterator(); routeNodeInstanceIter.hasNext();) {
326 			DocOperationIndexedParameter routeNodeInstanceOp = (DocOperationIndexedParameter) routeNodeInstanceIter.next();
327 			int index = routeNodeInstanceOp.getIndex().intValue();
328 			String opValue = routeNodeInstanceOp.getValue();
329             LOG.debug(opValue);
330 			RouteNodeInstance routeNodeInstance = (RouteNodeInstance)(routeNodeInstances.get(index));
331 			RouteNodeInstance routeNodeInstanceNew = (RouteNodeInstance)(docForm.getRouteNodeInstance(index));
332 			if (!KewApiConstants.UPDATE.equals(opValue) && !KewApiConstants.DELETE.equals(opValue) && !KewApiConstants.NOOP.equals(opValue)) {
333 				throw new WorkflowServiceErrorException("Route Node Instance Operation not defined", new WorkflowServiceErrorImpl("Route Node Instance Operation not defined", "docoperation.routenodeinstance.operation.invalid"));
334 			}
335 			if (KewApiConstants.UPDATE.equals(opValue)) {
336 				//LOG.debug("saving routeNodeInstance:"+routeNodeInstance.getRouteNodeInstanceId());
337 				//getRouteNodeService().save(routeNodeInstance);
338 				routeNodeInstance.setActive(routeNodeInstanceNew.isActive());
339 				LOG.debug(Boolean.toString(routeNodeInstanceNew.isActive()));
340 				routeNodeInstance.setComplete(routeNodeInstanceNew.isComplete());
341 				routeNodeInstance.setInitial(routeNodeInstanceNew.isInitial());
342 				List nodeStates=routeNodeInstance.getState();
343 				List nodeStatesNew=routeNodeInstanceNew.getState();
344 
345 				if(nodeStates!=null){
346 					for(int i=0;i<nodeStates.size();i++){
347 					   NodeState nodeState=(NodeState)nodeStates.get(i);
348 					   NodeState nodeStateNew=(NodeState)nodeStatesNew.get(i);
349 					   if(nodeStateNew.getKey()!=null && ! nodeStateNew.getKey().trim().equals("")){
350 					   nodeState.setKey(nodeStateNew.getKey());
351 					   LOG.debug(nodeState.getKey());
352 					   nodeState.setValue(nodeStateNew.getValue());
353 					   LOG.debug(nodeState.getValue());
354 					   }
355 				    }
356 				}
357 				getRouteNodeService().save(routeNodeInstance);
358 				LOG.debug("saved");
359 				change = true;
360 			}
361 
362 
363 			if (KewApiConstants.DELETE.equals(opValue)) {
364 				List nodeStates=routeNodeInstance.getState();
365 				List nodeStatesNew=routeNodeInstanceNew.getState();
366 
367 				if(nodeStates!=null){
368 					for(int i=0;i<nodeStates.size();i++){
369 					   NodeState nodeState=(NodeState)nodeStates.get(i);
370 					   NodeState nodeStateNew=(NodeState)nodeStatesNew.get(i);
371 					   if(nodeStateNew.getKey()==null || nodeStateNew.getKey().trim().equals("")){
372 					     statesToBeDeleted.remove(nodeState.getNodeStateId());
373 					   }
374 				    }
375 				}
376 				getRouteNodeService().deleteByRouteNodeInstance(routeNodeInstance);
377 				LOG.debug(routeNodeInstance.getRouteNodeInstanceId()+" is deleted");
378 				change = true;
379 				break;
380 			}
381 
382 			if (KewApiConstants.NOOP.equals(opValue)){
383 				routeNodeInstanceNew.setActive(routeNodeInstance.isActive());
384 				routeNodeInstanceNew.setComplete(routeNodeInstance.isComplete());
385 				routeNodeInstanceNew.setInitial(routeNodeInstance.isInitial());
386 				List nodeStates=routeNodeInstance.getState();
387 				List nodeStatesNew=routeNodeInstanceNew.getState();
388 				if(nodeStates!=null){
389 				   for(int i=0;i<nodeStates.size();i++){
390 					   NodeState nodeState=(NodeState)nodeStates.get(i);
391 					   NodeState nodeStateNew=(NodeState)nodeStatesNew.get(i);
392 					   if(nodeStateNew.getKey()==null || nodeStateNew.getKey().trim().equals("")){
393 						     statesToBeDeleted.remove(nodeState.getNodeStateId());
394 					   }
395 					   nodeStateNew.setKey(nodeState.getKey());
396 					   nodeStateNew.setValue(nodeState.getValue());
397 				   }
398 				}
399 			}
400 
401 			//((DocOperationIndexedParameter)(docForm.getRouteNodeInstanceOps().get(index))).setValue(KewApiConstants.NOOP);
402 		}
403 
404 		if(statesToBeDeleted!=null && statesToBeDeleted.size()>0){
405 			getRouteNodeService().deleteNodeStates(statesToBeDeleted);
406 		}
407 
408 
409 		List branches=(List)(request.getSession().getAttribute("branches"));
410 		String branchStateIds = (docForm.getBranchStatesDelete() != null) ? docForm.getBranchStatesDelete().trim() : null;
411 		List branchStatesToBeDeleted=new ArrayList();
412 		if(branchStateIds!=null && !branchStateIds.equals("")){
413 		    StringTokenizer idSets=new StringTokenizer(branchStateIds);
414 		    while (idSets.hasMoreTokens()) {
415 		    	String id=idSets.nextToken().trim();
416 		    	branchStatesToBeDeleted.add(Long.valueOf(id));
417 		    }
418 		}
419 
420 		for (Iterator branchesOpIter = docForm.getBranchOps().iterator(); branchesOpIter.hasNext();) {
421 			DocOperationIndexedParameter branchesOp = (DocOperationIndexedParameter) branchesOpIter.next();
422 			int index = branchesOp.getIndex().intValue();
423 			String opValue = branchesOp.getValue();
424             LOG.debug(opValue);
425 			Branch branch = (Branch)(branches.get(index));
426 			Branch branchNew = (Branch)(docForm.getBranche(index));
427 			if (!KewApiConstants.UPDATE.equals(opValue) && !KewApiConstants.NOOP.equals(opValue)) {
428 				throw new WorkflowServiceErrorException("Route Node Instance Operation not defined", new WorkflowServiceErrorImpl("Route Node Instance Operation not defined", "docoperation.routenodeinstance.operation.invalid"));
429 			}
430 			if (KewApiConstants.UPDATE.equals(opValue)) {
431 				//LOG.debug("saving routeNodeInstance:"+routeNodeInstance.getRouteNodeInstanceId());
432 				//getRouteNodeService().save(routeNodeInstance);
433 				branch.setName(branchNew.getName());
434 				List branchStates=branch.getBranchState();
435 				List branchStatesNew=branchNew.getBranchState();
436 				if(branchStates!=null){
437 				   for(int i=0;i<branchStates.size();i++){
438 					   BranchState branchState=(BranchState)branchStates.get(i);
439 					   BranchState branchStateNew=(BranchState)branchStatesNew.get(i);
440 					   if(branchStateNew.getKey()!=null && ! branchStateNew.getKey().trim().equals("")){
441 					   branchState.setKey(branchStateNew.getKey());
442 					   LOG.debug(branchState.getKey());
443 					   branchState.setValue(branchStateNew.getValue());
444 					   LOG.debug(branchState.getValue());
445 					   }
446 				   }
447 				}
448 				getBranchService().save(branch);
449 				LOG.debug("branch saved");
450 				change = true;
451 
452 			}
453 
454 
455 			if (KewApiConstants.NOOP.equals(opValue)){
456 				branchNew.setName(branch.getName());
457 				List branchStates=branch.getBranchState();
458 				List branchStatesNew=branchNew.getBranchState();
459 				if(branchStates!=null){
460 				   for(int i=0;i<branchStates.size();i++){
461 					   BranchState branchState=(BranchState)branchStates.get(i);
462 					   BranchState branchStateNew=(BranchState)branchStatesNew.get(i);
463 					   if(branchStateNew.getKey()==null || branchStateNew.getKey().trim().equals("")){
464 						   branchStatesToBeDeleted.remove(branchState.getBranchStateId());
465 					   }
466 					   branchStateNew.setKey(branchState.getKey());
467 					   LOG.debug(branchState.getKey());
468 					   branchStateNew.setValue(branchState.getValue());
469 					   LOG.debug(branchState.getValue());
470 				   }
471 				}
472 			}
473 			//((DocOperationIndexedParameter)(docForm.getBranchOps().get(index))).setValue(KewApiConstants.NOOP);
474 		}
475 
476 		if(branchStatesToBeDeleted!=null && branchStatesToBeDeleted.size()>0){
477 			getBranchService().deleteBranchStates(branchStatesToBeDeleted);
478 		}
479 
480 		WorkflowDocument workflowDocument = WorkflowDocumentFactory.loadDocument(GlobalVariables.getUserSession().getPrincipalId(), docForm.getDocumentId());
481 
482 		String annotation = docForm.getAnnotation();
483 		if (StringUtils.isEmpty(annotation)) {
484 			annotation = DEFAULT_LOG_MSG;
485 		}
486 		workflowDocument.logAnnotation(annotation);
487 
488 		ActionMessages messages = new ActionMessages();
489 		String forward = null;
490 		if (change) {
491 			messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("docoperation.operation.saved"));
492 			docForm.setRouteHeader(getRouteHeaderService().getRouteHeader(docForm.getRouteHeader().getDocumentId()));
493 			forward = "summary";
494 		} else {
495 			messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("docoperation.operation.noop"));
496 			forward = "basic";
497 		}
498 		saveMessages(request, messages);
499 		return mapping.findForward(forward);
500 
501 	}
502 
503 	private RouteHeaderService getRouteHeaderService() {
504 		return (RouteHeaderService) KEWServiceLocator.getService(KEWServiceLocator.DOC_ROUTE_HEADER_SRV);
505 	}
506 
507 	private RouteNodeService getRouteNodeService(){
508 		return (RouteNodeService) KEWServiceLocator.getService(KEWServiceLocator.ROUTE_NODE_SERVICE);
509 	}
510 
511 	private ActionRequestService getActionRequestService() {
512 		return (ActionRequestService) KEWServiceLocator.getService(KEWServiceLocator.ACTION_REQUEST_SRV);
513 	}
514 
515 	private ActionTakenService getActionTakenService() {
516 		return (ActionTakenService) KEWServiceLocator.getService(KEWServiceLocator.ACTION_TAKEN_SRV);
517 	}
518 
519 	private ActionListService getActionListService() {
520 		return (ActionListService) KEWServiceLocator.getActionListService();
521 	}
522 
523 	private void setRouteHeaderTimestamps(DocumentOperationForm docForm) {
524 		if (docForm.getCreateDate() == null || docForm.getCreateDate().trim().equals("")) {
525 			throw new WorkflowServiceErrorException("Document create date empty", new WorkflowServiceErrorImpl("Document create date empty", "docoperation.routeheader.createdate.empty"));
526 		} else {
527 			try {
528 				
529 //				String a_pat = "yyyy-MM-dd hh:mm:ss";
530 //				SimpleDateFormat fmt = new SimpleDateFormat(a_pat);
531 //				
532 //				System.out.println("**********************new*******************");
533 //				System.out.println(docForm.getCreateDate());
534 //				System.out.println("**********************old*******************");
535 //				System.out.println(docForm.getRouteHeader().getCreateDate());
536 //				System.out.println("********************************************");
537 //	
538 	//			docForm.getRouteHeader().setCreateDate(new Timestamp(fmt.parse(docForm.getCreateDate()).getTime()));
539 
540 				docForm.getRouteHeader().setCreateDate(new Timestamp(RiceConstants.getDefaultDateAndTimeFormat().parse(docForm.getCreateDate()).getTime()));
541 			} catch (ParseException pe) {
542 				throw new WorkflowServiceErrorException("RouteHeader create date parsing error", new WorkflowServiceErrorImpl("Date parsing error", "docoperation.routeheader.createdate.invalid"));
543 			}
544 		}
545 
546 		if (docForm.getDateModified() == null || docForm.getDateModified().trim().equals("")) {
547 			throw new WorkflowServiceErrorException("Document doc status mod date empty", new WorkflowServiceErrorImpl("Document doc status mod date empty", "docoperation.routeheader.statusmoddate.empty"));
548 		} else {
549 			try {
550 				docForm.getRouteHeader().setDateModified(new Timestamp(
551                         RiceConstants.getDefaultDateAndTimeFormat().parse(docForm.getDateModified()).getTime()));
552 			} catch (ParseException pe) {
553 				throw new WorkflowServiceErrorException("Document doc status date parsing error", new WorkflowServiceErrorImpl("Document doc status mod date parsing error", "docoperation.routeheader.statusmoddate.invalid"));
554 			}
555 		}
556 
557 		if (docForm.getApprovedDate() != null && !docForm.getApprovedDate().trim().equals("")) {
558 			try {
559 				docForm.getRouteHeader().setApprovedDate(new Timestamp(RiceConstants.getDefaultDateAndTimeFormat().parse(docForm.getApprovedDate()).getTime()));
560 			} catch (ParseException pe) {
561 				throw new WorkflowServiceErrorException("Document approved date parsing error", new WorkflowServiceErrorImpl("Document approved date parsing error", "docoperation.routeheader.approveddate.invalid"));
562 			}
563 
564 		}
565 
566 		if (docForm.getFinalizedDate() != null && !docForm.getFinalizedDate().trim().equals("")) {
567 			try {
568 				docForm.getRouteHeader().setFinalizedDate(new Timestamp(RiceConstants.getDefaultDateAndTimeFormat().parse(docForm.getFinalizedDate()).getTime()));
569 			} catch (ParseException pe) {
570 				throw new WorkflowServiceErrorException("Document finalized date parsing error", new WorkflowServiceErrorImpl("Document finalized date parsing error", "docoperation.routeheader.finalizeddate.invalid"));
571 			}
572 		}
573 
574 		if (docForm.getRouteStatusDate() != null && !docForm.getRouteStatusDate().trim().equals("")) {
575 			try {
576 				docForm.getRouteHeader().setRouteStatusDate(new Timestamp(RiceConstants.getDefaultDateAndTimeFormat().parse(docForm.getRouteStatusDate()).getTime()));
577 			} catch (ParseException pe) {
578 				throw new WorkflowServiceErrorException("Document route status date parsing error", new WorkflowServiceErrorImpl("Document route status date parsing error", "docoperation.routeheader.routestatusdate.invalid"));
579 			}
580 
581 		}
582 	}
583 
584 	private void setRouteHeaderTimestampsToString(DocumentOperationForm docForm) {
585 		try {
586 			docForm.setCreateDate(RiceConstants.getDefaultDateAndTimeFormat().format(
587                     docForm.getRouteHeader().getCreateDate()));
588 			docForm.setDateModified(RiceConstants.getDefaultDateAndTimeFormat().format(
589                     docForm.getRouteHeader().getDateLastModified()));
590 			docForm.setApprovedDate(RiceConstants.getDefaultDateAndTimeFormat().format(
591                     docForm.getRouteHeader().getApprovedDate()));
592 			docForm.setFinalizedDate(RiceConstants.getDefaultDateAndTimeFormat().format(
593                     docForm.getRouteHeader().getFinalizedDate()));
594 			docForm.setRouteStatusDate(RiceConstants.getDefaultDateAndTimeFormat().format(
595                     docForm.getRouteHeader().getRouteStatusDate()));
596 
597 		} catch (Exception e) {
598 			LOG.info("One or more of the dates in routeHeader may be null");
599 		}
600 	}
601 
602 	public ActionForward refresh(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
603 		DocumentOperationForm docForm = (DocumentOperationForm) form;
604 		String lookupInvocationModule = docForm.getLookupInvocationModule();
605 		docForm.getRouteHeader().setDocumentId(docForm.getDocumentId());
606 
607 		if (lookupInvocationModule != null && !lookupInvocationModule.trim().equals("")) {
608 			String lookupField = docForm.getLookupInvocationField();
609 			int lookupIndex = new Integer(docForm.getLookupInvocationIndex()).intValue();
610 			String networkId = request.getParameter("networkId");
611 			String principalId = KEWServiceLocator.getIdentityHelperService().getIdForPrincipalName(networkId);
612 
613 			if (lookupInvocationModule.equals("RouteHeader")) {
614 				DocumentRouteHeaderValue routeHeader = docForm.getRouteHeader();
615 				if ("initiatorWorkflowId".equals(lookupField)) {
616 					routeHeader.setInitiatorWorkflowId(principalId);
617 				}
618 				if ("documentTypeId".equals(lookupField)) {
619 					DocumentType docType = getDocumentTypeService().findByName(request.getParameter("docTypeFullName"));
620 					routeHeader.setDocumentTypeId(docType.getDocumentTypeId());
621 				}
622 			}
623 
624 			if (lookupInvocationModule.equals("ActionRequest")) {
625 				ActionRequestValue actionRequest = docForm.getRouteHeader().getDocActionRequest(lookupIndex);
626 				if ("routeMethodName".equals(lookupField)) {
627 //					actionRequest.setRouteMethodName(null);
628 					String id = request.getParameter("ruleTemplate.ruleTemplateId");
629 					if (id != null && !"".equals(id.trim())) {
630 						RuleTemplateBo ruleTemplate = KEWServiceLocator.getRuleTemplateService().findByRuleTemplateId(id);
631 //						if (ruleTemplate != null) {
632 //							actionRequest.setRouteMethodName(ruleTemplate.getName());
633 //						}
634 					}
635 				}
636 				if ("workflowId".equals(lookupField)) {
637 					actionRequest.setPrincipalId(principalId);
638 				}
639 				if ("workgroupId".equals(lookupField)) {
640 					if (request.getParameter("workgroupId") != null && !"".equals(request.getParameter("workgroupId").trim())) {
641 						actionRequest.setGroupId(request.getParameter("workgroupId"));
642 					} else {
643 						actionRequest.setGroupId(null);
644 					}
645 				}
646 				if ("roleName".equals(lookupField)) {
647 					actionRequest.setRoleName(request.getParameter("roleName"));
648 				}
649 			}
650 			if (lookupInvocationModule.equals("ActionTaken")) {
651 				ActionTakenValue actionTaken = docForm.getRouteHeader().getDocActionTaken(lookupIndex);
652 				if ("workflowId".equals(lookupField)) {
653 					Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(networkId);
654 					if (principal != null) {
655 						actionTaken.setPrincipalId(principal.getPrincipalId());
656 					} else {
657 						LOG.info("action taken user not found");
658 						actionTaken.setPrincipalId(null);
659 					}
660 				}
661 				if ("delegatorPrincipalId".equals(lookupField)) {
662 					Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(networkId);
663 					if (principal != null) {
664 						actionTaken.setDelegatorPrincipalId(principal.getPrincipalId());
665 					} else {
666 						LOG.info("action taken delegator user not found");
667 						actionTaken.setDelegatorPrincipalId(null);
668 					}
669 				}
670 				if ("delegatorGroupId".equals(lookupField)) {
671 					if (request.getParameter("workgroupId") != null && !"".equals(request.getParameter("workgroupId").trim())) {
672 						actionTaken.setDelegatorGroupId(request.getParameter("workgroupId"));
673 					} else {
674 						actionTaken.setDelegatorGroupId(null);
675 					}
676 				}
677 			}
678 
679 			if (lookupInvocationModule.equals("ActionItem")) {
680 				ActionItem actionItem = docForm.getRouteHeader().getDocActionItem(lookupIndex);
681 				if ("workflowId".equals(lookupField)) {
682 					Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(networkId);
683 					if (principal != null) {
684 						actionItem.setPrincipalId(principal.getPrincipalId());
685 					} else {
686 						LOG.info("action item user not found");
687 						actionItem.setPrincipalId(null);
688 					}
689 				}
690 
691 				if ("workgroupId".equals(lookupField)) {
692 					if (request.getParameter("workgroupId") != null && !"".equals(request.getParameter("workgroupId").trim())) {
693 						actionItem.setGroupId(request.getParameter("workgroupId"));
694 					} else {
695 						actionItem.setGroupId(null);
696 					}
697 				}
698 				if ("roleName".equals(lookupField)) {
699 					actionItem.setRoleName(request.getParameter("roleName"));
700 				}
701 				if ("delegatorPrincipalId".equals(lookupField)) {
702 					Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(networkId);
703 					if (principal != null) {
704 						actionItem.setDelegatorPrincipalId(principal.getPrincipalId());
705 					} else {
706 						LOG.info("action item delegator user not found");
707 						actionItem.setDelegatorPrincipalId(null);
708 					}
709 				}
710 				if ("delegatorGroupId".equals(lookupField)) {
711 					if (request.getParameter("workgroupId") != null && !"".equals(request.getParameter("workgroupId").trim())) {
712 						actionItem.setDelegatorGroupId(request.getParameter("workgroupId"));
713 					} else {
714 						actionItem.setDelegatorGroupId(null);
715 					}
716 				}
717 				if ("docName".equals(lookupField)) {
718 					DocumentType docType = getDocumentTypeService().findByName(request.getParameter("docTypeFullName"));
719 					actionItem.setDocName(docType.getName());
720 					actionItem.setDocLabel(docType.getLabel());
721 					actionItem.setDocHandlerURL(docType.getResolvedDocumentHandlerUrl());
722 				}
723 			}
724 		}
725 
726 		return mapping.findForward("basic");
727 	}
728 
729 	public ActionForward queueDocument(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
730 		try {
731 			DocumentOperationForm docForm = (DocumentOperationForm) form;
732             DocumentProcessingQueue documentProcessingQueue = MessageServiceNames.getDocumentProcessingQueue(docForm.getRouteHeader());
733 			documentProcessingQueue.process(docForm.getDocumentId());
734 			ActionMessages messages = new ActionMessages();
735 			messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("general.message", "Document was successfully queued"));
736 			saveMessages(request, messages);
737 			return mapping.findForward("basic");
738 		} catch (Exception e) {
739 			throw new WorkflowRuntimeException(e);
740 		}
741 	}
742 
743 	public ActionForward indexSearchableAttributes(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
744 		DocumentOperationForm docForm = (DocumentOperationForm) form;
745         DocumentAttributeIndexingQueue queue = KewApiServiceLocator.getDocumentAttributeIndexingQueue(docForm.getRouteHeader().getDocumentType().getApplicationId());
746         queue.indexDocument(docForm.getRouteHeader().getDocumentId());
747 		ActionMessages messages = new ActionMessages();
748 		messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("general.message", "Searchable Attribute Indexing was successfully scheduled"));
749 		saveMessages(request, messages);
750 		return mapping.findForward("basic");
751 	}
752 
753 	public ActionForward queueDocumentRefresh(ActionMapping mapping, ActionForm form, HttpServletRequest request,
754             HttpServletResponse response) throws IOException, ServletException {
755 		DocumentOperationForm docForm = (DocumentOperationForm) form;
756 		DocumentRefreshQueue docRequeue = MessageServiceNames.getDocumentRequeuerService(docForm.getRouteHeader().getDocumentType().getApplicationId(), docForm.getRouteHeader().getDocumentId(), 0);
757 		docRequeue.refreshDocument(docForm.getRouteHeader().getDocumentId());
758 		ActionMessages messages = new ActionMessages();
759 		messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("general.message", "Document Requeuer was successfully scheduled"));
760 		saveMessages(request, messages);
761 		return mapping.findForward("basic");
762 	}
763 
764 	public ActionForward blanketApproveDocument(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
765 		try {
766 			DocumentOperationForm docForm = (DocumentOperationForm) form;
767 			String principalId = KimApiServiceLocator.getPersonService().getPersonByPrincipalName(docForm.getBlanketApproveUser()).getPrincipalId();
768 			Set<String> nodeNames = new HashSet<String>();
769 			if (!StringUtils.isBlank(docForm.getBlanketApproveNodes())) {
770 				String[] nodeNameArray = docForm.getBlanketApproveNodes().split(",");
771 				for (String nodeName : nodeNameArray) {
772 					nodeNames.add(nodeName.trim());
773 				}
774 			}
775 			DocumentOrchestrationQueue blanketApprove = MessageServiceNames.getDocumentOrchestrationQueue(
776                     docForm.getRouteHeader());
777             OrchestrationConfig orchestrationConfig = OrchestrationConfig.create(docForm.getBlanketApproveActionTakenId(), nodeNames);
778             DocumentProcessingOptions options = DocumentProcessingOptions.createDefault();
779 			blanketApprove.orchestrateDocument(docForm.getRouteHeader().getDocumentId(), principalId,
780                     orchestrationConfig, options);
781 			ActionMessages messages = new ActionMessages();
782 			messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("general.message", "Blanket Approve Processor was successfully scheduled"));
783 			saveMessages(request, messages);
784 			return mapping.findForward("basic");
785 		} catch (Exception e) {
786 			throw new WorkflowRuntimeException(e);
787 		}
788 	}
789 	
790 	public ActionForward moveDocument(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
791 		try {
792 			DocumentOperationForm docForm = (DocumentOperationForm) form;
793 			String principalId = KEWServiceLocator.getIdentityHelperService().getIdForPrincipalName(docForm.getBlanketApproveUser());
794 			Set<String> nodeNames = new HashSet<String>();
795 			if (!StringUtils.isBlank(docForm.getBlanketApproveNodes())) {
796 				String[] nodeNameArray = docForm.getBlanketApproveNodes().split(",");
797 				for (String nodeName : nodeNameArray) {
798 					nodeNames.add(nodeName.trim());
799 				}
800 			}
801             DocumentOrchestrationQueue orchestrationQueue = MessageServiceNames.getDocumentOrchestrationQueue(
802                     docForm.getRouteHeader());
803             OrchestrationConfig orchestrationConfig = OrchestrationConfig.create(docForm.getBlanketApproveActionTakenId(), nodeNames);
804             DocumentProcessingOptions options = DocumentProcessingOptions.create(true, true, false);
805             orchestrationQueue.orchestrateDocument(docForm.getDocumentId(), principalId, orchestrationConfig, options);
806 
807             ActionMessages messages = new ActionMessages();
808 			messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("general.message", "Move Document Processor was successfully scheduled"));
809 			saveMessages(request, messages);
810 			return mapping.findForward("basic");
811 		} catch (Exception e) {
812 			throw new WorkflowRuntimeException(e);
813 		}
814 	}
815 
816 	public ActionForward queueActionInvocation(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
817 		try {
818 			DocumentOperationForm docForm = (DocumentOperationForm) form;
819 			String principalId = KEWServiceLocator.getIdentityHelperService().getIdForPrincipalName(docForm.getActionInvocationUser());
820 			ActionInvocation invocation = ActionInvocation.create(ActionType.fromCode(
821                     docForm.getActionInvocationActionCode()), docForm.getActionInvocationActionItemId());
822 			ActionInvocationQueue actionInvocationQueue = MessageServiceNames.getActionInvocationProcessorService(docForm.getRouteHeader());
823 			actionInvocationQueue.invokeAction(principalId, docForm.getRouteHeader().getDocumentId(), invocation);
824 			ActionMessages messages = new ActionMessages();
825 			messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("general.message", "Action Invocation Processor was successfully scheduled"));
826 			saveMessages(request, messages);
827 			return mapping.findForward("basic");
828 		} catch (Exception e) {
829 			throw new WorkflowRuntimeException(e);
830 		}
831 	}
832 
833 	private DocumentTypeService getDocumentTypeService() {
834 		return (DocumentTypeService) KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE);
835 	}
836 
837 	private BranchService getBranchService(){
838 		return (BranchService) KEWServiceLocator.getService(KEWServiceLocator.BRANCH_SERVICE);
839 	}
840 }