]> git.mxchange.org Git - addressbook-mailer-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/activity/AddressbookUserActivityLogMessageBean.java
4d8b70e29ee533c65317078bbb280a0658df24ae
[addressbook-mailer-ejb.git] / src / java / org / mxchange / jusercore / model / user / activity / AddressbookUserActivityLogMessageBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
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.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.addressbook.database.BaseAddressbookDatabaseBean;
28 import org.mxchange.jusercore.model.user.User;
29
30 /**
31  * A message-driven bean for adding user activity log
32  * <p>
33  * @author Roland Häder<roland@mxchange.org>
34  */
35 @MessageDriven (
36                 name = "userActivityLog",
37                 description = "A message-driven bean for adding user activity log",
38                 activationConfig = {
39                         @ActivationConfigProperty (
40                                         propertyName = "destinationLookup",
41                                         propertyValue = "jms/addressbook-user-activity-log"
42                         ),
43                         @ActivationConfigProperty (
44                                         propertyName = "destinationType",
45                                         propertyValue = "javax.jms.Queue"
46                         )
47                 }
48 )
49 public class AddressbookUserActivityLogMessageBean extends BaseAddressbookDatabaseBean implements MessageListener {
50
51         /**
52          * Serial number
53          */
54         private static final long serialVersionUID = 14_920_686_785_732_054L;
55
56         /**
57          * Default constructor
58          */
59         public AddressbookUserActivityLogMessageBean () {
60                 // Call super constructor
61                 super();
62         }
63
64         @Override
65         public void onMessage (final Message message) {
66                 // Trace message
67                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
68
69                 // The parameter should be valid
70                 if (null == message) {
71                         // Throw NPE
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
76                 }
77
78                 // Securely cast it
79                 ObjectMessage objectMessage = (ObjectMessage) message;
80
81                 // Init variable
82                 Serializable serializable;
83
84                 try {
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);
90                         return;
91                 }
92
93                 // Debug message
94                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.onMessage: serializable={1}", this.getClass().getSimpleName(), serializable)); //NOI18N
95
96                 // Okay, is it the right interface?
97                 if (null == serializable) {
98                         // Throw NPE
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
103                 }
104
105                 // Securely cast it
106                 LogableUserActivity userActivity = (LogableUserActivity) serializable;
107
108                 // Should be valid
109                 if (null == userActivity) {
110                         // Throw NPE
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) {
116                         // Throw NPE again
117                         throw new NullPointerException("userActivity.activityUser is null"); //NOI18N
118                 } else if (userActivity.getActivityUser().getUserId() == null) {
119                         // Throw NPE again
120                         throw new NullPointerException("userActivity.activityUser.userId is null"); //NOI18N
121                 } else if (userActivity.getActivityUser().getUserId() < 1) {
122                         // Throw NPE again
123                         throw new IllegalArgumentException(MessageFormat.format("userActivity.activityUser.userId={0}  is not valid", userActivity.getActivityUser().getUserId())); //NOI18N
124                 } else if (userActivity.getActivityType() == null) {
125                         // Throw again ...
126                         throw new NullPointerException("userActivity.activityType is null"); //NOI18N
127                 } else if (userActivity.getActivityType().isEmpty()) {
128                         // Empty type
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) {
134                         // Throw NPE again
135                         throw new NullPointerException("userActivity.activityTimestamp is null"); //NOI18N
136                 }
137
138                 // Make user instance managed
139                 User managedUser = this.getManagedUser(userActivity.getActivityUser());
140
141                 // Set it back
142                 userActivity.setActivityUser(managedUser);
143
144                 // All fine, persist it
145                 this.getEntityManager().persist(userActivity);
146
147                 // Trace message
148                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N
149         }
150
151 }