]> 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.util.LinkedHashMap;
20 import java.util.LinkedList;
21 import java.util.List;
22 import java.util.Map;
23 import javax.annotation.PostConstruct;
24 import javax.enterprise.context.ApplicationScoped;
25 import javax.faces.view.facelets.FaceletException;
26 import javax.inject.Named;
27 import javax.naming.Context;
28 import javax.naming.InitialContext;
29 import javax.naming.NamingException;
30 import org.mxchange.jjobs.beans.BaseJobsController;
31 import org.mxchange.jusercore.model.user.User;
32 import org.mxchange.jusercore.model.user.activity.LogableUserActivity;
33 import org.mxchange.jusercore.model.user.activity.UserActivityLogSessionBeanRemote;
34
35 /**
36  * A controller (bean) for user activity log
37  * <p>
38  * @author Roland Haeder<rhaeder@cho-time.de>
39  */
40 @Named ("userActivityController")
41 @ApplicationScoped
42 public class JobsUserActivityWebApplicationBean extends BaseJobsController implements JobsUserActivityWebApplicationController {
43
44         /**
45          * Serial number
46          */
47         private static final long serialVersionUID = 192_586_376_717_856_904L;
48
49         /**
50          * EJB for user activity log
51          */
52         private UserActivityLogSessionBeanRemote userActivityBean;
53
54         /**
55          * "Cache" for activity log per user
56          */
57         private final Map<User, List<LogableUserActivity>> usersActivity;
58
59         /**
60          * Default constructor
61          */
62         @SuppressWarnings ("CollectionWithoutInitialCapacity")
63         public JobsUserActivityWebApplicationBean () {
64                 // Try to get EJB instance
65                 try {
66                         // Get initial context
67                         Context context = new InitialContext();
68
69                         // Try to lookup
70                         this.userActivityBean = (UserActivityLogSessionBeanRemote) context.lookup("java:global/jlandingpage-ejb/userActivity!org.mxchange.jusercore.model.user.activity.UserActivityLogSessionBeanRemote"); //NOI18N
71                 } catch (final NamingException e) {
72                         // Throw again
73                         throw new FaceletException(e);
74                 }
75
76                 // Init cache
77                 this.usersActivity = new LinkedHashMap<>();
78         }
79
80         /**
81          * Post-constructor method
82          */
83         @PostConstruct
84         public void init () {
85                 // Get whole list
86                 List<LogableUserActivity> list = this.userActivityBean.fetchAllUserActivityLog();
87
88                 // Put all in map, per-user
89                 for (final LogableUserActivity userActivity : list) {
90                         // Is the list there?
91                         if (!this.usersActivity.containsKey(userActivity.getActivityUser())) {
92                                 // Init list
93                                 this.usersActivity.put(userActivity.getActivityUser(), new LinkedList<LogableUserActivity>());
94                         }
95
96                         // Add by user instance
97                         boolean added = this.usersActivity.get(userActivity.getActivityUser()).add(userActivity);
98
99                         // Should be added
100                         assert(added) : "Activity log not added"; //NOI18N
101                 }
102         }
103
104 }