]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/activity/AddressbookUserActivitySessionBean.java
updated jar(s)
[addressbook-mailer-ejb.git] / src / java / org / mxchange / jusercore / model / user / activity / AddressbookUserActivitySessionBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
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.jusercore.model.user.activity;
18
19 import java.text.MessageFormat;
20 import java.util.Arrays;
21 import java.util.List;
22 import javax.ejb.EJBException;
23 import javax.ejb.Stateless;
24 import javax.jms.JMSException;
25 import javax.jms.ObjectMessage;
26 import javax.persistence.Query;
27 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
28 import org.mxchange.jusercore.model.user.User;
29
30 /**
31  * An EJB for user activity log. This class extends BaseDatabaseBean and not
32  * project-specific "base class". The simple reason is that this class requires
33  * no email queue as no emails are ever being sent from this class.
34  * <p>
35  * @author Roland Häder<roland@mxchange.org>
36  */
37 @Stateless (name = "userActivity", description = "A bean handling the user data")
38 public class AddressbookUserActivitySessionBean extends BaseAddressbookDatabaseBean implements UserActivityLogSessionBeanRemote {
39
40         /**
41          * Serial number
42          */
43         private static final long serialVersionUID = 219_568_677_671_054L;
44
45         /**
46          * Default constructor
47          */
48         public AddressbookUserActivitySessionBean () {
49                 // Call super constructor
50                 super("jms/jjobs-queue-factory", "jms/jjobs-user-activity-log"); //NOI18N
51         }
52
53         @Override
54         public void addUserActivityLog (final LogableUserActivity userActivity) {
55                 // Trace message
56                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUserActivityLog: userActivity={1} CALLED!", this.getClass().getSimpleName(), userActivity)); //NOI18N
57
58                 // Should be valid
59                 if (null == userActivity) {
60                         // Throw NPE
61                         throw new NullPointerException("userActivity is null"); //NOI18N
62                 } else if (userActivity.getActivityId() instanceof Long) {
63                         // Id number should not be set
64                         throw new IllegalArgumentException(MessageFormat.format("userActivity.activityId={0} should be null", userActivity.getActivityId())); //NOI18N
65                 } else if (userActivity.getActivityUser() == null) {
66                         // Throw NPE again
67                         throw new NullPointerException("userActivity.activityUser is null"); //NOI18N
68                 } else if (userActivity.getActivityType() == null) {
69                         // Throw again ...
70                         throw new NullPointerException("userActivity.activityType is null"); //NOI18N
71                 } else if (userActivity.getActivityType().isEmpty()) {
72                         // Empty type
73                         throw new NullPointerException("userActivity.activityType is empty"); //NOI18N
74                 } else if ((userActivity.getActivityMessage() instanceof String) && (userActivity.getActivityMessage().isEmpty())) {
75                         // Set but empty message
76                         throw new NullPointerException("userActivity.activityMessage is empty"); //NOI18N
77                 } else if (userActivity.getActivityTimestamp() == null) {
78                         // Throw NPE again
79                         throw new NullPointerException("userActivity.activityTimestamp is null"); //NOI18N
80                 }
81
82                 try {
83                         // Send out email change
84                         ObjectMessage message = this.getSession().createObjectMessage();
85                         message.setObject(userActivity);
86
87                         // Send message
88                         this.sendMessage(message);
89                 } catch (final JMSException ex) {
90                         // Throw again
91                         throw new EJBException(ex);
92                 }
93         }
94
95         @Override
96         @SuppressWarnings ("unchecked")
97         public List<LogableUserActivity> fetchAllUserActivityLog () {
98                 // Trace message
99                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUserActivityLog: CALLED!", this.getClass().getSimpleName())); //NOI18N
100
101                 // Search for user's activity
102                 Query query = this.getEntityManager().createNamedQuery("AllUserActivityLog", UserActivityLog.class); //NOI18N
103
104                 // Get list
105                 List<LogableUserActivity> list = query.getResultList();
106
107                 // Trace message
108                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUserActivityLog: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N
109
110                 // Return it
111                 return list;
112         }
113
114         @Override
115         @SuppressWarnings ("unchecked")
116         public List<LogableUserActivity> fetchAllUsersActivityLog (final User user) {
117                 // Trace message
118                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUsersActivityLog: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
119
120                 // Is user valid?
121                 if (null == user) {
122                         // Throw NPE
123                         throw new NullPointerException("user is null"); //NOI18N
124                 } else if (user.getUserId() == null) {
125                         // Throw again
126                         throw new NullPointerException("user.userId is null"); //NOI18N
127                 } else if (user.getUserId() < 1) {
128                         // Invalid id number
129                         throw new IllegalArgumentException(MessageFormat.format("user.userId{0} is not valid", user.getUserId())); //NOI18N
130                 }
131
132                 // Search for user's activity
133                 Query query = this.getEntityManager().createNamedQuery("FindAllUsersActivity", UserActivityLog.class); //NOI18N
134
135                 // Set parameter
136                 query.setParameter("activityUser", user); //NOI18N
137
138                 // Get list
139                 List<LogableUserActivity> list = query.getResultList();
140
141                 // Trace message
142                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUsersActivityLog: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N
143
144                 // Return it
145                 return list;
146         }
147
148         @Override
149         @SuppressWarnings ("unchecked")
150         public List<LogableUserActivity> fetchAllUsersActivityLogByMultipleType (final User user, final String[] activityTypes) {
151                 // Trace message
152                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUsersActivityLogByType: user={1},activityTypes={2} - CALLED!", this.getClass().getSimpleName(), user, Arrays.toString(activityTypes))); //NOI18N
153
154                 // Is user valid?
155                 if (null == user) {
156                         // Throw NPE
157                         throw new NullPointerException("user is null"); //NOI18N
158                 } else if (user.getUserId() == null) {
159                         // Throw again
160                         throw new NullPointerException("user.userId is null"); //NOI18N
161                 } else if (user.getUserId() < 1) {
162                         // Invalid id number
163                         throw new IllegalArgumentException(MessageFormat.format("user.userId{0} is not valid", user.getUserId())); //NOI18N
164                 } else if (null == activityTypes) {
165                         // Throw NPE again
166                         throw new NullPointerException("activityTypes is null"); //NOI18N
167                 } else if (activityTypes.length == 0) {
168                         // Should not be empty
169                         throw new IllegalArgumentException("activityTypes is empty"); //NOI18N
170                 }
171
172                 // Search for user's activity
173                 Query query = this.getEntityManager().createNamedQuery("FindUsersActivityByMultipleTypes", UserActivityLog.class); //NOI18N
174
175                 // Set parameters
176                 query.setParameter("activityUser", user); //NOI18N
177                 query.setParameter("activityTypes", Arrays.asList(activityTypes)); //NOI18N
178
179                 // Get list
180                 List<LogableUserActivity> list = query.getResultList();
181
182                 // Trace message
183                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUsersActivityLogByType: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N
184
185                 // Return it
186                 return list;
187         }
188
189         @Override
190         @SuppressWarnings ("unchecked")
191         public List<LogableUserActivity> fetchAllUsersActivityLogByType (final User user, final String activityType) {
192                 // Trace message
193                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUsersActivityLogByType: user={1},activityType={2} - CALLED!", this.getClass().getSimpleName(), user, activityType)); //NOI18N
194
195                 // Is user valid?
196                 if (null == user) {
197                         // Throw NPE
198                         throw new NullPointerException("user is null"); //NOI18N
199                 } else if (user.getUserId() == null) {
200                         // Throw again
201                         throw new NullPointerException("user.userId is null"); //NOI18N
202                 } else if (user.getUserId() < 1) {
203                         // Invalid id number
204                         throw new IllegalArgumentException(MessageFormat.format("user.userId{0} is not valid", user.getUserId())); //NOI18N
205                 } else if (null == activityType) {
206                         // Throw NPE again
207                         throw new NullPointerException("activityType is null"); //NOI18N
208                 } else if (activityType.isEmpty()) {
209                         // Should not be empty
210                         throw new IllegalArgumentException("activityType is empty"); //NOI18N
211                 }
212
213                 // Search for user's activity
214                 Query query = this.getEntityManager().createNamedQuery("FindUsersActivityByType", UserActivityLog.class); //NOI18N
215
216                 // Set parameters
217                 query.setParameter("activityUser", user); //NOI18N
218                 query.setParameter("activityType", activityType); //NOI18N
219
220                 // Get list
221                 List<LogableUserActivity> list = query.getResultList();
222
223                 // Trace message
224                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUsersActivityLogByType: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N
225
226                 // Return it
227                 return list;
228         }
229
230 }