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