]> git.mxchange.org Git - addressbook-war.git/blob
7d8407f179d434ef3c2714c5f1ee5ec678c632d3
[addressbook-war.git] /
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.addressbook.beans.user.activity;
18
19 import java.text.MessageFormat;
20 import java.util.Collections;
21 import java.util.GregorianCalendar;
22 import java.util.LinkedHashMap;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Map;
26 import javax.annotation.PostConstruct;
27 import javax.enterprise.context.ApplicationScoped;
28 import javax.enterprise.event.Observes;
29 import javax.faces.view.facelets.FaceletException;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import javax.naming.Context;
33 import javax.naming.InitialContext;
34 import javax.naming.NamingException;
35 import org.mxchange.addressbook.beans.BaseAddressbookController;
36 import org.mxchange.addressbook.beans.helper.AddressbookWebRequestHelperController;
37 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
38 import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent;
39 import org.mxchange.jusercore.events.user.delete.ObservableAdminDeletedUserEvent;
40 import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent;
41 import org.mxchange.jusercore.events.user.locked.ObservableAdminLockedUserEvent;
42 import org.mxchange.jusercore.events.user.unlocked.ObservableAdminUnlockedUserEvent;
43 import org.mxchange.jusercore.events.user.update.ObservableAdminUpdatedUserDataEvent;
44 import org.mxchange.jusercore.events.user.update.ObservableUpdatedUserPersonalDataEvent;
45 import org.mxchange.jusercore.model.user.User;
46 import org.mxchange.jusercore.model.user.activity.LogableUserActivity;
47 import org.mxchange.jusercore.model.user.activity.UserActivityLog;
48 import org.mxchange.jusercore.model.user.activity.UserActivityLogSessionBeanRemote;
49 import org.mxchange.jusercore.model.user.activity.comparator.UserActivityLogTimestampComparator;
50 import org.mxchange.juserlogincore.events.confirmation.ObservableUserConfirmedAccountEvent;
51 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
52 import org.mxchange.juserlogincore.events.logout.ObservableUserLogoutEvent;
53 import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredEvent;
54 import org.mxchange.juserlogincore.events.resendlink.ObservableUserResendLinkAccountEvent;
55 import org.mxchange.juserlogincore.events.user.password_change.ObservableUpdatedUserPasswordEvent;
56
57 /**
58  * A controller (bean) for user activity log
59  * <p>
60  * @author Roland Häder<roland@mxchange.org>
61  */
62 @Named ("userActivityController")
63 @ApplicationScoped
64 public class AddressbookUserActivityWebApplicationBean extends BaseAddressbookController implements AddressbookUserActivityWebApplicationController {
65
66         /**
67          * Serial number
68          */
69         private static final long serialVersionUID = 192_586_376_717_856_904L;
70
71         /**
72          * Bean helper
73          */
74         @Inject
75         private AddressbookWebRequestHelperController beanHelper;
76
77         /**
78          * User instance
79          */
80         private User user;
81
82         /**
83          * EJB for user activity log
84          */
85         private UserActivityLogSessionBeanRemote userActivityBean;
86
87         /**
88          * "Cache" for activity log per user
89          */
90         private final Map<User, List<LogableUserActivity>> usersActivity;
91
92         /**
93          * Default constructor
94          */
95         @SuppressWarnings ("CollectionWithoutInitialCapacity")
96         public AddressbookUserActivityWebApplicationBean () {
97                 // Init cache
98                 this.usersActivity = new LinkedHashMap<>();
99         }
100
101         /**
102          * Event observer for newly added users by administrator
103          * <p>
104          * @param event Event being fired
105          */
106         public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
107                 // event should not be null
108                 if (null == event) {
109                         // Throw NPE
110                         throw new NullPointerException("event is null"); //NOI18N
111                 } else if (event.getAddedUser() == null) {
112                         // Throw NPE again
113                         throw new NullPointerException("event.addedUser is null"); //NOI18N
114                 } else if (event.getAddedUser().getUserId() == null) {
115                         // userId is null
116                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
117                 } else if (event.getAddedUser().getUserId() < 1) {
118                         // Not avalid id
119                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
120                 }
121
122                 // Update user list
123                 this.addUserActivity(event.getAddedUser(), "ADMIN_ADDED_USER_ACCOUNT"); //NOI18N
124         }
125
126         /**
127          * Event observer for deleted user accounts (by administrator)
128          * <p>
129          * @param event Event being fired
130          */
131         public void afterAdminDeletedUserEvent (@Observes final ObservableAdminDeletedUserEvent event) {
132                 // event should not be null
133                 if (null == event) {
134                         // Throw NPE
135                         throw new NullPointerException("event is null"); //NOI18N
136                 } else if (event.getDeletedUser() == null) {
137                         // Throw NPE again
138                         throw new NullPointerException("event.deletedUser is null"); //NOI18N
139                 } else if (event.getDeletedUser().getUserId() == null) {
140                         // userId is null
141                         throw new NullPointerException("event.deletedUser.userId is null"); //NOI18N
142                 } else if (event.getDeletedUser().getUserId() < 1) {
143                         // Not avalid id
144                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getDeletedUser(), event.getDeletedUser().getUserId())); //NOI18N
145                 }
146
147                 // Add acitivity
148                 this.addUserActivity(event.getDeletedUser(), "ADMIN_DELETED_USER_ACCOUNT", event.getUserDeleteReason()); //NOI18N
149         }
150
151         /**
152          * Event observer for linked users with existing contact data
153          * <p>
154          * @param event Event being fired
155          */
156         public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) {
157                 // event should not be null
158                 if (null == event) {
159                         // Throw NPE
160                         throw new NullPointerException("event is null"); //NOI18N
161                 } else if (event.getLinkedUser() == null) {
162                         // Throw NPE again
163                         throw new NullPointerException("event.linkedUser is null"); //NOI18N
164                 } else if (event.getLinkedUser().getUserId() == null) {
165                         // userId is null
166                         throw new NullPointerException("event.linkedUser.userId is null"); //NOI18N
167                 } else if (event.getLinkedUser().getUserId() < 1) {
168                         // Not avalid id
169                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserId())); //NOI18N
170                 }
171
172                 // Update user list
173                 this.addUserActivity(event.getLinkedUser(), "ADMIN_LINKED_USER_ACCOUNT"); //NOI18N
174         }
175
176         /**
177          * Event observer for locked users
178          * <p>
179          * @param event Event being fired
180          */
181         public void afterAdminLockedUserEvent (@Observes final ObservableAdminLockedUserEvent event) {
182                 // event should not be null
183                 if (null == event) {
184                         // Throw NPE
185                         throw new NullPointerException("event is null"); //NOI18N
186                 } else if (event.getLockedUser() == null) {
187                         // Throw NPE again
188                         throw new NullPointerException("event.lockedUser is null"); //NOI18N
189                 } else if (event.getLockedUser().getUserId() == null) {
190                         // userId is null
191                         throw new NullPointerException("event.lockedUser.userId is null"); //NOI18N
192                 } else if (event.getLockedUser().getUserId() < 1) {
193                         // Not avalid id
194                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLockedUser(), event.getLockedUser().getUserId())); //NOI18N
195                 }
196
197                 // Update user list
198                 this.addUserActivity(event.getLockedUser(), "ADMIN_LOCKED_USER_ACCOUNT", event.getLockedUser().getUserLastLockedReason()); //NOI18N
199         }
200
201         /**
202          * Event observer for unlocked users
203          * <p>
204          * @param event Event being fired
205          */
206         public void afterAdminUnlockedUserEvent (@Observes final ObservableAdminUnlockedUserEvent event) {
207                 // event should not be null
208                 if (null == event) {
209                         // Throw NPE
210                         throw new NullPointerException("event is null"); //NOI18N
211                 } else if (event.getUnlockedUser() == null) {
212                         // Throw NPE again
213                         throw new NullPointerException("event.unlockedUser is null"); //NOI18N
214                 } else if (event.getUnlockedUser().getUserId() == null) {
215                         // userId is null
216                         throw new NullPointerException("event.unlockedUser.userId is null"); //NOI18N
217                 } else if (event.getUnlockedUser().getUserId() < 1) {
218                         // Not avalid id
219                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUnlockedUser(), event.getUnlockedUser().getUserId())); //NOI18N
220                 }
221
222                 // Update user list
223                 this.addUserActivity(event.getUnlockedUser(), "ADMIN_UNLOCKED_USER_ACCOUNT"); //NOI18N
224         }
225
226         /**
227          * Event observer for updated user data by administrator
228          * <p>
229          * @param event Event being updated
230          */
231         public void afterAdminUpdatedUserDataEvent (@Observes final ObservableAdminUpdatedUserDataEvent event) {
232                 // event should not be null
233                 if (null == event) {
234                         // Throw NPE
235                         throw new NullPointerException("event is null"); //NOI18N
236                 } else if (event.getUpdatedUser() == null) {
237                         // Throw NPE again
238                         throw new NullPointerException("event.updatedUser is null"); //NOI18N
239                 } else if (event.getUpdatedUser().getUserId() == null) {
240                         // userId is null
241                         throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
242                 } else if (event.getUpdatedUser().getUserId() < 1) {
243                         // Not avalid id
244                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N
245                 }
246
247                 // Update user list
248                 this.addUserActivity(event.getUpdatedUser(), "ADMIN_UPDATED_USER_PERSONAL_DATA"); //NOI18N
249         }
250
251         /**
252          * Event observer for when a bean helper has successfully created a user
253          * instance, means the user exists. If the user does not exist, this event
254          * should not fire but instead a proper exception must be thrown.
255          * <p>
256          * @param event User created event
257          */
258         public void afterCreatedUserEvent (@Observes final ObservableCreatedUserEvent event) {
259                 // Is the instance valid?
260                 if (null == event) {
261                         // Throw NPE
262                         throw new NullPointerException("event is null"); //NOI18N
263                 } else if (event.getCreatedUser() == null) {
264                         // Throw NPE again
265                         throw new NullPointerException("event.createdUser is null"); //NOI18N
266                 } else if (event.getCreatedUser().getUserId() == null) {
267                         // Throw NPE again
268                         throw new NullPointerException("event.createdUser.userId is null"); //NOI18N
269                 } else if (event.getCreatedUser().getUserId() < 1) {
270                         // Throw NPE again
271                         throw new NullPointerException(MessageFormat.format("event.createdUser.userId={0} is not valid", event.getCreatedUser().getUserId())); //NOI18N
272                 }
273
274                 // Set whole user
275                 this.setUser(event.getCreatedUser());
276         }
277
278         /**
279          * Event observer when user confirmed account.
280          * <p>
281          * @param event Event being fired
282          */
283         public void afterUserConfirmedAccountEvent (@Observes final ObservableUserConfirmedAccountEvent event) {
284                 // event should not be null
285                 if (null == event) {
286                         // Throw NPE
287                         throw new NullPointerException("event is null"); //NOI18N
288                 } else if (event.getConfirmedUser() == null) {
289                         // Throw NPE again
290                         throw new NullPointerException("event.confirmedUser is null"); //NOI18N
291                 } else if (event.getConfirmedUser().getUserId() == null) {
292                         // userId is null
293                         throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
294                 } else if (event.getConfirmedUser().getUserId() < 1) {
295                         // Not avalid id
296                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
297                 }
298
299                 // Update user list
300                 this.addUserActivity(event.getConfirmedUser(), "USER_CONFIRMED_ACCOUNT"); //NOI18N
301         }
302
303         /**
304          * Event observer for logged-in user
305          * <p>
306          * @param event Event being fired
307          */
308         public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
309                 // event should not be null
310                 if (null == event) {
311                         // Throw NPE
312                         throw new NullPointerException("event is null"); //NOI18N
313                 } else if (event.getLoggedInUser() == null) {
314                         // Throw NPE again
315                         throw new NullPointerException("event.registeredUser is null"); //NOI18N
316                 } else if (event.getLoggedInUser().getUserId() == null) {
317                         // userId is null
318                         throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
319                 } else if (event.getLoggedInUser().getUserId() < 1) {
320                         // Not avalid id
321                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
322                 }
323
324                 // Copy all data to this bean
325                 this.addUserActivity(event.getLoggedInUser(), "USER_LOGGED_IN"); //NOI18N
326         }
327
328         /**
329          * Event observer for logged-out user
330          * <p>
331          * @param event Event instance
332          */
333         public void afterUserLogoutEvent (@Observes final ObservableUserLogoutEvent event) {
334                 // event should not be null
335                 if (null == event) {
336                         // Throw NPE
337                         throw new NullPointerException("event is null"); //NOI18N
338                 } else if (event.getLoggedOutUser() == null) {
339                         // Throw NPE again
340                         throw new NullPointerException("event.loggedOutUser is null"); //NOI18N
341                 } else if (event.getLoggedOutUser().getUserId() == null) {
342                         // userId is null
343                         throw new NullPointerException("event.loggedOutUser.userId is null"); //NOI18N
344                 } else if (event.getLoggedOutUser().getUserId() < 1) {
345                         // Not avalid id
346                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedOutUser(), event.getLoggedOutUser().getUserId())); //NOI18N
347                 }
348
349                 // Update user list
350                 this.addUserActivity(event.getLoggedOutUser(), "USER_LOGGED_OUT"); //NOI18N
351         }
352
353         /**
354          * Event observer for new user registrations
355          * <p>
356          * @param event Event being fired
357          */
358         public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
359                 // event should not be null
360                 if (null == event) {
361                         // Throw NPE
362                         throw new NullPointerException("event is null"); //NOI18N
363                 } else if (event.getRegisteredUser() == null) {
364                         // Throw NPE again
365                         throw new NullPointerException("event.registeredUser is null"); //NOI18N
366                 } else if (event.getRegisteredUser().getUserId() == null) {
367                         // userId is null
368                         throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
369                 } else if (event.getRegisteredUser().getUserId() < 1) {
370                         // Not avalid id
371                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
372                 }
373
374                 // Update user list
375                 this.addUserActivity(event.getRegisteredUser(), "USER_REGISTERED_NEW_ACCOUNT"); //NOI18N
376         }
377
378         /**
379          * Event observer for users resending their confirmation link
380          * <p>
381          * @param event Event being fired
382          */
383         public void afterUserResendConfirmationLinkEvent (@Observes final ObservableUserResendLinkAccountEvent event) {
384                 // event should not be null
385                 if (null == event) {
386                         // Throw NPE
387                         throw new NullPointerException("event is null"); //NOI18N
388                 } else if (event.getResendLinkUser() == null) {
389                         // Throw NPE again
390                         throw new NullPointerException("event.resendLinkUser is null"); //NOI18N
391                 } else if (event.getResendLinkUser().getUserId() == null) {
392                         // userId is null
393                         throw new NullPointerException("event.resendLinkUser.userId is null"); //NOI18N
394                 } else if (event.getResendLinkUser().getUserId() < 1) {
395                         // Not avalid id
396                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getResendLinkUser(), event.getResendLinkUser().getUserId())); //NOI18N
397                 }
398
399                 // Copy all data to this bean
400                 this.addUserActivity(event.getResendLinkUser(), "USER_RESEND_CONFIRMATION_LINK"); //NOI18N
401         }
402
403         /**
404          * Method being call after user's password has been updated (and history
405          * entry has been created).
406          * <p>
407          * @param event Event being observed
408          */
409         public void afterUserUpdatedPasswordEvent (@Observes final ObservableUpdatedUserPasswordEvent event) {
410                 // Check parameter
411                 if (null == event) {
412                         // Throw NPE
413                         throw new NullPointerException("event is null"); //NOI18N
414                 } else if (event.getPasswordHistory() == null) {
415                         // Throw NPE again
416                         throw new NullPointerException("event.passwordHistory is null"); //NOI18N
417                 } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) {
418                         // ... and again
419                         throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N
420                 } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) {
421                         // Invalid value
422                         throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N
423                 }
424
425                 // Update user list
426                 this.addUserActivity(event.getPasswordHistory().getUserPasswordHistoryUser(), "USER_UPDATED_PASSWORD"); //NOI18N
427         }
428
429         /**
430          * Listens to fired event when user updated personal data
431          * <p>
432          * @param event Event being fired
433          */
434         public void afterUserUpdatedPersonalDataEvent (@Observes final ObservableUpdatedUserPersonalDataEvent event) {
435                 // Check parameter
436                 if (null == event) {
437                         // Throw NPE
438                         throw new NullPointerException("event is null"); //NOI18N
439                 } else if (event.getUpdatedUser() == null) {
440                         // Throw NPE again
441                         throw new NullPointerException("event.updatedUser is null"); //NOI18N
442                 } else if (event.getUpdatedUser().getUserId() == null) {
443                         // ... and again
444                         throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
445                 } else if (event.getUpdatedUser().getUserId() < 1) {
446                         // Invalid value
447                         throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
448                 }
449
450                 // Update user list
451                 this.addUserActivity(event.getUpdatedUser(), "USER_UPDATED_PERSONAL_DATA"); //NOI18N
452         }
453
454         /**
455          * Expands given activity type into a i18n string for administrators
456          * <p>
457          * @param activityType Activity type
458          * <p>
459          * @return Expanded i18n string
460          */
461         public String expandAdminActivityType (final String activityType) {
462                 // Is it valid?
463                 if (null == activityType) {
464                         // Throw NPE
465                         throw new NullPointerException("activityType is null"); //NOI18N
466                 } else if (activityType.isEmpty()) {
467                         // Is empty
468                         throw new IllegalArgumentException("activityType is empty"); //NOI18N
469                 }
470
471                 // Expand it
472                 return "ADMIN_ACTIVITY_" + activityType; //NOI18N
473         }
474
475         /**
476          * Expands given activity type into a i18n string for users
477          * <p>
478          * @param activityType Activity type
479          * <p>
480          * @return Expanded i18n string
481          */
482         public String expandUserActivityType (final String activityType) {
483                 // Is it valid?
484                 if (null == activityType) {
485                         // Throw NPE
486                         throw new NullPointerException("activityType is null"); //NOI18N
487                 } else if (activityType.isEmpty()) {
488                         // Is empty
489                         throw new IllegalArgumentException("activityType is empty"); //NOI18N
490                 }
491
492                 // Expand it
493                 return "USER_ACTIVITY_" + activityType; //NOI18N
494         }
495
496         /**
497          * Returns a list of in beanHelper set user instance's activity log
498          * <p>
499          * @return List of user's activity log
500          */
501         public List<LogableUserActivity> fetchCurrentUsersActivityLog () {
502                 // beanHelper.user should be set and valid
503                 if (this.getUser() == null) {
504                         // Is not set
505                         throw new NullPointerException("this.beanHelper.user is null"); //NOI18N
506                 } else if (this.getUser().getUserId() == null) {
507                         // Throw NPE again
508                         throw new NullPointerException("this.beanHelper.user.userId is null"); //NOI18N
509                 } else if (this.getUser().getUserId() < 1) {
510                         // Invalid id number
511                         throw new IllegalArgumentException(MessageFormat.format("this.beanHelper.user.userId={0} is not valid", this.getUser().getUserId())); //NOI18N
512                 }
513
514                 // Init list
515                 List<LogableUserActivity> list = new LinkedList<>();
516
517                 // Is the user set?
518                 if (this.usersActivity.containsKey(this.getUser())) {
519                         // Return it
520                         list.addAll(this.usersActivity.get(this.getUser()));
521
522                         // Sort list and reverse it
523                         Collections.sort(list, new UserActivityLogTimestampComparator());
524                         Collections.reverse(list);
525                 }
526
527                 // Return it
528                 return list;
529         }
530
531         /**
532          * Getter for user instance
533          * <p>
534          * @return User instance
535          */
536         public User getUser () {
537                 return this.user;
538         }
539
540         /**
541          * Setter for user instance
542          * <p>
543          * @param user User instance
544          */
545         public void setUser (final User user) {
546                 this.user = user;
547         }
548
549         /**
550          * Post-constructor method
551          */
552         @PostConstruct
553         public void init () {
554                 // Try to get EJB instance
555                 try {
556                         // Get initial context
557                         Context context = new InitialContext();
558
559                         // Try to lookup
560                         this.userActivityBean = (UserActivityLogSessionBeanRemote) context.lookup("java:global/addressbook-ejb/userActivity!org.mxchange.jusercore.model.user.activity.UserActivityLogSessionBeanRemote"); //NOI18N
561                 } catch (final NamingException e) {
562                         // Throw again
563                         throw new FaceletException(e);
564                 }
565
566                 // Get whole list
567                 List<LogableUserActivity> list = this.userActivityBean.fetchAllUserActivityLog();
568
569                 // Put all in map, per-user
570                 for (final LogableUserActivity userActivity : list) {
571                         // Is the list there?
572                         if (!this.usersActivity.containsKey(userActivity.getActivityUser())) {
573                                 // Init list
574                                 this.usersActivity.put(userActivity.getActivityUser(), new LinkedList<LogableUserActivity>());
575                         }
576
577                         // Add by user instance
578                         boolean added = this.usersActivity.get(userActivity.getActivityUser()).add(userActivity);
579
580                         // Should be added
581                         assert (added) : "Activity log not added"; //NOI18N
582                 }
583         }
584
585         /**
586          * Adds user activity entry with given type
587          * <p>
588          * @param user         User instance
589          * @param activityType Activity type
590          */
591         private void addUserActivity (final User user, final String activityType) {
592                 // Better re-validate
593                 if (null == user) {
594                         // Throw NPE
595                         throw new NullPointerException("user is null"); //NOI18N
596                 } else if (user.getUserId() == null) {
597                         // Throw again
598                         throw new NullPointerException("user.userId is null"); //NOI18N
599                 } else if (user.getUserId() < 1) {
600                         // Invalid id number
601                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
602                 } else if (null == activityType) {
603                         // Throw NPE again
604                         throw new NullPointerException("activityType is null"); //NOI18N
605                 } else if (activityType.isEmpty()) {
606                         // Is empty
607                         throw new IllegalArgumentException("activityType is empty"); //NOI18N
608                 }
609
610                 // Create new activity object
611                 LogableUserActivity userActivity = new UserActivityLog(activityType, user, new GregorianCalendar(), this.determinePrincipalName());
612
613                 // Call bean to add it
614                 this.userActivityBean.addUserActivityLog(userActivity);
615
616                 // Add to cache, too
617                 this.addUserActivityToCache(userActivity);
618         }
619
620         /**
621          * Adds user activity log with type and message
622          * <p>
623          * @param user         User instance
624          * @param activityType Activity type
625          * @param message      Activity message
626          */
627         private void addUserActivity (final User user, final String activityType, final String message) {
628                 // Better re-validate
629                 if (null == user) {
630                         // Throw NPE
631                         throw new NullPointerException("user is null"); //NOI18N
632                 } else if (user.getUserId() == null) {
633                         // Throw again
634                         throw new NullPointerException("user.userId is null"); //NOI18N
635                 } else if (user.getUserId() < 1) {
636                         // Invalid id number
637                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
638                 } else if (null == activityType) {
639                         // Throw NPE again
640                         throw new NullPointerException("activityType is null"); //NOI18N
641                 } else if (activityType.isEmpty()) {
642                         // Is empty
643                         throw new IllegalArgumentException("activityType is empty"); //NOI18N
644                 } else if (null == message) {
645                         // Throw NPE again
646                         throw new NullPointerException("message is null"); //NOI18N
647                 } else if (message.isEmpty()) {
648                         // Is empty
649                         throw new IllegalArgumentException("message is empty"); //NOI18N
650                 }
651
652                 // Create new activity object
653                 LogableUserActivity userActivity = new UserActivityLog(message, activityType, user, new GregorianCalendar(), this.determinePrincipalName());
654
655                 // Call bean to add it
656                 this.userActivityBean.addUserActivityLog(userActivity);
657
658                 // Add to cache, too
659                 this.addUserActivityToCache(userActivity);
660         }
661
662         /**
663          * Adds given user activity instance to "cache" (local map)
664          * <p>
665          * @param userActivity User activity instance
666          */
667         private void addUserActivityToCache (final LogableUserActivity userActivity) {
668                 // Is the instance valid
669                 if (null == userActivity) {
670                         // Throw NPE again
671                         throw new NullPointerException("userActivity is null");
672                 } else if (userActivity.getActivityId() instanceof Long) {
673                         // Is not null
674                         throw new IllegalArgumentException("userActivity.activityId=" + userActivity.getActivityId() + " is not null");
675                 } else if (userActivity.getActivityUser() == null) {
676                         // Throw NPE again
677                         throw new NullPointerException("userActivity.activityUser is null");
678                 } else if (userActivity.getActivityUser().getUserId() == null) {
679                         // Throw it again
680                         throw new NullPointerException("userActivity.activityUser.userId is null");
681                 } else if (userActivity.getActivityUser().getUserId() < 1) {
682                         // Invalid id number
683                         throw new IllegalArgumentException("userActivity.activityUser.userId=" + userActivity.getActivityUser().getUserId() + " is not valid");
684                 }
685
686                 // Is it there?
687                 if (!this.usersActivity.containsKey(userActivity.getActivityUser())) {
688                         // Init it
689                         this.usersActivity.put(userActivity.getActivityUser(), new LinkedList<LogableUserActivity>());
690                 }
691
692                 // Add activity to log
693                 this.usersActivity.get(userActivity.getActivityUser()).add(userActivity);
694         }
695
696 }