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.io.Serializable;
20 import java.text.MessageFormat;
21 import javax.ejb.ActivationConfigProperty;
22 import javax.ejb.MessageDriven;
23 import javax.jms.JMSException;
24 import javax.jms.Message;
25 import javax.jms.MessageListener;
26 import javax.jms.ObjectMessage;
27 import org.mxchange.jjobs.database.BaseJobsDatabaseBean;
28 import org.mxchange.jusercore.model.user.User;
31 * A message-driven bean for adding user activity log
33 * @author Roland Häder<roland@mxchange.org>
36 name = "userActivityLog",
37 description = "A message-driven bean for adding user activity log",
39 @ActivationConfigProperty (
40 propertyName = "destinationLookup",
41 propertyValue = "jms/jjobs-user-activity-log"
43 @ActivationConfigProperty (
44 propertyName = "destinationType",
45 propertyValue = "javax.jms.Queue"
49 public class JobsUserActivityLogMessageBean extends BaseJobsDatabaseBean implements MessageListener {
54 private static final long serialVersionUID = 14_920_686_785_732_054L;
59 public JobsUserActivityLogMessageBean () {
60 // Call super constructor
65 public void onMessage (final Message message) {
67 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
69 // The parameter should be valid
70 if (null == message) {
72 throw new NullPointerException("message is null"); //NOI18N
73 } else if (!(message instanceof ObjectMessage)) {
74 // Not implementing right interface
75 throw new IllegalArgumentException(MessageFormat.format("message={0} does not implemented ObjectMessage", message)); //NOI18N
79 ObjectMessage objectMessage = (ObjectMessage) message;
82 Serializable serializable;
85 // Get object from message
86 serializable = objectMessage.getObject();
87 } catch (final JMSException ex) {
88 // Log it and don't continue any further
89 this.getLoggerBeanLocal().logException(ex);
94 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.onMessage: serializable={1}", this.getClass().getSimpleName(), serializable)); //NOI18N
96 // Okay, is it the right interface?
97 if (null == serializable) {
99 throw new NullPointerException("serializable is null"); //NOI18N
100 } else if (!(serializable instanceof LogableUserActivity)) {
101 // Not correct object send
102 throw new IllegalArgumentException(MessageFormat.format("serializable={0} does not implement LogableUserActivity", serializable)); //NOI18N
106 LogableUserActivity userActivity = (LogableUserActivity) serializable;
109 if (null == userActivity) {
111 throw new NullPointerException("userActivity is null"); //NOI18N
112 } else if (userActivity.getActivityId() instanceof Long) {
113 // Id number should not be set
114 throw new IllegalArgumentException(MessageFormat.format("userActivity.activityId={0} should be null", userActivity.getActivityId())); //NOI18N
115 } else if (userActivity.getActivityUser() == null) {
117 throw new NullPointerException("userActivity.activityUser is null"); //NOI18N
118 } else if (userActivity.getActivityUser().getUserId() == null) {
120 throw new NullPointerException("userActivity.activityUser.userId is null"); //NOI18N
121 } else if (userActivity.getActivityUser().getUserId() < 1) {
123 throw new IllegalArgumentException(MessageFormat.format("userActivity.activityUser.userId={0} is not valid", userActivity.getActivityUser().getUserId())); //NOI18N
124 } else if (userActivity.getActivityType() == null) {
126 throw new NullPointerException("userActivity.activityType is null"); //NOI18N
127 } else if (userActivity.getActivityType().isEmpty()) {
129 throw new IllegalArgumentException("userActivity.activityType is empty"); //NOI18N
130 } else if ((userActivity.getActivityMessage() instanceof String) && (userActivity.getActivityMessage().isEmpty())) {
131 // Set but empty message
132 throw new IllegalArgumentException("userActivity.activityMessage is empty"); //NOI18N
133 } else if (userActivity.getActivityTimestamp() == null) {
135 throw new NullPointerException("userActivity.activityTimestamp is null"); //NOI18N
138 // Make user instance managed
139 User managedUser = this.getManagedUser(userActivity.getActivityUser());
142 userActivity.setActivityUser(managedUser);
144 // All fine, persist it
145 this.getEntityManager().persist(userActivity);
148 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N