2 * Copyright (C) 2016, 2017 Roland Häder
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.
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.
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/>.
17 package org.mxchange.addressbook.beans.user.activity;
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;
26 import javax.annotation.PostConstruct;
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;
53 * A controller (bean) for user activity log
55 * @author Roland Häder<roland@mxchange.org>
57 @Named ("userActivityController")
59 public class AddressbookUserActivityWebApplicationBean extends BaseAddressbookController implements AddressbookUserActivityWebApplicationController {
64 private static final long serialVersionUID = 192_586_376_717_856_904L;
72 * EJB for user activity log
74 @EJB (lookup = "java:global/addressbook-ejb/userActivity!org.mxchange.jusercore.model.user.activity.UserActivityLogSessionBeanRemote")
75 private UserActivityLogSessionBeanRemote userActivityBean;
78 * "Cache" for activity log per user
80 private final Map<User, List<LogableUserActivity>> usersActivity;
85 @SuppressWarnings ("CollectionWithoutInitialCapacity")
86 public AddressbookUserActivityWebApplicationBean () {
88 this.usersActivity = new LinkedHashMap<>();
92 * Event observer for newly added users by administrator
94 * @param event Event being fired
96 public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
97 // event should not be null
100 throw new NullPointerException("event is null"); //NOI18N
101 } else if (event.getAddedUser() == null) {
103 throw new NullPointerException("event.addedUser is null"); //NOI18N
104 } else if (event.getAddedUser().getUserId() == null) {
106 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
107 } else if (event.getAddedUser().getUserId() < 1) {
109 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
113 this.addUserActivity(event.getAddedUser(), "ADMIN_ADDED_USER_ACCOUNT"); //NOI18N
117 * Event observer for deleted user accounts (by administrator)
119 * @param event Event being fired
121 public void afterAdminDeletedUserEvent (@Observes final ObservableAdminDeletedUserEvent event) {
122 // event should not be null
125 throw new NullPointerException("event is null"); //NOI18N
126 } else if (event.getDeletedUser() == null) {
128 throw new NullPointerException("event.deletedUser is null"); //NOI18N
129 } else if (event.getDeletedUser().getUserId() == null) {
131 throw new NullPointerException("event.deletedUser.userId is null"); //NOI18N
132 } else if (event.getDeletedUser().getUserId() < 1) {
134 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getDeletedUser(), event.getDeletedUser().getUserId())); //NOI18N
138 this.addUserActivity(event.getDeletedUser(), "ADMIN_DELETED_USER_ACCOUNT", event.getUserDeleteReason()); //NOI18N
142 * Event observer for linked users with existing contact data
144 * @param event Event being fired
146 public void afterAdminLinkedUserEvent (@Observes final ObservableAdminLinkedUserEvent event) {
147 // event should not be null
150 throw new NullPointerException("event is null"); //NOI18N
151 } else if (event.getLinkedUser() == null) {
153 throw new NullPointerException("event.linkedUser is null"); //NOI18N
154 } else if (event.getLinkedUser().getUserId() == null) {
156 throw new NullPointerException("event.linkedUser.userId is null"); //NOI18N
157 } else if (event.getLinkedUser().getUserId() < 1) {
159 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserId())); //NOI18N
163 this.addUserActivity(event.getLinkedUser(), "ADMIN_LINKED_USER_ACCOUNT"); //NOI18N
167 * Event observer for locked users
169 * @param event Event being fired
171 public void afterAdminLockedUserEvent (@Observes final ObservableAdminLockedUserEvent event) {
172 // event should not be null
175 throw new NullPointerException("event is null"); //NOI18N
176 } else if (event.getLockedUser() == null) {
178 throw new NullPointerException("event.lockedUser is null"); //NOI18N
179 } else if (event.getLockedUser().getUserId() == null) {
181 throw new NullPointerException("event.lockedUser.userId is null"); //NOI18N
182 } else if (event.getLockedUser().getUserId() < 1) {
184 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLockedUser(), event.getLockedUser().getUserId())); //NOI18N
188 this.addUserActivity(event.getLockedUser(), "ADMIN_LOCKED_USER_ACCOUNT", event.getLockedUser().getUserLastLockedReason()); //NOI18N
192 * Event observer for unlocked users
194 * @param event Event being fired
196 public void afterAdminUnlockedUserEvent (@Observes final ObservableAdminUnlockedUserEvent event) {
197 // event should not be null
200 throw new NullPointerException("event is null"); //NOI18N
201 } else if (event.getUnlockedUser() == null) {
203 throw new NullPointerException("event.unlockedUser is null"); //NOI18N
204 } else if (event.getUnlockedUser().getUserId() == null) {
206 throw new NullPointerException("event.unlockedUser.userId is null"); //NOI18N
207 } else if (event.getUnlockedUser().getUserId() < 1) {
209 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUnlockedUser(), event.getUnlockedUser().getUserId())); //NOI18N
213 this.addUserActivity(event.getUnlockedUser(), "ADMIN_UNLOCKED_USER_ACCOUNT"); //NOI18N
217 * Event observer for updated user data by administrator
219 * @param event Event being updated
221 public void afterAdminUpdatedUserDataEvent (@Observes final ObservableAdminUpdatedUserDataEvent event) {
222 // event should not be null
225 throw new NullPointerException("event is null"); //NOI18N
226 } else if (event.getUpdatedUser() == null) {
228 throw new NullPointerException("event.updatedUser is null"); //NOI18N
229 } else if (event.getUpdatedUser().getUserId() == null) {
231 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
232 } else if (event.getUpdatedUser().getUserId() < 1) {
234 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N
238 this.addUserActivity(event.getUpdatedUser(), "ADMIN_UPDATED_USER_PERSONAL_DATA"); //NOI18N
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.
246 * @param event User created event
248 public void afterCreatedUserEvent (@Observes final ObservableCreatedUserEvent event) {
249 // Is the instance valid?
252 throw new NullPointerException("event is null"); //NOI18N
253 } else if (event.getCreatedUser() == null) {
255 throw new NullPointerException("event.createdUser is null"); //NOI18N
256 } else if (event.getCreatedUser().getUserId() == null) {
258 throw new NullPointerException("event.createdUser.userId is null"); //NOI18N
259 } else if (event.getCreatedUser().getUserId() < 1) {
261 throw new NullPointerException(MessageFormat.format("event.createdUser.userId={0} is not valid", event.getCreatedUser().getUserId())); //NOI18N
265 this.setUser(event.getCreatedUser());
269 * Event observer when user confirmed account.
271 * @param event Event being fired
273 public void afterUserConfirmedAccountEvent (@Observes final ObservableUserConfirmedAccountEvent event) {
274 // event should not be null
277 throw new NullPointerException("event is null"); //NOI18N
278 } else if (event.getConfirmedUser() == null) {
280 throw new NullPointerException("event.confirmedUser is null"); //NOI18N
281 } else if (event.getConfirmedUser().getUserId() == null) {
283 throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
284 } else if (event.getConfirmedUser().getUserId() < 1) {
286 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
290 this.addUserActivity(event.getConfirmedUser(), "USER_CONFIRMED_ACCOUNT"); //NOI18N
294 * Event observer for logged-in user
296 * @param event Event being fired
298 public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
299 // event should not be null
302 throw new NullPointerException("event is null"); //NOI18N
303 } else if (event.getLoggedInUser() == null) {
305 throw new NullPointerException("event.registeredUser is null"); //NOI18N
306 } else if (event.getLoggedInUser().getUserId() == null) {
308 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
309 } else if (event.getLoggedInUser().getUserId() < 1) {
311 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
314 // Copy all data to this bean
315 this.addUserActivity(event.getLoggedInUser(), "USER_LOGGED_IN"); //NOI18N
319 * Event observer for logged-out user
321 * @param event Event instance
323 public void afterUserLogoutEvent (@Observes final ObservableUserLogoutEvent event) {
324 // event should not be null
327 throw new NullPointerException("event is null"); //NOI18N
328 } else if (event.getLoggedOutUser() == null) {
330 throw new NullPointerException("event.loggedOutUser is null"); //NOI18N
331 } else if (event.getLoggedOutUser().getUserId() == null) {
333 throw new NullPointerException("event.loggedOutUser.userId is null"); //NOI18N
334 } else if (event.getLoggedOutUser().getUserId() < 1) {
336 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedOutUser(), event.getLoggedOutUser().getUserId())); //NOI18N
340 this.addUserActivity(event.getLoggedOutUser(), "USER_LOGGED_OUT"); //NOI18N
344 * Event observer for new user registrations
346 * @param event Event being fired
348 public void afterUserRegistrationEvent (@Observes final ObservableUserRegisteredEvent event) {
349 // event should not be null
352 throw new NullPointerException("event is null"); //NOI18N
353 } else if (event.getRegisteredUser() == null) {
355 throw new NullPointerException("event.registeredUser is null"); //NOI18N
356 } else if (event.getRegisteredUser().getUserId() == null) {
358 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
359 } else if (event.getRegisteredUser().getUserId() < 1) {
361 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
365 this.addUserActivity(event.getRegisteredUser(), "USER_REGISTERED_NEW_ACCOUNT"); //NOI18N
369 * Event observer for users resending their confirmation link
371 * @param event Event being fired
373 public void afterUserResendConfirmationLinkEvent (@Observes final ObservableUserResendLinkAccountEvent event) {
374 // event should not be null
377 throw new NullPointerException("event is null"); //NOI18N
378 } else if (event.getResendLinkUser() == null) {
380 throw new NullPointerException("event.resendLinkUser is null"); //NOI18N
381 } else if (event.getResendLinkUser().getUserId() == null) {
383 throw new NullPointerException("event.resendLinkUser.userId is null"); //NOI18N
384 } else if (event.getResendLinkUser().getUserId() < 1) {
386 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getResendLinkUser(), event.getResendLinkUser().getUserId())); //NOI18N
389 // Copy all data to this bean
390 this.addUserActivity(event.getResendLinkUser(), "USER_RESEND_CONFIRMATION_LINK"); //NOI18N
394 * Method being call after user's password has been updated (and history
395 * entry has been created).
397 * @param event Event being observed
399 public void afterUserUpdatedPasswordEvent (@Observes final ObservableUpdatedUserPasswordEvent event) {
403 throw new NullPointerException("event is null"); //NOI18N
404 } else if (event.getPasswordHistory() == null) {
406 throw new NullPointerException("event.passwordHistory is null"); //NOI18N
407 } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) {
409 throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N
410 } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) {
412 throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N
416 this.addUserActivity(event.getPasswordHistory().getUserPasswordHistoryUser(), "USER_UPDATED_PASSWORD"); //NOI18N
420 * Listens to fired event when user updated personal data
422 * @param event Event being fired
424 public void afterUserUpdatedPersonalDataEvent (@Observes final ObservableUpdatedUserPersonalDataEvent event) {
428 throw new NullPointerException("event is null"); //NOI18N
429 } else if (event.getUpdatedUser() == null) {
431 throw new NullPointerException("event.updatedUser is null"); //NOI18N
432 } else if (event.getUpdatedUser().getUserId() == null) {
434 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
435 } else if (event.getUpdatedUser().getUserId() < 1) {
437 throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
441 this.addUserActivity(event.getUpdatedUser(), "USER_UPDATED_PERSONAL_DATA"); //NOI18N
445 * Expands given activity type into a i18n string for administrators
447 * @param activityType Activity type
449 * @return Expanded i18n string
451 public String expandAdminActivityType (final String activityType) {
453 if (null == activityType) {
455 throw new NullPointerException("activityType is null"); //NOI18N
456 } else if (activityType.isEmpty()) {
458 throw new IllegalArgumentException("activityType is empty"); //NOI18N
462 return "ADMIN_ACTIVITY_" + activityType; //NOI18N
466 * Expands given activity type into a i18n string for users
468 * @param activityType Activity type
470 * @return Expanded i18n string
472 public String expandUserActivityType (final String activityType) {
474 if (null == activityType) {
476 throw new NullPointerException("activityType is null"); //NOI18N
477 } else if (activityType.isEmpty()) {
479 throw new IllegalArgumentException("activityType is empty"); //NOI18N
483 return "USER_ACTIVITY_" + activityType; //NOI18N
487 * Returns a list of in beanHelper set user instance's activity log
489 * @return List of user's activity log
491 public List<LogableUserActivity> fetchCurrentUsersActivityLog () {
492 // beanHelper.user should be set and valid
493 if (this.getUser() == null) {
495 throw new NullPointerException("this.beanHelper.user is null"); //NOI18N
496 } else if (this.getUser().getUserId() == null) {
498 throw new NullPointerException("this.beanHelper.user.userId is null"); //NOI18N
499 } else if (this.getUser().getUserId() < 1) {
501 throw new IllegalArgumentException(MessageFormat.format("this.beanHelper.user.userId={0} is not valid", this.getUser().getUserId())); //NOI18N
505 List<LogableUserActivity> list = new LinkedList<>();
508 if (this.usersActivity.containsKey(this.getUser())) {
510 list.addAll(this.usersActivity.get(this.getUser()));
512 // Sort list and reverse it
513 Collections.sort(list, new UserActivityLogTimestampComparator());
514 Collections.reverse(list);
522 * Getter for user instance
524 * @return User instance
526 public User getUser () {
531 * Setter for user instance
533 * @param user User instance
535 public void setUser (final User user) {
540 * Post-constructor method
543 public void init () {
545 List<LogableUserActivity> list = this.userActivityBean.fetchAllUserActivityLog();
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())) {
552 this.usersActivity.put(userActivity.getActivityUser(), new LinkedList<LogableUserActivity>());
555 // Add by user instance
556 boolean added = this.usersActivity.get(userActivity.getActivityUser()).add(userActivity);
559 assert (added) : "Activity log not added"; //NOI18N
564 * Adds user activity entry with given type
566 * @param user User instance
567 * @param activityType Activity type
569 private void addUserActivity (final User user, final String activityType) {
570 // Better re-validate
573 throw new NullPointerException("user is null"); //NOI18N
574 } else if (user.getUserId() == null) {
576 throw new NullPointerException("user.userId is null"); //NOI18N
577 } else if (user.getUserId() < 1) {
579 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
580 } else if (null == activityType) {
582 throw new NullPointerException("activityType is null"); //NOI18N
583 } else if (activityType.isEmpty()) {
585 throw new IllegalArgumentException("activityType is empty"); //NOI18N
588 // Create new activity object
589 LogableUserActivity userActivity = new UserActivityLog(activityType, user, new GregorianCalendar(), this.determinePrincipalName());
591 // Call bean to add it
592 this.userActivityBean.addUserActivityLog(userActivity);
595 this.addUserActivityToCache(userActivity);
599 * Adds user activity log with type and message
601 * @param user User instance
602 * @param activityType Activity type
603 * @param message Activity message
605 private void addUserActivity (final User user, final String activityType, final String message) {
606 // Better re-validate
609 throw new NullPointerException("user is null"); //NOI18N
610 } else if (user.getUserId() == null) {
612 throw new NullPointerException("user.userId is null"); //NOI18N
613 } else if (user.getUserId() < 1) {
615 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
616 } else if (null == activityType) {
618 throw new NullPointerException("activityType is null"); //NOI18N
619 } else if (activityType.isEmpty()) {
621 throw new IllegalArgumentException("activityType is empty"); //NOI18N
622 } else if (null == message) {
624 throw new NullPointerException("message is null"); //NOI18N
625 } else if (message.isEmpty()) {
627 throw new IllegalArgumentException("message is empty"); //NOI18N
630 // Create new activity object
631 LogableUserActivity userActivity = new UserActivityLog(message, activityType, user, new GregorianCalendar(), this.determinePrincipalName());
633 // Call bean to add it
634 this.userActivityBean.addUserActivityLog(userActivity);
637 this.addUserActivityToCache(userActivity);
641 * Adds given user activity instance to "cache" (local map)
643 * @param userActivity User activity instance
645 private void addUserActivityToCache (final LogableUserActivity userActivity) {
646 // Is the instance valid
647 if (null == userActivity) {
649 throw new NullPointerException("userActivity is null");
650 } else if (userActivity.getActivityId() instanceof Long) {
652 throw new IllegalArgumentException("userActivity.activityId=" + userActivity.getActivityId() + " is not null");
653 } else if (userActivity.getActivityUser() == null) {
655 throw new NullPointerException("userActivity.activityUser is null");
656 } else if (userActivity.getActivityUser().getUserId() == null) {
658 throw new NullPointerException("userActivity.activityUser.userId is null");
659 } else if (userActivity.getActivityUser().getUserId() < 1) {
661 throw new IllegalArgumentException("userActivity.activityUser.userId=" + userActivity.getActivityUser().getUserId() + " is not valid");
665 if (!this.usersActivity.containsKey(userActivity.getActivityUser())) {
667 this.usersActivity.put(userActivity.getActivityUser(), new LinkedList<LogableUserActivity>());
670 // Add activity to log
671 this.usersActivity.get(userActivity.getActivityUser()).add(userActivity);