View Javadoc
1   package org.kuali.ole.sys.context;
2   
3   import org.kuali.ole.OLEConstants;
4   import org.kuali.ole.sys.batch.AbstractStep;
5   import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
6   import org.kuali.rice.coreservice.api.CoreServiceApiServiceLocator;
7   import org.kuali.rice.coreservice.api.parameter.Parameter;
8   import org.kuali.rice.coreservice.api.parameter.ParameterKey;
9   import org.kuali.rice.coreservice.framework.parameter.ParameterService;
10  import org.kuali.rice.kew.api.KewApiServiceLocator;
11  import org.kuali.rice.kew.api.action.ActionItem;
12  import org.kuali.rice.kew.api.action.DocumentActionParameters;
13  import org.kuali.rice.kew.api.action.InvalidActionTakenException;
14  import org.kuali.rice.kew.api.action.WorkflowDocumentActionsService;
15  import org.kuali.rice.kew.api.actionlist.ActionListService;
16  import org.kuali.rice.kew.api.document.InvalidDocumentContentException;
17  
18  import java.util.ArrayList;
19  import java.util.Date;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  /**
24   * Created by pvsubrah on 10/28/14.
25   */
26  public class ActionListBatchProcessor extends AbstractStep {
27      private WorkflowDocumentActionsService workflowDocumentActionsService;
28      private ActionListService actionListService;
29      private ParameterService parameterService;
30  
31      @Override
32      public boolean execute(String jobName, Date jobRunDate) throws InterruptedException {
33          ParameterKey principalIdParameterKey = ParameterKey.create(OLEConstants.APPL_ID_OLE, OLEConstants.SYS_NMSPC, OLEConstants.BATCH_CMPNT, "ACTN_LIST_BATCH_PRNPL_NM");
34          Parameter principalId = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(principalIdParameterKey);
35          String principal = principalId.getValue();
36  
37  
38          ParameterKey requestCodeParameterKey = ParameterKey.create(OLEConstants.APPL_ID_OLE, OLEConstants.SYS_NMSPC, OLEConstants.BATCH_CMPNT,"ACTN_LIST_BATCH_RQST_CD");
39          Parameter requestCode = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(requestCodeParameterKey);
40          String request = requestCode.getValue();
41  
42          ParameterKey docTypeParameterKey = ParameterKey.create(OLEConstants.APPL_ID_OLE, OLEConstants.SYS_NMSPC, OLEConstants.BATCH_CMPNT,"ACTN_LIST_BATCH_DOC_TYP_CD");
43          Parameter docTypeCode = CoreServiceApiServiceLocator.getParameterRepositoryService().getParameter(docTypeParameterKey);
44  
45          List<ActionItem> actionItems = getActionListService().getActionItemsForPrincipal(principal);
46          List<String> docIds = new ArrayList<String>();
47          List<ActionItem> filteredActionItems = new ArrayList<ActionItem>();
48  
49          if(docTypeCode.getValue().equals("*")){
50              filteredActionItems = actionItems;
51          } else {
52              for (Iterator<ActionItem> iterator = actionItems.iterator(); iterator.hasNext(); ) {
53                  ActionItem actionItem = iterator.next();
54                  if(docTypeCode.getValue().equals(actionItem.getDocName())){
55                      filteredActionItems.add(actionItem);
56                  }
57              }
58          }
59  
60          for (Iterator<ActionItem> iterator = filteredActionItems.iterator(); iterator.hasNext(); ) {
61              ActionItem actionItem = iterator.next();
62              if(actionItem.getActionRequestCd().equals(requestCode.getValue())){
63                  docIds.add(actionItem.getDocumentId());
64              }
65          }
66  
67          switch (request){
68              case "A":
69                  approve(docIds, principal);
70                  break;
71              case "K":
72                  acknowledge(docIds, principal);
73                  break;
74              case "F":
75                  fyi(docIds, principal);
76                  break;
77              case "C":
78                  complete(docIds, principal);
79                  break;
80          }
81          return true;
82      }
83  
84      private void complete(List<String> docIds, String principal) {
85          for (Iterator<String> iterator = docIds.iterator(); iterator.hasNext(); ) {
86              String docId = iterator.next();
87              try {
88                  getWorkflowDocumentActionsService().complete(DocumentActionParameters.create(docId, principal));
89              } catch (RiceIllegalArgumentException | InvalidDocumentContentException | InvalidActionTakenException e) {
90                  System.out.println(docId);
91                  e.printStackTrace();
92              }
93          }
94      }
95  
96      private void fyi(List<String> docIds, String principal) {
97          for (Iterator<String> iterator = docIds.iterator(); iterator.hasNext(); ) {
98              String docId = iterator.next();
99              try {
100                 getWorkflowDocumentActionsService().clearFyi(DocumentActionParameters.create(docId, principal));
101             } catch (RiceIllegalArgumentException | InvalidDocumentContentException | InvalidActionTakenException e) {
102                 System.out.println(docId);
103                 e.printStackTrace();
104             }
105         }
106     }
107 
108     private void acknowledge(List<String> docIds, String principal) {
109         for (Iterator<String> iterator = docIds.iterator(); iterator.hasNext(); ) {
110             String docId = iterator.next();
111             try {
112                 getWorkflowDocumentActionsService().acknowledge(DocumentActionParameters.create(docId, principal));
113             } catch (RiceIllegalArgumentException | InvalidDocumentContentException | InvalidActionTakenException e) {
114                 System.out.println(docId);
115                 e.printStackTrace();
116             }
117         }
118     }
119 
120     private void approve(List<String> docIds, String principal) {
121         for (Iterator<String> iterator = docIds.iterator(); iterator.hasNext(); ) {
122             String docId = iterator.next();
123             try {
124                 getWorkflowDocumentActionsService().approve(DocumentActionParameters.create(docId, principal));
125             } catch (RiceIllegalArgumentException | InvalidDocumentContentException | InvalidActionTakenException e) {
126                 e.printStackTrace();
127             }
128         }
129     }
130 
131     private WorkflowDocumentActionsService getWorkflowDocumentActionsService() {
132         if (null == workflowDocumentActionsService) {
133             workflowDocumentActionsService = KewApiServiceLocator.getWorkflowDocumentActionsService();
134         }
135         return workflowDocumentActionsService;
136     }
137 
138     public org.kuali.rice.kew.api.actionlist.ActionListService getActionListService() {
139         if (null == actionListService) {
140             actionListService = KewApiServiceLocator.getActionListService();
141         }
142         return actionListService;
143     }
144 
145     @Override
146     public ParameterService getParameterService() {
147         if(null == parameterService){
148             parameterService = (ParameterService) SpringContext.getService("parameterService");
149         }
150         return parameterService;
151     }
152 }