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.List;
21 import javax.ejb.EJBException;
22 import javax.ejb.Stateless;
23 import javax.jms.JMSException;
24 import javax.jms.ObjectMessage;
25 import javax.persistence.Query;
26 import org.mxchange.jcoreee.bean.ejb.BaseEnterpriseBean;
27 import org.mxchange.jusercore.model.user.User;
30 * An EJB for user activity log. This class extends BaseEnterpriseBean and not
31 * project-specific "base class". The simple reason is that this class requires
32 * no email queue as no emails are ever being sent from this class.
34 * @author Roland Häder<roland@mxchange.org>
36 @Stateless (name = "userActivity", description = "A bean handling the user data")
37 public class JobsUserActivitySessionBean extends BaseEnterpriseBean implements UserActivityLogSessionBeanRemote {
42 private static final long serialVersionUID = 219_568_677_671_054L;
47 public JobsUserActivitySessionBean () {
48 // Call super constructor
49 super("jms/jjobs-queue-factory", "jms/jjobs-user-activity-log"); //NOI18N
53 public void addUserActivityLog (final LogableUserActivity userActivity) {
55 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUserActivityLog: userActivity={1} CALLED!", this.getClass().getSimpleName(), userActivity)); //NOI18N
58 if (null == userActivity) {
60 throw new NullPointerException("userActivity is null"); //NOI18N
61 } else if (userActivity.getActivityId() instanceof Long) {
62 // Id number should not be set
63 throw new IllegalArgumentException(MessageFormat.format("userActivity.activityId={0} should be null", userActivity.getActivityId())); //NOI18N
64 } else if (userActivity.getActivityUser() == null) {
66 throw new NullPointerException("userActivity.activityUser is null"); //NOI18N
67 } else if (userActivity.getActivityType() == null) {
69 throw new NullPointerException("userActivity.activityType is null"); //NOI18N
70 } else if (userActivity.getActivityType().isEmpty()) {
72 throw new NullPointerException("userActivity.activityType is empty"); //NOI18N
73 } else if ((userActivity.getActivityMessage() instanceof String) && (userActivity.getActivityMessage().isEmpty())) {
74 // Set but empty message
75 throw new NullPointerException("userActivity.activityMessage is empty"); //NOI18N
76 } else if (userActivity.getActivityTimestamp() == null) {
78 throw new NullPointerException("userActivity.activityTimestamp is null"); //NOI18N
82 // Send out email change
83 ObjectMessage message = this.getSession().createObjectMessage();
84 message.setObject(userActivity);
87 this.sendMessage(message);
88 } catch (final JMSException ex) {
90 throw new EJBException(ex);
95 @SuppressWarnings ("unchecked")
96 public List<LogableUserActivity> fetchAllUserActivityLog () {
98 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUserActivityLog: CALLED!", this.getClass().getSimpleName())); //NOI18N
100 // Search for user's activity
101 Query query = this.getEntityManager().createNamedQuery("AllUserActivityLog", UserActivityLog.class); //NOI18N
104 List<LogableUserActivity> list = query.getResultList();
107 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUserActivityLog: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N
114 @SuppressWarnings ("unchecked")
115 public List<LogableUserActivity> fetchAllUsersActivityLog (final User user) {
117 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUsersActivityLog: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
122 throw new NullPointerException("user is null"); //NOI18N
123 } else if (user.getUserId() == null) {
125 throw new NullPointerException("user.userId is null"); //NOI18N
126 } else if (user.getUserId() < 1) {
128 throw new IllegalArgumentException(MessageFormat.format("user.userId{0} is not valid", user.getUserId())); //NOI18N
131 // Search for user's activity
132 Query query = this.getEntityManager().createNamedQuery("FindAllUsersActivity", UserActivityLog.class); //NOI18N
135 query.setParameter("activityUser", user); //NOI18N
138 List<LogableUserActivity> list = query.getResultList();
141 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUsersActivityLog: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N