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