]> git.mxchange.org Git - jjobs-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/activity/JobsUserActivitySessionBean.java
Updated copyright year
[jjobs-ejb.git] / src / java / org / mxchange / jusercore / model / user / activity / JobsUserActivitySessionBean.java
1 /*
2  * Copyright (C) 2016 - 2024 Free Software Foundation
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.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;
28
29 /**
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.
33  * <p>
34  * @author Roland Häder<roland@mxchange.org>
35  */
36 @Stateless (name = "userActivity", description = "A bean handling the user data")
37 public class JobsUserActivitySessionBean extends BaseEnterpriseBean implements UserActivityLogSessionBeanRemote {
38
39         /**
40          * Serial number
41          */
42         private static final long serialVersionUID = 219_568_677_671_054L;
43
44         /**
45          * Default constructor
46          */
47         public JobsUserActivitySessionBean () {
48                 // Call super constructor
49                 super("jms/jjobs-queue-factory", "jms/jjobs-user-activity-log"); //NOI18N
50         }
51
52         @Override
53         public void addUserActivityLog (final LogableUserActivity userActivity) {
54                 // Trace message
55                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUserActivityLog: userActivity={1} CALLED!", this.getClass().getSimpleName(), userActivity)); //NOI18N
56
57                 // Should be valid
58                 if (null == userActivity) {
59                         // Throw NPE
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) {
65                         // Throw NPE again
66                         throw new NullPointerException("userActivity.activityUser is null"); //NOI18N
67                 } else if (userActivity.getActivityType() == null) {
68                         // Throw again ...
69                         throw new NullPointerException("userActivity.activityType is null"); //NOI18N
70                 } else if (userActivity.getActivityType().isEmpty()) {
71                         // Empty type
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) {
77                         // Throw NPE again
78                         throw new NullPointerException("userActivity.activityTimestamp is null"); //NOI18N
79                 }
80
81                 try {
82                         // Send out email change
83                         ObjectMessage message = this.getSession().createObjectMessage();
84                         message.setObject(userActivity);
85
86                         // Send message
87                         this.sendMessage(message);
88                 } catch (final JMSException ex) {
89                         // Throw again
90                         throw new EJBException(ex);
91                 }
92         }
93
94         @Override
95         @SuppressWarnings ("unchecked")
96         public List<LogableUserActivity> fetchAllUserActivityLog () {
97                 // Trace message
98                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUserActivityLog: CALLED!", this.getClass().getSimpleName())); //NOI18N
99
100                 // Search for user's activity
101                 Query query = this.getEntityManager().createNamedQuery("AllUserActivityLog", UserActivityLog.class); //NOI18N
102
103                 // Get list
104                 List<LogableUserActivity> list = query.getResultList();
105
106                 // Trace message
107                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUserActivityLog: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N
108
109                 // Return it
110                 return list;
111         }
112
113         @Override
114         @SuppressWarnings ("unchecked")
115         public List<LogableUserActivity> fetchAllUsersActivityLog (final User user) {
116                 // Trace message
117                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUsersActivityLog: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
118
119                 // Is user valid?
120                 if (null == user) {
121                         // Throw NPE
122                         throw new NullPointerException("user is null"); //NOI18N
123                 } else if (user.getUserId() == null) {
124                         // Throw again
125                         throw new NullPointerException("user.userId is null"); //NOI18N
126                 } else if (user.getUserId() < 1) {
127                         // Invalid id number
128                         throw new IllegalArgumentException(MessageFormat.format("user.userId{0} is not valid", user.getUserId())); //NOI18N
129                 }
130
131                 // Search for user's activity
132                 Query query = this.getEntityManager().createNamedQuery("FindAllUsersActivity", UserActivityLog.class); //NOI18N
133
134                 // Set parameter
135                 query.setParameter("activityUser", user); //NOI18N
136
137                 // Get list
138                 List<LogableUserActivity> list = query.getResultList();
139
140                 // Trace message
141                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUsersActivityLog: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N
142
143                 // Return it
144                 return list;
145         }
146
147 }