View Javadoc
1   package org.kuali.ole.alert.service;
2   
3   import org.kuali.ole.alert.bo.AlertConditionAndReceiverInformation;
4   import org.kuali.ole.alert.bo.AlertDocument;
5   import org.kuali.ole.alert.bo.AlertBo;
6   import org.kuali.ole.alert.bo.AlertEvent;
7   import org.kuali.ole.alert.bo.AlertEventField;
8   import org.kuali.ole.alert.document.OlePersistableBusinessObjectBase;
9   import org.kuali.ole.alert.document.OleTransactionalDocumentBase;
10  import org.kuali.ole.alert.service.impl.AlertServiceImpl;
11  import org.kuali.rice.core.api.util.type.KualiDecimal;
12  import org.kuali.rice.kim.api.group.GroupService;
13  import org.kuali.rice.kim.api.role.Role;
14  import org.kuali.rice.kim.api.role.RoleService;
15  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
16  import org.kuali.rice.krad.document.Document;
17  import org.kuali.rice.krad.maintenance.MaintenanceDocumentBase;
18  import org.kuali.rice.krad.service.BusinessObjectService;
19  import org.kuali.rice.krad.service.KRADServiceLocator;
20  import org.kuali.rice.krad.util.GlobalVariables;
21  
22  import java.lang.reflect.Field;
23  import java.math.BigDecimal;
24  import java.sql.*;
25  import java.util.*;
26  import java.util.Date;
27  
28  /**
29   * Created by maheswarang on 12/26/14.
30   */
31  public class AlertGlobalConfigurationServiceImpl {
32      private BusinessObjectService businessObjectService;
33      private GroupService groupService = KimApiServiceLocator.getGroupService();
34      private RoleService roleService = KimApiServiceLocator.getRoleService();
35      private AlertServiceImpl alertService = new AlertServiceImpl();
36      public BusinessObjectService getBusinessObjectService() {
37          if(businessObjectService == null){
38              businessObjectService = KRADServiceLocator.getBusinessObjectService();
39          }
40          return businessObjectService;
41      }
42  
43      public void setBusinessObjectService(BusinessObjectService businessObjectService) {
44          this.businessObjectService = businessObjectService;
45      }
46  
47  
48  
49      public void processAlert(Document document){
50  
51          List<AlertBo> alertBos = new ArrayList<>();
52          if(document instanceof OleTransactionalDocumentBase){
53              OleTransactionalDocumentBase oleTransactionalDocumentBase = (OleTransactionalDocumentBase)document;
54              alertBos =   processDocument(oleTransactionalDocumentBase,oleTransactionalDocumentBase.getDocumentHeader().getWorkflowDocument().getDocumentTypeName(),oleTransactionalDocumentBase.getDocumentNumber());
55             if(alertBos!=null && alertBos.size()>0){
56                 for(AlertBo alert : alertBos){
57                     alertService.updateActionList(alert,oleTransactionalDocumentBase);
58                 }
59             }
60  
61          }
62          if(document instanceof MaintenanceDocumentBase){
63              MaintenanceDocumentBase maintenanceDocumentBase = (MaintenanceDocumentBase) document;
64              if(maintenanceDocumentBase.getDocumentDataObject() instanceof OlePersistableBusinessObjectBase){
65                  OlePersistableBusinessObjectBase olePersistableBusinessObjectBase = (OlePersistableBusinessObjectBase)maintenanceDocumentBase.getDocumentDataObject();
66                  alertBos = processDocument(olePersistableBusinessObjectBase,maintenanceDocumentBase.getDocumentHeader().getWorkflowDocument().getDocumentTypeName(),maintenanceDocumentBase.getDocumentNumber());
67                  if(alertBos!=null && alertBos.size()>0){
68                      for(AlertBo alert : alertBos){
69                          olePersistableBusinessObjectBase.getAlertBoList().add(alert);
70                          alertService.updateActionList(alert,maintenanceDocumentBase);
71                      }
72                  }
73              }
74      }
75  
76  
77      }
78  
79  
80      public List<AlertBo> processDocument(Object olePersistableBusinessObjectBase,String documentTypeName,String documentNumber){
81          AlertDocument alertDocument = getAlertDocument(documentTypeName);
82          List<AlertBo> alertBoList = new ArrayList<AlertBo>();
83          AlertBo alertBo = null;
84          if(alertDocument!=null && alertDocument.getAlertConditionAndReceiverInformations()!=null && alertDocument.getAlertConditionAndReceiverInformations().size()>0){
85              for(AlertConditionAndReceiverInformation alertConditionAndReceiverInformation : alertDocument.getAlertConditionAndReceiverInformations()){
86                  if(processEvent(alertConditionAndReceiverInformation.getAlertEvent(),olePersistableBusinessObjectBase)){
87                    alertBoList = generateAlertList(alertConditionAndReceiverInformation,documentNumber,alertDocument.isRepeatable());
88                  }
89              }
90          }
91          return alertBoList;
92      }
93  
94  
95  
96  
97  
98  
99      public AlertDocument getAlertDocument(String documentTypeName){
100         Map<String,String> alertDocumentMap = new HashMap<String,String>();
101         AlertDocument alertDocument= null;
102         alertDocumentMap.put("documentTypeName",documentTypeName);
103         List<AlertDocument> alertDocumentList = (List<AlertDocument>)getBusinessObjectService().findMatching(AlertDocument.class,alertDocumentMap);
104         if(alertDocumentList!=null && alertDocumentList.size()>0){
105             alertDocument = alertDocumentList.get(0);
106         }
107 
108         return alertDocument;
109     }
110 
111 
112     public boolean processEvent(AlertEvent alertEvent,Object olePersistableBusinessObjectBase){
113        boolean processed = true;
114         if(alertEvent.getAlertEventFieldList()!=null && alertEvent.getAlertEventFieldList().size()>0){
115             for(AlertEventField alertEventField  :  alertEvent.getAlertEventFieldList()){
116                  if(!processEventField(alertEventField,olePersistableBusinessObjectBase)){
117                      processed = false;
118                  break;
119                  }
120             }
121 
122         }
123 
124             return processed;
125     }
126 
127 
128 
129 
130 public boolean processEventField(AlertEventField alertEventField , Object olePersistableBusinessObjectBase){
131     boolean processed = true;
132 
133     if(alertEventField.getAlertFieldType().equals("boolean")){
134         processed = validateBooleanField(alertEventField.getAlertFieldName(), alertEventField.getAlertFieldValue(), alertEventField.getAlertCriteria(), olePersistableBusinessObjectBase);
135     }
136    else if(alertEventField.getAlertFieldType().equals("String")){
137        processed =validateStringField(alertEventField.getAlertFieldName(),alertEventField.getAlertFieldValue(),alertEventField.getAlertCriteria(),olePersistableBusinessObjectBase);
138     }
139     else if(alertEventField.getAlertFieldType().equals("Integer")){
140         processed =validateIntegerField(alertEventField.getAlertFieldName(),alertEventField.getAlertFieldValue(),alertEventField.getAlertCriteria(),olePersistableBusinessObjectBase);
141     }
142     else if(alertEventField.getAlertFieldType().equals("BigDecimal")){
143         processed =validateBigDecimalField(alertEventField.getAlertFieldName(),alertEventField.getAlertFieldValue(),alertEventField.getAlertCriteria(),olePersistableBusinessObjectBase);
144     }
145 
146     else if(alertEventField.getAlertFieldType().equals("KualiDecimal")){
147         processed =validateKualiDecimalField(alertEventField.getAlertFieldName(),alertEventField.getAlertFieldValue(),alertEventField.getAlertCriteria(),olePersistableBusinessObjectBase);
148     }
149 
150     else if(alertEventField.getAlertFieldType().equals("Timestamp")){
151         processed =validateTimeStampField(alertEventField.getAlertFieldName(),alertEventField.getAlertFieldValue(),alertEventField.getAlertCriteria(),olePersistableBusinessObjectBase);
152     }
153     else if(alertEventField.getAlertFieldType().equals("util.Date")){
154         processed =validateUtilDateField(alertEventField.getAlertFieldName(),alertEventField.getAlertFieldValue(),alertEventField.getAlertCriteria(),olePersistableBusinessObjectBase);
155     }
156     else if(alertEventField.getAlertFieldType().equals("sql.Date")){
157         processed =validateSqlDateField(alertEventField.getAlertFieldName(),alertEventField.getAlertFieldValue(),alertEventField.getAlertCriteria(),olePersistableBusinessObjectBase);
158     }
159 
160     return processed;
161 }
162 
163 
164 
165 
166 
167 public boolean validateBooleanField(String fieldName,String fieldValue,String criteria,Object olePersistableBusinessObjectBase){
168     boolean processed = false;
169     boolean objectValue = (boolean)getObjectValue(fieldName, olePersistableBusinessObjectBase);
170     boolean exactFieldValue = false;
171     if(fieldValue.equals("true") || fieldValue.equalsIgnoreCase("Y")){
172         exactFieldValue = true;
173     }
174     if(criteria.equals("equalTo")){
175     if(exactFieldValue = objectValue){
176         processed = true;
177     }
178     }else if(criteria.equals("notEqualTo")){
179         if(exactFieldValue != objectValue){
180             processed = true;
181         }
182     }
183 return processed;
184 }
185 
186 
187 
188 public Object getObjectValue(String fieldName,Object olePersistableBusinessObjectBase){
189     Class clas = olePersistableBusinessObjectBase.getClass();
190     Object value = null;
191     try{
192     Field field = clas.getDeclaredField(fieldName);
193         field.setAccessible(true);
194       value = field.get(olePersistableBusinessObjectBase);
195     }catch(Exception e){
196 
197     }
198     return value;
199 }
200  
201     public boolean validateStringField(String fieldName,String fieldValue,String criteria,Object olePersistableBusinessObjectBase){
202 
203         boolean processed = false;
204         String objectValue = (String)getObjectValue(fieldName, olePersistableBusinessObjectBase);
205         if(criteria.equals("equalTo")){
206             if(objectValue.equalsIgnoreCase(fieldValue)){
207                 processed = true;
208             }
209         }else if(criteria.equals("notEqualTo")){
210             if(!objectValue.equalsIgnoreCase(fieldValue)){
211                 processed = true;
212             }
213         }
214 
215         return processed;
216     }
217 
218     public boolean validateIntegerField(String fieldName,String fieldValue,String criteria,Object olePersistableBusinessObjectBase){
219         boolean processed = false;
220         Integer objectValue = (Integer)getObjectValue(fieldName, olePersistableBusinessObjectBase);
221         Integer exactFieldValue = new Integer(fieldValue);
222 
223         if(criteria.equals("equalTo")){
224             if(objectValue.compareTo(exactFieldValue) == 0){
225                 processed = true;
226             }
227         }else if(criteria.equals("notEqualTo")){
228             if(objectValue.compareTo(exactFieldValue) != 0){
229                 processed = true;
230             }
231         }else if (criteria.equals("greaterThan")){
232             if(objectValue.compareTo(exactFieldValue) > 0){
233                 processed = true;
234             }
235         }else if(criteria.equals("lessThan")){
236             if(objectValue.compareTo(exactFieldValue) < 0 ){
237                 processed = true;
238             }
239         }else if (criteria.equals("greaterThanOrEqualTo")){
240             if(objectValue.compareTo(exactFieldValue) >= 0){
241                 processed = true;
242             }
243         }else if(criteria.equals("lessThanOrEqualTo")){
244             if(objectValue.compareTo(exactFieldValue) <= 0){
245                 processed = true;
246             }
247         }
248    return processed;
249     }
250 
251 
252 
253     public boolean validateBigDecimalField(String fieldName,String fieldValue,String criteria,Object olePersistableBusinessObjectBase){
254         boolean processed = false;
255         BigDecimal objectValue = (BigDecimal)getObjectValue(fieldName, olePersistableBusinessObjectBase);
256         double doubleValue = Double.valueOf(fieldValue);
257         BigDecimal exactFieldValue = BigDecimal.valueOf(doubleValue);
258 
259         if(criteria.equals("equalTo")){
260             if(objectValue.compareTo(exactFieldValue) == 0){
261                 processed = true;
262             }
263         }else if(criteria.equals("notEqualTo")){
264             if(objectValue.compareTo(exactFieldValue) != 0){
265                 processed = true;
266             }
267         }else if (criteria.equals("greaterThan")){
268             if(objectValue.compareTo(exactFieldValue) > 0){
269                 processed = true;
270             }
271         }else if(criteria.equals("lessThan")){
272             if(objectValue.compareTo(exactFieldValue) < 0 ){
273                 processed = true;
274             }
275         }else if (criteria.equals("greaterThanOrEqualTo")){
276             if(objectValue.compareTo(exactFieldValue) >= 0){
277                 processed = true;
278             }
279         }else if(criteria.equals("lessThanOrEqualTo")){
280             if(objectValue.compareTo(exactFieldValue) <= 0){
281                 processed = true;
282             }
283         }
284         return processed;
285     }
286 
287     public boolean validateKualiDecimalField(String fieldName,String fieldValue,String criteria,Object olePersistableBusinessObjectBase){
288         boolean processed = false;
289         KualiDecimal objectValue = (KualiDecimal)getObjectValue(fieldName, olePersistableBusinessObjectBase);
290 
291         KualiDecimal exactFieldValue = new KualiDecimal(fieldValue);
292         if(criteria.equals("equalTo")){
293             if(objectValue.compareTo(exactFieldValue) == 0){
294                 processed = true;
295             }
296         }else if(criteria.equals("notEqualTo")){
297             if(objectValue.compareTo(exactFieldValue) != 0){
298                 processed = true;
299             }
300         }else if (criteria.equals("greaterThan")){
301             if(objectValue.compareTo(exactFieldValue) > 0){
302                 processed = true;
303             }
304         }else if(criteria.equals("lessThan")){
305             if(objectValue.compareTo(exactFieldValue) < 0 ){
306                 processed = true;
307             }
308         }else if (criteria.equals("greaterThanOrEqualTo")){
309             if(objectValue.compareTo(exactFieldValue) >= 0){
310                 processed = true;
311             }
312         }else if(criteria.equals("lessThanOrEqualTo")){
313             if(objectValue.compareTo(exactFieldValue) <= 0){
314                 processed = true;
315             }
316         }
317         return processed;
318     }
319 
320 
321 
322     public boolean validateUtilDateField(String fieldName,String fieldValue,String criteria,Object olePersistableBusinessObjectBase){
323         boolean processed = false;
324         Date objectValue = (Date)getObjectValue(fieldName, olePersistableBusinessObjectBase);
325         Date exactFieldValue = new Date(fieldValue);
326         if(criteria.equals("equalTo")){
327             if(objectValue.compareTo(exactFieldValue) == 0){
328                 processed = true;
329             }
330         }else if(criteria.equals("notEqualTo")){
331             if(objectValue.compareTo(exactFieldValue) != 0){
332                 processed = true;
333             }
334         }else if (criteria.equals("greaterThan")){
335             if(objectValue.compareTo(exactFieldValue) > 0){
336                 processed = true;
337             }
338         }else if(criteria.equals("lessThan")){
339             if(objectValue.compareTo(exactFieldValue) < 0 ){
340                 processed = true;
341             }
342         }else if (criteria.equals("greaterThanOrEqualTo")){
343             if(objectValue.compareTo(exactFieldValue) >= 0){
344                 processed = true;
345             }
346         }else if(criteria.equals("lessThanOrEqualTo")){
347             if(objectValue.compareTo(exactFieldValue) <= 0){
348                 processed = true;
349             }
350         }
351         return processed;
352     }
353 
354 
355     public boolean validateSqlDateField(String fieldName,String fieldValue,String criteria,Object olePersistableBusinessObjectBase){
356         boolean processed = false;
357         java.sql.Date objectValue = (java.sql.Date)getObjectValue(fieldName, olePersistableBusinessObjectBase);
358         Date utilDate = new Date(fieldValue);
359         java.sql.Date exactFieldValue = new java.sql.Date(utilDate.getTime());
360         if(criteria.equals("equalTo")){
361             if(objectValue.compareTo(exactFieldValue) == 0){
362                 processed = true;
363             }
364         }else if(criteria.equals("notEqualTo")){
365             if(objectValue.compareTo(exactFieldValue) != 0){
366                 processed = true;
367             }
368         }else if (criteria.equals("greaterThan")){
369             if(objectValue.compareTo(exactFieldValue) > 0){
370                 processed = true;
371             }
372         }else if(criteria.equals("lessThan")){
373             if(objectValue.compareTo(exactFieldValue) < 0 ){
374                 processed = true;
375             }
376         }else if (criteria.equals("greaterThanOrEqualTo")){
377             if(objectValue.compareTo(exactFieldValue) >= 0){
378                 processed = true;
379             }
380         }else if(criteria.equals("lessThanOrEqualTo")){
381             if(objectValue.compareTo(exactFieldValue) <= 0){
382                 processed = true;
383             }
384         }
385         return processed;
386     }
387 
388     public boolean validateTimeStampField(String fieldName,String fieldValue,String criteria,Object olePersistableBusinessObjectBase){
389         boolean processed = false;
390         Timestamp objectValue = (Timestamp)getObjectValue(fieldName, olePersistableBusinessObjectBase);
391         Date utilDate = new Date(fieldValue);
392         Timestamp exactFieldValue = Timestamp.valueOf(fieldName);
393         if(criteria.equals("equalTo")){
394             if(objectValue.compareTo(exactFieldValue) == 0){
395                 processed = true;
396             }
397         }else if(criteria.equals("notEqualTo")){
398             if(objectValue.compareTo(exactFieldValue) != 0){
399                 processed = true;
400             }
401         }else if (criteria.equals("greaterThan")){
402             if(objectValue.compareTo(exactFieldValue) > 0){
403                 processed = true;
404             }
405         }else if(criteria.equals("lessThan")){
406             if(objectValue.compareTo(exactFieldValue) < 0 ){
407                 processed = true;
408             }
409         }else if (criteria.equals("greaterThanOrEqualTo")){
410             if(objectValue.compareTo(exactFieldValue) >= 0){
411                 processed = true;
412             }
413         }else if(criteria.equals("lessThanOrEqualTo")){
414             if(objectValue.compareTo(exactFieldValue) <= 0){
415                 processed = true;
416             }
417         }
418         return processed;
419     }
420 
421 
422 
423     public List<AlertBo> generateAlertList(AlertConditionAndReceiverInformation alertConditionAndReceiverInformation,String documentNumber,boolean isRepeatable){
424         List<AlertBo> alertBoList = new ArrayList<AlertBo>();
425         String initiatorId = GlobalVariables.getUserSession().getPrincipalId();
426         String principalName = GlobalVariables.getUserSession().getPrincipalName();
427         AlertBo alertBo = null;
428         String receivingUserName=null;
429         String groupName = null;
430         if(alertConditionAndReceiverInformation.getPrincipalId()!=null){
431             receivingUserName = KimApiServiceLocator.getPersonService().getPerson(alertConditionAndReceiverInformation.getPrincipalId()).getPrincipalName();
432             alertBo = getAlert(documentNumber,alertConditionAndReceiverInformation.getAlertInterval(),isRepeatable,initiatorId,alertConditionAndReceiverInformation.getAlertNote(),principalName,alertConditionAndReceiverInformation.getPrincipalId(),receivingUserName,null,null,null,null);
433             alertBoList.add(alertBo);
434         }
435         if(alertConditionAndReceiverInformation.getGroupId()!=null){
436                             List<String> memberIds = groupService.getMemberPrincipalIds(alertConditionAndReceiverInformation.getGroupId());
437             groupName = KimApiServiceLocator.getGroupService().getGroup(alertConditionAndReceiverInformation.getGroupId()).getName();
438             if(memberIds!=null && memberIds.size()>0){
439                 for(String memberId :memberIds ){
440                     receivingUserName = KimApiServiceLocator.getPersonService().getPerson(memberId).getPrincipalName();
441 
442                  alertBo = getAlert(documentNumber,alertConditionAndReceiverInformation.getAlertInterval(),isRepeatable,initiatorId,alertConditionAndReceiverInformation.getAlertNote(),principalName,memberId,receivingUserName,null,null,alertConditionAndReceiverInformation.getGroupId(),groupName);
443                alertBoList.add(alertBo);
444                 }
445             }
446             }
447         if(alertConditionAndReceiverInformation.getRoleId()!=null){
448             List<String> roleIds = new ArrayList<String>();
449             roleIds.add(alertConditionAndReceiverInformation.getRoleId());
450            Role role =  roleService.getRole(alertConditionAndReceiverInformation.getRoleId());
451           Collection collection  =  (Collection)roleService.getRoleMemberPrincipalIds(role.getNamespaceCode(),role.getName(),null);
452          List<String> memberIds = new ArrayList<String>();
453             memberIds.addAll(collection);
454             if(memberIds!=null && memberIds.size()>0){
455              for(String memberId : memberIds){
456                  receivingUserName = KimApiServiceLocator.getPersonService().getPerson(memberId).getPrincipalName();
457                  alertBo = getAlert(documentNumber,alertConditionAndReceiverInformation.getAlertInterval(),isRepeatable,initiatorId,alertConditionAndReceiverInformation.getAlertNote(),principalName,memberId,receivingUserName,alertConditionAndReceiverInformation.getRoleId(),role.getName(),null,null);
458             alertBoList.add(alertBo);
459              }
460            }
461             }
462         return alertBoList;
463         }
464 
465 
466 
467 
468 
469     public AlertBo getAlert(String documentId,String alertInterval,boolean isRepeatable,String initiatorId,String note,String principalName,String receivingUserId,String receivingUserName,String roleId,String roleName,String groupId,String groupName){
470         AlertBo alertBo = new AlertBo();
471         alertBo.setDocumentId(documentId);
472         alertBo.setAlertDate(new java.sql.Date(System.currentTimeMillis()));
473         alertBo.setAlertCreateDate(new java.sql.Date(System.currentTimeMillis()));
474         alertBo.setAlertInitiatorId(initiatorId);
475         alertBo.setAlertInitiatorName(principalName);
476         alertBo.setAlertNote(note);
477         alertBo.setAlertStatus(true);
478         if(alertInterval !=null && !alertInterval.trim().isEmpty()){
479         alertBo.setAlertInterval(alertInterval);
480         }
481         if(alertBo.isRepeatable()){
482             alertBo.setRepeatable(true);
483         }
484         alertBo.setReceivingUserId(receivingUserId);
485         alertBo.setReceivingUserName(receivingUserName);
486         if(groupId!=null){
487         alertBo.setReceivingGroupId(groupId);
488         alertBo.setReceivingGroupName(groupName);
489         }
490         if(roleId!=null){
491         alertBo.setReceivingRoleId(roleId);
492         alertBo.setReceivingRoleName(roleName);
493         }
494         return alertBo;
495     }
496 
497 
498 }