2 * Copyright (C) 2016 Roland Haeder<rhaeder@cho-time.de>
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.jjobs.beans.user.activity;
19 import java.text.MessageFormat;
20 import java.util.GregorianCalendar;
21 import java.util.LinkedHashMap;
22 import java.util.LinkedList;
23 import java.util.List;
25 import javax.annotation.PostConstruct;
26 import javax.enterprise.context.ApplicationScoped;
27 import javax.enterprise.event.Observes;
28 import javax.faces.view.facelets.FaceletException;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import javax.naming.Context;
32 import javax.naming.InitialContext;
33 import javax.naming.NamingException;
34 import org.mxchange.jjobs.beans.BaseJobsController;
35 import org.mxchange.jjobs.beans.helper.JobsWebViewHelperController;
36 import org.mxchange.jusercore.events.confirmation.UserConfirmedAccountEvent;
37 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
38 import org.mxchange.jusercore.events.registration.UserRegisteredEvent;
39 import org.mxchange.jusercore.events.resendlink.UserResendLinkAccountEvent;
40 import org.mxchange.jusercore.events.user.add.AdminAddedUserEvent;
41 import org.mxchange.jusercore.events.user.linked.AdminLinkedUserEvent;
42 import org.mxchange.jusercore.events.user.locked.AdminLockedUserEvent;
43 import org.mxchange.jusercore.events.user.password_change.UpdatedUserPasswordEvent;
44 import org.mxchange.jusercore.events.user.unlocked.AdminUnlockedUserEvent;
45 import org.mxchange.jusercore.events.user.update.AdminUpdatedUserDataEvent;
46 import org.mxchange.jusercore.events.user.update.UpdatedUserPersonalDataEvent;
47 import org.mxchange.jusercore.model.user.User;
48 import org.mxchange.jusercore.model.user.activity.LogableUserActivity;
49 import org.mxchange.jusercore.model.user.activity.UserActivityLog;
50 import org.mxchange.jusercore.model.user.activity.UserActivityLogSessionBeanRemote;
53 * A controller (bean) for user activity log
55 * @author Roland Haeder<rhaeder@cho-time.de>
57 @Named ("userActivityController")
59 public class JobsUserActivityWebApplicationBean extends BaseJobsController implements JobsUserActivityWebApplicationController {
64 private static final long serialVersionUID = 192_586_376_717_856_904L;
70 private JobsWebViewHelperController beanHelper;
73 * EJB for user activity log
75 private UserActivityLogSessionBeanRemote userActivityBean;
78 * "Cache" for activity log per user
80 private final Map<User, List<LogableUserActivity>> usersActivity;
85 @SuppressWarnings ("CollectionWithoutInitialCapacity")
86 public JobsUserActivityWebApplicationBean () {
87 // Try to get EJB instance
89 // Get initial context
90 Context context = new InitialContext();
93 this.userActivityBean = (UserActivityLogSessionBeanRemote) context.lookup("java:global/jlandingpage-ejb/userActivity!org.mxchange.jusercore.model.user.activity.UserActivityLogSessionBeanRemote"); //NOI18N
94 } catch (final NamingException e) {
96 throw new FaceletException(e);
100 this.usersActivity = new LinkedHashMap<>();
104 public void addUserActivity (final User user, final String activityType) {
105 // Better re-validate
108 throw new NullPointerException("user is null"); //NOI18N
109 } else if (user.getUserId() == null) {
111 throw new NullPointerException("user.userId is null"); //NOI18N
112 } else if (user.getUserId() < 1) {
114 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
115 } else if (null == activityType) {
117 throw new NullPointerException("activityType is null"); //NOI18N
118 } else if (activityType.isEmpty()) {
120 throw new IllegalArgumentException("activityType is empty"); //NOI18N
123 // Create new activity object
124 LogableUserActivity userActivity = new UserActivityLog(activityType, user, new GregorianCalendar());
126 // Call bean to add it
127 this.userActivityBean.addUserActivityLog(userActivity);
131 public void addUserActivity (final User user, final String activityType, final String message) {
132 // Better re-validate
135 throw new NullPointerException("user is null"); //NOI18N
136 } else if (user.getUserId() == null) {
138 throw new NullPointerException("user.userId is null"); //NOI18N
139 } else if (user.getUserId() < 1) {
141 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
142 } else if (null == activityType) {
144 throw new NullPointerException("activityType is null"); //NOI18N
145 } else if (activityType.isEmpty()) {
147 throw new IllegalArgumentException("activityType is empty"); //NOI18N
148 } else if (null == message) {
150 throw new NullPointerException("message is null"); //NOI18N
151 } else if (message.isEmpty()) {
153 throw new IllegalArgumentException("message is empty"); //NOI18N
156 // Create new activity object
157 LogableUserActivity userActivity = new UserActivityLog(message, activityType, user, new GregorianCalendar());
159 // Call bean to add it
160 this.userActivityBean.addUserActivityLog(userActivity);
164 public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
165 // event should not be null
168 throw new NullPointerException("event is null"); //NOI18N
169 } else if (event.getAddedUser() == null) {
171 throw new NullPointerException("event.addedUser is null"); //NOI18N
172 } else if (event.getAddedUser().getUserId() == null) {
174 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
175 } else if (event.getAddedUser().getUserId() < 1) {
177 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
181 this.addUserActivity(event.getAddedUser(), "ADMIN_ADDED_USER_ACCOUNT"); //NOI18N
185 public void afterAdminLinkedUserEvent (@Observes final AdminLinkedUserEvent event) {
186 // event should not be null
189 throw new NullPointerException("event is null"); //NOI18N
190 } else if (event.getLinkedUser() == null) {
192 throw new NullPointerException("event.linkedUser is null"); //NOI18N
193 } else if (event.getLinkedUser().getUserId() == null) {
195 throw new NullPointerException("event.linkedUser.userId is null"); //NOI18N
196 } else if (event.getLinkedUser().getUserId() < 1) {
198 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserId())); //NOI18N
202 this.addUserActivity(event.getLinkedUser(), "ADMIN_LINKED_USER_ACCOUNT"); //NOI18N
206 public void afterAdminLockedUserEvent (@Observes final AdminLockedUserEvent event) {
207 // event should not be null
210 throw new NullPointerException("event is null"); //NOI18N
211 } else if (event.getLockedUser() == null) {
213 throw new NullPointerException("event.lockedUser is null"); //NOI18N
214 } else if (event.getLockedUser().getUserId() == null) {
216 throw new NullPointerException("event.lockedUser.userId is null"); //NOI18N
217 } else if (event.getLockedUser().getUserId() < 1) {
219 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLockedUser(), event.getLockedUser().getUserId())); //NOI18N
223 this.addUserActivity(event.getLockedUser(), "ADMIN_LOCKED_USER_ACCOUNT", event.getLockedUser().getUserLastLockedReason()); //NOI18N
227 public void afterAdminUnlockedUserEvent (@Observes final AdminUnlockedUserEvent event) {
228 // event should not be null
231 throw new NullPointerException("event is null"); //NOI18N
232 } else if (event.getUnlockedUser() == null) {
234 throw new NullPointerException("event.unlockedUser is null"); //NOI18N
235 } else if (event.getUnlockedUser().getUserId() == null) {
237 throw new NullPointerException("event.unlockedUser.userId is null"); //NOI18N
238 } else if (event.getUnlockedUser().getUserId() < 1) {
240 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUnlockedUser(), event.getUnlockedUser().getUserId())); //NOI18N
244 this.addUserActivity(event.getUnlockedUser(), "ADMIN_UNLOCKED_USER_ACCOUNT"); //NOI18N
248 public void afterAdminUpdatedUserDataEvent (@Observes final AdminUpdatedUserDataEvent event) {
249 // event should not be null
252 throw new NullPointerException("event is null"); //NOI18N
253 } else if (event.getUpdatedUser() == null) {
255 throw new NullPointerException("event.updatedUser is null"); //NOI18N
256 } else if (event.getUpdatedUser().getUserId() == null) {
258 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
259 } else if (event.getUpdatedUser().getUserId() < 1) {
261 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N
265 this.addUserActivity(event.getUpdatedUser(), "ADMIN_UPDATED_USER_PERSONAL_DATA"); //NOI18N
269 public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
270 // event should not be null
273 throw new NullPointerException("event is null"); //NOI18N
274 } else if (event.getRegisteredUser() == null) {
276 throw new NullPointerException("event.registeredUser is null"); //NOI18N
277 } else if (event.getRegisteredUser().getUserId() == null) {
279 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
280 } else if (event.getRegisteredUser().getUserId() < 1) {
282 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
286 this.addUserActivity(event.getRegisteredUser(), "USER_REGISTERED_NEW_ACCOUNT"); //NOI18N
290 public void afterUserConfirmedAccountEvent (@Observes final UserConfirmedAccountEvent event) {
291 // event should not be null
294 throw new NullPointerException("event is null"); //NOI18N
295 } else if (event.getConfirmedUser() == null) {
297 throw new NullPointerException("event.confirmedUser is null"); //NOI18N
298 } else if (event.getConfirmedUser().getUserId() == null) {
300 throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
301 } else if (event.getConfirmedUser().getUserId() < 1) {
303 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
307 this.addUserActivity(event.getConfirmedUser(), "USER_CONFIRMED_ACCOUNT"); //NOI18N
311 public void afterUserLoginEvent (@Observes final UserLoggedInEvent event) {
312 // event should not be null
315 throw new NullPointerException("event is null"); //NOI18N
316 } else if (event.getLoggedInUser() == null) {
318 throw new NullPointerException("event.registeredUser is null"); //NOI18N
319 } else if (event.getLoggedInUser().getUserId() == null) {
321 throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
322 } else if (event.getLoggedInUser().getUserId() < 1) {
324 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
327 // Copy all data to this bean
328 this.addUserActivity(event.getLoggedInUser(), "USER_LOGGED_IN"); //NOI18N
332 public void afterUserResendConfirmationLinkEvent (@Observes final UserResendLinkAccountEvent event) {
333 // event should not be null
336 throw new NullPointerException("event is null"); //NOI18N
337 } else if (event.getResendLinkUser()== null) {
339 throw new NullPointerException("event.resendLinkUser is null"); //NOI18N
340 } else if (event.getResendLinkUser().getUserId() == null) {
342 throw new NullPointerException("event.resendLinkUser.userId is null"); //NOI18N
343 } else if (event.getResendLinkUser().getUserId() < 1) {
345 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getResendLinkUser(), event.getResendLinkUser().getUserId())); //NOI18N
348 // Copy all data to this bean
349 this.addUserActivity(event.getResendLinkUser(), "USER_RESEND_CONFIRMATION_LINK"); //NOI18N
353 public void afterUserUpdatedPasswordEvent (@Observes final UpdatedUserPasswordEvent event) {
357 throw new NullPointerException("event is null"); //NOI18N
358 } else if (event.getPasswordHistory() == null) {
360 throw new NullPointerException("event.passwordHistory is null"); //NOI18N
361 } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) {
363 throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N
364 } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) {
366 throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N
370 this.addUserActivity(event.getPasswordHistory().getUserPasswordHistoryUser(), "USER_UPDATED_PASSWORD"); //NOI18N
374 public void afterUserUpdatedPersonalDataEvent (@Observes final UpdatedUserPersonalDataEvent event) {
378 throw new NullPointerException("event is null"); //NOI18N
379 } else if (event.getUpdatedUser() == null) {
381 throw new NullPointerException("event.updatedUser is null"); //NOI18N
382 } else if (event.getUpdatedUser().getUserId() == null) {
384 throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
385 } else if (event.getUpdatedUser().getUserId() < 1) {
387 throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
391 this.addUserActivity(event.getUpdatedUser(), "USER_UPDATED_PERSONAL_DATA"); //NOI18N
395 public List<LogableUserActivity> allCurrentUsersActivityLog () {
397 User user = this.beanHelper.getUser();
399 // beanHelper.user should be set and valid
402 throw new NullPointerException("this.beanHelper.user is null");
403 } else if (user.getUserId() == null) {
405 throw new NullPointerException("this.beanHelper.user.userId is null");
406 } else if (user.getUserId() < 1) {
408 throw new IllegalArgumentException(MessageFormat.format("this.beanHelper.user.userId={0} is not valid", user.getUserId()));
412 List<LogableUserActivity> list = new LinkedList<>();
415 if (this.usersActivity.containsKey(user)) {
417 list.addAll(this.usersActivity.get(user));
425 * Post-constructor method
428 public void init () {
430 List<LogableUserActivity> list = this.userActivityBean.fetchAllUserActivityLog();
432 // Put all in map, per-user
433 for (final LogableUserActivity userActivity : list) {
434 // Is the list there?
435 if (!this.usersActivity.containsKey(userActivity.getActivityUser())) {
437 this.usersActivity.put(userActivity.getActivityUser(), new LinkedList<LogableUserActivity>());
440 // Add by user instance
441 boolean added = this.usersActivity.get(userActivity.getActivityUser()).add(userActivity);
444 assert (added) : "Activity log not added"; //NOI18N