]> git.mxchange.org Git - pizzaservice-mailer-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/activity/AddressbookUserActivityLogMessageBean.java
added EJBs for user-activity logging/reading
[pizzaservice-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         }
61
62         @Override
63         public void onMessage (final Message message) {
64                 // Trace message
65                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N
66
67                 // The parameter should be valid
68                 if (null == message) {
69                         // Throw NPE
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
74                 }
75
76                 // Securely cast it
77                 ObjectMessage objectMessage = (ObjectMessage) message;
78
79                 // Init variable
80                 Serializable serializable;
81
82                 try {
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);
88                         return;
89                 }
90
91                 // Debug message
92                 this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.onMessage: serializable={1}", this.getClass().getSimpleName(), serializable)); //NOI18N
93
94                 // Okay, is it the right interface?
95                 if (null == serializable) {
96                         // Throw NPE
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
101                 }
102
103                 // Securely cast it
104                 LogableUserActivity userActivity = (LogableUserActivity) serializable;
105
106                 // Should be valid
107                 if (null == userActivity) {
108                         // Throw NPE
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) {
114                         // Throw NPE again
115                         throw new NullPointerException("userActivity.activityUser is null"); //NOI18N
116                 } else if (userActivity.getActivityUser().getUserId() == null) {
117                         // Throw NPE again
118                         throw new NullPointerException("userActivity.activityUser.userId is null"); //NOI18N
119                 } else if (userActivity.getActivityUser().getUserId() < 1) {
120                         // Throw NPE again
121                         throw new IllegalArgumentException(MessageFormat.format("userActivity.activityUser.userId={0}  is not valid", userActivity.getActivityUser().getUserId())); //NOI18N
122                 } else if (userActivity.getActivityType() == null) {
123                         // Throw again ...
124                         throw new NullPointerException("userActivity.activityType is null"); //NOI18N
125                 } else if (userActivity.getActivityType().isEmpty()) {
126                         // Empty type
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) {
132                         // Throw NPE again
133                         throw new NullPointerException("userActivity.activityTimestamp is null"); //NOI18N
134                 }
135
136                 // Make user instance managed
137                 User managedUser = this.getManagedUser(userActivity.getActivityUser());
138
139                 // Set it back
140                 userActivity.setActivityUser(managedUser);
141
142                 // All fine, persist it
143                 this.getEntityManager().persist(userActivity);
144
145                 // Trace message
146                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N
147         }
148
149 }