2 * Copyright (C) 2016, 2017 Roland Häder
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.jusercore.model.user.activity;
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.jcoreee.database.BaseDatabaseBean;
28 import org.mxchange.jusercore.model.user.User;
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.
35 * @author Roland Häder<roland@mxchange.org>
37 @Stateless (name = "userActivity", description = "A bean handling the user data")
38 public class JobsUserActivitySessionBean extends BaseDatabaseBean implements UserActivityLogSessionBeanRemote {
43 private static final long serialVersionUID = 219_568_677_671_054L;
48 public JobsUserActivitySessionBean () {
49 // Call super constructor
50 super("jms/jjobs-queue-factory", "jms/jjobs-user-activity-log"); //NOI18N
54 public void addUserActivityLog (final LogableUserActivity userActivity) {
56 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUserActivityLog: userActivity={1} CALLED!", this.getClass().getSimpleName(), userActivity)); //NOI18N
59 if (null == userActivity) {
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) {
67 throw new NullPointerException("userActivity.activityUser is null"); //NOI18N
68 } else if (userActivity.getActivityType() == null) {
70 throw new NullPointerException("userActivity.activityType is null"); //NOI18N
71 } else if (userActivity.getActivityType().isEmpty()) {
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) {
79 throw new NullPointerException("userActivity.activityTimestamp is null"); //NOI18N
83 // Send out email change
84 ObjectMessage message = this.getSession().createObjectMessage();
85 message.setObject(userActivity);
88 this.sendMessage(message);
89 } catch (final JMSException ex) {
91 throw new EJBException(ex);
96 @SuppressWarnings ("unchecked")
97 public List<LogableUserActivity> fetchAllUserActivityLog () {
99 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUserActivityLog: CALLED!", this.getClass().getSimpleName())); //NOI18N
101 // Search for user's activity
102 Query query = this.getEntityManager().createNamedQuery("AllUserActivityLog", UserActivityLog.class); //NOI18N
105 List<LogableUserActivity> list = query.getResultList();
108 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUserActivityLog: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N
115 @SuppressWarnings ("unchecked")
116 public List<LogableUserActivity> fetchAllUsersActivityLog (final User user) {
118 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUsersActivityLog: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
123 throw new NullPointerException("user is null"); //NOI18N
124 } else if (user.getUserId() == null) {
126 throw new NullPointerException("user.userId is null"); //NOI18N
127 } else if (user.getUserId() < 1) {
129 throw new IllegalArgumentException(MessageFormat.format("user.userId{0} is not valid", user.getUserId())); //NOI18N
132 // Search for user's activity
133 Query query = this.getEntityManager().createNamedQuery("FindAllUsersActivity", UserActivityLog.class); //NOI18N
136 query.setParameter("activityUser", user); //NOI18N
139 List<LogableUserActivity> list = query.getResultList();
142 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUsersActivityLog: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N
149 @SuppressWarnings ("unchecked")
150 public List<LogableUserActivity> fetchAllUsersActivityLogByMultipleType (final User user, final String[] activityTypes) {
152 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUsersActivityLogByType: user={1},activityTypes={2} - CALLED!", this.getClass().getSimpleName(), user, Arrays.toString(activityTypes))); //NOI18N
157 throw new NullPointerException("user is null"); //NOI18N
158 } else if (user.getUserId() == null) {
160 throw new NullPointerException("user.userId is null"); //NOI18N
161 } else if (user.getUserId() < 1) {
163 throw new IllegalArgumentException(MessageFormat.format("user.userId{0} is not valid", user.getUserId())); //NOI18N
164 } else if (null == activityTypes) {
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
172 // Search for user's activity
173 Query query = this.getEntityManager().createNamedQuery("FindUsersActivityByMultipleTypes", UserActivityLog.class); //NOI18N
176 query.setParameter("activityUser", user); //NOI18N
177 query.setParameter("activityTypes", Arrays.asList(activityTypes)); //NOI18N
180 List<LogableUserActivity> list = query.getResultList();
183 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUsersActivityLogByType: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N
190 @SuppressWarnings ("unchecked")
191 public List<LogableUserActivity> fetchAllUsersActivityLogByType (final User user, final String activityType) {
193 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUsersActivityLogByType: user={1},activityType={2} - CALLED!", this.getClass().getSimpleName(), user, activityType)); //NOI18N
198 throw new NullPointerException("user is null"); //NOI18N
199 } else if (user.getUserId() == null) {
201 throw new NullPointerException("user.userId is null"); //NOI18N
202 } else if (user.getUserId() < 1) {
204 throw new IllegalArgumentException(MessageFormat.format("user.userId{0} is not valid", user.getUserId())); //NOI18N
205 } else if (null == activityType) {
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
213 // Search for user's activity
214 Query query = this.getEntityManager().createNamedQuery("FindUsersActivityByType", UserActivityLog.class); //NOI18N
217 query.setParameter("activityUser", user); //NOI18N
218 query.setParameter("activityType", activityType); //NOI18N
221 List<LogableUserActivity> list = query.getResultList();
224 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUsersActivityLogByType: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N