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