View Javadoc
1   package org.kuali.ole.alert.service.impl;
2   
3   import org.apache.commons.collections.CollectionUtils;
4   import org.apache.commons.lang.StringUtils;
5   import org.apache.commons.lang.time.DateUtils;
6   import org.apache.log4j.Logger;
7   import org.joda.time.DateTime;
8   import org.kuali.ole.alert.bo.ActionListAlertBo;
9   import org.kuali.ole.alert.bo.AlertBo;
10  import org.kuali.ole.alert.bo.AlertDocumentType;
11  import org.kuali.ole.alert.service.AlertGlobalConfigurationServiceImpl;
12  import org.kuali.ole.alert.service.AlertHelperService;
13  import org.kuali.ole.deliver.calendar.service.DateUtil;
14  import org.kuali.rice.kew.api.KewApiServiceLocator;
15  import org.kuali.rice.kew.api.action.ActionItem;
16  import org.kuali.rice.kew.identity.service.impl.IdentityHelperServiceImpl;
17  import org.kuali.rice.kim.api.identity.principal.Principal;
18  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
19  import org.kuali.rice.kim.impl.identity.principal.PrincipalBo;
20  import org.kuali.rice.krad.service.*;
21  import org.kuali.rice.krad.util.GlobalVariables;
22  
23  import java.sql.Date;
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  /**
30   * Created by maheswarang on 11/4/14.
31   */
32  public class AlertHelperServiceImpl implements AlertHelperService {
33      private static final Logger LOG = Logger.getLogger(AlertHelperServiceImpl.class);
34      private BusinessObjectService businessObjectService = KRADServiceLocator.getBusinessObjectService();
35      private AlertGlobalConfigurationServiceImpl alertGlobalConfigurationService = new AlertGlobalConfigurationServiceImpl();
36      private AlertServiceImpl alertService = new AlertServiceImpl();
37  
38  
39      /**
40       * This method is used to get the the list of alerts for the current day to the given user id
41       * @param userId
42       * @return
43       */
44      @Override
45      public List<ActionListAlertBo> getActionListAlertsByUserId(String userId) {
46          LOG.info("Inside the getActionListAlertsByUserId method for the user Id : " + userId);
47          List<ActionListAlertBo> actionListAlertBos = new ArrayList<>();
48          try{
49              Map<String,Object> actionMap = new HashMap();
50              actionMap.put("alertUserId",userId);
51              actionMap.put("active",Boolean.TRUE);
52              List<ActionListAlertBo> actionListAlertBosFromDb = (List<ActionListAlertBo>)businessObjectService.findMatchingOrderBy(ActionListAlertBo.class,actionMap,"alertDate",false);
53              if(actionListAlertBosFromDb!=null && actionListAlertBosFromDb.size()>0){
54                  for(ActionListAlertBo actionListAlertBo : actionListAlertBosFromDb){
55                      if(actionListAlertBo.getAlertApproverId()==null && actionListAlertBo.getAlertDate().compareTo(new Date(System.currentTimeMillis()))<=0){
56                          actionListAlertBo.setAlertInitiatorName(getName(actionListAlertBo.getAlertInitiatorId()));
57                          actionListAlertBos.add(actionListAlertBo);
58                      }
59                  }
60              }
61          }catch(Exception e){
62              LOG.info("Exception occured while getting the alert information to the user : " + userId);
63              LOG.error(e,e);
64          }
65          return actionListAlertBos;
66      }
67  
68      /**
69       * This method is used to approve the selected alert and also updates the corresponding alert
70       * @param actionListAlertBo
71       */
72      @Override
73      public void approveActionListAlert(ActionListAlertBo actionListAlertBo){
74          LOG.info("Inside approveActionListAlert for the alert with alert id : " + actionListAlertBo.getAlertId());
75          AlertBo alertBo = new AlertBo();
76          List<AlertBo> alertBos = new ArrayList<>();
77          List<ActionListAlertBo> actionListAlertBos = new ArrayList<>();
78          Map<String,String> alertMap = new HashMap<>();
79          alertMap.put("documentId", actionListAlertBo.getDocumentId());
80          alertBos = (List<AlertBo>)businessObjectService.findMatching(AlertBo.class, alertMap);
81          actionListAlertBos = (List<ActionListAlertBo>)businessObjectService.findMatching(ActionListAlertBo.class, alertMap);
82          if(CollectionUtils.isNotEmpty(alertBos)) {
83              for(AlertBo alertBo1 : alertBos) {
84                  if(actionListAlertBo.getAlertId().equals(alertBo1.getAlertId())) {
85                      alertBo = alertBo1;
86                      break;
87                  }
88              }
89          }
90          if(StringUtils.isNotEmpty(alertBo.getReceivingRoleId())) {
91              approveAlertBaseOnRole(alertBos, alertBo, actionListAlertBos);
92          } else if(StringUtils.isNotEmpty(alertBo.getReceivingGroupId())) {
93              approveAlertBaseOnGroup(alertBos, alertBo, actionListAlertBos);
94          } else {
95              alertBo.setAlertStatus(false);
96              alertBo.setAlertApproverId(GlobalVariables.getUserSession().getPrincipalId());
97              alertBo.setAlertApprovedDate(new Date(System.currentTimeMillis()));
98              businessObjectService.save(alertBo);
99              AlertBo alertBoRepeatable = null;
100             if(alertBo.isRepeatable()) {
101                 alertBoRepeatable = createNewAlertBo(alertBo);
102                 businessObjectService.save(alertBoRepeatable);
103             }
104             saveAlert(actionListAlertBo,alertBoRepeatable);
105         }
106     }
107 
108     public void approveAlertBaseOnRole(List<AlertBo> alertBos, AlertBo alertBo, List<ActionListAlertBo> actionListAlertBos) {
109         for(AlertBo alertBo1 : alertBos) {
110             if(alertBo.getReceivingRoleId().equals(alertBo1.getReceivingRoleId())) {
111                 if(alertBo.getAlertCreateDate() != null && alertBo.getAlertCreateDate().equals(alertBo1.getAlertCreateDate())) {
112                     alertBo1.setAlertStatus(false);
113                     alertBo1.setAlertApproverId(GlobalVariables.getUserSession().getPrincipalId());
114                     alertBo1.setAlertApprovedDate(new Date(System.currentTimeMillis()));
115                     businessObjectService.save(alertBo1);
116                     AlertBo alertBoRepeatable = null;
117                     if(alertBo1.isRepeatable()) {
118                         alertBoRepeatable = createNewAlertBo(alertBo1);
119                         businessObjectService.save(alertBoRepeatable);
120                     }
121                     for(ActionListAlertBo actionListAlertBo : actionListAlertBos) {
122                         if(StringUtils.isNotEmpty(actionListAlertBo.getAlertId()) && actionListAlertBo.getAlertId().equals(alertBo1.getAlertId())) {
123                             saveAlert(actionListAlertBo,alertBoRepeatable);
124                         }
125                     }
126                 }
127             }
128         }
129     }
130 
131     public void approveAlertBaseOnGroup(List<AlertBo> alertBos, AlertBo alertBo, List<ActionListAlertBo> actionListAlertBos) {
132         for(AlertBo alertBo1 : alertBos) {
133             if(alertBo.getReceivingGroupId().equals(alertBo1.getReceivingGroupId())) {
134                 if(alertBo.getAlertCreateDate().equals(alertBo1.getAlertCreateDate())) {
135                     alertBo1.setAlertStatus(false);
136                     alertBo1.setAlertApproverId(GlobalVariables.getUserSession().getPrincipalId());
137                     alertBo1.setAlertApprovedDate(new Date(System.currentTimeMillis()));
138                     businessObjectService.save(alertBo1);
139                     AlertBo alertBoRepeatable = null;
140                     if(alertBo1.isRepeatable()) {
141                         alertBoRepeatable = createNewAlertBo(alertBo1);
142                         businessObjectService.save(alertBoRepeatable);
143                     }
144                     for(ActionListAlertBo actionListAlertBo : actionListAlertBos) {
145                         if(StringUtils.isNotEmpty(actionListAlertBo.getAlertId()) && actionListAlertBo.getAlertId().equals(alertBo1.getAlertId())) {
146                             saveAlert(actionListAlertBo,alertBoRepeatable);
147                         }
148                     }
149                 }
150             }
151         }
152     }
153 
154     private void saveAlert(ActionListAlertBo actionListAlertBo, AlertBo alertBo) {
155         actionListAlertBo.setActive(false);
156         actionListAlertBo.setAlertApprovedDate(new Date(System.currentTimeMillis()));
157         actionListAlertBo.setAlertApproverId(GlobalVariables.getUserSession().getPrincipalId());
158         businessObjectService.save(actionListAlertBo);
159         if(alertBo != null) {
160             ActionListAlertBo actionListAlertBo1 = getActionListAlertBo(alertBo,actionListAlertBo.getRecordType(),actionListAlertBo.getTitle(),actionListAlertBo.getAlertUserId());
161             businessObjectService.save(actionListAlertBo1);
162         }
163     }
164 
165 
166     /**
167      * This method is used to retrieve alert based on the given alert id
168      * @param alertId
169      * @return
170      */
171     public AlertBo getAlertBo(String alertId){
172         AlertBo alertBo = null;
173         List<AlertBo> alertBos = new ArrayList<>();
174         try{
175             Map<String,Object> alertMap = new HashMap<String,Object>();
176 /*        alertMap.put("documentId",actionListAlertBo.getDocumentId());
177         alertMap.put("receivingUserId",actionListAlertBo.getAlertUserId());
178         alertMap.put("alertDate",actionListAlertBo.getAlertDate());*/
179             alertMap.put("alertId",alertId);
180             alertBos = (List<AlertBo>)businessObjectService.findMatching(AlertBo.class,alertMap);
181         }catch(Exception e){
182             LOG.info("Exception occured while getting the alert information for an alert with alert Id : " + alertId);
183         }
184         if(alertBos!=null && alertBos.size()>0){
185             alertBo = alertBos.get(0);
186         }
187         return alertBo;
188     }
189 
190 
191     /**
192      * This method is used to get the principal name for the given principal id
193      * @param principalId
194      * @return
195      */
196     public String getName(String principalId){
197         Principal principal = KimApiServiceLocator.getIdentityService().getPrincipal(principalId);
198         return   principal.getPrincipalName();
199     }
200 
201 
202 
203     public AlertBo createNewAlertBo(AlertBo alertBo){
204         AlertBo alertBo1 = new AlertBo();
205         alertBo1.setDocumentId(alertBo.getDocumentId());
206         alertBo1.setAlertCreateDate(alertBo.getAlertCreateDate());
207         alertBo1.setAlertInitiatorId(alertBo.getAlertInitiatorId());
208         alertBo1.setAlertNote(alertBo.getAlertNote());
209         alertBo1.setReceivingUserId(alertBo.getReceivingUserId());
210         alertBo1.setAlertStatus(true);
211         alertBo1.setReceivingGroupId(alertBo.getReceivingGroupId());
212         alertBo1.setAlertInterval(alertBo.getAlertInterval());
213         alertBo1.setReceivingRoleId(alertBo.getReceivingRoleId());
214         alertBo1.setRepeatable(alertBo.isRepeatable());
215         if(alertBo.getAlertInterval()==null || (alertBo.getAlertInterval()!=null && !alertBo.getAlertInterval().trim().isEmpty())){
216             java.util.Date alertDate = DateUtils.addDays(new java.util.Date(alertBo.getAlertDate().getTime()), Integer.parseInt(alertBo.getAlertInterval()));
217             alertBo1.setAlertDate(new Date(alertDate.getTime()));
218         }else{
219             alertBo1.setAlertDate(alertBo.getAlertDate());
220         }
221 
222 
223         return alertBo1;
224     }
225 
226 
227     public ActionListAlertBo getActionListAlertBo(AlertBo alertBo,String documentTypeName,String title,String alertUserId){
228         ActionListAlertBo actionListAlertBo = new ActionListAlertBo();
229         actionListAlertBo.setDocumentId(alertBo.getDocumentId());
230         actionListAlertBo.setActive(alertBo.isAlertStatus());
231         actionListAlertBo.setAlertDate(alertBo.getAlertDate());
232         actionListAlertBo.setRecordType(documentTypeName);
233         actionListAlertBo.setTitle(title);
234         actionListAlertBo.setNote(alertBo.getAlertNote());
235         actionListAlertBo.setAlertUserId(alertUserId);
236         actionListAlertBo.setAlertInitiatorId(alertBo.getAlertInitiatorId());
237         actionListAlertBo.setAlertApproverId(alertBo.getAlertApproverId());
238         actionListAlertBo.setAlertApprovedDate(alertBo.getAlertApprovedDate());
239         actionListAlertBo.setAlertId(alertBo.getAlertId());
240         return actionListAlertBo;
241     }
242 
243 
244 
245     public void processApprovalPendingDocuments(String principalId){
246         List<ActionItem> actionItems = KewApiServiceLocator.getActionListService().getActionItemsForPrincipal(principalId);
247         String reminderInterval = null;
248         AlertBo alertBo = null;
249         ActionListAlertBo actionListAlertBo = null;
250         if(actionItems!=null && actionItems.size()>0){
251             for(ActionItem actionItem : actionItems){
252                 List<AlertBo> alertBos = getAlertBoBasedOnDocumentNumber(actionItem.getDocumentId(),false,principalId);
253               if(alertBos == null || (alertBos!=null && alertBos.size()==0)){
254                reminderInterval =  getReminderInterval(actionItem.getDocName());
255                 if(reminderInterval!=null){
256                      if(isApprovalDateBarred(actionItem.getDateTimeAssigned(),reminderInterval)){
257                     alertBo = alertGlobalConfigurationService.getAlert(actionItem.getDocumentId(),null,false,principalId,"note",principalId,principalId,principalId,null,null,null,null);
258                        if(alertBo!=null){
259                         businessObjectService.save(alertBo);
260                         actionListAlertBo =  alertService.getActionListAlertBo(alertBo,actionItem.getDocName(),actionItem.getDocTitle(),principalId);
261                            businessObjectService.save(actionListAlertBo);
262                      }
263                      }
264                 }
265 
266             }
267            }
268         }
269     }
270 
271     public String getReminderInterval(String docTypeName){
272         String interval= null;
273         List<AlertDocumentType> alertDocumentTypes = null;
274         Map<String,String> alertDocumentMap = new HashMap<String,String>();
275         alertDocumentMap.put("alertDocumentTypeName",docTypeName);
276         alertDocumentTypes = (List<AlertDocumentType>)KRADServiceLocator.getBusinessObjectService().findMatching(AlertDocumentType.class,alertDocumentMap);
277         if(alertDocumentTypes!=null && alertDocumentTypes.size()>0){
278             interval = alertDocumentTypes.get(0).getAlertReminderInterval();
279         }
280         return interval;
281     }
282 
283    public boolean isApprovalDateBarred(DateTime dateTime,String reminderInterval){
284        boolean expired = false;
285        DateTime expiryDate = dateTime.plusDays(Integer.parseInt(reminderInterval));
286        if(expiryDate.isEqualNow() || expiryDate.isBeforeNow()){
287           expired = true;
288        }
289         return expired;
290     }
291 
292 public List<AlertBo> getAlertBoBasedOnDocumentNumber (String documentNumber,boolean active,String receivingUserId){
293     Map<String,String> alertMap = new HashMap<String,String>();
294     alertMap.put("documentId",documentNumber);
295     alertMap.put("receivingUserId",receivingUserId);
296     if(active){
297         alertMap.put("alertStatus","true");
298     }
299     alertMap.put("alertDate",new java.sql.Date(System.currentTimeMillis()).toString());
300     List<AlertBo> alertBoList = (List<AlertBo>)KRADServiceLocator.getBusinessObjectService().findMatching(AlertBo.class,alertMap);
301 
302     return alertBoList;
303 
304 
305 }
306 
307 
308 
309 }