From: Roland Häder Date: Tue, 6 Jun 2017 20:52:39 +0000 (+0200) Subject: added EJBs for user-activity logging/reading X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=522b722c4c827781a7f30aa68433f350be572c9d;p=jjobs-mailer-ejb.git added EJBs for user-activity logging/reading Signed-off-by: Roland Häder --- diff --git a/src/java/org/mxchange/jusercore/model/user/activity/AddressbookUserActivityLogMessageBean.java b/src/java/org/mxchange/jusercore/model/user/activity/AddressbookUserActivityLogMessageBean.java new file mode 100644 index 0000000..c15fec1 --- /dev/null +++ b/src/java/org/mxchange/jusercore/model/user/activity/AddressbookUserActivityLogMessageBean.java @@ -0,0 +1,149 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.jusercore.model.user.activity; + +import java.io.Serializable; +import java.text.MessageFormat; +import javax.ejb.ActivationConfigProperty; +import javax.ejb.MessageDriven; +import javax.jms.JMSException; +import javax.jms.Message; +import javax.jms.MessageListener; +import javax.jms.ObjectMessage; +import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean; +import org.mxchange.jusercore.model.user.User; + +/** + * A message-driven bean for adding user activity log + *

+ * @author Roland Häder + */ +@MessageDriven ( + name = "userActivityLog", + description = "A message-driven bean for adding user activity log", + activationConfig = { + @ActivationConfigProperty ( + propertyName = "destinationLookup", + propertyValue = "jms/addressbook-user-activity-log" + ), + @ActivationConfigProperty ( + propertyName = "destinationType", + propertyValue = "javax.jms.Queue" + ) + } +) +public class AddressbookUserActivityLogMessageBean extends BaseAddressbookDatabaseBean implements MessageListener { + + /** + * Serial number + */ + private static final long serialVersionUID = 14_920_686_785_732_054L; + + /** + * Default constructor + */ + public AddressbookUserActivityLogMessageBean () { + } + + @Override + public void onMessage (final Message message) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N + + // The parameter should be valid + if (null == message) { + // Throw NPE + throw new NullPointerException("message is null"); //NOI18N + } else if (!(message instanceof ObjectMessage)) { + // Not implementing right interface + throw new IllegalArgumentException(MessageFormat.format("message={0} does not implemented ObjectMessage", message)); //NOI18N + } + + // Securely cast it + ObjectMessage objectMessage = (ObjectMessage) message; + + // Init variable + Serializable serializable; + + try { + // Get object from message + serializable = objectMessage.getObject(); + } catch (final JMSException ex) { + // Log it and don't continue any further + this.getLoggerBeanLocal().logException(ex); + return; + } + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.onMessage: serializable={1}", this.getClass().getSimpleName(), serializable)); //NOI18N + + // Okay, is it the right interface? + if (null == serializable) { + // Throw NPE + throw new NullPointerException("serializable is null"); //NOI18N + } else if (!(serializable instanceof LogableUserActivity)) { + // Not correct object send + throw new IllegalArgumentException(MessageFormat.format("serializable={0} does not implement LogableUserActivity", serializable)); //NOI18N + } + + // Securely cast it + LogableUserActivity userActivity = (LogableUserActivity) serializable; + + // Should be valid + if (null == userActivity) { + // Throw NPE + throw new NullPointerException("userActivity is null"); //NOI18N + } else if (userActivity.getActivityId() instanceof Long) { + // Id number should not be set + throw new IllegalArgumentException(MessageFormat.format("userActivity.activityId={0} should be null", userActivity.getActivityId())); //NOI18N + } else if (userActivity.getActivityUser() == null) { + // Throw NPE again + throw new NullPointerException("userActivity.activityUser is null"); //NOI18N + } else if (userActivity.getActivityUser().getUserId() == null) { + // Throw NPE again + throw new NullPointerException("userActivity.activityUser.userId is null"); //NOI18N + } else if (userActivity.getActivityUser().getUserId() < 1) { + // Throw NPE again + throw new IllegalArgumentException(MessageFormat.format("userActivity.activityUser.userId={0} is not valid", userActivity.getActivityUser().getUserId())); //NOI18N + } else if (userActivity.getActivityType() == null) { + // Throw again ... + throw new NullPointerException("userActivity.activityType is null"); //NOI18N + } else if (userActivity.getActivityType().isEmpty()) { + // Empty type + throw new IllegalArgumentException("userActivity.activityType is empty"); //NOI18N + } else if ((userActivity.getActivityMessage() instanceof String) && (userActivity.getActivityMessage().isEmpty())) { + // Set but empty message + throw new IllegalArgumentException("userActivity.activityMessage is empty"); //NOI18N + } else if (userActivity.getActivityTimestamp() == null) { + // Throw NPE again + throw new NullPointerException("userActivity.activityTimestamp is null"); //NOI18N + } + + // Make user instance managed + User managedUser = this.getManagedUser(userActivity.getActivityUser()); + + // Set it back + userActivity.setActivityUser(managedUser); + + // All fine, persist it + this.getEntityManager().persist(userActivity); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N + } + +} diff --git a/src/java/org/mxchange/jusercore/model/user/activity/AddressbookUserActivitySessionBean.java b/src/java/org/mxchange/jusercore/model/user/activity/AddressbookUserActivitySessionBean.java new file mode 100644 index 0000000..3198d43 --- /dev/null +++ b/src/java/org/mxchange/jusercore/model/user/activity/AddressbookUserActivitySessionBean.java @@ -0,0 +1,230 @@ +/* + * Copyright (C) 2016, 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package org.mxchange.jusercore.model.user.activity; + +import java.text.MessageFormat; +import java.util.Arrays; +import java.util.List; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.jms.JMSException; +import javax.jms.ObjectMessage; +import javax.persistence.Query; +import org.mxchange.jcoreee.database.BaseDatabaseBean; +import org.mxchange.jusercore.model.user.User; + +/** + * An EJB for user activity log. This class extends BaseDatabaseBean and not + * project-specific "base class". The simple reason is that this class requires + * no email queue as no emails are ever being sent from this class. + *

+ * @author Roland Häder + */ +@Stateless (name = "userActivity", description = "A bean handling the user data") +public class AddressbookUserActivitySessionBean extends BaseDatabaseBean implements UserActivityLogSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 219_568_677_671_054L; + + /** + * Default constructor + */ + public AddressbookUserActivitySessionBean () { + // Call super constructor + super("jms/jjobs-queue-factory", "jms/jjobs-user-activity-log"); //NOI18N + } + + @Override + public void addUserActivityLog (final LogableUserActivity userActivity) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUserActivityLog: userActivity={1} CALLED!", this.getClass().getSimpleName(), userActivity)); //NOI18N + + // Should be valid + if (null == userActivity) { + // Throw NPE + throw new NullPointerException("userActivity is null"); //NOI18N + } else if (userActivity.getActivityId() instanceof Long) { + // Id number should not be set + throw new IllegalArgumentException(MessageFormat.format("userActivity.activityId={0} should be null", userActivity.getActivityId())); //NOI18N + } else if (userActivity.getActivityUser() == null) { + // Throw NPE again + throw new NullPointerException("userActivity.activityUser is null"); //NOI18N + } else if (userActivity.getActivityType() == null) { + // Throw again ... + throw new NullPointerException("userActivity.activityType is null"); //NOI18N + } else if (userActivity.getActivityType().isEmpty()) { + // Empty type + throw new NullPointerException("userActivity.activityType is empty"); //NOI18N + } else if ((userActivity.getActivityMessage() instanceof String) && (userActivity.getActivityMessage().isEmpty())) { + // Set but empty message + throw new NullPointerException("userActivity.activityMessage is empty"); //NOI18N + } else if (userActivity.getActivityTimestamp() == null) { + // Throw NPE again + throw new NullPointerException("userActivity.activityTimestamp is null"); //NOI18N + } + + try { + // Send out email change + ObjectMessage message = this.getSession().createObjectMessage(); + message.setObject(userActivity); + + // Send message + this.sendMessage(message); + } catch (final JMSException ex) { + // Throw again + throw new EJBException(ex); + } + } + + @Override + @SuppressWarnings ("unchecked") + public List fetchAllUserActivityLog () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUserActivityLog: CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Search for user's activity + Query query = this.getEntityManager().createNamedQuery("AllUserActivityLog", UserActivityLog.class); //NOI18N + + // Get list + List list = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUserActivityLog: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N + + // Return it + return list; + } + + @Override + @SuppressWarnings ("unchecked") + public List fetchAllUsersActivityLog (final User user) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUsersActivityLog: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N + + // Is user valid? + if (null == user) { + // Throw NPE + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Throw again + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Invalid id number + throw new IllegalArgumentException(MessageFormat.format("user.userId{0} is not valid", user.getUserId())); //NOI18N + } + + // Search for user's activity + Query query = this.getEntityManager().createNamedQuery("FindAllUsersActivity", UserActivityLog.class); //NOI18N + + // Set parameter + query.setParameter("activityUser", user); //NOI18N + + // Get list + List list = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUsersActivityLog: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N + + // Return it + return list; + } + + @Override + @SuppressWarnings ("unchecked") + public List fetchAllUsersActivityLogByMultipleType (final User user, final String[] activityTypes) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUsersActivityLogByType: user={1},activityTypes={2} - CALLED!", this.getClass().getSimpleName(), user, activityTypes)); //NOI18N + + // Is user valid? + if (null == user) { + // Throw NPE + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Throw again + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Invalid id number + throw new IllegalArgumentException(MessageFormat.format("user.userId{0} is not valid", user.getUserId())); //NOI18N + } else if (null == activityTypes) { + // Throw NPE again + throw new NullPointerException("activityTypes is null"); //NOI18N + } else if (activityTypes.length == 0) { + // Should not be empty + throw new IllegalArgumentException("activityTypes is empty"); //NOI18N + } + + // Search for user's activity + Query query = this.getEntityManager().createNamedQuery("FindUsersActivityByMultipleTypes", UserActivityLog.class); //NOI18N + + // Set parameters + query.setParameter("activityUser", user); //NOI18N + query.setParameter("activityTypes", Arrays.asList(activityTypes)); //NOI18N + + // Get list + List list = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUsersActivityLogByType: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N + + // Return it + return list; + } + + @Override + @SuppressWarnings ("unchecked") + public List fetchAllUsersActivityLogByType (final User user, final String activityType) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUsersActivityLogByType: user={1},activityType={2} - CALLED!", this.getClass().getSimpleName(), user, activityType)); //NOI18N + + // Is user valid? + if (null == user) { + // Throw NPE + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Throw again + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Invalid id number + throw new IllegalArgumentException(MessageFormat.format("user.userId{0} is not valid", user.getUserId())); //NOI18N + } else if (null == activityType) { + // Throw NPE again + throw new NullPointerException("activityType is null"); //NOI18N + } else if (activityType.isEmpty()) { + // Should not be empty + throw new IllegalArgumentException("activityType is empty"); //NOI18N + } + + // Search for user's activity + Query query = this.getEntityManager().createNamedQuery("FindUsersActivityByType", UserActivityLog.class); //NOI18N + + // Set parameters + query.setParameter("activityUser", user); //NOI18N + query.setParameter("activityType", activityType); //NOI18N + + // Get list + List list = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fetchAllUsersActivityLogByType: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N + + // Return it + return list; + } + +}