1818package com .esofthead .mycollab .common .interceptor .aspect ;
1919
2020import java .io .Serializable ;
21+ import java .lang .reflect .InvocationTargetException ;
2122import java .lang .reflect .Method ;
2223import java .util .GregorianCalendar ;
2324
@@ -134,15 +135,89 @@ public void traceAfterUpdateActivity(JoinPoint joinPoint, Object bean,
134135 }
135136 }
136137
137- Auditable auditAnnotation = cls .getAnnotation (Auditable .class );
138+ int sAccountId ;
139+ try {
140+ sAccountId = (Integer ) PropertyUtils
141+ .getProperty (bean , "saccountid" );
142+ } catch (IllegalAccessException | InvocationTargetException
143+ | NoSuchMethodException e ) {
144+ log .error ("Can not define saccountid field of bean {}" , bean );
145+ return ;
146+ }
147+
148+ Integer auditLogId = saveAuditLog (cls , bean , username , sAccountId ,
149+ activityStreamId );
150+
151+ Watchable watchableAnnotation = cls .getAnnotation (Watchable .class );
152+ if (watchableAnnotation != null ) {
153+ try {
154+ int monitorTypeId = (Integer ) PropertyUtils .getProperty (bean ,
155+ "id" );
156+ String monitorType = watchableAnnotation .type ();
157+
158+ String moreUser = (String ) PropertyUtils .getProperty (bean ,
159+ watchableAnnotation .userFieldName ());
160+
161+ Integer extraTypeId = null ;
162+ if (!"" .equals (watchableAnnotation .extraTypeId ())) {
163+ extraTypeId = (Integer ) PropertyUtils .getProperty (bean ,
164+ watchableAnnotation .extraTypeId ());
165+ }
166+ // check whether the current user is in monitor list, if
167+ // not add him in
168+ if (moreUser != null ) {
169+ if (!monitorItemService .isUserWatchingItem (moreUser ,
170+ monitorType , monitorTypeId )) {
171+ MonitorItem monitorItem = new MonitorItem ();
172+ monitorItem .setMonitorDate (new GregorianCalendar ()
173+ .getTime ());
174+ monitorItem .setType (monitorType );
175+ monitorItem .setTypeid (monitorTypeId );
176+ monitorItem .setUser (moreUser );
177+ monitorItem .setExtratypeid (extraTypeId );
178+ monitorItem .setSaccountid (sAccountId );
179+ monitorItemService .saveWithSession (monitorItem ,
180+ moreUser );
181+ }
182+ }
183+
184+ // Save notification email
185+ RelayEmailNotification relayNotification = new RelayEmailNotification ();
186+ relayNotification .setChangeby (username );
187+ relayNotification .setChangecomment ("" );
188+ relayNotification .setSaccountid (sAccountId );
189+ relayNotification .setType (monitorType );
190+ relayNotification .setTypeid (monitorTypeId );
191+ relayNotification .setEmailhandlerbean (watchableAnnotation
192+ .emailHandlerBean ().getName ());
193+ if (auditLogId != null ) {
194+ relayNotification .setExtratypeid (auditLogId );
195+ }
196+
197+ relayNotification .setAction (MonitorTypeConstants .UPDATE_ACTION );
198+
199+ relayEmailNotificationService .saveWithSession (
200+ relayNotification , username );
201+ } catch (Exception e ) {
202+ log .error (
203+ "Error when save audit for save action of service "
204+ + cls .getName () + "and bean: "
205+ + BeanUtility .printBeanObj (bean ), e );
206+ }
207+ }
208+ }
209+
210+ private Integer saveAuditLog (Class <?> targetCls , Object bean ,
211+ String username , Integer sAccountId , Integer activityStreamId ) {
212+ Auditable auditAnnotation = targetCls .getAnnotation (Auditable .class );
138213 if (auditAnnotation != null ) {
139214 String key = null ;
215+ String changeSet = "" ;
140216 try {
141217
142218 int typeid = (Integer ) PropertyUtils .getProperty (bean , "id" );
143219 key = bean .toString () + auditAnnotation .type () + typeid ;
144- int sAccountId = (Integer ) PropertyUtils .getProperty (bean ,
145- "saccountid" );
220+
146221 Object oldValue = caches .get (key );
147222 if (oldValue != null ) {
148223 AuditLog auditLog = new AuditLog ();
@@ -152,82 +227,25 @@ public void traceAfterUpdateActivity(JoinPoint joinPoint, Object bean,
152227 auditLog .setTypeid (typeid );
153228 auditLog .setSaccountid (sAccountId );
154229 auditLog .setPosteddate (new GregorianCalendar ().getTime ());
155- auditLog . setChangeset ( AuditLogUtil .getChangeSet (oldValue ,
156- bean ) );
230+ changeSet = AuditLogUtil .getChangeSet (oldValue , bean );
231+ auditLog . setChangeset ( changeSet );
157232 auditLog .setObjectClass (oldValue .getClass ().getName ());
158233 if (activityStreamId != null ) {
159234 auditLog .setActivitylogid (activityStreamId );
160235 }
161236
162- int auditLogId = auditLogService .saveWithSession (auditLog ,
163- "" );
164-
165- caches .remove (key );
166-
167- // Add watchable item to relay email notify associate with
168- // change
169- Watchable watchableAnnotation = cls
170- .getAnnotation (Watchable .class );
171- if (watchableAnnotation != null ) {
172- int monitorTypeId = (Integer ) PropertyUtils
173- .getProperty (bean , "id" );
174- String monitorType = watchableAnnotation .type ();
175-
176- String moreUser = (String ) PropertyUtils .getProperty (
177- bean , watchableAnnotation .userFieldName ());
178-
179- Integer extraTypeId = null ;
180- if (!"" .equals (watchableAnnotation .extraTypeId ())) {
181- extraTypeId = (Integer ) PropertyUtils .getProperty (
182- bean , watchableAnnotation .extraTypeId ());
183- }
184- // check whether the current user is in monitor list, if
185- // not add him in
186- if (moreUser != null ) {
187- if (!monitorItemService .isUserWatchingItem (
188- moreUser , monitorType , monitorTypeId )) {
189- MonitorItem monitorItem = new MonitorItem ();
190- monitorItem
191- .setMonitorDate (new GregorianCalendar ()
192- .getTime ());
193- monitorItem .setType (monitorType );
194- monitorItem .setTypeid (monitorTypeId );
195- monitorItem .setUser (moreUser );
196- monitorItem .setExtratypeid (extraTypeId );
197- monitorItem .setSaccountid (sAccountId );
198- monitorItemService .saveWithSession (monitorItem ,
199- moreUser );
200- }
201- }
202-
203- // Save notification email
204- RelayEmailNotification relayNotification = new RelayEmailNotification ();
205- relayNotification .setChangeby (username );
206- relayNotification .setChangecomment ("" );
207- relayNotification .setSaccountid (sAccountId );
208- relayNotification .setType (monitorType );
209- relayNotification .setTypeid (monitorTypeId );
210- relayNotification
211- .setEmailhandlerbean (watchableAnnotation
212- .emailHandlerBean ().getName ());
213- relayNotification .setExtratypeid (auditLogId );
214- relayNotification
215- .setAction (MonitorTypeConstants .UPDATE_ACTION );
216-
217- relayEmailNotificationService .saveWithSession (
218- relayNotification , username );
219- }
237+ return auditLogService .saveWithSession (auditLog , "" );
220238 }
221239 } catch (Exception e ) {
222240 log .error (
223241 "Error when save audit for save action of service "
224- + cls .getName () + "and bean: "
225- + BeanUtility .printBeanObj (bean ), e );
226- } finally {
227- if (key != null ) {
228- caches .remove (key );
229- }
242+ + targetCls .getName () + "and bean: "
243+ + BeanUtility .printBeanObj (bean )
244+ + " and changeset is " + changeSet , e );
245+ return null ;
230246 }
231247 }
248+
249+ return null ;
232250 }
233251}
0 commit comments