]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/user/activity/JobsUserActivityWebApplicationBean.java
updated jar(s)
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / user / activity / JobsUserActivityWebApplicationBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder<rhaeder@cho-time.de>
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.jjobs.beans.user.activity;
18
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;
24 import java.util.Map;
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;
51
52 /**
53  * A controller (bean) for user activity log
54  * <p>
55  * @author Roland Haeder<rhaeder@cho-time.de>
56  */
57 @Named ("userActivityController")
58 @ApplicationScoped
59 public class JobsUserActivityWebApplicationBean extends BaseJobsController implements JobsUserActivityWebApplicationController {
60
61         /**
62          * Serial number
63          */
64         private static final long serialVersionUID = 192_586_376_717_856_904L;
65
66         /**
67          * Bean helper
68          */
69         @Inject
70         private JobsWebViewHelperController beanHelper;
71
72         /**
73          * EJB for user activity log
74          */
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 JobsUserActivityWebApplicationBean () {
87                 // Try to get EJB instance
88                 try {
89                         // Get initial context
90                         Context context = new InitialContext();
91
92                         // Try to lookup
93                         this.userActivityBean = (UserActivityLogSessionBeanRemote) context.lookup("java:global/jlandingpage-ejb/userActivity!org.mxchange.jusercore.model.user.activity.UserActivityLogSessionBeanRemote"); //NOI18N
94                 } catch (final NamingException e) {
95                         // Throw again
96                         throw new FaceletException(e);
97                 }
98
99                 // Init cache
100                 this.usersActivity = new LinkedHashMap<>();
101         }
102
103         @Override
104         public void addUserActivity (final User user, final String activityType) {
105                 // Better re-validate
106                 if (null == user) {
107                         // Throw NPE
108                         throw new NullPointerException("user is null"); //NOI18N
109                 } else if (user.getUserId() == null) {
110                         // Throw again
111                         throw new NullPointerException("user.userId is null"); //NOI18N
112                 } else if (user.getUserId() < 1) {
113                         // Invalid id number
114                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
115                 } else if (null == activityType) {
116                         // Throw NPE again
117                         throw new NullPointerException("activityType is null"); //NOI18N
118                 } else if (activityType.isEmpty()) {
119                         // Is empty
120                         throw new IllegalArgumentException("activityType is empty"); //NOI18N
121                 }
122
123                 // Create new activity object
124                 LogableUserActivity userActivity = new UserActivityLog(activityType, user, new GregorianCalendar());
125
126                 // Call bean to add it
127                 this.userActivityBean.addUserActivityLog(userActivity);
128         }
129
130         @Override
131         public void addUserActivity (final User user, final String activityType, final String message) {
132                 // Better re-validate
133                 if (null == user) {
134                         // Throw NPE
135                         throw new NullPointerException("user is null"); //NOI18N
136                 } else if (user.getUserId() == null) {
137                         // Throw again
138                         throw new NullPointerException("user.userId is null"); //NOI18N
139                 } else if (user.getUserId() < 1) {
140                         // Invalid id number
141                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
142                 } else if (null == activityType) {
143                         // Throw NPE again
144                         throw new NullPointerException("activityType is null"); //NOI18N
145                 } else if (activityType.isEmpty()) {
146                         // Is empty
147                         throw new IllegalArgumentException("activityType is empty"); //NOI18N
148                 } else if (null == message) {
149                         // Throw NPE again
150                         throw new NullPointerException("message is null"); //NOI18N
151                 } else if (message.isEmpty()) {
152                         // Is empty
153                         throw new IllegalArgumentException("message is empty"); //NOI18N
154                 }
155
156                 // Create new activity object
157                 LogableUserActivity userActivity = new UserActivityLog(message, activityType, user, new GregorianCalendar());
158
159                 // Call bean to add it
160                 this.userActivityBean.addUserActivityLog(userActivity);
161         }
162
163         @Override
164         public void afterAdminAddedUserEvent (@Observes final AdminAddedUserEvent event) {
165                 // event should not be null
166                 if (null == event) {
167                         // Throw NPE
168                         throw new NullPointerException("event is null"); //NOI18N
169                 } else if (event.getAddedUser() == null) {
170                         // Throw NPE again
171                         throw new NullPointerException("event.addedUser is null"); //NOI18N
172                 } else if (event.getAddedUser().getUserId() == null) {
173                         // userId is null
174                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
175                 } else if (event.getAddedUser().getUserId() < 1) {
176                         // Not avalid id
177                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
178                 }
179
180                 // Update user list
181                 this.addUserActivity(event.getAddedUser(), "ADMIN_ADDED_USER_ACCOUNT"); //NOI18N
182         }
183
184         @Override
185         public void afterAdminLinkedUserEvent (@Observes final AdminLinkedUserEvent event) {
186                 // event should not be null
187                 if (null == event) {
188                         // Throw NPE
189                         throw new NullPointerException("event is null"); //NOI18N
190                 } else if (event.getLinkedUser() == null) {
191                         // Throw NPE again
192                         throw new NullPointerException("event.linkedUser is null"); //NOI18N
193                 } else if (event.getLinkedUser().getUserId() == null) {
194                         // userId is null
195                         throw new NullPointerException("event.linkedUser.userId is null"); //NOI18N
196                 } else if (event.getLinkedUser().getUserId() < 1) {
197                         // Not avalid id
198                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLinkedUser(), event.getLinkedUser().getUserId())); //NOI18N
199                 }
200
201                 // Update user list
202                 this.addUserActivity(event.getLinkedUser(), "ADMIN_LINKED_USER_ACCOUNT"); //NOI18N
203         }
204
205         @Override
206         public void afterAdminLockedUserEvent (@Observes final AdminLockedUserEvent event) {
207                 // event should not be null
208                 if (null == event) {
209                         // Throw NPE
210                         throw new NullPointerException("event is null"); //NOI18N
211                 } else if (event.getLockedUser() == null) {
212                         // Throw NPE again
213                         throw new NullPointerException("event.lockedUser is null"); //NOI18N
214                 } else if (event.getLockedUser().getUserId() == null) {
215                         // userId is null
216                         throw new NullPointerException("event.lockedUser.userId is null"); //NOI18N
217                 } else if (event.getLockedUser().getUserId() < 1) {
218                         // Not avalid id
219                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLockedUser(), event.getLockedUser().getUserId())); //NOI18N
220                 }
221
222                 // Update user list
223                 this.addUserActivity(event.getLockedUser(), "ADMIN_LOCKED_USER_ACCOUNT", event.getLockedUser().getUserLastLockedReason()); //NOI18N
224         }
225
226         @Override
227         public void afterAdminUnlockedUserEvent (@Observes final AdminUnlockedUserEvent event) {
228                 // event should not be null
229                 if (null == event) {
230                         // Throw NPE
231                         throw new NullPointerException("event is null"); //NOI18N
232                 } else if (event.getUnlockedUser() == null) {
233                         // Throw NPE again
234                         throw new NullPointerException("event.unlockedUser is null"); //NOI18N
235                 } else if (event.getUnlockedUser().getUserId() == null) {
236                         // userId is null
237                         throw new NullPointerException("event.unlockedUser.userId is null"); //NOI18N
238                 } else if (event.getUnlockedUser().getUserId() < 1) {
239                         // Not avalid id
240                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUnlockedUser(), event.getUnlockedUser().getUserId())); //NOI18N
241                 }
242
243                 // Update user list
244                 this.addUserActivity(event.getUnlockedUser(), "ADMIN_UNLOCKED_USER_ACCOUNT"); //NOI18N
245         }
246
247         @Override
248         public void afterAdminUpdatedUserDataEvent (@Observes final AdminUpdatedUserDataEvent event) {
249                 // event should not be null
250                 if (null == event) {
251                         // Throw NPE
252                         throw new NullPointerException("event is null"); //NOI18N
253                 } else if (event.getUpdatedUser() == null) {
254                         // Throw NPE again
255                         throw new NullPointerException("event.updatedUser is null"); //NOI18N
256                 } else if (event.getUpdatedUser().getUserId() == null) {
257                         // userId is null
258                         throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
259                 } else if (event.getUpdatedUser().getUserId() < 1) {
260                         // Not avalid id
261                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getUpdatedUser(), event.getUpdatedUser().getUserId())); //NOI18N
262                 }
263
264                 // Update user list
265                 this.addUserActivity(event.getUpdatedUser(), "ADMIN_UPDATED_USER_PERSONAL_DATA"); //NOI18N
266         }
267
268         @Override
269         public void afterRegistrationEvent (@Observes final UserRegisteredEvent event) {
270                 // event should not be null
271                 if (null == event) {
272                         // Throw NPE
273                         throw new NullPointerException("event is null"); //NOI18N
274                 } else if (event.getRegisteredUser() == null) {
275                         // Throw NPE again
276                         throw new NullPointerException("event.registeredUser is null"); //NOI18N
277                 } else if (event.getRegisteredUser().getUserId() == null) {
278                         // userId is null
279                         throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
280                 } else if (event.getRegisteredUser().getUserId() < 1) {
281                         // Not avalid id
282                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getRegisteredUser(), event.getRegisteredUser().getUserId())); //NOI18N
283                 }
284
285                 // Update user list
286                 this.addUserActivity(event.getRegisteredUser(), "USER_REGISTERED_NEW_ACCOUNT"); //NOI18N
287         }
288
289         @Override
290         public void afterUserConfirmedAccountEvent (@Observes final UserConfirmedAccountEvent event) {
291                 // event should not be null
292                 if (null == event) {
293                         // Throw NPE
294                         throw new NullPointerException("event is null"); //NOI18N
295                 } else if (event.getConfirmedUser() == null) {
296                         // Throw NPE again
297                         throw new NullPointerException("event.confirmedUser is null"); //NOI18N
298                 } else if (event.getConfirmedUser().getUserId() == null) {
299                         // userId is null
300                         throw new NullPointerException("event.confirmedUser.userId is null"); //NOI18N
301                 } else if (event.getConfirmedUser().getUserId() < 1) {
302                         // Not avalid id
303                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getConfirmedUser(), event.getConfirmedUser().getUserId())); //NOI18N
304                 }
305
306                 // Update user list
307                 this.addUserActivity(event.getConfirmedUser(), "USER_CONFIRMED_ACCOUNT"); //NOI18N
308         }
309
310         @Override
311         public void afterUserLoginEvent (@Observes final UserLoggedInEvent event) {
312                 // event should not be null
313                 if (null == event) {
314                         // Throw NPE
315                         throw new NullPointerException("event is null"); //NOI18N
316                 } else if (event.getLoggedInUser() == null) {
317                         // Throw NPE again
318                         throw new NullPointerException("event.registeredUser is null"); //NOI18N
319                 } else if (event.getLoggedInUser().getUserId() == null) {
320                         // userId is null
321                         throw new NullPointerException("event.registeredUser.userId is null"); //NOI18N
322                 } else if (event.getLoggedInUser().getUserId() < 1) {
323                         // Not avalid id
324                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
325                 }
326
327                 // Copy all data to this bean
328                 this.addUserActivity(event.getLoggedInUser(), "USER_LOGGED_IN"); //NOI18N
329         }
330
331         @Override
332         public void afterUserResendConfirmationLinkEvent (@Observes final UserResendLinkAccountEvent event) {
333                 // event should not be null
334                 if (null == event) {
335                         // Throw NPE
336                         throw new NullPointerException("event is null"); //NOI18N
337                 } else if (event.getResendLinkUser()== null) {
338                         // Throw NPE again
339                         throw new NullPointerException("event.resendLinkUser is null"); //NOI18N
340                 } else if (event.getResendLinkUser().getUserId() == null) {
341                         // userId is null
342                         throw new NullPointerException("event.resendLinkUser.userId is null"); //NOI18N
343                 } else if (event.getResendLinkUser().getUserId() < 1) {
344                         // Not avalid id
345                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getResendLinkUser(), event.getResendLinkUser().getUserId())); //NOI18N
346                 }
347
348                 // Copy all data to this bean
349                 this.addUserActivity(event.getResendLinkUser(), "USER_RESEND_CONFIRMATION_LINK"); //NOI18N
350         }
351
352         @Override
353         public void afterUserUpdatedPasswordEvent (@Observes final UpdatedUserPasswordEvent event) {
354                 // Check parameter
355                 if (null == event) {
356                         // Throw NPE
357                         throw new NullPointerException("event is null"); //NOI18N
358                 } else if (event.getPasswordHistory() == null) {
359                         // Throw NPE again
360                         throw new NullPointerException("event.passwordHistory is null"); //NOI18N
361                 } else if (event.getPasswordHistory().getUserPasswordHistoryId() == null) {
362                         // ... and again
363                         throw new NullPointerException("event.passwordHistory.userPasswordHistoryId is null"); //NOI18N
364                 } else if (event.getPasswordHistory().getUserPasswordHistoryId() < 1) {
365                         // Invalid value
366                         throw new IllegalArgumentException(MessageFormat.format("event.passwordHistory.userPasswordHistoryId={0} is in valid", event.getPasswordHistory().getUserPasswordHistoryId())); //NOI18N
367                 }
368
369                 // Update user list
370                 this.addUserActivity(event.getPasswordHistory().getUserPasswordHistoryUser(), "USER_UPDATED_PASSWORD"); //NOI18N
371         }
372
373         @Override
374         public void afterUserUpdatedPersonalDataEvent (@Observes final UpdatedUserPersonalDataEvent event) {
375                 // Check parameter
376                 if (null == event) {
377                         // Throw NPE
378                         throw new NullPointerException("event is null"); //NOI18N
379                 } else if (event.getUpdatedUser() == null) {
380                         // Throw NPE again
381                         throw new NullPointerException("event.updatedUser is null"); //NOI18N
382                 } else if (event.getUpdatedUser().getUserId() == null) {
383                         // ... and again
384                         throw new NullPointerException("event.updatedUser.userId is null"); //NOI18N
385                 } else if (event.getUpdatedUser().getUserId() < 1) {
386                         // Invalid value
387                         throw new IllegalArgumentException(MessageFormat.format("event.updatedUser.userId={0} is in valid", event.getUpdatedUser().getUserId())); //NOI18N
388                 }
389
390                 // Update user list
391                 this.addUserActivity(event.getUpdatedUser(), "USER_UPDATED_PERSONAL_DATA"); //NOI18N
392         }
393
394         @Override
395         public List<LogableUserActivity> allCurrentUsersActivityLog () {
396                 // Get user
397                 User user = this.beanHelper.getUser();
398
399                 // beanHelper.user should be set and valid
400                 if (null == user) {
401                         // Is not set
402                         throw new NullPointerException("this.beanHelper.user is null");
403                 } else if (user.getUserId() == null) {
404                         // Throw NPE again
405                         throw new NullPointerException("this.beanHelper.user.userId is null");
406                 } else if (user.getUserId() < 1) {
407                         // Invalid id number
408                         throw new IllegalArgumentException(MessageFormat.format("this.beanHelper.user.userId={0} is not valid", user.getUserId()));
409                 }
410
411                 // Init list
412                 List<LogableUserActivity> list = new LinkedList<>();
413
414                 // Is the user set?
415                 if (this.usersActivity.containsKey(user)) {
416                         // Return it
417                         list.addAll(this.usersActivity.get(user));
418                 }
419
420                 // Return it
421                 return list;
422         }
423
424         /**
425          * Post-constructor method
426          */
427         @PostConstruct
428         public void init () {
429                 // Get whole list
430                 List<LogableUserActivity> list = this.userActivityBean.fetchAllUserActivityLog();
431
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())) {
436                                 // Init list
437                                 this.usersActivity.put(userActivity.getActivityUser(), new LinkedList<LogableUserActivity>());
438                         }
439
440                         // Add by user instance
441                         boolean added = this.usersActivity.get(userActivity.getActivityUser()).add(userActivity);
442
443                         // Should be added
444                         assert (added) : "Activity log not added"; //NOI18N
445                 }
446         }
447
448 }