2 * Copyright (C) 2016 Cho-Time GmbH
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 Haeder<rhaeder@cho-time.de>
36 name = "userActivityLog",
37 description = "A message-driven bean for adding user activity log",
39 @ActivationConfigProperty (
40 propertyName = "destinationLookup",
41 propertyValue = "jms/jlanding-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 () {
63 public void onMessage (final Message message) {
65 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
67 // The parameter should be valid
68 if (null == message) {
70 throw new NullPointerException("message is null"); //NOI18N
71 } else if (!(message instanceof ObjectMessage)) {
72 // Not implementing right interface
73 throw new IllegalArgumentException(MessageFormat.format("message={0} does not implemented ObjectMessage", message)); //NOI18N
77 ObjectMessage objectMessage = (ObjectMessage) message;
80 Serializable serializable;
83 // Get object from message
84 serializable = objectMessage.getObject();
85 } catch (final JMSException ex) {
86 // Log it and don't continue any further
87 this.getLoggerBeanLocal().logException(ex);
92 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.onMessage: serializable={1}", this.getClass().getSimpleName(), serializable)); //NOI18N
94 // Okay, is it the right interface?
95 if (null == serializable) {
97 throw new NullPointerException("serializable is null"); //NOI18N
98 } else if (!(serializable instanceof LogableUserActivity)) {
99 // Not correct object send
100 throw new IllegalArgumentException(MessageFormat.format("serializable={0} does not implement LogableUserActivity", serializable)); //NOI18N
104 LogableUserActivity userActivity = (LogableUserActivity) serializable;
107 if (null == userActivity) {
109 throw new NullPointerException("userActivity is null"); //NOI18N
110 } else if (userActivity.getActivityId() instanceof Long) {
111 // Id number should not be set
112 throw new IllegalArgumentException(MessageFormat.format("userActivity.activityId={0} should be null", userActivity.getActivityId())); //NOI18N
113 } else if (userActivity.getActivityUser() == null) {
115 throw new NullPointerException("userActivity.activityUser is null"); //NOI18N
116 } else if (userActivity.getActivityUser().getUserId() == null) {
118 throw new NullPointerException("userActivity.activityUser.userId is null"); //NOI18N
119 } else if (userActivity.getActivityUser().getUserId() < 1) {
121 throw new IllegalArgumentException(MessageFormat.format("userActivity.activityUser.userId={0} is not valid", userActivity.getActivityUser().getUserId())); //NOI18N
122 } else if (userActivity.getActivityType() == null) {
124 throw new NullPointerException("userActivity.activityType is null"); //NOI18N
125 } else if (userActivity.getActivityType().isEmpty()) {
127 throw new IllegalArgumentException("userActivity.activityType is empty"); //NOI18N
128 } else if ((userActivity.getActivityMessage() instanceof String) && (userActivity.getActivityMessage().isEmpty())) {
129 // Set but empty message
130 throw new IllegalArgumentException("userActivity.activityMessage is empty"); //NOI18N
131 } else if (userActivity.getActivityTimestamp() == null) {
133 throw new NullPointerException("userActivity.activityTimestamp is null"); //NOI18N
136 // Make user instance managed
137 User managedUser = this.getManagedUser(userActivity.getActivityUser());
140 userActivity.setActivityUser(managedUser);
142 // All fine, persist it
143 this.getEntityManager().persist(userActivity);
146 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N