From: Roland Haeder <roland@mxchange.org> Date: Thu, 13 Apr 2017 12:58:21 +0000 (+0200) Subject: renamed namespace to jfinancials (Java naming-convention) + renamed *Addressbook... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=e638bc6589517ff5c0285aa6b171f7670ee8bf78;p=jfinancials-ejb.git renamed namespace to jfinancials (Java naming-convention) + renamed *Addressbook* to *Financials* to not have confusions in IDEs like Netbeans Signed-off-by: Roland Häder <roland@mxchange.org> --- diff --git a/lib/addressbook-core.jar b/lib/addressbook-core.jar deleted file mode 100644 index 36c8f87..0000000 Binary files a/lib/addressbook-core.jar and /dev/null differ diff --git a/lib/addressbook-mailer.jar b/lib/addressbook-mailer.jar deleted file mode 100644 index 4c2cc0e..0000000 Binary files a/lib/addressbook-mailer.jar and /dev/null differ diff --git a/lib/jfinancials-core.jar b/lib/jfinancials-core.jar new file mode 100644 index 0000000..c7773ed Binary files /dev/null and b/lib/jfinancials-core.jar differ diff --git a/lib/jfinancials-mailer.jar b/lib/jfinancials-mailer.jar new file mode 100644 index 0000000..dde8ae6 Binary files /dev/null and b/lib/jfinancials-mailer.jar differ diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml index b88ad99..af1a18f 100644 --- a/nbproject/build-impl.xml +++ b/nbproject/build-impl.xml @@ -871,7 +871,7 @@ exists or setup the property manually. For example like this: <basename file="${reference.jfinancials-mailer.jar}" property="manifest.reference.jfinancials-mailer.jar"/> <basename file="${file.reference.cdi-api.jar}" property="manifest.file.reference.cdi-api.jar"/> <manifest file="${build.ear.classes.dir}/META-INF/MANIFEST.MF" mode="update"> - <attribute name="Extension-List" value="jar-1 jar-2 jar-3 jar-4 jar-5 jar-6 jar-7 jar-8 jar-9 jar-10 jar-11 jar-12 jar-13 jar-14 jar-15 jar-16 jar-17 jar-18 "/> + <attribute name="Extension-List" value="jar-1 jar-2 jar-3 jar-4 jar-5 jar-6 jar-7 jar-8 jar-9 jar-10 jar-11 jar-12 jar-13 jar-14 jar-15 "/> <attribute name="jar-1-Extension-Name" value="${manifest.file.reference.jcoreee.jar}"/> <attribute name="jar-2-Extension-Name" value="${manifest.file.reference.jcore-logger-lib.jar}"/> <attribute name="jar-3-Extension-Name" value="${manifest.file.reference.jcountry-core.jar}"/> @@ -880,13 +880,13 @@ exists or setup the property manually. For example like this: <attribute name="jar-6-Extension-Name" value="${manifest.file.reference.jphone-lib.jar}"/> <attribute name="jar-7-Extension-Name" value="${manifest.file.reference.jcontacts-core.jar}"/> <attribute name="jar-8-Extension-Name" value="${manifest.file.reference.jcontacts-lib.jar}"/> - <attribute name="jar-10-Extension-Name" value="${manifest.file.reference.juser-core.jar}"/> - <attribute name="jar-11-Extension-Name" value="${manifest.file.reference.juser-lib.jar}"/> - <attribute name="jar-14-Extension-Name" value="${manifest.file.reference.jmailer-ee.jar}"/> - <attribute name="jar-15-Extension-Name" value="${manifest.file.reference.jfinancials-core.jar}"/> - <attribute name="jar-16-Extension-Name" value="${manifest.reference.jfinancials-lib.jar}"/> - <attribute name="jar-17-Extension-Name" value="${manifest.reference.jfinancials-mailer.jar}"/> - <attribute name="jar-18-Extension-Name" value="${manifest.file.reference.cdi-api.jar}"/> + <attribute name="jar-9-Extension-Name" value="${manifest.file.reference.juser-core.jar}"/> + <attribute name="jar-10-Extension-Name" value="${manifest.file.reference.juser-lib.jar}"/> + <attribute name="jar-11-Extension-Name" value="${manifest.file.reference.jmailer-ee.jar}"/> + <attribute name="jar-12-Extension-Name" value="${manifest.file.reference.jfinancials-core.jar}"/> + <attribute name="jar-13-Extension-Name" value="${manifest.reference.jfinancials-lib.jar}"/> + <attribute name="jar-14-Extension-Name" value="${manifest.reference.jfinancials-mailer.jar}"/> + <attribute name="jar-15-Extension-Name" value="${manifest.file.reference.cdi-api.jar}"/> </manifest> </target> <target depends="compile" name="library-inclusion-in-manifest"> diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties index 765a886..70d335f 100644 --- a/nbproject/genfiles.properties +++ b/nbproject/genfiles.properties @@ -3,6 +3,6 @@ build.xml.script.CRC32=7d41e0fd build.xml.stylesheet.CRC32=5910fda3@1.51.1 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. -nbproject/build-impl.xml.data.CRC32=a1065659 -nbproject/build-impl.xml.script.CRC32=a95e8592 +nbproject/build-impl.xml.data.CRC32=0e5e9614 +nbproject/build-impl.xml.script.CRC32=5b56e085 nbproject/build-impl.xml.stylesheet.CRC32=6096d939@1.55.1 diff --git a/src/java/org/mxchange/addressbook/beans/resendlink/AddressbookResendLinkSessionBean.java b/src/java/org/mxchange/addressbook/beans/resendlink/AddressbookResendLinkSessionBean.java deleted file mode 100644 index f08bbf5..0000000 --- a/src/java/org/mxchange/addressbook/beans/resendlink/AddressbookResendLinkSessionBean.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. - */ -package org.mxchange.addressbook.beans.resendlink; - -import java.text.MessageFormat; -import java.util.Locale; -import javax.ejb.EJBException; -import javax.ejb.Stateless; -import javax.mail.Address; -import javax.mail.internet.AddressException; -import javax.mail.internet.InternetAddress; -import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean; -import org.mxchange.jusercore.model.user.User; -import org.mxchange.jusercore.model.user.status.UserAccountStatus; - -/** - * A session-based EJB for resending confirmation links - * <p> - * @author Roland Häder<roland@mxchange.org> - */ -@Stateless (name = "resendLink", description = "A bean resending confirmation links") -public class AddressbookResendLinkSessionBean extends BaseAddressbookDatabaseBean implements ResendLinkSessionBeanRemote { - - /** - * Serial number - */ - private static final long serialVersionUID = 71_546_726_857_195_360L; - - @Override - public void resendConfirmationLink (final User user, final Locale locale, final String baseUrl) { - // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.resendConfirmationLink: user={1},locale={2},baseUrl={3} - CALLED!", this.getClass().getSimpleName(), user, locale, baseUrl)); //NOI18N - - // The user instance should be valid - if (null == user) { - // Throw NPE - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() == null) { - // Throw NPE 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 (user.getUserConfirmKey() == null) { - // Throw NPE again - throw new NullPointerException("this.userConfirmKey is null"); //NOI18N - } else if (user.getUserAccountStatus() != UserAccountStatus.UNCONFIRMED) { - // User account status is not UNCONFIRMED - throw new IllegalStateException(MessageFormat.format("Account status from user.userId={0} is not UNCONFIRMED:{1}", user.getUserId(), user.getUserAccountStatus())); //NOI18N - } else if (null == locale) { - // Locale should be set - throw new NullPointerException("locale is null"); //NOI18N - } - - // @TODO Unfinished: Change key + merge database - - // Init variable - Address emailAddress; - - try { - // Create email address and set - emailAddress = new InternetAddress(user.getUserContact().getContactEmailAddress()); - } catch (final AddressException ex) { - // Throw again - throw new EJBException(ex); - } - - // Send email - // TODO: Internationlize the subject line somehow - this.sendEmail("Resend confirmation link", "resend_confirmation_link", emailAddress, user, baseUrl); //NOI18N - - // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.resendConfirmationLink: EXIT!", this.getClass().getSimpleName())); //NOI18N - } - -} diff --git a/src/java/org/mxchange/addressbook/mailer/model/delivery/AddressbookEmailDeliveryMessageBean.java b/src/java/org/mxchange/addressbook/mailer/model/delivery/AddressbookEmailDeliveryMessageBean.java deleted file mode 100644 index ea99d16..0000000 --- a/src/java/org/mxchange/addressbook/mailer/model/delivery/AddressbookEmailDeliveryMessageBean.java +++ /dev/null @@ -1,188 +0,0 @@ -/* - * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. - */ -package org.mxchange.addressbook.mailer.model.delivery; - -import java.io.Serializable; -import java.text.MessageFormat; -import java.util.Properties; -import java.util.ResourceBundle; -import javax.annotation.PostConstruct; -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 javax.mail.MessagingException; -import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean; -import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery; - -/** - * A message-driven bean for sending out emails - * <p> - * @author Roland Häder<roland@mxchange.org> - */ -@MessageDriven (activationConfig = { - @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/addressbook-email-queue"), - @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue") -}) -public class AddressbookEmailDeliveryMessageBean extends BaseAddressbookDatabaseBean implements MessageListener { - - /** - * Serial number - */ - private static final long serialVersionUID = 75_638_176_619_024L; - - /** - * Configuration file - */ - private final String configFile = "org.mxchange.jmailer.config"; //NOI18N//NOI18N - - /** - * Mailer instance - */ - private final DeliverableAddressbookEmail mailer; - - /** - * Default constructor - */ - public AddressbookEmailDeliveryMessageBean () { - // Init mailer instance - this.mailer = new AddressbookMailer(); - } - - /** - * Post-construction - */ - @PostConstruct - public void init () { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: CALLED!", this.getClass().getSimpleName())); //NOI18N - - // Try to load bundle - ResourceBundle bundle = ResourceBundle.getBundle(this.configFile); - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: bundle={1}", this.getClass().getSimpleName(), bundle)); //NOI18N - - // The bunble should be valid - if (null == bundle) { - // Throw NPE - throw new NullPointerException(MessageFormat.format("bundle is null, maybe file {0} does not exist?", this.configFile)); //NOI18N - } - - // Init Properties - Properties properties = new Properties(); - - // Is the bundle not empty? - if (!bundle.keySet().isEmpty()) { - // Loop through all - for (final String key : bundle.keySet()) { - // Log debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: key={1}", this.getClass().getSimpleName(), key)); //NOI18N - - // Get string from bundle and set it in properties - properties.put(key, bundle.getString(key)); - } - } - - // Handle it over to the mailer - this.mailer.init(properties); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: EXIT!", this.getClass().getSimpleName())); //NOI18N - } - - @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 WrapableEmailDelivery)) { - // Not correct object send - throw new IllegalArgumentException(MessageFormat.format("serializable={0} does not implement WrapableEmailDelivery", serializable)); //NOI18N - } - - // Securely cast it - WrapableEmailDelivery wrapper = (WrapableEmailDelivery) serializable; - - // Is all required set? - if (wrapper.getLocale() == null) { - // Throw NPE - throw new NullPointerException("wrapper.locale is null"); //NOI18N - } else if (wrapper.getRecipient() == null) { - // Throw again ... - throw new NullPointerException("wrapper.recipient is null"); //NOI18N - } else if (wrapper.getSubjectLine() == null) { - // ... and again - throw new NullPointerException("wrapper.subjectLine is null"); //NOI18N - } else if (wrapper.getSubjectLine().isEmpty()) { - // Is empty - throw new IllegalArgumentException("wrapper.subjectLine is empty"); //NOI18N - } else if (wrapper.getTemplateName() == null) { - // Throw NPE again - throw new NullPointerException("wrapper.templateName is null"); //NOI18N - } else if (wrapper.getTemplateName().isEmpty()) { - // Is empty - throw new IllegalArgumentException("wrapper.templateName is empty"); //NOI18N - } - - try { - // Send email out - this.mailer.sendDeliverableMail(wrapper); - } catch (final MessagingException ex) { - // Opps, something went wrong - this.getLoggerBeanLocal().logException(ex); - return; - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage - EXIT!", this.getClass().getSimpleName())); //NOI18N - } - -} diff --git a/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java b/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java deleted file mode 100644 index f1c6b98..0000000 --- a/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java +++ /dev/null @@ -1,405 +0,0 @@ -/* - * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. - */ -package org.mxchange.addressbook.model.addressbook; - -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.GregorianCalendar; -import java.util.LinkedList; -import java.util.List; -import javax.ejb.Stateless; -import javax.persistence.NoResultException; -import javax.persistence.Query; -import org.mxchange.jaddressbookcore.exceptions.AddressbookNameAlreadyUsedException; -import org.mxchange.jaddressbookcore.exceptions.AddressbookNotFoundException; -import org.mxchange.jaddressbookcore.model.addressbook.Addressbook; -import org.mxchange.jaddressbookcore.model.addressbook.UserAddressbook; -import org.mxchange.jaddressbookcore.model.addressbook.entry.AddressbookEntry; -import org.mxchange.jaddressbookcore.model.addressbook.shared.ShareableAddressbook; -import org.mxchange.jcoreee.database.BaseDatabaseBean; -import org.mxchange.jusercore.model.user.User; - -/** - * A stateless bean handling addressbooks - * <p> - * @author Roland Häder<roland@mxchange.org> - */ -@Stateless (name = "addressbook", description = "A stateless bean for handling Addressbook addressbooks") -public class AddressbookSessionBean extends BaseDatabaseBean implements AddressbookSessionBeanRemote { - - /** - * Serial number - */ - private static final long serialVersionUID = 129_857_871_287_691L; - - @Override - @SuppressWarnings ("unchecked") - public List<AddressbookEntry> allEntries (final Addressbook addressbook) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("allEntries: addressbook={0} - CALLED!", addressbook)); //NOI18N - - // Validate parameter - if (null == addressbook) { - // Throw NPE - throw new NullPointerException("addressbook is null"); - } else if (addressbook.getAddressbookId() == null) { - // Throw NPE again - throw new NullPointerException("addressbook.addressbookId is null"); - } else if (addressbook.getAddressbookId() < 1) { - // Invalid id number - throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); - } else if (addressbook.getAddressbookUser() == null) { - // Throw again NPE - throw new NullPointerException("addressbook.addressbookUser is null"); - } else if (addressbook.getAddressbookUser().getUserId() == null) { - // Throw again NPE - throw new NullPointerException("addressbook.addressbookUser.userId is null"); - } else if (addressbook.getAddressbookUser().getUserId() < 1) { - // Invalid id number again - throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookUser.userId={0} is invalid", addressbook.getAddressbookUser().getUserId())); - } - - // Generate query - Query query = this.getEntityManager().createNamedQuery("SearchUsersAddressbookEntries", List.class); //NOI18N - - // Set parameters - query.setParameter("addressbook", addressbook); //NOI18N - query.setParameter("owner", addressbook.getAddressbookUser()); //NOI18N - - // Return it - return query.getResultList(); - } - - @Override - @SuppressWarnings ("unchecked") - public List<User> allUsersNotSharing (final User user, final Addressbook addressbook) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: user={0},addressbook={1} - CALLED!", user, addressbook)); //NOI18N - - // Test parameter - if (null == user) { - // Throw NPE - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() == null) { - // Throw NPE again - throw new NullPointerException("user.userId is null"); //NOI18N - } else if (user.getUserId() < 1) { - // Invalid id - throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N - } else if (null == addressbook) { - // Again NPE - throw new NullPointerException("addressbook is null"); //NOI18N - } else if (addressbook.getAddressbookId() == null) { - // Again NPE - throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N - } else if (addressbook.getAddressbookId() < 1) { - // Invalid id - throw new IllegalArgumentException(MessageFormat.format("addressbook.getAddressbookId={0} is invalid", addressbook.getAddressbookId())); //NOI18N - } - - // Get named query for a user list without given user - Query allUsersExceptQuery = this.getEntityManager().createNamedQuery("SearchAllUsersExcept", List.class); //NOI18N - - // Set parameter - allUsersExceptQuery.setParameter("user", user); //NOI18N - - // Get full list - List<User> allUsersExcept = allUsersExceptQuery.getResultList(); - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allUsersExcept.size()={0}", allUsersExcept.size())); //NOI18N - - // Now get all shares this user has created - Query allSharesQuery = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N - - // Set parameter - allSharesQuery.setParameter("user", user); //NOI18N - - // Get full list again - List<ShareableAddressbook> allShares = allSharesQuery.getResultList(); - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: allShares.size()={0}", allShares.size())); //NOI18N - - // List for users aharing with given - List<User> sharingUsers = new ArrayList<>(allShares.size()); - - // Check all entries - for (final ShareableAddressbook share : allShares) { - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: share.shareUserSharee={0}", share.getShareUserSharee())); //NOI18N - - // Add it - sharingUsers.add(share.getShareUserSharee()); - } - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: sharingUsers.size()={0}", sharingUsers.size())); //NOI18N - - // Init final user list - List<User> userList = new LinkedList<>(); - - // Walk through all users - for (final User foundUser : allUsersExcept) { - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: foundUser={0}", foundUser)); //NOI18N - - // Does the list contain it ? - if (!sharingUsers.contains(foundUser)) { - // Found one to add - this.getLoggerBeanLocal().logDebug(MessageFormat.format("allUsersNotSharing: foundUser={0} - ADDING!", foundUser)); //NOI18N - - // Add it - userList.add(foundUser); - } - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("allUsersNotSharing: userList.size()={0} - EXIT!", userList.size())); //NOI18N - - // Return it - return userList; - } - - @Override - @SuppressWarnings ("unchecked") - public Integer countAllUserSharedAddressbooks (final User user) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: user={0} - CALLED!", user)); //NOI18N - - // user should be valid - if (null == user) { - // Throw NPE - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() == null) { - // Throw NPE again - throw new NullPointerException("user.userId is null"); //NOI18N - } else if (user.getUserId() < 1) { - // Invalid id - throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N - } - - // Get named query - Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N - - // Set parameter - query.setParameter("user", user); //NOI18N - - // Default is zero - Integer count = 0; - - // Try it - try { - // Get whole list - List<ShareableAddressbook> dummy = query.getResultList(); - - // Set size - count = dummy.size(); - } catch (final NoResultException ex) { - // Need to catch this, so log it - this.getLoggerBeanLocal().logDebug(MessageFormat.format("countAllUserSharedAddressbooks: getResultList() failed: {0}", ex)); //NOI18N - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("countAllUserSharedAddressbooks: count={0} - EXIT!", count)); //NOI18N - - // Return count - return count; - } - - @Override - public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException { - // Is it not null? - if (null == addressbook) { - // Abort here - throw new NullPointerException("addressbook is null"); //NOI18N - } else if (addressbook.getAddressbookUser() == null) { - // User instance is null - throw new NullPointerException("addressbook.user should not be null."); //NOI18N - } else if (addressbook.getAddressbookName() == null) { - // Address book name not set - throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N - } else if (addressbook.getAddressbookName().isEmpty()) { - // Address book name not set - throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N - } else if (this.isAddressbookNameUsed(addressbook)) { - // The assigned user already used that name - throw new AddressbookNameAlreadyUsedException(addressbook); - } - - // Add timestamp of creation - addressbook.setAddressbookCreated(new GregorianCalendar()); - - // Persist it now - this.getEntityManager().persist(addressbook); - - // Flush it to get all data - this.getEntityManager().flush(); - - // Return it updated - return addressbook; - } - - @Override - public Addressbook getAddressbookById (final Long addressbookId) throws AddressbookNotFoundException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("getAddressbookById: addressbookId={0} - CALLED!", addressbookId)); //NOI18N - - // addressbookId should not be null or below 1 - if (null == addressbookId) { - // Throw NPE - throw new NullPointerException("addressbookId is null"); //NOI18N - } else if (addressbookId < 1) { - // Not valid - throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N - } else if (!this.isAddressbookIdUsed(addressbookId)) { - // No address book found - throw new AddressbookNotFoundException(addressbookId); - } - - // Get named query instance - Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N - - // Set parameter - query.setParameter("id", addressbookId); //NOI18N - - // Return it - return (Addressbook) query.getSingleResult(); - } - - @Override - @SuppressWarnings ("unchecked") - public List<Addressbook> getUsersAddressbookList (final User loggedInUser) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser)); //NOI18N - - // Is the user instance null? - if (null == loggedInUser) { - // Abort here - throw new NullPointerException("loggedInUser is null"); //NOI18N - } - - // Get query instance - Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N - - // Set parameter - query.setParameter("param", loggedInUser); //NOI18N - - // Get full list from JPA - List<Addressbook> addressbooks = query.getResultList(); - - // Return it - return addressbooks; - } - - @Override - public boolean isAddressbookIdUsed (final Long addressbookId) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} - CALLED!", addressbookId)); //NOI18N - - // Is it null or zero? - if (null == addressbookId) { - // Throw NPE - throw new NullPointerException("addressbookId is null"); //NOI18N - } else if (addressbookId < 1) { - // Not valid id number - throw new IllegalArgumentException(MessageFormat.format("addressbookId is not valid: {0}", addressbookId)); //NOI18N - } - - // Get query instance - Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N - - // Set parameter - query.setParameter("id", addressbookId); //NOI18N - - // Default is not valid - boolean isValid = false; - - // Try it again, yes no other way - try { - // Get single result - Addressbook addressbook = (Addressbook) query.getSingleResult(); - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbook={0} - FOUND!", addressbook)); //NOI18N - - // Found one! - isValid = true; - } catch (final NoResultException ex) { - // Debug log only, maybe out-dated link followed - this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookIdUsed: addressbookId={0} is not valid: {1}", addressbookId, ex)); //NOI18N - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("isAddressbookIdUsed: isValid={0} - EXIT!", isValid)); //NOI18N - - // Return result - return isValid; - } - - @Override - public boolean isAddressbookNameUsed (final Addressbook addressbook) { - // Is it not null? - if (null == addressbook) { - // Abort here - throw new NullPointerException("addressbook is null"); //NOI18N - } else if (addressbook.getAddressbookUser() == null) { - // User instance is null - throw new NullPointerException("addressbook.addressbookUser is null."); //NOI18N - } else if (addressbook.getAddressbookUser().getUserId() == null) { - // User instance is null - throw new NullPointerException("addressbook.addressbookUser.userId is null."); //NOI18N - } else if (addressbook.getAddressbookUser().getUserId() < 1) { - // User instance is null - throw new NullPointerException(MessageFormat.format("addressbook.addressbookUser.userId={0} is invalid.", addressbook.getAddressbookUser().getUserId())); //NOI18N - } else if (addressbook.getAddressbookName() == null) { - // Address book name not set - throw new NullPointerException("addressbook.addressbookName should not be null"); //NOI18N - } else if (addressbook.getAddressbookName().isEmpty()) { - // Address book name not set - throw new IllegalArgumentException("addressbook.addressbookName should not be empty"); //NOI18N - } - - // Get query instance - Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class); //NOI18N - - // Set parameter - query.setParameter("user", addressbook.getAddressbookUser()); //NOI18N - query.setParameter("name", addressbook.getAddressbookName()); //NOI18N - - // Default is not found - boolean isUsed = false; - - // Try it - try { - // Get a single result - Addressbook dummy = (Addressbook) query.getSingleResult(); - - // Log it - this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: Found an address book: {0}", dummy)); //NOI18N - - // Found one - isUsed = true; - } catch (final NoResultException ex) { - // No result found, so log it away - this.getLoggerBeanLocal().logDebug(MessageFormat.format("isAddressbookNameUsed: getSingleResult() did not return a result: {0}", ex)); //NOI18N - } - - // Return result - return isUsed; - } -} diff --git a/src/java/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBean.java b/src/java/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBean.java deleted file mode 100644 index 8ff75bc..0000000 --- a/src/java/org/mxchange/addressbook/model/shared/SharedAddressbooksSessionBean.java +++ /dev/null @@ -1,207 +0,0 @@ -/* - * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. - */ -package org.mxchange.addressbook.model.shared; - -import java.text.MessageFormat; -import java.util.List; -import java.util.Objects; -import javax.ejb.Stateless; -import javax.persistence.NoResultException; -import javax.persistence.Query; -import org.mxchange.jaddressbookcore.exceptions.UserAlreadySharingAddressbookException; -import org.mxchange.jaddressbookcore.model.addressbook.Addressbook; -import org.mxchange.jaddressbookcore.model.addressbook.shared.AddressbookShare; -import org.mxchange.jaddressbookcore.model.addressbook.shared.ShareableAddressbook; -import org.mxchange.jcoreee.database.BaseDatabaseBean; -import org.mxchange.jusercore.model.user.User; - -/** - * A stateless bean for handling address book sharing - * <p> - * @author Roland Häder<roland@mxchange.org> - */ -@Stateless (name = "share", description = "A stateless bean for handling shared addressbooks") -public class SharedAddressbooksSessionBean extends BaseDatabaseBean implements SharedAddressbooksSessionBeanRemote { - - /** - * Serial number - */ - private static final long serialVersionUID = 136_984_697_285_694_710L; - - @Override - @SuppressWarnings ("unchecked") - public List<ShareableAddressbook> allSharedAddressbooks (final User user) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("allSharedAddressbooks: user={0} - CALLED!", user)); //NOI18N - - // Is user null? - if (null == user) { - // Throw NPE - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() == null) { - // Null userId is not allowed - throw new NullPointerException("user.userId is null"); //NOI18N - } else if (user.getUserId() < 1) { - // Not allowed value - throw new IllegalArgumentException(MessageFormat.format("user.UserId={0} is an invalid value", user.getUserId())); //NOI18N - } - - // Get named query - Query query = this.getEntityManager().createNamedQuery("SearchUserSharedAddressbooks", List.class); //NOI18N - - // Set parameter - query.setParameter("user", user); //NOI18N - - // Return full list - List<ShareableAddressbook> list = query.getResultList(); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("allSharedAddressbooks: list.size()={0} - EXIT!", list.size())); - - // Return list - return list; - } - - @Override - public Boolean isUserSharingAddressbooks (final User user) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: user={0} - CALLED!", user)); //NOI18N - - // Is user null? - if (null == user) { - // Throw NPE - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() == null) { - // Null userId is not allowed - throw new NullPointerException("user.userId is null"); //NOI18N - } else if (user.getUserId() < 1) { - // Not allowed value - throw new IllegalArgumentException(MessageFormat.format("user.UserId={0} is an invalid value", user.getUserId())); //NOI18N - } - - // Get results - List<ShareableAddressbook> list = this.allSharedAddressbooks(user); - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserSharingAddressbooks: list.size()={0}", list.size())); //NOI18N - - // Is it not empty? - Boolean isSharing = (!list.isEmpty()); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserSharingAddressbooks: iSharing={0} - EXIT!", isSharing)); //NOI18N - - // Return it - return isSharing; - } - - @Override - public ShareableAddressbook startSharing (final User sharee, final Addressbook addressbook) throws UserAlreadySharingAddressbookException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("startSharing: sharee={0},addressbook={1} - CALLED!", sharee, addressbook)); //NOI18N - - // Check all conditions - if (null == sharee) { - // Throw NPE - throw new NullPointerException("sharee is null"); //NOI18N - } else if (sharee.getUserId() == null) { - // Throw NPE again - throw new NullPointerException("sharee.userId is null"); //NOI18N - } else if (sharee.getUserId() < 1) { - // Invalid id number - throw new IllegalStateException(MessageFormat.format("sharee.userId={0} is invalid", sharee.getUserId())); //NOI18N - } else if (null == addressbook) { - // Throw NPE again - throw new NullPointerException("addressbook is null"); //NOI18N - } else if (addressbook.getAddressbookId() == null) { - // Throw NPE again - throw new NullPointerException("addressbook.addressbookId is null"); //NOI18N - } else if (addressbook.getAddressbookId() < 1) { - // Invalid id number - throw new IllegalArgumentException(MessageFormat.format("addressbook.addressbookId={0} is invalid.", addressbook.getAddressbookId())); //NOI18N - } else if (Objects.equals(addressbook.getAddressbookUser(), sharee)) { - // Sharing with yourself! - throw new IllegalStateException("User tries to share with himself."); //NOI18N - } - - // Is the entry already there? - if (this.isUserAlreadySharingAddressbook(addressbook, sharee)) { - // Abort here - throw new UserAlreadySharingAddressbookException(addressbook, sharee); - } - - // All fine so far, then create the instance - ShareableAddressbook share = new AddressbookShare(addressbook, sharee); - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("startSharing: share={0}", share)); //NOI18N - - // Persist it - this.getEntityManager().persist(share); - - // Flush to get id number - this.getEntityManager().flush(); - - // Return updated instance - return share; - } - - /** - * Checks whether the owner of the given address book is already sharing it - * with the sharee. - * <p> - * @param addressbook Address book to be shared with - * @param sharee User sharee instance - * <p> - * @return Wether the address book is already shared with the sharee - */ - private boolean isUserAlreadySharingAddressbook (final Addressbook addressbook, final User sharee) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserAlreadySharingAddressbook: addressbook={0},sharee={1} - CALLED!", addressbook, sharee)); //NOI18N - - // Get named query - Query query = this.getEntityManager().createNamedQuery("SearchShareeAddressbookShare", AddressbookShare.class); //NOI18N - - // Set parameter - query.setParameter("addressbook", addressbook); //NOI18N - query.setParameter("sharee", sharee); //NOI18N - - // Default is not found - boolean isFound = false; - - // Try it - try { - // Get single instance - ShareableAddressbook share = (ShareableAddressbook) query.getSingleResult(); - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserAlreadySharingAddressbook: share={0} - FOUND!", share)); //NOI18N - - // Set found - isFound = true; - } catch (final NoResultException ex) { - // Not found, log exception - this.getLoggerBeanLocal().logDebug(MessageFormat.format("isUserAlreadySharingAddressbook: Notfound. Exception: {0}", ex)); //NOI18N - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("isUserAlreadySharingAddressbook: isFound={0} - EXIT!", isFound)); //NOI18N - - // Return it - return isFound; - } -} diff --git a/src/java/org/mxchange/jcontacts/contact/AddressbookAdminContactSessionBean.java b/src/java/org/mxchange/jcontacts/contact/AddressbookAdminContactSessionBean.java deleted file mode 100644 index 4ed5a40..0000000 --- a/src/java/org/mxchange/jcontacts/contact/AddressbookAdminContactSessionBean.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. - */ -package org.mxchange.jcontacts.contact; - -import java.text.MessageFormat; -import java.util.GregorianCalendar; -import javax.ejb.Stateless; -import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean; -import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException; - -/** - * An administrative contact EJB - * <p> - * @author Roland Häder<roland@mxchange.org> - */ -@Stateless (name = "adminContact", description = "An administrative contact EJB") -public class AddressbookAdminContactSessionBean extends BaseAddressbookDatabaseBean implements AdminContactSessionBeanRemote { - - /** - * Serial number - */ - private static final long serialVersionUID = 542_145_347_916L; - - /** - * Default constructor - */ - public AddressbookAdminContactSessionBean () { - } - - @Override - public Contact addContact (final Contact contact) throws ContactAlreadyAddedException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N - - // Is the instance set? - if (null == contact) { - // Throw NPE - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactId() != null) { - // Should be null - throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} - is not null", contact.getContactId())); //NOI18N - } - - // Set created timestamp - contact.setContactCreated(new GregorianCalendar()); - - // Set all created timestamps, if instance is there - this.setAllContactPhoneEntriesCreated(contact); - - // Persist it - this.getEntityManager().persist(contact); - - // Flush it to get contactId set - this.getEntityManager().flush(); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact.contactId={1} after persisting - EXIT!", this.getClass().getSimpleName(), contact.getContactId())); //NOI18N - - // Return it - return contact; - } - - @Override - public void deleteContactData (final Contact contact) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N - - // Is the instance set? - if (null == contact) { - // Throw NPE - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactId() == null) { - // Should not be null - throw new NullPointerException("contact.contactId is null"); //NOI18N - } else if (contact.getContactId() < 1) { - // Not valid - throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N - } - - // Merge it to get a managed entity back - Contact managedContact = this.getEntityManager().getReference(contact.getClass(), contact.getContactId()); - - // Remove it from database - this.getEntityManager().remove(managedContact); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteContactData: EXIT!", this.getClass().getSimpleName())); //NOI18N - } - -} diff --git a/src/java/org/mxchange/jcontacts/contact/AddressbookContactSessionBean.java b/src/java/org/mxchange/jcontacts/contact/AddressbookContactSessionBean.java deleted file mode 100644 index 410ee2e..0000000 --- a/src/java/org/mxchange/jcontacts/contact/AddressbookContactSessionBean.java +++ /dev/null @@ -1,328 +0,0 @@ -/* - * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. - */ -package org.mxchange.jcontacts.contact; - -import java.text.MessageFormat; -import java.util.Iterator; -import java.util.List; -import java.util.Objects; -import javax.ejb.Stateless; -import javax.persistence.NoResultException; -import javax.persistence.Query; -import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean; -import org.mxchange.jcontacts.contact.utils.ContactUtils; -import org.mxchange.jcontacts.exceptions.ContactNotFoundException; - -/** - * A contact EJB - * <p> - * @author Roland Häder<roland@mxchange.org> - */ -@Stateless (name = "contact", description = "A bean handling contact data") -public class AddressbookContactSessionBean extends BaseAddressbookDatabaseBean implements ContactSessionBeanRemote { - - /** - * Serial number - */ - private static final long serialVersionUID = 542_145_347_916L; - - /** - * Default constructor - */ - public AddressbookContactSessionBean () { - } - - @Override - public Contact findContactByEmailAddress (final String emailAddress) throws ContactNotFoundException { - // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N - - // The parameter must be valid - if (null == emailAddress) { - // Throw NPE - throw new NullPointerException("emailAddress is null"); //NOI18N - } else if (emailAddress.isEmpty()) { - // Not valid - throw new IllegalArgumentException("emailAddress is empty"); //NOI18N - } - - // Get query instance - Query query = this.getEntityManager().createNamedQuery("SearchContactByEmailAddress", UserContact.class); //NOI18N - - // Set parameter - query.setParameter("emailAddress", emailAddress); //NOI18N - - // Init contact instance - Contact contact; - - // Try to find a result - try { - // Find a single result - contact = (Contact) query.getSingleResult(); - - // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N - } catch (final NoResultException ex) { - // No result found - throw new ContactNotFoundException(emailAddress, ex); - } - - // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N - - // Return found instance - return contact; - } - - @Override - public Contact findContactById (final Long contactId) throws ContactNotFoundException { - // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contactId={1} - CALLED!", this.getClass().getSimpleName(), contactId)); //NOI18N - - // The parameter must be valid - if (null == contactId) { - // Throw NPE - throw new NullPointerException("contactId is null"); //NOI18N - } else if (contactId < 1) { - // Not valid - throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N - } - - // Get query instance - Query query = this.getEntityManager().createNamedQuery("SearchContactById", UserContact.class); //NOI18N - - // Set parameter - query.setParameter("contactId", contactId); //NOI18N - - // Init contact instance - Contact contact; - - // Try to find a result - try { - // Find a single result - contact = (Contact) query.getSingleResult(); - - // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N - } catch (final NoResultException ex) { - // No result found - throw new ContactNotFoundException(contactId, ex); - } - - // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N - - // Return found instance - return contact; - } - - @Override - @SuppressWarnings ("unchecked") - public List<Contact> getAllContacts () { - // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts - CALLED!", this.getClass().getSimpleName())); //NOI18N - - // Create query instance - Query query = this.getEntityManager().createNamedQuery("AllContacts", UserContact.class); //NOI18N - - // Get list - List<Contact> contacts = query.getResultList(); - - // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts: contacts.size()={1} - EXIT!", this.getClass().getSimpleName(), contacts.size())); //NOI18N - - // Return it - return contacts; - } - - @Override - @SuppressWarnings ("unchecked") - public List<String> getEmailAddressList () { - // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList - CALLED!", this.getClass().getSimpleName())); //NOI18N - - // Create query instance - Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", String.class); //NOI18N - - // Get list - List<String> emailAddresses = query.getResultList(); - - // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: emailAddresses.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddresses.size())); //NOI18N - - // Return it - return emailAddresses; - } - - @Override - public boolean isEmailAddressRegistered (final String emailAddress) { - // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N - - // The email address should be valid - if (null == emailAddress) { - // Is null - throw new NullPointerException("emailAddress is null"); //NOI18N - } else if (emailAddress.isEmpty()) { - // Is empty - throw new IllegalArgumentException("emailAddress is empty"); //NOI18N - } - - // Default is not found - boolean isFound = false; - - try { - // Ask other method for contact instance - Contact contact = this.findContactByEmailAddress(emailAddress); - - // Log debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: Found contact={1} for emailAddress={2}", this.getClass().getSimpleName(), contact, emailAddress)); //NOI18N - - // It is found ... - isFound = true; - } catch (final ContactNotFoundException ex) { - // @TODO Was not found, log exception for spam check? - } - - // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N - - // Return status - return isFound; - } - - @Override - public Contact lookupContact (final Contact contact) { - // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N - - // Parameter should be valid - if (null == contact) { - // Throw NPE - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactId() > 0) { - try { - // Id set, ask other method - return this.findContactById(contact.getContactId()); - } catch (final ContactNotFoundException ex) { - // Not found, should not happen - throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N - } - } - - // Default is not found - Contact foundContact = null; - - // Get whole list - List<Contact> contacts = this.getAllContacts(); - - // Is the list empty? - if (contacts.isEmpty()) { - // Then abort here - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: No contacts registered, returning NULL ...", this.getClass().getSimpleName())); //NOI18N - return null; - } - - // Get iterator - Iterator<Contact> iterator = contacts.iterator(); - - // Loop through all - while (iterator.hasNext()) { - // Get contact - Contact next = iterator.next(); - - // Is same contact? - if ((Objects.equals(contact, next)) || (ContactUtils.isSameContact(contact, next))) { - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isContactFound: Found same contact: contactId={1}", this.getClass().getSimpleName(), next.getContactId())); //NOI18N - - // Found it - foundContact = next; - break; - } - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: foundContact={1} - EXIT!", this.getClass().getSimpleName(), foundContact)); //NOI18N - - // Return found contact - return foundContact; - } - - @Override - public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) { - // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1},isCellphoneUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED!", this.getClass().getSimpleName(), contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N - - // The contact instance must be valid - if (null == contact) { - // Throw NPE again - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactId() == null) { - // Throw NPE again - throw new NullPointerException("contact.contactId is null"); //NOI18N - } else if (contact.getContactId() < 1) { - // Not valid - throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N - } - - // Set updated timestamp - this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked); - - // Merge cellphone, land-line and fix - Contact detachedContact = this.mergeContactData(contact); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N - - // Return it - return detachedContact; - } - - @Override - public Contact updateContactData (final Contact contact) { - // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N - - // The contact instance must be valid - if (null == contact) { - // Throw NPE again - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactId() == null) { - // Throw NPE again - throw new NullPointerException("contact.contactId is null"); //NOI18N - } else if (contact.getContactId() < 1) { - // Not valid - throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N - } - - // Is cell phone/land-line/fax number unlinked? - boolean isCellphoneUnlinked = (contact.getContactMobileNumber() == null); - boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null); - boolean isFaxUnlinked = (contact.getContactFaxNumber() == null); - - // Call other Method - Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N - - // Return it - return detachedContact; - } - -} diff --git a/src/java/org/mxchange/jcontacts/contact/FinancialsAdminContactSessionBean.java b/src/java/org/mxchange/jcontacts/contact/FinancialsAdminContactSessionBean.java new file mode 100644 index 0000000..6c0e00c --- /dev/null +++ b/src/java/org/mxchange/jcontacts/contact/FinancialsAdminContactSessionBean.java @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. + */ +package org.mxchange.jcontacts.contact; + +import java.text.MessageFormat; +import java.util.GregorianCalendar; +import javax.ejb.Stateless; +import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean; +import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException; + +/** + * An administrative contact EJB + * <p> + * @author Roland Häder<roland@mxchange.org> + */ +@Stateless (name = "adminContact", description = "An administrative contact EJB") +public class FinancialsAdminContactSessionBean extends BaseFinancialsDatabaseBean implements AdminContactSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 542_145_347_916L; + + /** + * Default constructor + */ + public FinancialsAdminContactSessionBean () { + } + + @Override + public Contact addContact (final Contact contact) throws ContactAlreadyAddedException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N + + // Is the instance set? + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() != null) { + // Should be null + throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} - is not null", contact.getContactId())); //NOI18N + } + + // Set created timestamp + contact.setContactCreated(new GregorianCalendar()); + + // Set all created timestamps, if instance is there + this.setAllContactPhoneEntriesCreated(contact); + + // Persist it + this.getEntityManager().persist(contact); + + // Flush it to get contactId set + this.getEntityManager().flush(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addContact: contact.contactId={1} after persisting - EXIT!", this.getClass().getSimpleName(), contact.getContactId())); //NOI18N + + // Return it + return contact; + } + + @Override + public void deleteContactData (final Contact contact) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N + + // Is the instance set? + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { + // Should not be null + throw new NullPointerException("contact.contactId is null"); //NOI18N + } else if (contact.getContactId() < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N + } + + // Merge it to get a managed entity back + Contact managedContact = this.getEntityManager().getReference(contact.getClass(), contact.getContactId()); + + // Remove it from database + this.getEntityManager().remove(managedContact); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteContactData: EXIT!", this.getClass().getSimpleName())); //NOI18N + } + +} diff --git a/src/java/org/mxchange/jcontacts/contact/FinancialsContactSessionBean.java b/src/java/org/mxchange/jcontacts/contact/FinancialsContactSessionBean.java new file mode 100644 index 0000000..e6fe705 --- /dev/null +++ b/src/java/org/mxchange/jcontacts/contact/FinancialsContactSessionBean.java @@ -0,0 +1,328 @@ +/* + * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. + */ +package org.mxchange.jcontacts.contact; + +import java.text.MessageFormat; +import java.util.Iterator; +import java.util.List; +import java.util.Objects; +import javax.ejb.Stateless; +import javax.persistence.NoResultException; +import javax.persistence.Query; +import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean; +import org.mxchange.jcontacts.contact.utils.ContactUtils; +import org.mxchange.jcontacts.exceptions.ContactNotFoundException; + +/** + * A contact EJB + * <p> + * @author Roland Häder<roland@mxchange.org> + */ +@Stateless (name = "contact", description = "A bean handling contact data") +public class FinancialsContactSessionBean extends BaseFinancialsDatabaseBean implements ContactSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 542_145_347_916L; + + /** + * Default constructor + */ + public FinancialsContactSessionBean () { + } + + @Override + public Contact findContactByEmailAddress (final String emailAddress) throws ContactNotFoundException { + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N + + // The parameter must be valid + if (null == emailAddress) { + // Throw NPE + throw new NullPointerException("emailAddress is null"); //NOI18N + } else if (emailAddress.isEmpty()) { + // Not valid + throw new IllegalArgumentException("emailAddress is empty"); //NOI18N + } + + // Get query instance + Query query = this.getEntityManager().createNamedQuery("SearchContactByEmailAddress", UserContact.class); //NOI18N + + // Set parameter + query.setParameter("emailAddress", emailAddress); //NOI18N + + // Init contact instance + Contact contact; + + // Try to find a result + try { + // Find a single result + contact = (Contact) query.getSingleResult(); + + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N + } catch (final NoResultException ex) { + // No result found + throw new ContactNotFoundException(emailAddress, ex); + } + + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactByEmailAddress: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N + + // Return found instance + return contact; + } + + @Override + public Contact findContactById (final Long contactId) throws ContactNotFoundException { + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contactId={1} - CALLED!", this.getClass().getSimpleName(), contactId)); //NOI18N + + // The parameter must be valid + if (null == contactId) { + // Throw NPE + throw new NullPointerException("contactId is null"); //NOI18N + } else if (contactId < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("contactId={0} is not valid", contactId)); //NOI18N + } + + // Get query instance + Query query = this.getEntityManager().createNamedQuery("SearchContactById", UserContact.class); //NOI18N + + // Set parameter + query.setParameter("contactId", contactId); //NOI18N + + // Init contact instance + Contact contact; + + // Try to find a result + try { + // Find a single result + contact = (Contact) query.getSingleResult(); + + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: Found contact={1}", this.getClass().getSimpleName(), contact)); //NOI18N + } catch (final NoResultException ex) { + // No result found + throw new ContactNotFoundException(contactId, ex); + } + + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findContactById: contact={1} - EXIT!", this.getClass().getSimpleName(), contact)); //NOI18N + + // Return found instance + return contact; + } + + @Override + @SuppressWarnings ("unchecked") + public List<Contact> getAllContacts () { + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts - CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Create query instance + Query query = this.getEntityManager().createNamedQuery("AllContacts", UserContact.class); //NOI18N + + // Get list + List<Contact> contacts = query.getResultList(); + + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getAllContacts: contacts.size()={1} - EXIT!", this.getClass().getSimpleName(), contacts.size())); //NOI18N + + // Return it + return contacts; + } + + @Override + @SuppressWarnings ("unchecked") + public List<String> getEmailAddressList () { + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList - CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Create query instance + Query query = this.getEntityManager().createNamedQuery("AllContactEmailAddresses", String.class); //NOI18N + + // Get list + List<String> emailAddresses = query.getResultList(); + + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: emailAddresses.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddresses.size())); //NOI18N + + // Return it + return emailAddresses; + } + + @Override + public boolean isEmailAddressRegistered (final String emailAddress) { + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N + + // The email address should be valid + if (null == emailAddress) { + // Is null + throw new NullPointerException("emailAddress is null"); //NOI18N + } else if (emailAddress.isEmpty()) { + // Is empty + throw new IllegalArgumentException("emailAddress is empty"); //NOI18N + } + + // Default is not found + boolean isFound = false; + + try { + // Ask other method for contact instance + Contact contact = this.findContactByEmailAddress(emailAddress); + + // Log debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: Found contact={1} for emailAddress={2}", this.getClass().getSimpleName(), contact, emailAddress)); //NOI18N + + // It is found ... + isFound = true; + } catch (final ContactNotFoundException ex) { + // @TODO Was not found, log exception for spam check? + } + + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N + + // Return status + return isFound; + } + + @Override + public Contact lookupContact (final Contact contact) { + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N + + // Parameter should be valid + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() > 0) { + try { + // Id set, ask other method + return this.findContactById(contact.getContactId()); + } catch (final ContactNotFoundException ex) { + // Not found, should not happen + throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is set, but not found.", contact.getContactId()), ex); //NOI18N + } + } + + // Default is not found + Contact foundContact = null; + + // Get whole list + List<Contact> contacts = this.getAllContacts(); + + // Is the list empty? + if (contacts.isEmpty()) { + // Then abort here + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: No contacts registered, returning NULL ...", this.getClass().getSimpleName())); //NOI18N + return null; + } + + // Get iterator + Iterator<Contact> iterator = contacts.iterator(); + + // Loop through all + while (iterator.hasNext()) { + // Get contact + Contact next = iterator.next(); + + // Is same contact? + if ((Objects.equals(contact, next)) || (ContactUtils.isSameContact(contact, next))) { + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isContactFound: Found same contact: contactId={1}", this.getClass().getSimpleName(), next.getContactId())); //NOI18N + + // Found it + foundContact = next; + break; + } + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isContactFound: foundContact={1} - EXIT!", this.getClass().getSimpleName(), foundContact)); //NOI18N + + // Return found contact + return foundContact; + } + + @Override + public Contact updateContactData (final Contact contact, final boolean isCellphoneUnlinked, final boolean isLandlineUnlinked, final boolean isFaxUnlinked) { + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1},isCellphoneUnlinked={2},isLandlineUnlinked={3},isFaxUnlinked={4} - CALLED!", this.getClass().getSimpleName(), contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked)); //NOI18N + + // The contact instance must be valid + if (null == contact) { + // Throw NPE again + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { + // Throw NPE again + throw new NullPointerException("contact.contactId is null"); //NOI18N + } else if (contact.getContactId() < 1) { + // Not valid + throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N + } + + // Set updated timestamp + this.setAllContactPhoneEntriesUpdated(contact, isCellphoneUnlinked, isLandlineUnlinked, isFaxUnlinked); + + // Merge cellphone, land-line and fix + Contact detachedContact = this.mergeContactData(contact); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N + + // Return it + return detachedContact; + } + + @Override + public Contact updateContactData (final Contact contact) { + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: contact={1} - CALLED!", this.getClass().getSimpleName(), contact)); //NOI18N + + // The contact instance must be valid + if (null == contact) { + // Throw NPE again + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { + // Throw NPE again + throw new NullPointerException("contact.contactId is null"); //NOI18N + } else if (contact.getContactId() < 1) { + // Not valid + throw new IllegalStateException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N + } + + // Is cell phone/land-line/fax number unlinked? + boolean isCellphoneUnlinked = (contact.getContactMobileNumber() == null); + boolean isLandLineUnlinked = (contact.getContactLandLineNumber() == null); + boolean isFaxUnlinked = (contact.getContactFaxNumber() == null); + + // Call other Method + Contact detachedContact = this.updateContactData(contact, isCellphoneUnlinked, isLandLineUnlinked, isFaxUnlinked); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateContactData: detachedContact={1} - EXIT!", this.getClass().getSimpleName(), detachedContact)); //NOI18N + + // Return it + return detachedContact; + } + +} diff --git a/src/java/org/mxchange/jcontacts/phone/AddressbookAdminContactPhoneSessionBean.java b/src/java/org/mxchange/jcontacts/phone/AddressbookAdminContactPhoneSessionBean.java deleted file mode 100644 index f5d6110..0000000 --- a/src/java/org/mxchange/jcontacts/phone/AddressbookAdminContactPhoneSessionBean.java +++ /dev/null @@ -1,508 +0,0 @@ -/* - * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. - */ -package org.mxchange.jcontacts.phone; - -import java.text.MessageFormat; -import java.util.GregorianCalendar; -import java.util.Objects; -import javax.ejb.EJB; -import javax.ejb.Stateless; -import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean; -import org.mxchange.jcontacts.contact.Contact; -import org.mxchange.jcontacts.contact.ContactSessionBeanRemote; -import org.mxchange.jphone.exceptions.PhoneNumberAlreadyLinkedException; -import org.mxchange.jphone.exceptions.PhoneNumberNotLinkedException; -import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; -import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; -import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber; - -/** - * A session EJB for administrative contact's phone number purposes - * <p> - * @author Roland Häder<roland@mxchange.org> - */ -@Stateless (name = "adminContactPhone", description = "An administrative bean handling contact's phone data") -public class AddressbookAdminContactPhoneSessionBean extends BaseAddressbookDatabaseBean implements AdminContactsPhoneSessionBeanRemote { - - /** - * Serial number - */ - private static final long serialVersionUID = 189_217_561_460_237_108L; - - /** - * Contact EJB - */ - @EJB - private ContactSessionBeanRemote contactBean; - - @Override - public Contact linkExistingFaxNumberWithContact (final Contact contact, final DialableFaxNumber faxNumber) throws PhoneNumberAlreadyLinkedException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingFaxNumberWithContact: contact={1},faxNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, faxNumber)); //NOI18N - - // Is the contact set? - if (null == contact) { - // Throw NPE - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactId() == null) { - // ... and throw again - throw new NullPointerException("contact.contactId is null"); //NOI18N - } else if (contact.getContactId() < 1) { - // Invalid id number - throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N - } else if (contact.getContactFaxNumber() instanceof DialableFaxNumber) { - // Not set cell phone instance - throw new PhoneNumberAlreadyLinkedException(faxNumber); - } else if (null == faxNumber) { - // Throw NPE - throw new NullPointerException("faxNumber is null"); //NOI18N - } else if (faxNumber.getPhoneId() == null) { - // Throw it again - throw new NullPointerException("faxNumber.phoneId is null"); //NOI18N - } else if (faxNumber.getPhoneId() < 1) { - // Invalid id - throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneId={0} is not valid", faxNumber.getPhoneId())); //NOI18N - } else if (faxNumber.getPhoneCountry() == null) { - // ... and again - throw new NullPointerException("faxNumber.phoneCountry is null"); //NOI18N - } else if (faxNumber.getPhoneAreaCode() == null) { - // Throw it again - throw new NullPointerException("faxNumber.phoneAreaCode is null"); //NOI18N - } else if (faxNumber.getPhoneAreaCode() < 1) { - // Invalid id - throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneAreaCode={0} is not valid", faxNumber.getPhoneAreaCode())); //NOI18N - } else if (faxNumber.getPhoneNumber() == null) { - // Throw it again - throw new NullPointerException("faxNumber.phoneNumber is null"); //NOI18N - } else if (faxNumber.getPhoneNumber() < 1) { - // Invalid id - throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneNumber={0} is not valid", faxNumber.getPhoneNumber())); //NOI18N - } - - // Set fax number in contact - contact.setContactFaxNumber(faxNumber); - - // Update database - Contact updatedContact = this.contactBean.updateContactData(contact); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingFaxNumberWithContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N - - // Return it - return updatedContact; - } - - @Override - public Contact linkExistingLandLineNumberWithContact (final Contact contact, final DialableLandLineNumber landLineNumber) throws PhoneNumberAlreadyLinkedException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingLandLineNumberWithContact: contact={1},landLineNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, landLineNumber)); //NOI18N - - // Is the contact set? - if (null == contact) { - // Throw NPE - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactId() == null) { - // ... and throw again - throw new NullPointerException("contact.contactId is null"); //NOI18N - } else if (contact.getContactId() < 1) { - // Invalid id number - throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N - } else if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) { - // Not set cell phone instance - throw new PhoneNumberAlreadyLinkedException(landLineNumber); - } else if (null == landLineNumber) { - // Throw NPE - throw new NullPointerException("landLineNumber is null"); //NOI18N - } else if (landLineNumber.getPhoneId() == null) { - // Throw it again - throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N - } else if (landLineNumber.getPhoneId() < 1) { - // Invalid id - throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneId={0} is not valid", landLineNumber.getPhoneId())); //NOI18N - } else if (landLineNumber.getPhoneCountry() == null) { - // ... and again - throw new NullPointerException("landLineNumber.phoneCountry is null"); //NOI18N - } else if (landLineNumber.getPhoneAreaCode() == null) { - // Throw it again - throw new NullPointerException("landLineNumber.phoneAreaCode is null"); //NOI18N - } else if (landLineNumber.getPhoneAreaCode() < 1) { - // Invalid id - throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneAreaCode={0} is not valid", landLineNumber.getPhoneAreaCode())); //NOI18N - } else if (landLineNumber.getPhoneNumber() == null) { - // Throw it again - throw new NullPointerException("landLineNumber.phoneNumber is null"); //NOI18N - } else if (landLineNumber.getPhoneNumber() < 1) { - // Invalid id - throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneNumber={0} is not valid", landLineNumber.getPhoneNumber())); //NOI18N - } - - // Set land-line number in contact - contact.setContactLandLineNumber(landLineNumber); - - // Update database - Contact updatedContact = this.contactBean.updateContactData(contact); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingLandLineNumberWithContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N - - // Return it - return updatedContact; - } - - @Override - public Contact linkExistingMobileNumberWithContact (final Contact contact, final DialableMobileNumber mobileNumber) throws PhoneNumberAlreadyLinkedException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingMobileNumberWithContact: contact={1},mobileNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, mobileNumber)); //NOI18N - - // Is the contact set? - if (null == contact) { - // Throw NPE - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactId() == null) { - // ... and throw again - throw new NullPointerException("contact.contactId is null"); //NOI18N - } else if (contact.getContactId() < 1) { - // Invalid id number - throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N - } else if (contact.getContactMobileNumber() instanceof DialableMobileNumber) { - // Not set cell phone instance - throw new PhoneNumberAlreadyLinkedException(mobileNumber); - } else if (null == mobileNumber) { - // Throw NPE - throw new NullPointerException("mobileNumber is null"); //NOI18N - } else if (mobileNumber.getPhoneId() == null) { - // Throw it again - throw new NullPointerException("mobileNumber.phoneId is null"); //NOI18N - } else if (mobileNumber.getPhoneId() < 1) { - // Invalid id - throw new IllegalArgumentException(MessageFormat.format("mobileNumber.phoneId={0} is not valid", mobileNumber.getPhoneId())); //NOI18N - } else if (mobileNumber.getMobileProvider() == null) { - // Throw NPE again - throw new NullPointerException("mobileNumber.mobileProvider is null"); //NOI18N - } else if (mobileNumber.getMobileProvider().getProviderId() == null) { - // Throw NPE again - throw new NullPointerException("mobileNumber.mobileProvider.providerId is null"); //NOI18N - } else if (mobileNumber.getMobileProvider().getProviderId() < 1) { - // Throw NPE again - throw new IllegalArgumentException(MessageFormat.format("mobileNumber.mobileProvider.providerId={0} is not valid", mobileNumber.getMobileProvider().getProviderId())); //NOI18N - } - - // Set mobile number in contact - contact.setContactMobileNumber(mobileNumber); - - // Update database - Contact updatedContact = this.contactBean.updateContactData(contact); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingMobileNumberWithContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N - - // Return it - return updatedContact; - } - - @Override - public Contact linkNewFaxNumberWithContact (final Contact contact, final DialableFaxNumber faxNumber) throws PhoneNumberAlreadyLinkedException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkNewFaxNumberWithContact: contact={1},faxNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, faxNumber)); //NOI18N - - // Is the contact set? - if (null == contact) { - // Throw NPE - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactId() == null) { - // ... and throw again - throw new NullPointerException("contact.contactId is null"); //NOI18N - } else if (contact.getContactId() < 1) { - // Invalid id number - throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N - } else if (contact.getContactFaxNumber() instanceof DialableFaxNumber) { - // Not set cell phone instance - throw new PhoneNumberAlreadyLinkedException(faxNumber); - } else if (null == faxNumber) { - // Throw NPE - throw new NullPointerException("faxNumber is null"); //NOI18N - } else if (faxNumber.getPhoneId() instanceof Long) { - // Throw it again - throw new IllegalStateException(MessageFormat.format("faxNumber.phoneId={0} is not null", faxNumber.getPhoneId())); //NOI18N - } else if (faxNumber.getPhoneCountry() == null) { - // ... and again - throw new NullPointerException("faxNumber.phoneCountry is null"); //NOI18N - } else if (faxNumber.getPhoneAreaCode() == null) { - // Throw it again - throw new NullPointerException("faxNumber.phoneAreaCode is null"); //NOI18N - } else if (faxNumber.getPhoneAreaCode() < 1) { - // Invalid id - throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneAreaCode={0} is not valid", faxNumber.getPhoneAreaCode())); //NOI18N - } else if (faxNumber.getPhoneNumber() == null) { - // Throw it again - throw new NullPointerException("faxNumber.phoneNumber is null"); //NOI18N - } else if (faxNumber.getPhoneNumber() < 1) { - // Invalid id - throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneNumber={0} is not valid", faxNumber.getPhoneNumber())); //NOI18N - } - - // Set created instance - faxNumber.setPhoneEntryCreated(new GregorianCalendar()); - - // Set fax number in contact - contact.setContactFaxNumber(faxNumber); - - // Update database - Contact updatedContact = this.contactBean.updateContactData(contact); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkNewFaxNumberWithContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N - - // Return it - return updatedContact; - } - - @Override - public Contact linkNewLandLineNumberWithContact (final Contact contact, final DialableLandLineNumber landLineNumber) throws PhoneNumberAlreadyLinkedException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkNewLandLineNumberWithContact: contact={1},landLineNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, landLineNumber)); //NOI18N - - // Is the contact set? - if (null == contact) { - // Throw NPE - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactId() == null) { - // ... and throw again - throw new NullPointerException("contact.contactId is null"); //NOI18N - } else if (contact.getContactId() < 1) { - // Invalid id number - throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N - } else if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) { - // Not set cell phone instance - throw new PhoneNumberAlreadyLinkedException(landLineNumber); - } else if (null == landLineNumber) { - // Throw NPE - throw new NullPointerException("landLineNumber is null"); //NOI18N - } else if (landLineNumber.getPhoneId() instanceof Long) { - // Throw it again - throw new IllegalStateException(MessageFormat.format("landLineNumber.phoneId={0} is not null", landLineNumber.getPhoneId())); //NOI18N - } else if (landLineNumber.getPhoneCountry() == null) { - // ... and again - throw new NullPointerException("landLineNumber.phoneCountry is null"); //NOI18N - } else if (landLineNumber.getPhoneAreaCode() == null) { - // Throw it again - throw new NullPointerException("landLineNumber.phoneAreaCode is null"); //NOI18N - } else if (landLineNumber.getPhoneAreaCode() < 1) { - // Invalid id - throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneAreaCode={0} is not valid", landLineNumber.getPhoneAreaCode())); //NOI18N - } else if (landLineNumber.getPhoneNumber() == null) { - // Throw it again - throw new NullPointerException("landLineNumber.phoneNumber is null"); //NOI18N - } else if (landLineNumber.getPhoneNumber() < 1) { - // Invalid id - throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneNumber={0} is not valid", landLineNumber.getPhoneNumber())); //NOI18N - } - - // Set created instance - landLineNumber.setPhoneEntryCreated(new GregorianCalendar()); - - // Set landLine number in contact - contact.setContactLandLineNumber(landLineNumber); - - // Update database - Contact updatedContact = this.contactBean.updateContactData(contact); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkNewLandLineNumberWithContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N - - // Return it - return updatedContact; - } - - @Override - public Contact linkNewMobileNumberWithContact (final Contact contact, final DialableMobileNumber mobileNumber) throws PhoneNumberAlreadyLinkedException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkNewMobileNumberWithContact: contact={1},mobileNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, mobileNumber)); //NOI18N - - // Is the contact set? - if (null == contact) { - // Throw NPE - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactId() == null) { - // ... and throw again - throw new NullPointerException("contact.contactId is null"); //NOI18N - } else if (contact.getContactId() < 1) { - // Invalid id number - throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N - } else if (contact.getContactMobileNumber() instanceof DialableMobileNumber) { - // Not set cell phone instance - throw new PhoneNumberAlreadyLinkedException(mobileNumber); - } else if (null == mobileNumber) { - // Throw NPE - throw new NullPointerException("mobileNumber is null"); //NOI18N - } else if (mobileNumber.getPhoneId() instanceof Long) { - // Throw it again - throw new IllegalStateException(MessageFormat.format("mobileNumber.phoneId={0} is not null", mobileNumber.getPhoneId())); //NOI18N - } else if (mobileNumber.getMobileProvider() == null) { - // Throw NPE again - throw new NullPointerException("mobileNumber.mobileProvider is null"); //NOI18N - } else if (mobileNumber.getMobileProvider().getProviderId() == null) { - // Throw NPE again - throw new NullPointerException("mobileNumber.mobileProvider.providerId is null"); //NOI18N - } else if (mobileNumber.getMobileProvider().getProviderId() < 1) { - // Throw NPE again - throw new IllegalArgumentException(MessageFormat.format("mobileNumber.mobileProvider.providerId={0} is not valid", mobileNumber.getMobileProvider().getProviderId())); //NOI18N - } - - // Set created instance - mobileNumber.setPhoneEntryCreated(new GregorianCalendar()); - - // Set mobile number in contact - contact.setContactMobileNumber(mobileNumber); - - // Update database - Contact updatedContact = this.contactBean.updateContactData(contact); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkNewMobileNumberWithContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N - - // Return it - return updatedContact; - } - - @Override - public Contact unlinkFaxDataFromContact (final Contact contact, final DialableFaxNumber faxNumber) throws PhoneNumberNotLinkedException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkFaxDataFromContact: contact={1},faxNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, faxNumber)); //NOI18N - - // Is the contact set? - if (null == contact) { - // Throw NPE - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactId() == null) { - // ... and throw again - throw new NullPointerException("contact.contactId is null"); //NOI18N - } else if (contact.getContactId() < 1) { - // Invalid id number - throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N - } else if (contact.getContactFaxNumber() == null) { - // Not set cell phone instance - throw new PhoneNumberNotLinkedException(faxNumber); - } else if (contact.getContactFaxNumber().getPhoneId() == null) { - // Throw NPE again - throw new NullPointerException("contact.contactFaxNumber.phoneId is null"); //NOI18N - } else if (contact.getContactFaxNumber().getPhoneId() < 1) { - // Invalid id number - throw new IllegalArgumentException(MessageFormat.format("contact.contactFaxNumber.phoneId={0} is invalid.", contact.getContactFaxNumber().getPhoneId())); //NOI18N - } else if (!Objects.equals(faxNumber.getPhoneId(), contact.getContactFaxNumber().getPhoneId())) { - // Not same object - throw new IllegalArgumentException(MessageFormat.format("contact.contactFaxNumber.phoneId={0} and faxNumber.phoneId={1} are not the same.", contact.getContactFaxNumber().getPhoneId(), faxNumber.getPhoneId())); //NOI18N - } - - // Remove it from contact - contact.setContactFaxNumber(null); - - // Update database - Contact updatedContact = this.contactBean.updateContactData(contact); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkFaxDataFromContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N - - // Return it - return updatedContact; - } - - @Override - public Contact unlinkLandLineDataFromContact (final Contact contact, final DialableLandLineNumber landLineNumber) throws PhoneNumberNotLinkedException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkLandLineDataFromContact: contact={1},landLineNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, landLineNumber)); //NOI18N - - // Is the contact set? - if (null == contact) { - // Throw NPE - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactId() == null) { - // ... and throw again - throw new NullPointerException("contact.contactId is null"); //NOI18N - } else if (contact.getContactId() < 1) { - // Invalid id number - throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N - } else if (contact.getContactLandLineNumber() == null) { - // Not set cell phone instance - throw new PhoneNumberNotLinkedException(landLineNumber); - } else if (contact.getContactLandLineNumber().getPhoneId() == null) { - // Throw NPE again - throw new NullPointerException("contact.contactLandLineNumber.phoneId is null"); //NOI18N - } else if (contact.getContactLandLineNumber().getPhoneId() < 1) { - // Invalid id number - throw new IllegalArgumentException(MessageFormat.format("contact.contactLandLineNumber.phoneId={0} is invalid.", contact.getContactLandLineNumber().getPhoneId())); //NOI18N - } else if (!Objects.equals(landLineNumber.getPhoneId(), contact.getContactLandLineNumber().getPhoneId())) { - // Not same object - throw new IllegalArgumentException(MessageFormat.format("contact.contactLandLineNumber.phoneId={0} and landLineNumber.phoneId={1} are not the same.", contact.getContactLandLineNumber().getPhoneId(), landLineNumber.getPhoneId())); //NOI18N - } - - // Remove it from contact - contact.setContactLandLineNumber(null); - - // Update database - Contact updatedContact = this.contactBean.updateContactData(contact); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkLandLineDataFromContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N - - // Return it - return updatedContact; - } - - @Override - public Contact unlinkMobileDataFromContact (final Contact contact, final DialableMobileNumber mobileNumber) throws PhoneNumberNotLinkedException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkMobileDataFromContact: contact={1},mobileNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, mobileNumber)); //NOI18N - - // Is the contact set? - if (null == contact) { - // Throw NPE - throw new NullPointerException("contact is null"); //NOI18N - } else if (contact.getContactId() == null) { - // ... and throw again - throw new NullPointerException("contact.contactId is null"); //NOI18N - } else if (contact.getContactId() < 1) { - // Invalid id number - throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N - } else if (contact.getContactMobileNumber() == null) { - // Not set cell phone instance - throw new PhoneNumberNotLinkedException(mobileNumber); - } else if (contact.getContactMobileNumber().getPhoneId() == null) { - // Throw NPE again - throw new NullPointerException("contact.contactMobileNumber.phoneId is null"); //NOI18N - } else if (contact.getContactMobileNumber().getPhoneId() < 1) { - // Invalid id number - throw new IllegalArgumentException(MessageFormat.format("contact.contactMobileNumber.phoneId={0} is invalid.", contact.getContactMobileNumber().getPhoneId())); //NOI18N - } else if (!Objects.equals(mobileNumber.getPhoneId(), contact.getContactMobileNumber().getPhoneId())) { - // Not same object - throw new IllegalArgumentException(MessageFormat.format("contact.contactMobileNumber.phoneId={0} and mobileNumber.phoneId={1} are not the same.", contact.getContactMobileNumber().getPhoneId(), mobileNumber.getPhoneId())); //NOI18N - } - - // Remove it from contact - contact.setContactMobileNumber(null); - - // Update database - Contact updatedContact = this.contactBean.updateContactData(contact); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkMobileDataFromContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N - - // Return it - return updatedContact; - } - -} diff --git a/src/java/org/mxchange/jcontacts/phone/FinancialsAdminContactPhoneSessionBean.java b/src/java/org/mxchange/jcontacts/phone/FinancialsAdminContactPhoneSessionBean.java new file mode 100644 index 0000000..5244ad1 --- /dev/null +++ b/src/java/org/mxchange/jcontacts/phone/FinancialsAdminContactPhoneSessionBean.java @@ -0,0 +1,508 @@ +/* + * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. + */ +package org.mxchange.jcontacts.phone; + +import java.text.MessageFormat; +import java.util.GregorianCalendar; +import java.util.Objects; +import javax.ejb.EJB; +import javax.ejb.Stateless; +import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean; +import org.mxchange.jcontacts.contact.Contact; +import org.mxchange.jcontacts.contact.ContactSessionBeanRemote; +import org.mxchange.jphone.exceptions.PhoneNumberAlreadyLinkedException; +import org.mxchange.jphone.exceptions.PhoneNumberNotLinkedException; +import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; +import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; +import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber; + +/** + * A session EJB for administrative contact's phone number purposes + * <p> + * @author Roland Häder<roland@mxchange.org> + */ +@Stateless (name = "adminContactPhone", description = "An administrative bean handling contact's phone data") +public class FinancialsAdminContactPhoneSessionBean extends BaseFinancialsDatabaseBean implements AdminContactsPhoneSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 189_217_561_460_237_108L; + + /** + * Contact EJB + */ + @EJB + private ContactSessionBeanRemote contactBean; + + @Override + public Contact linkExistingFaxNumberWithContact (final Contact contact, final DialableFaxNumber faxNumber) throws PhoneNumberAlreadyLinkedException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingFaxNumberWithContact: contact={1},faxNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, faxNumber)); //NOI18N + + // Is the contact set? + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { + // ... and throw again + throw new NullPointerException("contact.contactId is null"); //NOI18N + } else if (contact.getContactId() < 1) { + // Invalid id number + throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N + } else if (contact.getContactFaxNumber() instanceof DialableFaxNumber) { + // Not set cell phone instance + throw new PhoneNumberAlreadyLinkedException(faxNumber); + } else if (null == faxNumber) { + // Throw NPE + throw new NullPointerException("faxNumber is null"); //NOI18N + } else if (faxNumber.getPhoneId() == null) { + // Throw it again + throw new NullPointerException("faxNumber.phoneId is null"); //NOI18N + } else if (faxNumber.getPhoneId() < 1) { + // Invalid id + throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneId={0} is not valid", faxNumber.getPhoneId())); //NOI18N + } else if (faxNumber.getPhoneCountry() == null) { + // ... and again + throw new NullPointerException("faxNumber.phoneCountry is null"); //NOI18N + } else if (faxNumber.getPhoneAreaCode() == null) { + // Throw it again + throw new NullPointerException("faxNumber.phoneAreaCode is null"); //NOI18N + } else if (faxNumber.getPhoneAreaCode() < 1) { + // Invalid id + throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneAreaCode={0} is not valid", faxNumber.getPhoneAreaCode())); //NOI18N + } else if (faxNumber.getPhoneNumber() == null) { + // Throw it again + throw new NullPointerException("faxNumber.phoneNumber is null"); //NOI18N + } else if (faxNumber.getPhoneNumber() < 1) { + // Invalid id + throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneNumber={0} is not valid", faxNumber.getPhoneNumber())); //NOI18N + } + + // Set fax number in contact + contact.setContactFaxNumber(faxNumber); + + // Update database + Contact updatedContact = this.contactBean.updateContactData(contact); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingFaxNumberWithContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N + + // Return it + return updatedContact; + } + + @Override + public Contact linkExistingLandLineNumberWithContact (final Contact contact, final DialableLandLineNumber landLineNumber) throws PhoneNumberAlreadyLinkedException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingLandLineNumberWithContact: contact={1},landLineNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, landLineNumber)); //NOI18N + + // Is the contact set? + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { + // ... and throw again + throw new NullPointerException("contact.contactId is null"); //NOI18N + } else if (contact.getContactId() < 1) { + // Invalid id number + throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N + } else if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) { + // Not set cell phone instance + throw new PhoneNumberAlreadyLinkedException(landLineNumber); + } else if (null == landLineNumber) { + // Throw NPE + throw new NullPointerException("landLineNumber is null"); //NOI18N + } else if (landLineNumber.getPhoneId() == null) { + // Throw it again + throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N + } else if (landLineNumber.getPhoneId() < 1) { + // Invalid id + throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneId={0} is not valid", landLineNumber.getPhoneId())); //NOI18N + } else if (landLineNumber.getPhoneCountry() == null) { + // ... and again + throw new NullPointerException("landLineNumber.phoneCountry is null"); //NOI18N + } else if (landLineNumber.getPhoneAreaCode() == null) { + // Throw it again + throw new NullPointerException("landLineNumber.phoneAreaCode is null"); //NOI18N + } else if (landLineNumber.getPhoneAreaCode() < 1) { + // Invalid id + throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneAreaCode={0} is not valid", landLineNumber.getPhoneAreaCode())); //NOI18N + } else if (landLineNumber.getPhoneNumber() == null) { + // Throw it again + throw new NullPointerException("landLineNumber.phoneNumber is null"); //NOI18N + } else if (landLineNumber.getPhoneNumber() < 1) { + // Invalid id + throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneNumber={0} is not valid", landLineNumber.getPhoneNumber())); //NOI18N + } + + // Set land-line number in contact + contact.setContactLandLineNumber(landLineNumber); + + // Update database + Contact updatedContact = this.contactBean.updateContactData(contact); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingLandLineNumberWithContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N + + // Return it + return updatedContact; + } + + @Override + public Contact linkExistingMobileNumberWithContact (final Contact contact, final DialableMobileNumber mobileNumber) throws PhoneNumberAlreadyLinkedException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingMobileNumberWithContact: contact={1},mobileNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, mobileNumber)); //NOI18N + + // Is the contact set? + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { + // ... and throw again + throw new NullPointerException("contact.contactId is null"); //NOI18N + } else if (contact.getContactId() < 1) { + // Invalid id number + throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N + } else if (contact.getContactMobileNumber() instanceof DialableMobileNumber) { + // Not set cell phone instance + throw new PhoneNumberAlreadyLinkedException(mobileNumber); + } else if (null == mobileNumber) { + // Throw NPE + throw new NullPointerException("mobileNumber is null"); //NOI18N + } else if (mobileNumber.getPhoneId() == null) { + // Throw it again + throw new NullPointerException("mobileNumber.phoneId is null"); //NOI18N + } else if (mobileNumber.getPhoneId() < 1) { + // Invalid id + throw new IllegalArgumentException(MessageFormat.format("mobileNumber.phoneId={0} is not valid", mobileNumber.getPhoneId())); //NOI18N + } else if (mobileNumber.getMobileProvider() == null) { + // Throw NPE again + throw new NullPointerException("mobileNumber.mobileProvider is null"); //NOI18N + } else if (mobileNumber.getMobileProvider().getProviderId() == null) { + // Throw NPE again + throw new NullPointerException("mobileNumber.mobileProvider.providerId is null"); //NOI18N + } else if (mobileNumber.getMobileProvider().getProviderId() < 1) { + // Throw NPE again + throw new IllegalArgumentException(MessageFormat.format("mobileNumber.mobileProvider.providerId={0} is not valid", mobileNumber.getMobileProvider().getProviderId())); //NOI18N + } + + // Set mobile number in contact + contact.setContactMobileNumber(mobileNumber); + + // Update database + Contact updatedContact = this.contactBean.updateContactData(contact); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkExistingMobileNumberWithContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N + + // Return it + return updatedContact; + } + + @Override + public Contact linkNewFaxNumberWithContact (final Contact contact, final DialableFaxNumber faxNumber) throws PhoneNumberAlreadyLinkedException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkNewFaxNumberWithContact: contact={1},faxNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, faxNumber)); //NOI18N + + // Is the contact set? + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { + // ... and throw again + throw new NullPointerException("contact.contactId is null"); //NOI18N + } else if (contact.getContactId() < 1) { + // Invalid id number + throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N + } else if (contact.getContactFaxNumber() instanceof DialableFaxNumber) { + // Not set cell phone instance + throw new PhoneNumberAlreadyLinkedException(faxNumber); + } else if (null == faxNumber) { + // Throw NPE + throw new NullPointerException("faxNumber is null"); //NOI18N + } else if (faxNumber.getPhoneId() instanceof Long) { + // Throw it again + throw new IllegalStateException(MessageFormat.format("faxNumber.phoneId={0} is not null", faxNumber.getPhoneId())); //NOI18N + } else if (faxNumber.getPhoneCountry() == null) { + // ... and again + throw new NullPointerException("faxNumber.phoneCountry is null"); //NOI18N + } else if (faxNumber.getPhoneAreaCode() == null) { + // Throw it again + throw new NullPointerException("faxNumber.phoneAreaCode is null"); //NOI18N + } else if (faxNumber.getPhoneAreaCode() < 1) { + // Invalid id + throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneAreaCode={0} is not valid", faxNumber.getPhoneAreaCode())); //NOI18N + } else if (faxNumber.getPhoneNumber() == null) { + // Throw it again + throw new NullPointerException("faxNumber.phoneNumber is null"); //NOI18N + } else if (faxNumber.getPhoneNumber() < 1) { + // Invalid id + throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneNumber={0} is not valid", faxNumber.getPhoneNumber())); //NOI18N + } + + // Set created instance + faxNumber.setPhoneEntryCreated(new GregorianCalendar()); + + // Set fax number in contact + contact.setContactFaxNumber(faxNumber); + + // Update database + Contact updatedContact = this.contactBean.updateContactData(contact); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkNewFaxNumberWithContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N + + // Return it + return updatedContact; + } + + @Override + public Contact linkNewLandLineNumberWithContact (final Contact contact, final DialableLandLineNumber landLineNumber) throws PhoneNumberAlreadyLinkedException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkNewLandLineNumberWithContact: contact={1},landLineNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, landLineNumber)); //NOI18N + + // Is the contact set? + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { + // ... and throw again + throw new NullPointerException("contact.contactId is null"); //NOI18N + } else if (contact.getContactId() < 1) { + // Invalid id number + throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N + } else if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) { + // Not set cell phone instance + throw new PhoneNumberAlreadyLinkedException(landLineNumber); + } else if (null == landLineNumber) { + // Throw NPE + throw new NullPointerException("landLineNumber is null"); //NOI18N + } else if (landLineNumber.getPhoneId() instanceof Long) { + // Throw it again + throw new IllegalStateException(MessageFormat.format("landLineNumber.phoneId={0} is not null", landLineNumber.getPhoneId())); //NOI18N + } else if (landLineNumber.getPhoneCountry() == null) { + // ... and again + throw new NullPointerException("landLineNumber.phoneCountry is null"); //NOI18N + } else if (landLineNumber.getPhoneAreaCode() == null) { + // Throw it again + throw new NullPointerException("landLineNumber.phoneAreaCode is null"); //NOI18N + } else if (landLineNumber.getPhoneAreaCode() < 1) { + // Invalid id + throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneAreaCode={0} is not valid", landLineNumber.getPhoneAreaCode())); //NOI18N + } else if (landLineNumber.getPhoneNumber() == null) { + // Throw it again + throw new NullPointerException("landLineNumber.phoneNumber is null"); //NOI18N + } else if (landLineNumber.getPhoneNumber() < 1) { + // Invalid id + throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneNumber={0} is not valid", landLineNumber.getPhoneNumber())); //NOI18N + } + + // Set created instance + landLineNumber.setPhoneEntryCreated(new GregorianCalendar()); + + // Set landLine number in contact + contact.setContactLandLineNumber(landLineNumber); + + // Update database + Contact updatedContact = this.contactBean.updateContactData(contact); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkNewLandLineNumberWithContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N + + // Return it + return updatedContact; + } + + @Override + public Contact linkNewMobileNumberWithContact (final Contact contact, final DialableMobileNumber mobileNumber) throws PhoneNumberAlreadyLinkedException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkNewMobileNumberWithContact: contact={1},mobileNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, mobileNumber)); //NOI18N + + // Is the contact set? + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { + // ... and throw again + throw new NullPointerException("contact.contactId is null"); //NOI18N + } else if (contact.getContactId() < 1) { + // Invalid id number + throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N + } else if (contact.getContactMobileNumber() instanceof DialableMobileNumber) { + // Not set cell phone instance + throw new PhoneNumberAlreadyLinkedException(mobileNumber); + } else if (null == mobileNumber) { + // Throw NPE + throw new NullPointerException("mobileNumber is null"); //NOI18N + } else if (mobileNumber.getPhoneId() instanceof Long) { + // Throw it again + throw new IllegalStateException(MessageFormat.format("mobileNumber.phoneId={0} is not null", mobileNumber.getPhoneId())); //NOI18N + } else if (mobileNumber.getMobileProvider() == null) { + // Throw NPE again + throw new NullPointerException("mobileNumber.mobileProvider is null"); //NOI18N + } else if (mobileNumber.getMobileProvider().getProviderId() == null) { + // Throw NPE again + throw new NullPointerException("mobileNumber.mobileProvider.providerId is null"); //NOI18N + } else if (mobileNumber.getMobileProvider().getProviderId() < 1) { + // Throw NPE again + throw new IllegalArgumentException(MessageFormat.format("mobileNumber.mobileProvider.providerId={0} is not valid", mobileNumber.getMobileProvider().getProviderId())); //NOI18N + } + + // Set created instance + mobileNumber.setPhoneEntryCreated(new GregorianCalendar()); + + // Set mobile number in contact + contact.setContactMobileNumber(mobileNumber); + + // Update database + Contact updatedContact = this.contactBean.updateContactData(contact); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkNewMobileNumberWithContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N + + // Return it + return updatedContact; + } + + @Override + public Contact unlinkFaxDataFromContact (final Contact contact, final DialableFaxNumber faxNumber) throws PhoneNumberNotLinkedException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkFaxDataFromContact: contact={1},faxNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, faxNumber)); //NOI18N + + // Is the contact set? + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { + // ... and throw again + throw new NullPointerException("contact.contactId is null"); //NOI18N + } else if (contact.getContactId() < 1) { + // Invalid id number + throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N + } else if (contact.getContactFaxNumber() == null) { + // Not set cell phone instance + throw new PhoneNumberNotLinkedException(faxNumber); + } else if (contact.getContactFaxNumber().getPhoneId() == null) { + // Throw NPE again + throw new NullPointerException("contact.contactFaxNumber.phoneId is null"); //NOI18N + } else if (contact.getContactFaxNumber().getPhoneId() < 1) { + // Invalid id number + throw new IllegalArgumentException(MessageFormat.format("contact.contactFaxNumber.phoneId={0} is invalid.", contact.getContactFaxNumber().getPhoneId())); //NOI18N + } else if (!Objects.equals(faxNumber.getPhoneId(), contact.getContactFaxNumber().getPhoneId())) { + // Not same object + throw new IllegalArgumentException(MessageFormat.format("contact.contactFaxNumber.phoneId={0} and faxNumber.phoneId={1} are not the same.", contact.getContactFaxNumber().getPhoneId(), faxNumber.getPhoneId())); //NOI18N + } + + // Remove it from contact + contact.setContactFaxNumber(null); + + // Update database + Contact updatedContact = this.contactBean.updateContactData(contact); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkFaxDataFromContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N + + // Return it + return updatedContact; + } + + @Override + public Contact unlinkLandLineDataFromContact (final Contact contact, final DialableLandLineNumber landLineNumber) throws PhoneNumberNotLinkedException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkLandLineDataFromContact: contact={1},landLineNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, landLineNumber)); //NOI18N + + // Is the contact set? + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { + // ... and throw again + throw new NullPointerException("contact.contactId is null"); //NOI18N + } else if (contact.getContactId() < 1) { + // Invalid id number + throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N + } else if (contact.getContactLandLineNumber() == null) { + // Not set cell phone instance + throw new PhoneNumberNotLinkedException(landLineNumber); + } else if (contact.getContactLandLineNumber().getPhoneId() == null) { + // Throw NPE again + throw new NullPointerException("contact.contactLandLineNumber.phoneId is null"); //NOI18N + } else if (contact.getContactLandLineNumber().getPhoneId() < 1) { + // Invalid id number + throw new IllegalArgumentException(MessageFormat.format("contact.contactLandLineNumber.phoneId={0} is invalid.", contact.getContactLandLineNumber().getPhoneId())); //NOI18N + } else if (!Objects.equals(landLineNumber.getPhoneId(), contact.getContactLandLineNumber().getPhoneId())) { + // Not same object + throw new IllegalArgumentException(MessageFormat.format("contact.contactLandLineNumber.phoneId={0} and landLineNumber.phoneId={1} are not the same.", contact.getContactLandLineNumber().getPhoneId(), landLineNumber.getPhoneId())); //NOI18N + } + + // Remove it from contact + contact.setContactLandLineNumber(null); + + // Update database + Contact updatedContact = this.contactBean.updateContactData(contact); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkLandLineDataFromContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N + + // Return it + return updatedContact; + } + + @Override + public Contact unlinkMobileDataFromContact (final Contact contact, final DialableMobileNumber mobileNumber) throws PhoneNumberNotLinkedException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkMobileDataFromContact: contact={1},mobileNumber={2} - CALLED!", this.getClass().getSimpleName(), contact, mobileNumber)); //NOI18N + + // Is the contact set? + if (null == contact) { + // Throw NPE + throw new NullPointerException("contact is null"); //NOI18N + } else if (contact.getContactId() == null) { + // ... and throw again + throw new NullPointerException("contact.contactId is null"); //NOI18N + } else if (contact.getContactId() < 1) { + // Invalid id number + throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid.", contact.getContactId())); //NOI18N + } else if (contact.getContactMobileNumber() == null) { + // Not set cell phone instance + throw new PhoneNumberNotLinkedException(mobileNumber); + } else if (contact.getContactMobileNumber().getPhoneId() == null) { + // Throw NPE again + throw new NullPointerException("contact.contactMobileNumber.phoneId is null"); //NOI18N + } else if (contact.getContactMobileNumber().getPhoneId() < 1) { + // Invalid id number + throw new IllegalArgumentException(MessageFormat.format("contact.contactMobileNumber.phoneId={0} is invalid.", contact.getContactMobileNumber().getPhoneId())); //NOI18N + } else if (!Objects.equals(mobileNumber.getPhoneId(), contact.getContactMobileNumber().getPhoneId())) { + // Not same object + throw new IllegalArgumentException(MessageFormat.format("contact.contactMobileNumber.phoneId={0} and mobileNumber.phoneId={1} are not the same.", contact.getContactMobileNumber().getPhoneId(), mobileNumber.getPhoneId())); //NOI18N + } + + // Remove it from contact + contact.setContactMobileNumber(null); + + // Update database + Contact updatedContact = this.contactBean.updateContactData(contact); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.unlinkMobileDataFromContact: updatedContact={1} - EXIT!", this.getClass().getSimpleName(), updatedContact)); //NOI18N + + // Return it + return updatedContact; + } + +} diff --git a/src/java/org/mxchange/jcountry/data/AddressbookCountrySingletonBean.java b/src/java/org/mxchange/jcountry/data/AddressbookCountrySingletonBean.java deleted file mode 100644 index 4797f4e..0000000 --- a/src/java/org/mxchange/jcountry/data/AddressbookCountrySingletonBean.java +++ /dev/null @@ -1,149 +0,0 @@ -/* - * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. -*/ -package org.mxchange.jcountry.data; - -import java.text.MessageFormat; -import java.util.GregorianCalendar; -import java.util.List; -import javax.ejb.Singleton; -import javax.ejb.Startup; -import javax.persistence.NoResultException; -import javax.persistence.Query; -import org.mxchange.jcoreee.database.BaseDatabaseBean; -import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException; - -/** - * A singleton EJB for country informations - * <p> - * @author Roland Häder<roland@mxchange.org> - */ -@Startup -@Singleton (name = "country", description = "A singleton session bean for country informations") -public class AddressbookCountrySingletonBean extends BaseDatabaseBean implements CountrySingletonBeanRemote { - - /** - * Serial number - */ - private static final long serialVersionUID = 15_846_983_298_691_207L; - - @Override - public Country addCountry (final Country country) throws CountryAlreadyAddedException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - CALLED!", this.getClass().getSimpleName(), country)); //NOI18N - - // Is it already there? - if (null == country) { - // Throw NPE - throw new NullPointerException("country is null"); //NOI18N - } else if (country.getCountryCode().isEmpty()) { - // Code is not set - throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N - } else if (country.getCountryI18nKey().isEmpty()) { - // I18n key is not set - throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N - } else if (country.getCountryId() != null) { - // Should be null - throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N - } else if (this.isCountryAdded(country)) { - // Yes, then abort here - throw new CountryAlreadyAddedException(country); - } - - // Add timestamp - country.setCountryEntryCreated(new GregorianCalendar()); - - // It is not added, so persist it - this.getEntityManager().persist(country); - - // Flush it to get id number back, maybe it is directly needed? - this.getEntityManager().flush(); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - EXIT!", this.getClass().getSimpleName(), country)); //NOI18N - - // Return updated instance - return country; - } - - @Override - @SuppressWarnings ("unchecked") - public List<Country> allCountries () { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allCountries: CALLED!", this.getClass().getSimpleName())); //NOI18N - - // Init query - Query query = this.getEntityManager().createNamedQuery("AllCountries", CountryData.class); //NOI18N - - // Get list - List<Country> countries = query.getResultList(); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allCountries: countries.size()={1} - EXIT!", this.getClass().getSimpleName(), countries.size())); //NOI18N - - // Return it - return countries; - } - - /** - * Checks whether given country is already added by i18n key or country - * code, what comes first. - * <p> - * @param country Country instance to check - * <p> - * @return Whether the country was found - */ - private boolean isCountryAdded (final Country country) { - if (null == country) { - // Throw NPE - throw new NullPointerException("country is null"); //NOI18N - } else if (country.getCountryCode().isEmpty()) { - // Code is not set - throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N - } else if (country.getCountryI18nKey().isEmpty()) { - // I18n key is not set - throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N - } else if (country.getCountryId() != null) { - // Should be null - throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N - } - - // Default is not found - boolean isAdded = false; - - // Get query instance - Query query = this.getEntityManager().createNamedQuery("SearchCountryByCodeI18nKey", CountryData.class); //NOI18N - - // Assign all parameters - query.setParameter("code", country.getCountryCode()); //NOI18N - query.setParameter("key", country.getCountryI18nKey()); //NOI18N - - // Try to get a single result - try { - // Get single result - Country foundCountry = (Country) query.getSingleResult(); - - // Found it? - isAdded = (foundCountry instanceof Country); - } catch (final NoResultException ex) { - // Not found, don't log this - } - - // Return result - return isAdded; - } - -} diff --git a/src/java/org/mxchange/jcountry/data/FinancialsCountrySingletonBean.java b/src/java/org/mxchange/jcountry/data/FinancialsCountrySingletonBean.java new file mode 100644 index 0000000..3fce940 --- /dev/null +++ b/src/java/org/mxchange/jcountry/data/FinancialsCountrySingletonBean.java @@ -0,0 +1,149 @@ +/* + * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. +*/ +package org.mxchange.jcountry.data; + +import java.text.MessageFormat; +import java.util.GregorianCalendar; +import java.util.List; +import javax.ejb.Singleton; +import javax.ejb.Startup; +import javax.persistence.NoResultException; +import javax.persistence.Query; +import org.mxchange.jcoreee.database.BaseDatabaseBean; +import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException; + +/** + * A singleton EJB for country informations + * <p> + * @author Roland Häder<roland@mxchange.org> + */ +@Startup +@Singleton (name = "country", description = "A singleton session bean for country informations") +public class FinancialsCountrySingletonBean extends BaseDatabaseBean implements CountrySingletonBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 15_846_983_298_691_207L; + + @Override + public Country addCountry (final Country country) throws CountryAlreadyAddedException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - CALLED!", this.getClass().getSimpleName(), country)); //NOI18N + + // Is it already there? + if (null == country) { + // Throw NPE + throw new NullPointerException("country is null"); //NOI18N + } else if (country.getCountryCode().isEmpty()) { + // Code is not set + throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N + } else if (country.getCountryI18nKey().isEmpty()) { + // I18n key is not set + throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N + } else if (country.getCountryId() != null) { + // Should be null + throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N + } else if (this.isCountryAdded(country)) { + // Yes, then abort here + throw new CountryAlreadyAddedException(country); + } + + // Add timestamp + country.setCountryEntryCreated(new GregorianCalendar()); + + // It is not added, so persist it + this.getEntityManager().persist(country); + + // Flush it to get id number back, maybe it is directly needed? + this.getEntityManager().flush(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addCountry: country={1} - EXIT!", this.getClass().getSimpleName(), country)); //NOI18N + + // Return updated instance + return country; + } + + @Override + @SuppressWarnings ("unchecked") + public List<Country> allCountries () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allCountries: CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Init query + Query query = this.getEntityManager().createNamedQuery("AllCountries", CountryData.class); //NOI18N + + // Get list + List<Country> countries = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allCountries: countries.size()={1} - EXIT!", this.getClass().getSimpleName(), countries.size())); //NOI18N + + // Return it + return countries; + } + + /** + * Checks whether given country is already added by i18n key or country + * code, what comes first. + * <p> + * @param country Country instance to check + * <p> + * @return Whether the country was found + */ + private boolean isCountryAdded (final Country country) { + if (null == country) { + // Throw NPE + throw new NullPointerException("country is null"); //NOI18N + } else if (country.getCountryCode().isEmpty()) { + // Code is not set + throw new IllegalArgumentException("country.countryCode is empty"); //NOI18N + } else if (country.getCountryI18nKey().isEmpty()) { + // I18n key is not set + throw new IllegalArgumentException("country.countryI18nKey is empty"); //NOI18N + } else if (country.getCountryId() != null) { + // Should be null + throw new IllegalArgumentException(MessageFormat.format("country.countryId is not null ({0})", country.getCountryId())); //NOI18N + } + + // Default is not found + boolean isAdded = false; + + // Get query instance + Query query = this.getEntityManager().createNamedQuery("SearchCountryByCodeI18nKey", CountryData.class); //NOI18N + + // Assign all parameters + query.setParameter("code", country.getCountryCode()); //NOI18N + query.setParameter("key", country.getCountryI18nKey()); //NOI18N + + // Try to get a single result + try { + // Get single result + Country foundCountry = (Country) query.getSingleResult(); + + // Found it? + isAdded = (foundCountry instanceof Country); + } catch (final NoResultException ex) { + // Not found, don't log this + } + + // Return result + return isAdded; + } + +} diff --git a/src/java/org/mxchange/jfinancials/beans/resendlink/FinancialsResendLinkSessionBean.java b/src/java/org/mxchange/jfinancials/beans/resendlink/FinancialsResendLinkSessionBean.java new file mode 100644 index 0000000..53463a2 --- /dev/null +++ b/src/java/org/mxchange/jfinancials/beans/resendlink/FinancialsResendLinkSessionBean.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. + */ +package org.mxchange.jfinancials.beans.resendlink; + +import java.text.MessageFormat; +import java.util.Locale; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.mail.Address; +import javax.mail.internet.AddressException; +import javax.mail.internet.InternetAddress; +import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean; +import org.mxchange.jusercore.model.user.User; +import org.mxchange.jusercore.model.user.status.UserAccountStatus; + +/** + * A session-based EJB for resending confirmation links + * <p> + * @author Roland Häder<roland@mxchange.org> + */ +@Stateless (name = "resendLink", description = "A bean resending confirmation links") +public class FinancialsResendLinkSessionBean extends BaseFinancialsDatabaseBean implements ResendLinkSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 71_546_726_857_195_360L; + + @Override + public void resendConfirmationLink (final User user, final Locale locale, final String baseUrl) { + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.resendConfirmationLink: user={1},locale={2},baseUrl={3} - CALLED!", this.getClass().getSimpleName(), user, locale, baseUrl)); //NOI18N + + // The user instance should be valid + if (null == user) { + // Throw NPE + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Throw NPE 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 (user.getUserConfirmKey() == null) { + // Throw NPE again + throw new NullPointerException("this.userConfirmKey is null"); //NOI18N + } else if (user.getUserAccountStatus() != UserAccountStatus.UNCONFIRMED) { + // User account status is not UNCONFIRMED + throw new IllegalStateException(MessageFormat.format("Account status from user.userId={0} is not UNCONFIRMED:{1}", user.getUserId(), user.getUserAccountStatus())); //NOI18N + } else if (null == locale) { + // Locale should be set + throw new NullPointerException("locale is null"); //NOI18N + } + + // @TODO Unfinished: Change key + merge database + + // Init variable + Address emailAddress; + + try { + // Create email address and set + emailAddress = new InternetAddress(user.getUserContact().getContactEmailAddress()); + } catch (final AddressException ex) { + // Throw again + throw new EJBException(ex); + } + + // Send email + // TODO: Internationlize the subject line somehow + this.sendEmail("Resend confirmation link", "resend_confirmation_link", emailAddress, user, baseUrl); //NOI18N + + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.resendConfirmationLink: EXIT!", this.getClass().getSimpleName())); //NOI18N + } + +} diff --git a/src/java/org/mxchange/jfinancials/mailer/model/delivery/FinancialsEmailDeliveryMessageBean.java b/src/java/org/mxchange/jfinancials/mailer/model/delivery/FinancialsEmailDeliveryMessageBean.java new file mode 100644 index 0000000..cb33025 --- /dev/null +++ b/src/java/org/mxchange/jfinancials/mailer/model/delivery/FinancialsEmailDeliveryMessageBean.java @@ -0,0 +1,188 @@ +/* + * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. + */ +package org.mxchange.jfinancials.mailer.model.delivery; + +import java.io.Serializable; +import java.text.MessageFormat; +import java.util.Properties; +import java.util.ResourceBundle; +import javax.annotation.PostConstruct; +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 javax.mail.MessagingException; +import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean; +import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery; + +/** + * A message-driven bean for sending out emails + * <p> + * @author Roland Häder<roland@mxchange.org> + */ +@MessageDriven (activationConfig = { + @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/addressbook-email-queue"), + @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue") +}) +public class FinancialsEmailDeliveryMessageBean extends BaseFinancialsDatabaseBean implements MessageListener { + + /** + * Serial number + */ + private static final long serialVersionUID = 75_638_176_619_024L; + + /** + * Configuration file + */ + private final String configFile = "org.mxchange.jmailer.config"; //NOI18N//NOI18N + + /** + * Mailer instance + */ + private final DeliverableFinancialsEmail mailer; + + /** + * Default constructor + */ + public FinancialsEmailDeliveryMessageBean () { + // Init mailer instance + this.mailer = new FinancialsMailer(); + } + + /** + * Post-construction + */ + @PostConstruct + public void init () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Try to load bundle + ResourceBundle bundle = ResourceBundle.getBundle(this.configFile); + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: bundle={1}", this.getClass().getSimpleName(), bundle)); //NOI18N + + // The bunble should be valid + if (null == bundle) { + // Throw NPE + throw new NullPointerException(MessageFormat.format("bundle is null, maybe file {0} does not exist?", this.configFile)); //NOI18N + } + + // Init Properties + Properties properties = new Properties(); + + // Is the bundle not empty? + if (!bundle.keySet().isEmpty()) { + // Loop through all + for (final String key : bundle.keySet()) { + // Log debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: key={1}", this.getClass().getSimpleName(), key)); //NOI18N + + // Get string from bundle and set it in properties + properties.put(key, bundle.getString(key)); + } + } + + // Handle it over to the mailer + this.mailer.init(properties); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: EXIT!", this.getClass().getSimpleName())); //NOI18N + } + + @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 WrapableEmailDelivery)) { + // Not correct object send + throw new IllegalArgumentException(MessageFormat.format("serializable={0} does not implement WrapableEmailDelivery", serializable)); //NOI18N + } + + // Securely cast it + WrapableEmailDelivery wrapper = (WrapableEmailDelivery) serializable; + + // Is all required set? + if (wrapper.getLocale() == null) { + // Throw NPE + throw new NullPointerException("wrapper.locale is null"); //NOI18N + } else if (wrapper.getRecipient() == null) { + // Throw again ... + throw new NullPointerException("wrapper.recipient is null"); //NOI18N + } else if (wrapper.getSubjectLine() == null) { + // ... and again + throw new NullPointerException("wrapper.subjectLine is null"); //NOI18N + } else if (wrapper.getSubjectLine().isEmpty()) { + // Is empty + throw new IllegalArgumentException("wrapper.subjectLine is empty"); //NOI18N + } else if (wrapper.getTemplateName() == null) { + // Throw NPE again + throw new NullPointerException("wrapper.templateName is null"); //NOI18N + } else if (wrapper.getTemplateName().isEmpty()) { + // Is empty + throw new IllegalArgumentException("wrapper.templateName is empty"); //NOI18N + } + + try { + // Send email out + this.mailer.sendDeliverableMail(wrapper); + } catch (final MessagingException ex) { + // Opps, something went wrong + this.getLoggerBeanLocal().logException(ex); + return; + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage - EXIT!", this.getClass().getSimpleName())); //NOI18N + } + +} diff --git a/src/java/org/mxchange/jphone/phonenumbers/mobileprovider/AddressbookAdminMobileProviderSessionBean.java b/src/java/org/mxchange/jphone/phonenumbers/mobileprovider/AddressbookAdminMobileProviderSessionBean.java deleted file mode 100644 index b9939e3..0000000 --- a/src/java/org/mxchange/jphone/phonenumbers/mobileprovider/AddressbookAdminMobileProviderSessionBean.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. - */ -package org.mxchange.jphone.phonenumbers.mobileprovider; - -import java.text.MessageFormat; -import java.util.GregorianCalendar; -import javax.ejb.Stateless; -import org.mxchange.jcoreee.database.BaseDatabaseBean; -import org.mxchange.jphone.exceptions.MobileProviderAlreadyAddedException; - -/** - * An administrative singleton EJB for mobile provider informations - * <p> - * @author Roland Häder<roland@mxchange.org> - */ -@Stateless (name = "adminMobileProvider", description = "A singleton session bean for mobile provider informations, admin-edition") -public class AddressbookAdminMobileProviderSessionBean extends BaseDatabaseBean implements AdminMobileProviderSessionBeanRemote { - - /** - * Serial number - */ - private static final long serialVersionUID = 15_846_983_298_691_207L; - - @Override - public MobileProvider addMobileProvider (final MobileProvider mobileProvider) throws MobileProviderAlreadyAddedException { - // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addMobileProvider: mobileProvider={1} - CALLED!", this.getClass().getSimpleName(), mobileProvider)); //NOI18N - - // Is the instance valid? - if (null == mobileProvider) { - // Throw NPE - throw new NullPointerException("mobileProvider is null"); //NOI18N - } else if (mobileProvider.getProviderDialPrefix() == null) { - // Throw NPE again - throw new NullPointerException("mobileProvider.providerDialPrefix is null"); //NOI18N - } else if (mobileProvider.getProviderDialPrefix() < 1) { - // Not valid - throw new IllegalArgumentException(MessageFormat.format("mobileProvider.providerDialPrefix={0} is not valid.", mobileProvider.getProviderDialPrefix())); //NOI18N - } else if (mobileProvider.getProviderCountry() == null) { - // Throw again a NPE - throw new NullPointerException("mobileProvider.providerCountry is null"); //NOI18N - } else if (mobileProvider.getProviderMailPattern() == null) { - // ... and again ... - throw new NullPointerException("mobileProvider.providerMailPattern is null"); //NOI18N - } else if (mobileProvider.getProviderMailPattern().isEmpty()) { - // Empty pattern set (not allowed) - throw new IllegalArgumentException("mobileProvider.providerMailPattern is empty."); //NOI18N - } else if (!mobileProvider.getProviderMailPattern().contains("%s")) { //NOI18N - // No place-holder found - throw new IllegalArgumentException(MessageFormat.format("mobileProvider.providerMailPattern={0} does not contain '%s' which is need to be replaced with the full mobile number.", mobileProvider.getProviderMailPattern())); //NOI18N - } else if (mobileProvider.getProviderName() == null) { - // Throw NPE again - throw new NullPointerException("mobileProvider.providerName is null"); //NOI18N - } else if (mobileProvider.getProviderName().isEmpty()) { - // Empty name is not allowed - throw new IllegalArgumentException("mobileProvider.providerName is empty"); //NOI18N - } - - // Set creation timestamp - mobileProvider.setProviderEntryCreated(new GregorianCalendar()); - - // Persist it - this.getEntityManager().persist(mobileProvider); - - // ... and flush it to get id back - this.getEntityManager().flush(); - - // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addMobileProvider: mobileProvider.providerId={1} - EXIT!", this.getClass().getSimpleName(), mobileProvider.getProviderId())); //NOI18N - - // Return updated - return mobileProvider; - } - -} diff --git a/src/java/org/mxchange/jphone/phonenumbers/mobileprovider/AddressbookMobileProviderSingletonBean.java b/src/java/org/mxchange/jphone/phonenumbers/mobileprovider/AddressbookMobileProviderSingletonBean.java deleted file mode 100644 index 4f0122e..0000000 --- a/src/java/org/mxchange/jphone/phonenumbers/mobileprovider/AddressbookMobileProviderSingletonBean.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. - */ -package org.mxchange.jphone.phonenumbers.mobileprovider; - -import java.text.MessageFormat; -import java.util.List; -import javax.ejb.Singleton; -import javax.ejb.Startup; -import javax.persistence.Query; -import org.mxchange.jcoreee.database.BaseDatabaseBean; - -/** - * A singleton EJB for mobile provider informations - * <p> - * @author Roland Häder<roland@mxchange.org> - */ -@Startup -@Singleton (name = "mobileprovider", description = "A singleton session bean for SMS provider informations") -public class AddressbookMobileProviderSingletonBean extends BaseDatabaseBean implements MobileProviderSingletonBeanRemote { - - /** - * Serial number - */ - private static final long serialVersionUID = 15_846_983_298_691_207L; - - @Override - @SuppressWarnings ("unchecked") - public List<MobileProvider> allMobileProvider () { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMobileProvider: CALLED!", this.getClass().getSimpleName())); //NOI18N - - // Init query - Query query = this.getEntityManager().createNamedQuery("AllMobileProvider", CellphoneProvider.class); //NOI18N - - // Get list from it - List<MobileProvider> mobileProviders = query.getResultList(); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMobileProvider: mobileProviders.size()={1} - EXIT!", this.getClass().getSimpleName(), mobileProviders.size())); //NOI18N - - // Return it - return mobileProviders; - } - -} diff --git a/src/java/org/mxchange/jphone/phonenumbers/mobileprovider/FinancialsAdminMobileProviderSessionBean.java b/src/java/org/mxchange/jphone/phonenumbers/mobileprovider/FinancialsAdminMobileProviderSessionBean.java new file mode 100644 index 0000000..74986cd --- /dev/null +++ b/src/java/org/mxchange/jphone/phonenumbers/mobileprovider/FinancialsAdminMobileProviderSessionBean.java @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. + */ +package org.mxchange.jphone.phonenumbers.mobileprovider; + +import java.text.MessageFormat; +import java.util.GregorianCalendar; +import javax.ejb.Stateless; +import org.mxchange.jcoreee.database.BaseDatabaseBean; +import org.mxchange.jphone.exceptions.MobileProviderAlreadyAddedException; + +/** + * An administrative singleton EJB for mobile provider informations + * <p> + * @author Roland Häder<roland@mxchange.org> + */ +@Stateless (name = "adminMobileProvider", description = "A singleton session bean for mobile provider informations, admin-edition") +public class FinancialsAdminMobileProviderSessionBean extends BaseDatabaseBean implements AdminMobileProviderSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 15_846_983_298_691_207L; + + @Override + public MobileProvider addMobileProvider (final MobileProvider mobileProvider) throws MobileProviderAlreadyAddedException { + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addMobileProvider: mobileProvider={1} - CALLED!", this.getClass().getSimpleName(), mobileProvider)); //NOI18N + + // Is the instance valid? + if (null == mobileProvider) { + // Throw NPE + throw new NullPointerException("mobileProvider is null"); //NOI18N + } else if (mobileProvider.getProviderDialPrefix() == null) { + // Throw NPE again + throw new NullPointerException("mobileProvider.providerDialPrefix is null"); //NOI18N + } else if (mobileProvider.getProviderDialPrefix() < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("mobileProvider.providerDialPrefix={0} is not valid.", mobileProvider.getProviderDialPrefix())); //NOI18N + } else if (mobileProvider.getProviderCountry() == null) { + // Throw again a NPE + throw new NullPointerException("mobileProvider.providerCountry is null"); //NOI18N + } else if (mobileProvider.getProviderMailPattern() == null) { + // ... and again ... + throw new NullPointerException("mobileProvider.providerMailPattern is null"); //NOI18N + } else if (mobileProvider.getProviderMailPattern().isEmpty()) { + // Empty pattern set (not allowed) + throw new IllegalArgumentException("mobileProvider.providerMailPattern is empty."); //NOI18N + } else if (!mobileProvider.getProviderMailPattern().contains("%s")) { //NOI18N + // No place-holder found + throw new IllegalArgumentException(MessageFormat.format("mobileProvider.providerMailPattern={0} does not contain '%s' which is need to be replaced with the full mobile number.", mobileProvider.getProviderMailPattern())); //NOI18N + } else if (mobileProvider.getProviderName() == null) { + // Throw NPE again + throw new NullPointerException("mobileProvider.providerName is null"); //NOI18N + } else if (mobileProvider.getProviderName().isEmpty()) { + // Empty name is not allowed + throw new IllegalArgumentException("mobileProvider.providerName is empty"); //NOI18N + } + + // Set creation timestamp + mobileProvider.setProviderEntryCreated(new GregorianCalendar()); + + // Persist it + this.getEntityManager().persist(mobileProvider); + + // ... and flush it to get id back + this.getEntityManager().flush(); + + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addMobileProvider: mobileProvider.providerId={1} - EXIT!", this.getClass().getSimpleName(), mobileProvider.getProviderId())); //NOI18N + + // Return updated + return mobileProvider; + } + +} diff --git a/src/java/org/mxchange/jphone/phonenumbers/mobileprovider/FinancialsMobileProviderSingletonBean.java b/src/java/org/mxchange/jphone/phonenumbers/mobileprovider/FinancialsMobileProviderSingletonBean.java new file mode 100644 index 0000000..f469d0a --- /dev/null +++ b/src/java/org/mxchange/jphone/phonenumbers/mobileprovider/FinancialsMobileProviderSingletonBean.java @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. + */ +package org.mxchange.jphone.phonenumbers.mobileprovider; + +import java.text.MessageFormat; +import java.util.List; +import javax.ejb.Singleton; +import javax.ejb.Startup; +import javax.persistence.Query; +import org.mxchange.jcoreee.database.BaseDatabaseBean; + +/** + * A singleton EJB for mobile provider informations + * <p> + * @author Roland Häder<roland@mxchange.org> + */ +@Startup +@Singleton (name = "mobileprovider", description = "A singleton session bean for SMS provider informations") +public class FinancialsMobileProviderSingletonBean extends BaseDatabaseBean implements MobileProviderSingletonBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 15_846_983_298_691_207L; + + @Override + @SuppressWarnings ("unchecked") + public List<MobileProvider> allMobileProvider () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMobileProvider: CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Init query + Query query = this.getEntityManager().createNamedQuery("AllMobileProvider", CellphoneProvider.class); //NOI18N + + // Get list from it + List<MobileProvider> mobileProviders = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMobileProvider: mobileProviders.size()={1} - EXIT!", this.getClass().getSimpleName(), mobileProviders.size())); //NOI18N + + // Return it + return mobileProviders; + } + +} diff --git a/src/java/org/mxchange/jphone/phonenumbers/phone/AddressbookAdminPhoneSessionBean.java b/src/java/org/mxchange/jphone/phonenumbers/phone/AddressbookAdminPhoneSessionBean.java deleted file mode 100644 index d4fe99f..0000000 --- a/src/java/org/mxchange/jphone/phonenumbers/phone/AddressbookAdminPhoneSessionBean.java +++ /dev/null @@ -1,230 +0,0 @@ -/* - * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. - */ -package org.mxchange.jphone.phonenumbers.phone; - -import java.text.MessageFormat; -import java.util.GregorianCalendar; -import javax.ejb.Stateless; -import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean; -import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; -import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; -import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber; - -/** - * An EJB for administrative phone purposes - * <p> - * @author Roland Häder<roland@mxchange.org> - */ -@Stateless (name = "adminPhone", description = "An administrative bean handling phone data") -public class AddressbookAdminPhoneSessionBean extends BaseAddressbookDatabaseBean implements AdminPhoneSessionBeanRemote { - - /** - * Serial number - */ - private static final long serialVersionUID = 18_597_165_817_401_853L; - - @Override - public void deleteFaxData (final DialableFaxNumber faxNumber) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteMobileData: faxNumber={1} - CALLED!", this.getClass().getSimpleName(), faxNumber)); - - // Is all data set - if (null == faxNumber) { - // Not set, throw NPE - throw new NullPointerException("faxNumber is null"); //NOI18N - } else if (faxNumber.getPhoneId() == null) { - // Throw NPE again - throw new NullPointerException("faxNumber.phoneId is null"); //NOI18N - } else if (faxNumber.getPhoneId() < 1) { - // Invalid number - throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneId={0} is not valid", faxNumber.getPhoneId())); //NOI18N - } else if (faxNumber.getPhoneCountry()== null) { - // Throw NPE - throw new NullPointerException("faxNumber.phoneCountry is null"); //NOI18N - } else if (faxNumber.getPhoneCountry().getCountryId()== null) { - // ... throw again - throw new NullPointerException("faxNumber.phoneCountry.countryId is null"); //NOI18N - } else if (faxNumber.getPhoneCountry().getCountryId() < 1) { - // Id not valid - throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneCountry.countryId={0} is not valid.", faxNumber.getPhoneCountry().getCountryId())); //NOI18N - } else if (faxNumber.getPhoneNumber() == null) { - // Throw NPE again - throw new NullPointerException("faxNumber.phoneNumber is null"); //NOI18N - } else if (faxNumber.getPhoneNumber() < 1) { - // Throw NPE again - throw new NullPointerException(MessageFormat.format("faxNumber.phoneNumber={0} is not valid.", faxNumber.getPhoneNumber())); //NOI18N - } - - // Merge it to get a managed entity back - DialableFaxNumber managedNumber = this.getEntityManager().getReference(faxNumber.getClass(), faxNumber.getPhoneId()); - - // Remove it from database - this.getEntityManager().remove(managedNumber); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteMobileData: EXIT!", this.getClass().getSimpleName())); - } - - @Override - public void deleteLandLineData (final DialableLandLineNumber landLineNumber) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteMobileData: landLineNumber={1} - CALLED!", this.getClass().getSimpleName(), landLineNumber)); - - // Is all data set - if (null == landLineNumber) { - // Not set, throw NPE - throw new NullPointerException("landLineNumber is null"); //NOI18N - } else if (landLineNumber.getPhoneId() == null) { - // Throw NPE again - throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N - } else if (landLineNumber.getPhoneId() < 1) { - // Invalid number - throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneId={0} is not valid", landLineNumber.getPhoneId())); //NOI18N - } else if (landLineNumber.getPhoneCountry()== null) { - // Throw NPE - throw new NullPointerException("landLineNumber.phoneCountry is null"); //NOI18N - } else if (landLineNumber.getPhoneCountry().getCountryId()== null) { - // ... throw again - throw new NullPointerException("landLineNumber.phoneCountry.countryId is null"); //NOI18N - } else if (landLineNumber.getPhoneCountry().getCountryId() < 1) { - // Id not valid - throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneCountry.countryId={0} is not valid.", landLineNumber.getPhoneCountry().getCountryId())); //NOI18N - } else if (landLineNumber.getPhoneNumber() == null) { - // Throw NPE again - throw new NullPointerException("landLineNumber.phoneNumber is null"); //NOI18N - } else if (landLineNumber.getPhoneNumber() < 1) { - // Throw NPE again - throw new NullPointerException(MessageFormat.format("landLineNumber.phoneNumber={0} is not valid.", landLineNumber.getPhoneNumber())); //NOI18N - } - - // Merge it to get a managed entity back - DialableLandLineNumber managedNumber = this.getEntityManager().getReference(landLineNumber.getClass(), landLineNumber.getPhoneId()); - - // Remove it from database - this.getEntityManager().remove(managedNumber); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteMobileData: EXIT!", this.getClass().getSimpleName())); - } - - @Override - public void deleteMobileData (final DialableMobileNumber mobileNumber) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteMobileData: mobileNumber={1} - CALLED!", this.getClass().getSimpleName(), mobileNumber)); - - // Is all data set - if (null == mobileNumber) { - // Not set, throw NPE - throw new NullPointerException("mobileNumber is null"); //NOI18N - } else if (mobileNumber.getPhoneId() == null) { - // Throw NPE again - throw new NullPointerException("mobileNumber.phoneId is null"); //NOI18N - } else if (mobileNumber.getPhoneId() < 1) { - // Invalid number - throw new IllegalArgumentException(MessageFormat.format("mobileNumber.phoneId={0} is not valid", mobileNumber.getPhoneId())); //NOI18N - } else if (mobileNumber.getMobileProvider() == null) { - // Throw NPE - throw new NullPointerException("mobileNumber.cellphoneProvider is null"); //NOI18N - } else if (mobileNumber.getMobileProvider().getProviderId() == null) { - // ... throw again - throw new NullPointerException("mobileNumber.cellphoneProvider.providerId is null"); //NOI18N - } else if (mobileNumber.getMobileProvider().getProviderId() < 1) { - // Id not valid - throw new IllegalArgumentException(MessageFormat.format("mobileNumber.cellphoneProvider.providerId={0} is not valid.", mobileNumber.getMobileProvider().getProviderId())); //NOI18N - } else if (mobileNumber.getPhoneNumber() == null) { - // Throw NPE again - throw new NullPointerException("mobileNumber.phoneNumber is null"); //NOI18N - } else if (mobileNumber.getPhoneNumber() < 1) { - // Throw NPE again - throw new NullPointerException(MessageFormat.format("mobileNumber.phoneNumber={0} is not valid.", mobileNumber.getPhoneNumber())); //NOI18N - } - - // Merge it to get a managed entity back - DialableMobileNumber managedNumber = this.getEntityManager().getReference(mobileNumber.getClass(), mobileNumber.getPhoneId()); - - // Remove it from database - this.getEntityManager().remove(managedNumber); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteMobileData: EXIT!", this.getClass().getSimpleName())); - } - - @Override - public DialableFaxNumber updateFaxData (DialableFaxNumber faxNumber) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public DialableLandLineNumber updateLandLineData (DialableLandLineNumber landLineNumber) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public DialableMobileNumber updateMobileData (final DialableMobileNumber mobileNumber) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateCellphoneData: mobileNumber={1} - CALLED!", this.getClass().getSimpleName(), mobileNumber)); - - // Is all data set - if (null == mobileNumber) { - // Not set, throw NPE - throw new NullPointerException("mobileNumber is null"); //NOI18N - } else if (mobileNumber.getPhoneId() == null) { - // Throw NPE again - throw new NullPointerException("mobileNumber.phoneId is null"); //NOI18N - } else if (mobileNumber.getPhoneId() < 1) { - // Invalid number - throw new IllegalArgumentException(MessageFormat.format("mobileNumber.phoneId={0} is not valid", mobileNumber.getPhoneId())); //NOI18N - } else if (mobileNumber.getMobileProvider() == null) { - // Throw NPE - throw new NullPointerException("mobileNumber.cellphoneProvider is null"); //NOI18N - } else if (mobileNumber.getMobileProvider().getProviderId() == null) { - // ... throw again - throw new NullPointerException("mobileNumber.cellphoneProvider.providerId is null"); //NOI18N - } else if (mobileNumber.getMobileProvider().getProviderId() < 1) { - // Id not valid - throw new IllegalArgumentException(MessageFormat.format("mobileNumber.cellphoneProvider.providerId={0} is not valid.", mobileNumber.getMobileProvider().getProviderId())); //NOI18N - } else if (mobileNumber.getPhoneNumber() == null) { - // Throw NPE again - throw new NullPointerException("mobileNumber.phoneNumber is null"); //NOI18N - } else if (mobileNumber.getPhoneNumber() < 1) { - // Throw NPE again - throw new NullPointerException(MessageFormat.format("mobileNumber.phoneNumber={0} is not valid.", mobileNumber.getPhoneNumber())); //NOI18N - } - - // Set updated timestamp - mobileNumber.setPhoneEntryUpdated(new GregorianCalendar()); - - // Get contact from it and find it - DialableMobileNumber foundNumber = this.getEntityManager().find(mobileNumber.getClass(), mobileNumber.getPhoneId()); - - // Should be found - assert (foundNumber instanceof DialableMobileNumber) : MessageFormat.format("Cell phone number with id {0} not found, but should be.", mobileNumber.getPhoneId()); //NOI18N - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateCellphoneData: foundNumber.phoneId={0}", foundNumber.getPhoneId())); //NOI18N - - // Merge contact instance - DialableMobileNumber detachedNumber = this.getEntityManager().merge(foundNumber); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateCellphoneData: detachedNumber={1} - EXIT!", this.getClass().getSimpleName(), detachedNumber)); //NOI18N - - // Return it - return detachedNumber; - } - -} diff --git a/src/java/org/mxchange/jphone/phonenumbers/phone/AddressbookPhoneSessionBean.java b/src/java/org/mxchange/jphone/phonenumbers/phone/AddressbookPhoneSessionBean.java deleted file mode 100644 index 0a637d0..0000000 --- a/src/java/org/mxchange/jphone/phonenumbers/phone/AddressbookPhoneSessionBean.java +++ /dev/null @@ -1,226 +0,0 @@ -/* - * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. - */ -package org.mxchange.jphone.phonenumbers.phone; - -import java.text.MessageFormat; -import java.util.List; -import javax.ejb.Stateless; -import javax.persistence.NoResultException; -import javax.persistence.Query; -import org.mxchange.jcoreee.database.BaseDatabaseBean; -import org.mxchange.jphone.exceptions.PhoneEntityNotFoundException; -import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; -import org.mxchange.jphone.phonenumbers.fax.FaxNumber; -import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; -import org.mxchange.jphone.phonenumbers.landline.LandLineNumber; -import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber; -import org.mxchange.jphone.phonenumbers.mobile.MobileNumber; - -/** - * A general phone EJB - * <p> - * @author Roland Häder<roland@mxchange.org> - */ -@Stateless (name = "phone", description = "A bean handling phone data") -public class AddressbookPhoneSessionBean extends BaseDatabaseBean implements PhoneSessionBeanRemote { - - /** - * Serial number - */ - private static final long serialVersionUID = 134_945_698_127_601L; - - /** - * Default constructor - */ - public AddressbookPhoneSessionBean () { - } - - @SuppressWarnings ("unchecked") - @Override - public List<DialableFaxNumber> allFaxNumbers () { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allFaxNumbers: CALLED!", this.getClass().getSimpleName())); //NOI18N - - // Get query - Query query = this.getEntityManager().createNamedQuery("AllFaxNumbers", FaxNumber.class); //NOI18N - - // Get list from it - List<DialableFaxNumber> list = query.getResultList(); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allFaxNumbers: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N - - // Return it - return list; - } - - @SuppressWarnings ("unchecked") - @Override - public List<DialableLandLineNumber> allLandLineNumbers () { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allLandLineNumbers: CALLED!", this.getClass().getSimpleName())); //NOI18N - - // Get query - Query query = this.getEntityManager().createNamedQuery("AllLandLineNumbers", LandLineNumber.class); //NOI18N - - // Get list from it - List<DialableLandLineNumber> list = query.getResultList(); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allLandLineNumbers: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N - - // Return it - return list; - } - - @SuppressWarnings ("unchecked") - @Override - public List<DialableMobileNumber> allMobileNumbers () { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMobileNumbers: CALLED!", this.getClass().getSimpleName())); //NOI18N - - // Get query - Query query = this.getEntityManager().createNamedQuery("AllMobileNumbers", MobileNumber.class); //NOI18N - - // Get list from it - List<DialableMobileNumber> list = query.getResultList(); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMobileNumbers: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N - - // Return it - return list; - } - - @Override - public DialableFaxNumber findFaxNumberById (final Long faxNumberId) throws PhoneEntityNotFoundException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findFaxNumberById: mobileNumberId={1} - CALLED!", this.getClass().getSimpleName(), faxNumberId)); //NOI18N - - // The id number should be valid - if (null == faxNumberId) { - // Throw NPE - throw new NullPointerException("faxNumberId is null"); //NOI18N - } else if (faxNumberId < 1) { - // Not valid - throw new IllegalArgumentException(MessageFormat.format("faxNumberId={0} is not valid.", faxNumberId)); //NOI18N - } - - // Now find it - Query query = this.getEntityManager().createNamedQuery("SearchFaxNumberId", FaxNumber.class); //NOI18N - - // Set parameter - query.setParameter("faxNumberId", faxNumberId); //NOI18N - - // Init instance - DialableFaxNumber faxNumber = null; - - // Try to get a result - try { - // Get a single result - faxNumber = (DialableFaxNumber) query.getSingleResult(); - } catch (NoResultException ex) { - // The entry was not found, so throw it again - throw new PhoneEntityNotFoundException(faxNumberId, ex); - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findFaxNumberById: cellphone={1} - EXIT!", this.getClass().getSimpleName(), faxNumber)); //NOI18N - - // Return found instance - return faxNumber; - } - - @Override - public DialableLandLineNumber findLandLineNumberById (final Long landLineNumberId) throws PhoneEntityNotFoundException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findLandLineNumberById: mobileNumberId={1} - CALLED!", this.getClass().getSimpleName(), landLineNumberId)); //NOI18N - - // The id number should be valid - if (null == landLineNumberId) { - // Throw NPE - throw new NullPointerException("landLineNumberId is null"); //NOI18N - } else if (landLineNumberId < 1) { - // Not valid - throw new IllegalArgumentException(MessageFormat.format("landLineNumberId={0} is not valid.", landLineNumberId)); //NOI18N - } - - // Now find it - Query query = this.getEntityManager().createNamedQuery("SearchLandLineNumberId", LandLineNumber.class); //NOI18N - - // Set parameter - query.setParameter("landLineNumberId", landLineNumberId); //NOI18N - - // Init instance - DialableLandLineNumber landLineNumber = null; - - // Try to get a result - try { - // Get a single result - landLineNumber = (DialableLandLineNumber) query.getSingleResult(); - } catch (NoResultException ex) { - // The entry was not found, so throw it again - throw new PhoneEntityNotFoundException(landLineNumberId, ex); - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findLandLineNumberById: cellphone={1} - EXIT!", this.getClass().getSimpleName(), landLineNumber)); //NOI18N - - // Return found instance - return landLineNumber; - } - - @Override - public DialableMobileNumber findMobileNumberById (final Long mobileNumberId) throws PhoneEntityNotFoundException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findMobileNumberById: mobileNumberId={1} - CALLED!", this.getClass().getSimpleName(), mobileNumberId)); //NOI18N - - // The id number should be valid - if (null == mobileNumberId) { - // Throw NPE - throw new NullPointerException("mobileNumberId is null"); //NOI18N - } else if (mobileNumberId < 1) { - // Not valid - throw new IllegalArgumentException(MessageFormat.format("mobileNumberId={0} is not valid.", mobileNumberId)); //NOI18N - } - - // Now find it - Query query = this.getEntityManager().createNamedQuery("SearchMobileNumberId", MobileNumber.class); //NOI18N - - // Set parameter - query.setParameter("mobileNumberId", mobileNumberId); //NOI18N - - // Init instance - DialableMobileNumber cellphone = null; - - // Try to get a result - try { - // Get a single result - cellphone = (DialableMobileNumber) query.getSingleResult(); - } catch (NoResultException ex) { - // The entry was not found, so throw it again - throw new PhoneEntityNotFoundException(mobileNumberId, ex); - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findMobileNumberById: cellphone={1} - EXIT!", this.getClass().getSimpleName(), cellphone)); //NOI18N - - // Return found instance - return cellphone; - } - -} diff --git a/src/java/org/mxchange/jphone/phonenumbers/phone/FinancialsAdminPhoneSessionBean.java b/src/java/org/mxchange/jphone/phonenumbers/phone/FinancialsAdminPhoneSessionBean.java new file mode 100644 index 0000000..f6e622e --- /dev/null +++ b/src/java/org/mxchange/jphone/phonenumbers/phone/FinancialsAdminPhoneSessionBean.java @@ -0,0 +1,230 @@ +/* + * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. + */ +package org.mxchange.jphone.phonenumbers.phone; + +import java.text.MessageFormat; +import java.util.GregorianCalendar; +import javax.ejb.Stateless; +import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean; +import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; +import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; +import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber; + +/** + * An EJB for administrative phone purposes + * <p> + * @author Roland Häder<roland@mxchange.org> + */ +@Stateless (name = "adminPhone", description = "An administrative bean handling phone data") +public class FinancialsAdminPhoneSessionBean extends BaseFinancialsDatabaseBean implements AdminPhoneSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 18_597_165_817_401_853L; + + @Override + public void deleteFaxData (final DialableFaxNumber faxNumber) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteMobileData: faxNumber={1} - CALLED!", this.getClass().getSimpleName(), faxNumber)); + + // Is all data set + if (null == faxNumber) { + // Not set, throw NPE + throw new NullPointerException("faxNumber is null"); //NOI18N + } else if (faxNumber.getPhoneId() == null) { + // Throw NPE again + throw new NullPointerException("faxNumber.phoneId is null"); //NOI18N + } else if (faxNumber.getPhoneId() < 1) { + // Invalid number + throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneId={0} is not valid", faxNumber.getPhoneId())); //NOI18N + } else if (faxNumber.getPhoneCountry()== null) { + // Throw NPE + throw new NullPointerException("faxNumber.phoneCountry is null"); //NOI18N + } else if (faxNumber.getPhoneCountry().getCountryId()== null) { + // ... throw again + throw new NullPointerException("faxNumber.phoneCountry.countryId is null"); //NOI18N + } else if (faxNumber.getPhoneCountry().getCountryId() < 1) { + // Id not valid + throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneCountry.countryId={0} is not valid.", faxNumber.getPhoneCountry().getCountryId())); //NOI18N + } else if (faxNumber.getPhoneNumber() == null) { + // Throw NPE again + throw new NullPointerException("faxNumber.phoneNumber is null"); //NOI18N + } else if (faxNumber.getPhoneNumber() < 1) { + // Throw NPE again + throw new NullPointerException(MessageFormat.format("faxNumber.phoneNumber={0} is not valid.", faxNumber.getPhoneNumber())); //NOI18N + } + + // Merge it to get a managed entity back + DialableFaxNumber managedNumber = this.getEntityManager().getReference(faxNumber.getClass(), faxNumber.getPhoneId()); + + // Remove it from database + this.getEntityManager().remove(managedNumber); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteMobileData: EXIT!", this.getClass().getSimpleName())); + } + + @Override + public void deleteLandLineData (final DialableLandLineNumber landLineNumber) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteMobileData: landLineNumber={1} - CALLED!", this.getClass().getSimpleName(), landLineNumber)); + + // Is all data set + if (null == landLineNumber) { + // Not set, throw NPE + throw new NullPointerException("landLineNumber is null"); //NOI18N + } else if (landLineNumber.getPhoneId() == null) { + // Throw NPE again + throw new NullPointerException("landLineNumber.phoneId is null"); //NOI18N + } else if (landLineNumber.getPhoneId() < 1) { + // Invalid number + throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneId={0} is not valid", landLineNumber.getPhoneId())); //NOI18N + } else if (landLineNumber.getPhoneCountry()== null) { + // Throw NPE + throw new NullPointerException("landLineNumber.phoneCountry is null"); //NOI18N + } else if (landLineNumber.getPhoneCountry().getCountryId()== null) { + // ... throw again + throw new NullPointerException("landLineNumber.phoneCountry.countryId is null"); //NOI18N + } else if (landLineNumber.getPhoneCountry().getCountryId() < 1) { + // Id not valid + throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneCountry.countryId={0} is not valid.", landLineNumber.getPhoneCountry().getCountryId())); //NOI18N + } else if (landLineNumber.getPhoneNumber() == null) { + // Throw NPE again + throw new NullPointerException("landLineNumber.phoneNumber is null"); //NOI18N + } else if (landLineNumber.getPhoneNumber() < 1) { + // Throw NPE again + throw new NullPointerException(MessageFormat.format("landLineNumber.phoneNumber={0} is not valid.", landLineNumber.getPhoneNumber())); //NOI18N + } + + // Merge it to get a managed entity back + DialableLandLineNumber managedNumber = this.getEntityManager().getReference(landLineNumber.getClass(), landLineNumber.getPhoneId()); + + // Remove it from database + this.getEntityManager().remove(managedNumber); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteMobileData: EXIT!", this.getClass().getSimpleName())); + } + + @Override + public void deleteMobileData (final DialableMobileNumber mobileNumber) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteMobileData: mobileNumber={1} - CALLED!", this.getClass().getSimpleName(), mobileNumber)); + + // Is all data set + if (null == mobileNumber) { + // Not set, throw NPE + throw new NullPointerException("mobileNumber is null"); //NOI18N + } else if (mobileNumber.getPhoneId() == null) { + // Throw NPE again + throw new NullPointerException("mobileNumber.phoneId is null"); //NOI18N + } else if (mobileNumber.getPhoneId() < 1) { + // Invalid number + throw new IllegalArgumentException(MessageFormat.format("mobileNumber.phoneId={0} is not valid", mobileNumber.getPhoneId())); //NOI18N + } else if (mobileNumber.getMobileProvider() == null) { + // Throw NPE + throw new NullPointerException("mobileNumber.cellphoneProvider is null"); //NOI18N + } else if (mobileNumber.getMobileProvider().getProviderId() == null) { + // ... throw again + throw new NullPointerException("mobileNumber.cellphoneProvider.providerId is null"); //NOI18N + } else if (mobileNumber.getMobileProvider().getProviderId() < 1) { + // Id not valid + throw new IllegalArgumentException(MessageFormat.format("mobileNumber.cellphoneProvider.providerId={0} is not valid.", mobileNumber.getMobileProvider().getProviderId())); //NOI18N + } else if (mobileNumber.getPhoneNumber() == null) { + // Throw NPE again + throw new NullPointerException("mobileNumber.phoneNumber is null"); //NOI18N + } else if (mobileNumber.getPhoneNumber() < 1) { + // Throw NPE again + throw new NullPointerException(MessageFormat.format("mobileNumber.phoneNumber={0} is not valid.", mobileNumber.getPhoneNumber())); //NOI18N + } + + // Merge it to get a managed entity back + DialableMobileNumber managedNumber = this.getEntityManager().getReference(mobileNumber.getClass(), mobileNumber.getPhoneId()); + + // Remove it from database + this.getEntityManager().remove(managedNumber); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteMobileData: EXIT!", this.getClass().getSimpleName())); + } + + @Override + public DialableFaxNumber updateFaxData (DialableFaxNumber faxNumber) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public DialableLandLineNumber updateLandLineData (DialableLandLineNumber landLineNumber) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public DialableMobileNumber updateMobileData (final DialableMobileNumber mobileNumber) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateCellphoneData: mobileNumber={1} - CALLED!", this.getClass().getSimpleName(), mobileNumber)); + + // Is all data set + if (null == mobileNumber) { + // Not set, throw NPE + throw new NullPointerException("mobileNumber is null"); //NOI18N + } else if (mobileNumber.getPhoneId() == null) { + // Throw NPE again + throw new NullPointerException("mobileNumber.phoneId is null"); //NOI18N + } else if (mobileNumber.getPhoneId() < 1) { + // Invalid number + throw new IllegalArgumentException(MessageFormat.format("mobileNumber.phoneId={0} is not valid", mobileNumber.getPhoneId())); //NOI18N + } else if (mobileNumber.getMobileProvider() == null) { + // Throw NPE + throw new NullPointerException("mobileNumber.cellphoneProvider is null"); //NOI18N + } else if (mobileNumber.getMobileProvider().getProviderId() == null) { + // ... throw again + throw new NullPointerException("mobileNumber.cellphoneProvider.providerId is null"); //NOI18N + } else if (mobileNumber.getMobileProvider().getProviderId() < 1) { + // Id not valid + throw new IllegalArgumentException(MessageFormat.format("mobileNumber.cellphoneProvider.providerId={0} is not valid.", mobileNumber.getMobileProvider().getProviderId())); //NOI18N + } else if (mobileNumber.getPhoneNumber() == null) { + // Throw NPE again + throw new NullPointerException("mobileNumber.phoneNumber is null"); //NOI18N + } else if (mobileNumber.getPhoneNumber() < 1) { + // Throw NPE again + throw new NullPointerException(MessageFormat.format("mobileNumber.phoneNumber={0} is not valid.", mobileNumber.getPhoneNumber())); //NOI18N + } + + // Set updated timestamp + mobileNumber.setPhoneEntryUpdated(new GregorianCalendar()); + + // Get contact from it and find it + DialableMobileNumber foundNumber = this.getEntityManager().find(mobileNumber.getClass(), mobileNumber.getPhoneId()); + + // Should be found + assert (foundNumber instanceof DialableMobileNumber) : MessageFormat.format("Cell phone number with id {0} not found, but should be.", mobileNumber.getPhoneId()); //NOI18N + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateCellphoneData: foundNumber.phoneId={0}", foundNumber.getPhoneId())); //NOI18N + + // Merge contact instance + DialableMobileNumber detachedNumber = this.getEntityManager().merge(foundNumber); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateCellphoneData: detachedNumber={1} - EXIT!", this.getClass().getSimpleName(), detachedNumber)); //NOI18N + + // Return it + return detachedNumber; + } + +} diff --git a/src/java/org/mxchange/jphone/phonenumbers/phone/FinancialsPhoneSessionBean.java b/src/java/org/mxchange/jphone/phonenumbers/phone/FinancialsPhoneSessionBean.java new file mode 100644 index 0000000..d280e21 --- /dev/null +++ b/src/java/org/mxchange/jphone/phonenumbers/phone/FinancialsPhoneSessionBean.java @@ -0,0 +1,226 @@ +/* + * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. + */ +package org.mxchange.jphone.phonenumbers.phone; + +import java.text.MessageFormat; +import java.util.List; +import javax.ejb.Stateless; +import javax.persistence.NoResultException; +import javax.persistence.Query; +import org.mxchange.jcoreee.database.BaseDatabaseBean; +import org.mxchange.jphone.exceptions.PhoneEntityNotFoundException; +import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; +import org.mxchange.jphone.phonenumbers.fax.FaxNumber; +import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; +import org.mxchange.jphone.phonenumbers.landline.LandLineNumber; +import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber; +import org.mxchange.jphone.phonenumbers.mobile.MobileNumber; + +/** + * A general phone EJB + * <p> + * @author Roland Häder<roland@mxchange.org> + */ +@Stateless (name = "phone", description = "A bean handling phone data") +public class FinancialsPhoneSessionBean extends BaseDatabaseBean implements PhoneSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 134_945_698_127_601L; + + /** + * Default constructor + */ + public FinancialsPhoneSessionBean () { + } + + @SuppressWarnings ("unchecked") + @Override + public List<DialableFaxNumber> allFaxNumbers () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allFaxNumbers: CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Get query + Query query = this.getEntityManager().createNamedQuery("AllFaxNumbers", FaxNumber.class); //NOI18N + + // Get list from it + List<DialableFaxNumber> list = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allFaxNumbers: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N + + // Return it + return list; + } + + @SuppressWarnings ("unchecked") + @Override + public List<DialableLandLineNumber> allLandLineNumbers () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allLandLineNumbers: CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Get query + Query query = this.getEntityManager().createNamedQuery("AllLandLineNumbers", LandLineNumber.class); //NOI18N + + // Get list from it + List<DialableLandLineNumber> list = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allLandLineNumbers: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N + + // Return it + return list; + } + + @SuppressWarnings ("unchecked") + @Override + public List<DialableMobileNumber> allMobileNumbers () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMobileNumbers: CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Get query + Query query = this.getEntityManager().createNamedQuery("AllMobileNumbers", MobileNumber.class); //NOI18N + + // Get list from it + List<DialableMobileNumber> list = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMobileNumbers: list.size()={1} - EXIT!", this.getClass().getSimpleName(), list.size())); //NOI18N + + // Return it + return list; + } + + @Override + public DialableFaxNumber findFaxNumberById (final Long faxNumberId) throws PhoneEntityNotFoundException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findFaxNumberById: mobileNumberId={1} - CALLED!", this.getClass().getSimpleName(), faxNumberId)); //NOI18N + + // The id number should be valid + if (null == faxNumberId) { + // Throw NPE + throw new NullPointerException("faxNumberId is null"); //NOI18N + } else if (faxNumberId < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("faxNumberId={0} is not valid.", faxNumberId)); //NOI18N + } + + // Now find it + Query query = this.getEntityManager().createNamedQuery("SearchFaxNumberId", FaxNumber.class); //NOI18N + + // Set parameter + query.setParameter("faxNumberId", faxNumberId); //NOI18N + + // Init instance + DialableFaxNumber faxNumber = null; + + // Try to get a result + try { + // Get a single result + faxNumber = (DialableFaxNumber) query.getSingleResult(); + } catch (NoResultException ex) { + // The entry was not found, so throw it again + throw new PhoneEntityNotFoundException(faxNumberId, ex); + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findFaxNumberById: cellphone={1} - EXIT!", this.getClass().getSimpleName(), faxNumber)); //NOI18N + + // Return found instance + return faxNumber; + } + + @Override + public DialableLandLineNumber findLandLineNumberById (final Long landLineNumberId) throws PhoneEntityNotFoundException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findLandLineNumberById: mobileNumberId={1} - CALLED!", this.getClass().getSimpleName(), landLineNumberId)); //NOI18N + + // The id number should be valid + if (null == landLineNumberId) { + // Throw NPE + throw new NullPointerException("landLineNumberId is null"); //NOI18N + } else if (landLineNumberId < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("landLineNumberId={0} is not valid.", landLineNumberId)); //NOI18N + } + + // Now find it + Query query = this.getEntityManager().createNamedQuery("SearchLandLineNumberId", LandLineNumber.class); //NOI18N + + // Set parameter + query.setParameter("landLineNumberId", landLineNumberId); //NOI18N + + // Init instance + DialableLandLineNumber landLineNumber = null; + + // Try to get a result + try { + // Get a single result + landLineNumber = (DialableLandLineNumber) query.getSingleResult(); + } catch (NoResultException ex) { + // The entry was not found, so throw it again + throw new PhoneEntityNotFoundException(landLineNumberId, ex); + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findLandLineNumberById: cellphone={1} - EXIT!", this.getClass().getSimpleName(), landLineNumber)); //NOI18N + + // Return found instance + return landLineNumber; + } + + @Override + public DialableMobileNumber findMobileNumberById (final Long mobileNumberId) throws PhoneEntityNotFoundException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findMobileNumberById: mobileNumberId={1} - CALLED!", this.getClass().getSimpleName(), mobileNumberId)); //NOI18N + + // The id number should be valid + if (null == mobileNumberId) { + // Throw NPE + throw new NullPointerException("mobileNumberId is null"); //NOI18N + } else if (mobileNumberId < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("mobileNumberId={0} is not valid.", mobileNumberId)); //NOI18N + } + + // Now find it + Query query = this.getEntityManager().createNamedQuery("SearchMobileNumberId", MobileNumber.class); //NOI18N + + // Set parameter + query.setParameter("mobileNumberId", mobileNumberId); //NOI18N + + // Init instance + DialableMobileNumber cellphone = null; + + // Try to get a result + try { + // Get a single result + cellphone = (DialableMobileNumber) query.getSingleResult(); + } catch (NoResultException ex) { + // The entry was not found, so throw it again + throw new PhoneEntityNotFoundException(mobileNumberId, ex); + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findMobileNumberById: cellphone={1} - EXIT!", this.getClass().getSimpleName(), cellphone)); //NOI18N + + // Return found instance + return cellphone; + } + +} diff --git a/src/java/org/mxchange/jusercore/model/email_address/AddressbookUserEmailChangeSessionBean.java b/src/java/org/mxchange/jusercore/model/email_address/AddressbookUserEmailChangeSessionBean.java deleted file mode 100644 index dd9924f..0000000 --- a/src/java/org/mxchange/jusercore/model/email_address/AddressbookUserEmailChangeSessionBean.java +++ /dev/null @@ -1,244 +0,0 @@ -/* - * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. - */ -package org.mxchange.jusercore.model.email_address; - -import java.text.MessageFormat; -import java.util.GregorianCalendar; -import java.util.List; -import javax.ejb.EJB; -import javax.ejb.EJBException; -import javax.ejb.Stateless; -import javax.mail.Address; -import javax.mail.internet.AddressException; -import javax.mail.internet.InternetAddress; -import javax.persistence.NoResultException; -import javax.persistence.Query; -import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean; -import org.mxchange.jusercore.model.user.UserSessionBeanRemote; -import org.mxchange.jusercore.model.user.UserUtils; - -/** - * A session bean for changing email addresses - * <p> - * @author Roland Häder<roland@mxchange.org> - */ -@Stateless (name = "userEmailChange", description = "A bean handling user email changes") -public class AddressbookUserEmailChangeSessionBean extends BaseAddressbookDatabaseBean implements UserEmailChangeSessionBeanRemote { - - /** - * Serial number - */ - private static final long serialVersionUID = 182_698_165_971_548L; - - /** - * User bean - */ - @EJB - private UserSessionBeanRemote userBean; - - /** - * Default constructor - */ - public AddressbookUserEmailChangeSessionBean () { - } - - @Override - @SuppressWarnings ("unchecked") - public List<String> allQueuedAddresses () { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allQueuedAddresses: CALLED!", this.getClass().getSimpleName())); //NOI18N - - // Get named query - Query query = this.getEntityManager().createNamedQuery("AllEmailAddressChanges", String.class); //NOI18N - - // Get all entries - List<String> emailAddresses = query.getResultList(); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allQueuedAddresses: emailAddresses.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddresses.size())); //NOI18N - - // Return it - return emailAddresses; - } - - @Override - public void enqueueEmailAddressForChange (final ChangeableEmailAddress emailChange, final String baseUrl) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.enqueueEmailAddressForChange: emailChange={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), emailChange, baseUrl)); //NOI18N - - // Email address change should be valid - if (null == emailChange) { - // Abort here - throw new NullPointerException("emailChange is null"); //NOI18N - } else if (emailChange.getEmailChangeUser() == null) { - // Throw NPE again - throw new NullPointerException("emailChange.emailChangeUser is null"); //NOI18N - } else if (emailChange.getEmailChangeUser().getUserId() == null) { - // Throw NPE again - throw new NullPointerException("emailChange.emailChangeUser.userId is null"); //NOI18N - } else if (emailChange.getEmailChangeUser().getUserId() < 1) { - // Not valid id - throw new IllegalArgumentException(MessageFormat.format("emailChange.emailChangeUser.userId={0} is invalid.", emailChange.getEmailChangeUser().getUserId())); //NOI18N - } else if (!this.userBean.ifUserExists(emailChange.getEmailChangeUser())) { - // User does not exist - throw new EJBException(MessageFormat.format("Email change with id {0} does not exist.", emailChange.getEmailChangeId())); //NOI18N - } else if (emailChange.getEmailAddress().trim().isEmpty()) { - // Email address is empty - throw new IllegalArgumentException("emailChange.emaiLAddress is empty."); //NOI18N - } else if (this.isEmailAddressEnqueued(emailChange.getEmailAddress())) { - // Email address is already enqueued - throw new EJBException(MessageFormat.format("Email address {0} is already enqueued.", emailChange.getEmailAddress())); //NOI18N - } - - // The email change is not (yet) there, add secure hash and "created" timestamp - emailChange.setEmailChangeCreated(new GregorianCalendar()); - this.generateSecureHash(emailChange); - - // Persist it - //@TODO Fix email delivery then allow this: this.getEntityManager().persist(emailChange); - // Init variable - Address emailAddress; - - try { - // Create email address and set - emailAddress = new InternetAddress(emailChange.getEmailAddress()); - } catch (final AddressException ex) { - // Throw again - throw new EJBException(ex); - } - - // Send email - this.sendEmail("Email change", "email_change", emailAddress, emailChange.getEmailChangeUser(), baseUrl); //NOI18N - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.enqueueEmailAddressForChange - EXIT!", this.getClass().getSimpleName())); //NOI18N - } - - @Override - public boolean isEmailAddressEnqueued (final String emailAddress) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressEnqueued: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N - - // Create query instance - Query query = this.getEntityManager().createNamedQuery("SearchEmailChangeByEmail"); //NOI18N - - // Add email address as parameter - query.setParameter("email", emailAddress); //NOI18N - - // Initialize variable - boolean isFound = false; - - // Try it - try { - // Try to get single result - ChangeableEmailAddress dummy = (ChangeableEmailAddress) query.getSingleResult(); - - // Found it - isFound = true; - } catch (final NoResultException ex) { - // Log it - this.getLoggerBeanLocal().logException(ex); - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressEnqueued: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N - - // Return it - return isFound; - } - - @Override - public void updateEmailAddress (final ChangeableEmailAddress emailAddress) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateEmailAddress: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N - - // Email address change should be valid - if (null == emailAddress) { - // Abort here - throw new NullPointerException("emailAddress is null"); //NOI18N - } else if (emailAddress.getEmailChangeId() == null) { - // Throw NPE again - throw new NullPointerException("emailAddress.emailChangeId is null"); //NOI18N - } else if (emailAddress.getEmailChangeId() < 1) { - // Not valid - throw new IllegalArgumentException(MessageFormat.format("emailAddress.emailChangeId={0} is not valid.", emailAddress.getEmailChangeId())); //NOI18N - } else if (emailAddress.getEmailAddress().trim().isEmpty()) { - // Email address is empty - throw new IllegalArgumentException("emailAddress.emaiLAddress is empty."); //NOI18N - } else if (!this.userBean.ifUserExists(emailAddress.getEmailChangeUser())) { - // User does not exist - throw new EJBException(MessageFormat.format("Email change with id {0} does not exist.", emailAddress.getEmailChangeId())); //NOI18N - } else if (!this.isEmailAddressEnqueued(emailAddress.getEmailAddress())) { - // Email address is not enqueued - throw new EJBException(MessageFormat.format("Email address {0} is not enqueued.", emailAddress.getEmailAddress())); //NOI18N - } - - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. - } - - /** - * Generates a secure, unique hash for given email address change. This - * requires to check if the hash is really not there. - * <p> - * @param emailAddress Email address change - */ - private void generateSecureHash (final ChangeableEmailAddress emailAddress) { - // Email address change should be valid - if (null == emailAddress) { - // Abort here - throw new NullPointerException("emailAddress is null"); //NOI18N - } else if (emailAddress.getEmailAddress().trim().isEmpty()) { - // Email address is empty - throw new IllegalArgumentException("emailAddress.emaiLAddress is empty."); //NOI18N - } - - // Initialize loop with null - String hash = null; - - // Default is not used - boolean isUsed = true; - - // Search for free hash - while (isUsed) { - // Generate hash, there is already in UserUtils a nice method that can be used for this purpose. - hash = UserUtils.encryptPassword(String.format("%s:%s", emailAddress.getEmailAddress(), emailAddress.toString())); //NOI18N - - // The hash *may* be unique, better test it - Query query = this.getEntityManager().createNamedQuery("SearchEmailChangeByHash", EmailAddressChange.class); //NOI18N - - // Set hash as parameter - query.setParameter("hash", hash); //NOI18N - - // Try to get single result - try { - // Get single result - ChangeableEmailAddress dummy = (ChangeableEmailAddress) query.getSingleResult(); - } catch (final NoResultException ex) { - // Not found - isUsed = false; - } - } - - // hash should not be null and set - assert (hash != null) : "hash is null"; //NOI18N - assert (!hash.isEmpty()) : "hash is empty"; //NOI18N - - // Set it in email change - emailAddress.setEmailChangeHash(hash); - } - -} diff --git a/src/java/org/mxchange/jusercore/model/email_address/FinancialsUserEmailChangeSessionBean.java b/src/java/org/mxchange/jusercore/model/email_address/FinancialsUserEmailChangeSessionBean.java new file mode 100644 index 0000000..3963452 --- /dev/null +++ b/src/java/org/mxchange/jusercore/model/email_address/FinancialsUserEmailChangeSessionBean.java @@ -0,0 +1,244 @@ +/* + * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. + */ +package org.mxchange.jusercore.model.email_address; + +import java.text.MessageFormat; +import java.util.GregorianCalendar; +import java.util.List; +import javax.ejb.EJB; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.mail.Address; +import javax.mail.internet.AddressException; +import javax.mail.internet.InternetAddress; +import javax.persistence.NoResultException; +import javax.persistence.Query; +import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean; +import org.mxchange.jusercore.model.user.UserSessionBeanRemote; +import org.mxchange.jusercore.model.user.UserUtils; + +/** + * A session bean for changing email addresses + * <p> + * @author Roland Häder<roland@mxchange.org> + */ +@Stateless (name = "userEmailChange", description = "A bean handling user email changes") +public class FinancialsUserEmailChangeSessionBean extends BaseFinancialsDatabaseBean implements UserEmailChangeSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 182_698_165_971_548L; + + /** + * User bean + */ + @EJB + private UserSessionBeanRemote userBean; + + /** + * Default constructor + */ + public FinancialsUserEmailChangeSessionBean () { + } + + @Override + @SuppressWarnings ("unchecked") + public List<String> allQueuedAddresses () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allQueuedAddresses: CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Get named query + Query query = this.getEntityManager().createNamedQuery("AllEmailAddressChanges", String.class); //NOI18N + + // Get all entries + List<String> emailAddresses = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allQueuedAddresses: emailAddresses.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddresses.size())); //NOI18N + + // Return it + return emailAddresses; + } + + @Override + public void enqueueEmailAddressForChange (final ChangeableEmailAddress emailChange, final String baseUrl) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.enqueueEmailAddressForChange: emailChange={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), emailChange, baseUrl)); //NOI18N + + // Email address change should be valid + if (null == emailChange) { + // Abort here + throw new NullPointerException("emailChange is null"); //NOI18N + } else if (emailChange.getEmailChangeUser() == null) { + // Throw NPE again + throw new NullPointerException("emailChange.emailChangeUser is null"); //NOI18N + } else if (emailChange.getEmailChangeUser().getUserId() == null) { + // Throw NPE again + throw new NullPointerException("emailChange.emailChangeUser.userId is null"); //NOI18N + } else if (emailChange.getEmailChangeUser().getUserId() < 1) { + // Not valid id + throw new IllegalArgumentException(MessageFormat.format("emailChange.emailChangeUser.userId={0} is invalid.", emailChange.getEmailChangeUser().getUserId())); //NOI18N + } else if (!this.userBean.ifUserExists(emailChange.getEmailChangeUser())) { + // User does not exist + throw new EJBException(MessageFormat.format("Email change with id {0} does not exist.", emailChange.getEmailChangeId())); //NOI18N + } else if (emailChange.getEmailAddress().trim().isEmpty()) { + // Email address is empty + throw new IllegalArgumentException("emailChange.emaiLAddress is empty."); //NOI18N + } else if (this.isEmailAddressEnqueued(emailChange.getEmailAddress())) { + // Email address is already enqueued + throw new EJBException(MessageFormat.format("Email address {0} is already enqueued.", emailChange.getEmailAddress())); //NOI18N + } + + // The email change is not (yet) there, add secure hash and "created" timestamp + emailChange.setEmailChangeCreated(new GregorianCalendar()); + this.generateSecureHash(emailChange); + + // Persist it + //@TODO Fix email delivery then allow this: this.getEntityManager().persist(emailChange); + // Init variable + Address emailAddress; + + try { + // Create email address and set + emailAddress = new InternetAddress(emailChange.getEmailAddress()); + } catch (final AddressException ex) { + // Throw again + throw new EJBException(ex); + } + + // Send email + this.sendEmail("Email change", "email_change", emailAddress, emailChange.getEmailChangeUser(), baseUrl); //NOI18N + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.enqueueEmailAddressForChange - EXIT!", this.getClass().getSimpleName())); //NOI18N + } + + @Override + public boolean isEmailAddressEnqueued (final String emailAddress) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressEnqueued: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N + + // Create query instance + Query query = this.getEntityManager().createNamedQuery("SearchEmailChangeByEmail"); //NOI18N + + // Add email address as parameter + query.setParameter("email", emailAddress); //NOI18N + + // Initialize variable + boolean isFound = false; + + // Try it + try { + // Try to get single result + ChangeableEmailAddress dummy = (ChangeableEmailAddress) query.getSingleResult(); + + // Found it + isFound = true; + } catch (final NoResultException ex) { + // Log it + this.getLoggerBeanLocal().logException(ex); + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressEnqueued: isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N + + // Return it + return isFound; + } + + @Override + public void updateEmailAddress (final ChangeableEmailAddress emailAddress) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateEmailAddress: emailAddress={1} - CALLED!", this.getClass().getSimpleName(), emailAddress)); //NOI18N + + // Email address change should be valid + if (null == emailAddress) { + // Abort here + throw new NullPointerException("emailAddress is null"); //NOI18N + } else if (emailAddress.getEmailChangeId() == null) { + // Throw NPE again + throw new NullPointerException("emailAddress.emailChangeId is null"); //NOI18N + } else if (emailAddress.getEmailChangeId() < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("emailAddress.emailChangeId={0} is not valid.", emailAddress.getEmailChangeId())); //NOI18N + } else if (emailAddress.getEmailAddress().trim().isEmpty()) { + // Email address is empty + throw new IllegalArgumentException("emailAddress.emaiLAddress is empty."); //NOI18N + } else if (!this.userBean.ifUserExists(emailAddress.getEmailChangeUser())) { + // User does not exist + throw new EJBException(MessageFormat.format("Email change with id {0} does not exist.", emailAddress.getEmailChangeId())); //NOI18N + } else if (!this.isEmailAddressEnqueued(emailAddress.getEmailAddress())) { + // Email address is not enqueued + throw new EJBException(MessageFormat.format("Email address {0} is not enqueued.", emailAddress.getEmailAddress())); //NOI18N + } + + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + /** + * Generates a secure, unique hash for given email address change. This + * requires to check if the hash is really not there. + * <p> + * @param emailAddress Email address change + */ + private void generateSecureHash (final ChangeableEmailAddress emailAddress) { + // Email address change should be valid + if (null == emailAddress) { + // Abort here + throw new NullPointerException("emailAddress is null"); //NOI18N + } else if (emailAddress.getEmailAddress().trim().isEmpty()) { + // Email address is empty + throw new IllegalArgumentException("emailAddress.emaiLAddress is empty."); //NOI18N + } + + // Initialize loop with null + String hash = null; + + // Default is not used + boolean isUsed = true; + + // Search for free hash + while (isUsed) { + // Generate hash, there is already in UserUtils a nice method that can be used for this purpose. + hash = UserUtils.encryptPassword(String.format("%s:%s", emailAddress.getEmailAddress(), emailAddress.toString())); //NOI18N + + // The hash *may* be unique, better test it + Query query = this.getEntityManager().createNamedQuery("SearchEmailChangeByHash", EmailAddressChange.class); //NOI18N + + // Set hash as parameter + query.setParameter("hash", hash); //NOI18N + + // Try to get single result + try { + // Get single result + ChangeableEmailAddress dummy = (ChangeableEmailAddress) query.getSingleResult(); + } catch (final NoResultException ex) { + // Not found + isUsed = false; + } + } + + // hash should not be null and set + assert (hash != null) : "hash is null"; //NOI18N + assert (!hash.isEmpty()) : "hash is empty"; //NOI18N + + // Set it in email change + emailAddress.setEmailChangeHash(hash); + } + +} diff --git a/src/java/org/mxchange/jusercore/model/login/AddressbookUserLoginSessionBean.java b/src/java/org/mxchange/jusercore/model/login/AddressbookUserLoginSessionBean.java deleted file mode 100644 index 0f4cf51..0000000 --- a/src/java/org/mxchange/jusercore/model/login/AddressbookUserLoginSessionBean.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. - */ -package org.mxchange.jusercore.model.login; - -import java.text.MessageFormat; -import javax.ejb.EJB; -import javax.ejb.Stateless; -import org.mxchange.jcoreee.database.BaseDatabaseBean; -import org.mxchange.jusercore.container.login.LoginContainer; -import org.mxchange.jusercore.exceptions.UserNotFoundException; -import org.mxchange.jusercore.exceptions.UserPasswordMismatchException; -import org.mxchange.jusercore.exceptions.UserStatusLockedException; -import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException; -import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote; -import org.mxchange.jusercore.model.user.User; -import org.mxchange.jusercore.model.user.UserSessionBeanRemote; -import org.mxchange.jusercore.model.user.UserUtils; -import org.mxchange.jusercore.model.user.status.UserAccountStatus; - -/** - * A session EJB for user logins - * <p> - * @author Roland Häder<roland@mxchange.org> - */ -@Stateless (name = "login", description = "A bean handling the user login for Addressbook project") -public class AddressbookUserLoginSessionBean extends BaseDatabaseBean implements UserLoginSessionBeanRemote { - - /** - * Serial number - */ - private static final long serialVersionUID = 21_785_978_127_581_965L; - - /** - * Registration EJB - */ - @EJB - private UserRegistrationSessionBeanRemote registerBean; - - /** - * User EJB - */ - @EJB - private UserSessionBeanRemote userBean; - - @Override - public User validateUserAccountStatus (final LoginContainer container) throws UserNotFoundException, UserStatusLockedException, UserStatusUnconfirmedException, UserPasswordMismatchException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.loginUser: container={1} - CALLED!", this.getClass().getSimpleName(), container)); //NOI18N - - // Check some beans - assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N - assert (this.registerBean instanceof UserRegistrationSessionBeanRemote) : "this.registerBean is not set"; //NOI18N - - // user should not be null - if (null == container) { - // Abort here - throw new NullPointerException("container is null"); //NOI18N - } else if (container.getUser() == null) { - // NPE again - throw new NullPointerException("container.user is null"); //NOI18N - } else if (container.getUserPassword() == null) { - // And yet again NPE - throw new NullPointerException("container.userPassword is null"); //NOI18N - } else if (container.getUserPassword().isEmpty()) { - // Empty password is not allowed, hardcoded. - throw new IllegalArgumentException("container.userPassword is empty"); //NOI18N - } - - // Is the account there? - if (!this.registerBean.isUserNameRegistered(container.getUser())) { - // Not registered - throw new UserNotFoundException(container.getUser()); - } - - // Get user instance from persistance - User updatedUser = this.userBean.fillUserData(container.getUser()); - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("loginUser: updatedUser={0}", updatedUser)); //NOI18N - - // Is the user account unconfirmed? - if (updatedUser.getUserAccountStatus().equals(UserAccountStatus.UNCONFIRMED)) { - // Is unconfirmed - throw new UserStatusUnconfirmedException(container.getUser()); - } else if (updatedUser.getUserAccountStatus().equals(UserAccountStatus.LOCKED)) { - // Is locked - throw new UserStatusLockedException(container.getUser()); - } else if (!this.isPasswordMatching(container, updatedUser)) { - // Not matcing passwords - throw new UserPasswordMismatchException(container.getUser()); - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.loginUser: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N - - // Return it - return updatedUser; - } - - /** - * Checks if password matches of both instances. Both user instances must - * not match, the first one is the one from the calling bean/controller, the - * second is the from database. - * <p> - * @param container Container instance holding the user instance and - * unencrypted password - * @param updatedUser Updated user instance found for given user name - * <p> - * @return Whether the password matches - */ - private boolean isPasswordMatching (final LoginContainer container, final User updatedUser) { - // First math both instances - if (null == container) { - // Throw NPE - throw new NullPointerException("container is null"); //NOI18N - } else if (null == updatedUser) { - // Throw NPE - throw new NullPointerException("updatedUser is null"); //NOI18N - } else if (container.getUser().equals(updatedUser)) { - // Both same instance! - throw new IllegalArgumentException(MessageFormat.format("container.user matches updatedUser: {0}", container.getUser())); //NOI18N - } - - // Is it the same same password? - return UserUtils.ifPasswordMatches(container, updatedUser); - } - -} diff --git a/src/java/org/mxchange/jusercore/model/login/FinancialsUserLoginSessionBean.java b/src/java/org/mxchange/jusercore/model/login/FinancialsUserLoginSessionBean.java new file mode 100644 index 0000000..75f5ee7 --- /dev/null +++ b/src/java/org/mxchange/jusercore/model/login/FinancialsUserLoginSessionBean.java @@ -0,0 +1,142 @@ +/* + * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. + */ +package org.mxchange.jusercore.model.login; + +import java.text.MessageFormat; +import javax.ejb.EJB; +import javax.ejb.Stateless; +import org.mxchange.jcoreee.database.BaseDatabaseBean; +import org.mxchange.jusercore.container.login.LoginContainer; +import org.mxchange.jusercore.exceptions.UserNotFoundException; +import org.mxchange.jusercore.exceptions.UserPasswordMismatchException; +import org.mxchange.jusercore.exceptions.UserStatusLockedException; +import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException; +import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote; +import org.mxchange.jusercore.model.user.User; +import org.mxchange.jusercore.model.user.UserSessionBeanRemote; +import org.mxchange.jusercore.model.user.UserUtils; +import org.mxchange.jusercore.model.user.status.UserAccountStatus; + +/** + * A session EJB for user logins + * <p> + * @author Roland Häder<roland@mxchange.org> + */ +@Stateless (name = "login", description = "A bean handling the user login for Addressbook project") +public class FinancialsUserLoginSessionBean extends BaseDatabaseBean implements UserLoginSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 21_785_978_127_581_965L; + + /** + * Registration EJB + */ + @EJB + private UserRegistrationSessionBeanRemote registerBean; + + /** + * User EJB + */ + @EJB + private UserSessionBeanRemote userBean; + + @Override + public User validateUserAccountStatus (final LoginContainer container) throws UserNotFoundException, UserStatusLockedException, UserStatusUnconfirmedException, UserPasswordMismatchException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.loginUser: container={1} - CALLED!", this.getClass().getSimpleName(), container)); //NOI18N + + // Check some beans + assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N + assert (this.registerBean instanceof UserRegistrationSessionBeanRemote) : "this.registerBean is not set"; //NOI18N + + // user should not be null + if (null == container) { + // Abort here + throw new NullPointerException("container is null"); //NOI18N + } else if (container.getUser() == null) { + // NPE again + throw new NullPointerException("container.user is null"); //NOI18N + } else if (container.getUserPassword() == null) { + // And yet again NPE + throw new NullPointerException("container.userPassword is null"); //NOI18N + } else if (container.getUserPassword().isEmpty()) { + // Empty password is not allowed, hardcoded. + throw new IllegalArgumentException("container.userPassword is empty"); //NOI18N + } + + // Is the account there? + if (!this.registerBean.isUserNameRegistered(container.getUser())) { + // Not registered + throw new UserNotFoundException(container.getUser()); + } + + // Get user instance from persistance + User updatedUser = this.userBean.fillUserData(container.getUser()); + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("loginUser: updatedUser={0}", updatedUser)); //NOI18N + + // Is the user account unconfirmed? + if (updatedUser.getUserAccountStatus().equals(UserAccountStatus.UNCONFIRMED)) { + // Is unconfirmed + throw new UserStatusUnconfirmedException(container.getUser()); + } else if (updatedUser.getUserAccountStatus().equals(UserAccountStatus.LOCKED)) { + // Is locked + throw new UserStatusLockedException(container.getUser()); + } else if (!this.isPasswordMatching(container, updatedUser)) { + // Not matcing passwords + throw new UserPasswordMismatchException(container.getUser()); + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.loginUser: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N + + // Return it + return updatedUser; + } + + /** + * Checks if password matches of both instances. Both user instances must + * not match, the first one is the one from the calling bean/controller, the + * second is the from database. + * <p> + * @param container Container instance holding the user instance and + * unencrypted password + * @param updatedUser Updated user instance found for given user name + * <p> + * @return Whether the password matches + */ + private boolean isPasswordMatching (final LoginContainer container, final User updatedUser) { + // First math both instances + if (null == container) { + // Throw NPE + throw new NullPointerException("container is null"); //NOI18N + } else if (null == updatedUser) { + // Throw NPE + throw new NullPointerException("updatedUser is null"); //NOI18N + } else if (container.getUser().equals(updatedUser)) { + // Both same instance! + throw new IllegalArgumentException(MessageFormat.format("container.user matches updatedUser: {0}", container.getUser())); //NOI18N + } + + // Is it the same same password? + return UserUtils.ifPasswordMatches(container, updatedUser); + } + +} diff --git a/src/java/org/mxchange/jusercore/model/register/AddressbookUserRegistrationSessionBean.java b/src/java/org/mxchange/jusercore/model/register/AddressbookUserRegistrationSessionBean.java deleted file mode 100644 index 0e47344..0000000 --- a/src/java/org/mxchange/jusercore/model/register/AddressbookUserRegistrationSessionBean.java +++ /dev/null @@ -1,199 +0,0 @@ -/* - * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. - */ -package org.mxchange.jusercore.model.register; - -import java.text.MessageFormat; -import javax.ejb.EJB; -import javax.ejb.EJBException; -import javax.ejb.Stateless; -import javax.mail.Address; -import javax.mail.internet.AddressException; -import javax.mail.internet.InternetAddress; -import javax.persistence.NoResultException; -import javax.persistence.Query; -import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean; -import org.mxchange.jcontacts.contact.Contact; -import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException; -import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException; -import org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote; -import org.mxchange.jusercore.model.user.LoginUser; -import org.mxchange.jusercore.model.user.User; -import org.mxchange.jusercore.model.user.UserSessionBeanRemote; -import org.mxchange.jusercore.model.user.UserUtils; - -/** - * A session bean for user registration - * <p> - * @author Roland Häder<roland@mxchange.org> - */ -@Stateless (name = "register", description = "A bean handling the user registration") -public class AddressbookUserRegistrationSessionBean extends BaseAddressbookDatabaseBean implements UserRegistrationSessionBeanRemote { - - /** - * Serial number - */ - private static final long serialVersionUID = 12_348_958_986_818_627L; - - /** - * Administrative user bean - */ - @EJB - private AdminUserSessionBeanRemote adminUserBean; - - /** - * Regular user EJB - */ - @EJB - private UserSessionBeanRemote userBean; - - @Override - public String generateConfirmationKey (final User user) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N - - // user should not be null - if (null == user) { - // Abort here - throw new NullPointerException("user is null"); //NOI18N - } - - // Create named instance - Query query = this.getEntityManager().createNamedQuery("SearchUserByConfirmKey", LoginUser.class); //NOI18N - - // Init confirmation key - String confirmationKey = null; - - // Find a free one - while (confirmationKey == null) { - // Create new one - String key = UserUtils.generatedConfirmationKey(user); - - // Set key as parameter - query.setParameter("confirmKey", key); //NOI18N - - // Try it - try { - // Get contact instance - Contact contact = (Contact) query.getSingleResult(); - - // Warning message - this.getLoggerBeanLocal().logWarning(MessageFormat.format("{0}.generateConfirmationKey: key {1} already found: contact.contactId={2}", this.getClass().getSimpleName(), key, contact.getContactId())); //NOI18N - } catch (final NoResultException ex) { - // Not found, normal case - confirmationKey = key; - break; - } - } - - // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: confirmationKey={1} - EXIT!", this.getClass().getSimpleName(), confirmationKey)); //NOI18N - - // Return it - return confirmationKey; - } - - @Override - public boolean isEmailAddressRegistered (final User user) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N - - // Check bean - assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N - - // user should not be null - if (null == user) { - // Abort here - throw new NullPointerException("user is null"); //NOI18N - } - - // Call other bean - return this.userBean.isEmailAddressRegistered(user); - } - - @Override - public boolean isUserNameRegistered (final User user) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N - - // Check bean - assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N - - // user should not be null - if (null == user) { - // Abort here - throw new NullPointerException("user is null"); //NOI18N - } - - // Call other bean - return this.userBean.isUserNameRegistered(user); - } - - @Override - public User registerUser (final User user, final String baseUrl) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N - - // user should not be null - if (null == user) { - // Abort here - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserContact() == null) { - // Throw NPE again - throw new NullPointerException("user.userContact is null"); //NOI18N - } else if (user.getUserContact().getContactEmailAddress() == null) { - // Throw NPE again - throw new NullPointerException("user.userContact.contactEmailAddress is null"); //NOI18N - } else if (user.getUserContact().getContactEmailAddress().isEmpty()) { - // Is empty - throw new IllegalArgumentException("user.userContact.contactEmailAddress is empty"); //NOI18N - } - - // Check if user is registered - if (this.isUserNameRegistered(user)) { - // Abort here - throw new UserNameAlreadyRegisteredException(user); - } else if (this.isEmailAddressRegistered(user)) { - // Abort here - throw new EmailAddressAlreadyRegisteredException(user); - } - - // Call other EJB - User addedUser = this.adminUserBean.addUser(user); - - // Init variable - Address emailAddress; - - try { - // Create email address and set - emailAddress = new InternetAddress(addedUser.getUserContact().getContactEmailAddress()); - } catch (final AddressException ex) { - // Throw again - throw new EJBException(ex); - } - - // Send email - // TODO: Internationlize the subject line somehow - this.sendEmail("Registration", "registration", emailAddress, addedUser, baseUrl); //NOI18N - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: addedUser={1},addedUser.userId={2} - EXIT!", this.getClass().getSimpleName(), addedUser, addedUser.getUserId())); //NOI18N - - // Return it - return addedUser; - } - -} diff --git a/src/java/org/mxchange/jusercore/model/register/FinancialsUserRegistrationSessionBean.java b/src/java/org/mxchange/jusercore/model/register/FinancialsUserRegistrationSessionBean.java new file mode 100644 index 0000000..ed3858f --- /dev/null +++ b/src/java/org/mxchange/jusercore/model/register/FinancialsUserRegistrationSessionBean.java @@ -0,0 +1,199 @@ +/* + * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. + */ +package org.mxchange.jusercore.model.register; + +import java.text.MessageFormat; +import javax.ejb.EJB; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.mail.Address; +import javax.mail.internet.AddressException; +import javax.mail.internet.InternetAddress; +import javax.persistence.NoResultException; +import javax.persistence.Query; +import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean; +import org.mxchange.jcontacts.contact.Contact; +import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException; +import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException; +import org.mxchange.jusercore.model.user.AdminUserSessionBeanRemote; +import org.mxchange.jusercore.model.user.LoginUser; +import org.mxchange.jusercore.model.user.User; +import org.mxchange.jusercore.model.user.UserSessionBeanRemote; +import org.mxchange.jusercore.model.user.UserUtils; + +/** + * A session bean for user registration + * <p> + * @author Roland Häder<roland@mxchange.org> + */ +@Stateless (name = "register", description = "A bean handling the user registration") +public class FinancialsUserRegistrationSessionBean extends BaseFinancialsDatabaseBean implements UserRegistrationSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 12_348_958_986_818_627L; + + /** + * Administrative user bean + */ + @EJB + private AdminUserSessionBeanRemote adminUserBean; + + /** + * Regular user EJB + */ + @EJB + private UserSessionBeanRemote userBean; + + @Override + public String generateConfirmationKey (final User user) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } + + // Create named instance + Query query = this.getEntityManager().createNamedQuery("SearchUserByConfirmKey", LoginUser.class); //NOI18N + + // Init confirmation key + String confirmationKey = null; + + // Find a free one + while (confirmationKey == null) { + // Create new one + String key = UserUtils.generatedConfirmationKey(user); + + // Set key as parameter + query.setParameter("confirmKey", key); //NOI18N + + // Try it + try { + // Get contact instance + Contact contact = (Contact) query.getSingleResult(); + + // Warning message + this.getLoggerBeanLocal().logWarning(MessageFormat.format("{0}.generateConfirmationKey: key {1} already found: contact.contactId={2}", this.getClass().getSimpleName(), key, contact.getContactId())); //NOI18N + } catch (final NoResultException ex) { + // Not found, normal case + confirmationKey = key; + break; + } + } + + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateConfirmationKey: confirmationKey={1} - EXIT!", this.getClass().getSimpleName(), confirmationKey)); //NOI18N + + // Return it + return confirmationKey; + } + + @Override + public boolean isEmailAddressRegistered (final User user) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N + + // Check bean + assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } + + // Call other bean + return this.userBean.isEmailAddressRegistered(user); + } + + @Override + public boolean isUserNameRegistered (final User user) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N + + // Check bean + assert (this.userBean instanceof UserSessionBeanRemote) : "this.userBean is not set"; //NOI18N + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } + + // Call other bean + return this.userBean.isUserNameRegistered(user); + } + + @Override + public User registerUser (final User user, final String baseUrl) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserContact() == null) { + // Throw NPE again + throw new NullPointerException("user.userContact is null"); //NOI18N + } else if (user.getUserContact().getContactEmailAddress() == null) { + // Throw NPE again + throw new NullPointerException("user.userContact.contactEmailAddress is null"); //NOI18N + } else if (user.getUserContact().getContactEmailAddress().isEmpty()) { + // Is empty + throw new IllegalArgumentException("user.userContact.contactEmailAddress is empty"); //NOI18N + } + + // Check if user is registered + if (this.isUserNameRegistered(user)) { + // Abort here + throw new UserNameAlreadyRegisteredException(user); + } else if (this.isEmailAddressRegistered(user)) { + // Abort here + throw new EmailAddressAlreadyRegisteredException(user); + } + + // Call other EJB + User addedUser = this.adminUserBean.addUser(user); + + // Init variable + Address emailAddress; + + try { + // Create email address and set + emailAddress = new InternetAddress(addedUser.getUserContact().getContactEmailAddress()); + } catch (final AddressException ex) { + // Throw again + throw new EJBException(ex); + } + + // Send email + // TODO: Internationlize the subject line somehow + this.sendEmail("Registration", "registration", emailAddress, addedUser, baseUrl); //NOI18N + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.registerUser: addedUser={1},addedUser.userId={2} - EXIT!", this.getClass().getSimpleName(), addedUser, addedUser.getUserId())); //NOI18N + + // Return it + return addedUser; + } + +} diff --git a/src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java deleted file mode 100644 index 212969b..0000000 --- a/src/java/org/mxchange/jusercore/model/user/AddressbookAdminUserSessionBean.java +++ /dev/null @@ -1,356 +0,0 @@ -/* - * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. - */ -package org.mxchange.jusercore.model.user; - -import java.text.MessageFormat; -import java.util.GregorianCalendar; -import javax.ejb.EJB; -import javax.ejb.EJBException; -import javax.ejb.Stateless; -import javax.mail.Address; -import javax.mail.internet.AddressException; -import javax.mail.internet.InternetAddress; -import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean; -import org.mxchange.jcontacts.contact.Contact; -import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException; -import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException; -import org.mxchange.jusercore.exceptions.UserNotFoundException; -import org.mxchange.jusercore.exceptions.UserStatusConfirmedException; -import org.mxchange.jusercore.exceptions.UserStatusLockedException; -import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException; -import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote; -import org.mxchange.jusercore.model.user.status.UserAccountStatus; - -/** - * An administrative user EJB - * <p> - * @author Roland Häder<roland@mxchange.org> - */ -@Stateless (name = "adminUser", description = "A bean handling the user data") -public class AddressbookAdminUserSessionBean extends BaseAddressbookDatabaseBean implements AdminUserSessionBeanRemote { - - /** - * Serial number - */ - private static final long serialVersionUID = 542_145_347_916L; - - /** - * Registration EJB - */ - @EJB - private UserRegistrationSessionBeanRemote registerBean; - - /** - * Regular user bean - */ - @EJB - private UserSessionBeanRemote userBean; - - /** - * Default constructor - */ - public AddressbookAdminUserSessionBean () { - } - - @Override - public User addUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N - - // user should not be null - if (null == user) { - // Abort here - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() != null) { - // Not allowed here - throw new IllegalStateException(MessageFormat.format("user.userId must be null, is: {0}", user.getUserId())); //NOI18N - } - - // Check if user is registered - if (this.registerBean.isUserNameRegistered(user)) { - // Abort here - throw new UserNameAlreadyRegisteredException(user); - } else if (this.registerBean.isEmailAddressRegistered(user)) { - // Abort here - throw new EmailAddressAlreadyRegisteredException(user); - } - - // Set created timestamp - user.setUserCreated(new GregorianCalendar()); - user.getUserContact().setContactCreated(new GregorianCalendar()); - - // Update cellphone, land-line and fax instance - this.setAllContactPhoneEntriesCreated(user.getUserContact()); - - // Persist it - this.getEntityManager().persist(user); - - // Flush to get id back - this.getEntityManager().flush(); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1},user.userId={2} - EXIT!", this.getClass().getSimpleName(), user, user.getUserId())); //NOI18N - - // Return it - return user; - } - - @Override - public void deleteUser (final User user, final String userDeleteReason) throws UserNotFoundException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N - - // user should not be null - if (null == user) { - // Abort here - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() == null) { - // Id is set - throw new NullPointerException("user.userId is null"); //NOI18N - } else if (user.getUserId() < 1) { - // Not valid id number - throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N - } else if (user.getUserContact() == null) { - // Throw NPE again - throw new NullPointerException("user.userContact is null"); //NOI18N - } else if (user.getUserContact().getContactId() == null) { - // Throw NPE again - throw new NullPointerException("user.userContact.contactId is null"); //NOI18N - } else if (user.getUserContact().getContactId() < 1) { - // Not valid id number - throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N - } else if (user.getUserAccountStatus() == null) { - // Throw NPE again - throw new NullPointerException("user.userAccountStatus is null"); //NOI18N - } else if (!this.userBean.ifUserExists(user)) { - // Name already found - throw new UserNotFoundException(user); - } - - // Get a managed instance - User managedUser = this.getManagedUser(user); - - // Should be found! - assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N - - // Delete it - this.getEntityManager().remove(managedUser); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: EXIT!", this.getClass().getSimpleName())); //NOI18N - } - - @Override - public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={0} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N - - // user should not be null - if (null == user) { - // Abort here - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() instanceof Long) { - // Id is set - throw new IllegalArgumentException("user.userId is not null"); //NOI18N - } else if (user.getUserContact() == null) { - // Throw NPE again - throw new NullPointerException("user.userContact is null"); //NOI18N - } else if (user.getUserContact().getContactId() == null) { - // Throw NPE again - throw new NullPointerException("user.userContact.contactId is null"); //NOI18N - } else if (user.getUserContact().getContactId() < 1) { - // Not valid id number - throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N - } else if (user.getUserAccountStatus() == null) { - // Throw NPE again - throw new NullPointerException("user.userAccountStatus is null"); //NOI18N - } else if (this.userBean.ifUserNameExists(user.getUserName())) { - // Name already found - throw new UserNameAlreadyRegisteredException(user.getUserName()); - } - - // Try to find the contact - Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId()); - - // Set detached object in rexcruiter instance - user.setUserContact(foundContact); - - // Set timestamp - user.setUserCreated(new GregorianCalendar()); - - // Perist it - this.getEntityManager().persist(user); - - // Flush it to get updated instance back - this.getEntityManager().flush(); - - // Log trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N - - // Return updated instanc - return user; - } - - @Override - public User lockUserAccount (final User user, final String userLockReason, final String baseUrl) throws UserStatusLockedException, UserStatusUnconfirmedException, UserNotFoundException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},userLockReason={2},baseUrl={3} - CALLED!", this.getClass().getSimpleName(), user, userLockReason, baseUrl)); //NOI18N - - // user should not be null - if (null == user) { - // Abort here - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() == null) { - // Id is set - throw new NullPointerException("user.userId is null"); //NOI18N - } else if (user.getUserId() < 1) { - // Id is set - throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N - } else if (user.getUserContact() == null) { - // Throw NPE again - throw new NullPointerException("user.userContact is null"); //NOI18N - } else if (user.getUserContact().getContactId() == null) { - // Throw NPE again - throw new NullPointerException("user.userContact.contactId is null"); //NOI18N - } else if (user.getUserContact().getContactId() < 1) { - // Not valid id number - throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N - } else if (user.getUserAccountStatus() == null) { - // Throw NPE again - throw new NullPointerException("user.userAccountStatus is null"); //NOI18N - } else if (!this.userBean.ifUserExists(user)) { - // Name already found - throw new UserNotFoundException(user); - } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) { - // Account is locked - throw new UserStatusLockedException(user); - } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) { - // Account is unconfirmed - throw new UserStatusUnconfirmedException(user); - } else if (null == userLockReason) { - // Throw NPE again - throw new NullPointerException("userLockReason is null"); //NOI18N - } else if (userLockReason.isEmpty()) { - // Is empty - throw new IllegalArgumentException("userLockReason is empty"); //NOI18N - } - - // Remove contact instance as this is not updated - user.setUserContact(null); - - // Set as locked, set timestamp and lock reason - user.setUserAccountStatus(UserAccountStatus.LOCKED); - user.setUserLastLocked(new GregorianCalendar()); - user.setUserLastLockedReason(userLockReason); - - // Update user - User updatedUser = this.userBean.updateUserData(user); - - // @TODO Create user lock history entry - // Init variable - Address emailAddress; - - try { - // Create email address and set - emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress()); - } catch (final AddressException ex) { - // Throw again - throw new EJBException(ex); - } - - // Send out email - // @TODO externalize subject line - this.sendEmail("Account locked", "account_locked", emailAddress, updatedUser, baseUrl); //NOI18N - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N - - // Return detached (and updated) user - return updatedUser; - } - - @Override - public User unlockUserAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusUnconfirmedException, UserNotFoundException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N - - // user should not be null - if (null == user) { - // Abort here - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() == null) { - // Id is set - throw new NullPointerException("user.userId is null"); //NOI18N - } else if (user.getUserId() < 1) { - // Id is set - throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N - } else if (user.getUserContact() == null) { - // Throw NPE again - throw new NullPointerException("user.userContact is null"); //NOI18N - } else if (user.getUserContact().getContactId() == null) { - // Throw NPE again - throw new NullPointerException("user.userContact.contactId is null"); //NOI18N - } else if (user.getUserContact().getContactId() < 1) { - // Not valid id number - throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N - } else if (user.getUserAccountStatus() == null) { - // Throw NPE again - throw new NullPointerException("user.userAccountStatus is null"); //NOI18N - } else if (!this.userBean.ifUserExists(user)) { - // Name already found - throw new UserNotFoundException(user); - } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) { - // Account is confirmed - throw new UserStatusConfirmedException(user); - } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) { - // Account is unconfirmed - throw new UserStatusUnconfirmedException(user); - } - - // Remove contact instance as this is not updated - user.setUserContact(null); - - // Unlock account - user.setUserAccountStatus(UserAccountStatus.CONFIRMED); - - // Update user - User updatedUser = this.userBean.updateUserData(user); - - // @TODO Create user lock history entry - // Init variable - Address emailAddress; - - try { - // Create email address and set - emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress()); - } catch (final AddressException ex) { - // Throw again - throw new EJBException(ex); - } - - // Send out email - // @TODO externalize subject line - this.sendEmail("Account unlocked", "account_unlocked", emailAddress, updatedUser, baseUrl); //NOI18N - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N - - // Return changed account - return updatedUser; - } - -} diff --git a/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java deleted file mode 100644 index 06ab9ad..0000000 --- a/src/java/org/mxchange/jusercore/model/user/AddressbookUserSessionBean.java +++ /dev/null @@ -1,802 +0,0 @@ -/* - * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. - */ -package org.mxchange.jusercore.model.user; - -import java.text.MessageFormat; -import java.util.GregorianCalendar; -import java.util.List; -import javax.ejb.EJB; -import javax.ejb.EJBException; -import javax.ejb.Stateless; -import javax.mail.Address; -import javax.mail.internet.AddressException; -import javax.mail.internet.InternetAddress; -import javax.persistence.NoResultException; -import javax.persistence.PersistenceException; -import javax.persistence.Query; -import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean; -import org.mxchange.jcontacts.contact.Contact; -import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; -import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; -import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber; -import org.mxchange.jusercore.exceptions.UserNotFoundException; -import org.mxchange.jusercore.exceptions.UserStatusConfirmedException; -import org.mxchange.jusercore.exceptions.UserStatusLockedException; -import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException; -import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote; -import org.mxchange.jusercore.model.user.password_history.PasswordHistory; -import org.mxchange.jusercore.model.user.password_history.UserPasswordHistory; -import org.mxchange.jusercore.model.user.profilemodes.ProfileMode; -import org.mxchange.jusercore.model.user.status.UserAccountStatus; - -/** - * A user EJB - * <p> - * @author Roland Häder<roland@mxchange.org> - */ -@Stateless (name = "user", description = "A bean handling the user data") -public class AddressbookUserSessionBean extends BaseAddressbookDatabaseBean implements UserSessionBeanRemote { - - /** - * Serial number - */ - private static final long serialVersionUID = 542_145_347_916L; - - /** - * Registration EJB - */ - @EJB - private UserRegistrationSessionBeanRemote registerBean; - - /** - * Default constructor - */ - public AddressbookUserSessionBean () { - } - - @Override - @SuppressWarnings ("unchecked") - public List<User> allMemberPublicVisibleUsers () { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMemberPublicVisibleUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N - - // Get named query - Query query = this.getEntityManager().createNamedQuery("AllMemberPublicUsers", LoginUser.class); //NOI18N - - // Set parameters - query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N - query.setParameter("members", ProfileMode.MEMBERS); //NOI18N - query.setParameter("public", ProfileMode.PUBLIC); //NOI18N - - // Get result - List<User> users = query.getResultList(); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMemberPublicVisibleUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N - - // Return full list - return users; - } - - @Override - @SuppressWarnings ("unchecked") - public List<User> allPublicUsers () { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allPublicUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N - - // Get named query - Query query = this.getEntityManager().createNamedQuery("AllPublicUsers", LoginUser.class); //NOI18N - - // Set parameters - query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N - query.setParameter("mode", ProfileMode.PUBLIC); //NOI18N - - // Get result - List<User> users = query.getResultList(); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allPublicUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N - - // Return full list - return users; - } - - @Override - @SuppressWarnings ("unchecked") - public List<User> allUsers () { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N - - // Get named query - Query query = this.getEntityManager().createNamedQuery("AllUsers", LoginUser.class); //NOI18N - - // Get result - List<User> users = query.getResultList(); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N - - // Return full list - return users; - } - - @Override - public User confirmAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusLockedException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N - - // Parameter must be valid - if (null == user) { - // Abort here - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() == null) { - // Abort here - throw new NullPointerException("user.userId is null"); //NOI18N - } else if (user.getUserId() < 1) { - // Invalid number - throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N - } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) { - // Account is already confirmed - throw new UserStatusConfirmedException(user); - } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) { - // Account is already confirmed - throw new UserStatusLockedException(user); - } else if (user.getUserConfirmKey() == null) { - // Throw NPE - throw new NullPointerException("user.userConfirmKey is null"); //NOI18N - } - - // Update user status and remove confirmation key - user.setUserAccountStatus(UserAccountStatus.CONFIRMED); - user.setUserConfirmKey(null); - user.setUserUpdated(new GregorianCalendar()); - - // Update user account - User updatedUser = this.updateUserData(user); - - // Init variable - Address emailAddress; - - try { - // Create email address and set - emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress()); - } catch (final AddressException ex) { - // Throw again - throw new EJBException(ex); - } - - // Send out email - this.sendEmail("Account confirmed", "account_confirmed", emailAddress, updatedUser, baseUrl); //NOI18N - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N - - // Return updated instance - return updatedUser; - } - - @Override - public User fillUserData (final User user) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fillUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N - - // user should not be null - if (null == user) { - // Abort here - throw new NullPointerException("user is null"); //NOI18N - } - - // Try to locate it - Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N - - // Set parameter - query.setParameter("userName", user.getUserName()); //NOI18N - - // Initialize variable - User foundUser = null; - - // Try it - try { - // Try to get single result - foundUser = (User) query.getSingleResult(); - } catch (final NoResultException ex) { - // Log it - this.getLoggerBeanLocal().logException(ex); - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fillUserData: foundUser={1} - EXIT!", this.getClass().getSimpleName(), foundUser)); //NOI18N - - // Return prepared instance - return foundUser; - } - - @Override - public User findUserById (final Long userId) throws UserNotFoundException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findUserById: userId={1} - CALLED!", this.getClass().getSimpleName(), userId)); //NOI18N - - // Is the parameter valid? - if (null == userId) { - // Throw NPE - throw new NullPointerException("userId is null"); //NOI18N - } else if (userId < 1) { - // Not valid - throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N - } else if (!this.ifUserIdExists(userId)) { - // Does not exist - throw new UserNotFoundException(userId); - } - - // Create query instance - Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N - - // Set user id - query.setParameter("id", userId); //NOI18N - - // Fetch the result, it should be there by now - User user = (User) query.getSingleResult(); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findUserById: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N - - // Return found user - return user; - } - - @Override - public String generateRandomUserName () { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateRandomUserName - CALLED!", this.getClass().getSimpleName())); //NOI18N - - // Get full list - List<String> userList = this.getUserNameList(); - - // Init variable - String userName = null; - - // Loop until a user name is found - while ((userName == null) || (userList.contains(userName))) { - // Generate random name - userName = UserUtils.generateRandomUserName(); - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateRandomUserName: userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N - - // Found one, so return it - return userName; - } - - @Override - @SuppressWarnings ("unchecked") - public List<String> getEmailAddressList () { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: CALLED!", this.getClass().getSimpleName())); //NOI18N - - // Get query - Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N - - // Get result list - List<String> emailAddressList = query.getResultList(); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: emailAddressList.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddressList.size())); //NOI18N - - // Return it - return emailAddressList; - } - - @Override - @SuppressWarnings ("unchecked") - public List<String> getUserNameList () { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserNameList: CALLED!", this.getClass().getSimpleName())); //NOI18N - - // Get query - Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N - - // Get result list - List<String> userNameList = query.getResultList(); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserNameList: userNameList.size()={1} - EXIT!", this.getClass().getSimpleName(), userNameList.size())); //NOI18N - - // Return it - return userNameList; - } - - @Override - public boolean ifUserExists (final User user) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N - - // userId should not be null - if (null == user) { - // Abort here - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() == null) { - // Abort here - throw new NullPointerException("user.userId is null"); //NOI18N - } else if (user.getUserId() < 1) { - // Invalid number - throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N - } - - // Generate query - Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N - - // Set parameter - query.setParameter("id", user.getUserId()); //NOI18N - - // Try this - try { - User dummy = (User) query.getSingleResult(); - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N - } catch (final NoResultException ex) { - // Log it - this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N - - // User name does not exist - return false; - } catch (final PersistenceException ex) { - // Something bad happened - this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N - - // Throw again - throw ex; - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: Found user {1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N - - // Found it - return true; - } - - @Override - public boolean ifUserIdExists (final Long userId) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserIdExists: userId={1} - CALLED!", this.getClass().getSimpleName(), userId)); //NOI18N - - // userId should not be null - if (null == userId) { - // Abort here - throw new NullPointerException("userId is null"); //NOI18N - } else if (userId < 1) { - // Invalid number - throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", userId)); //NOI18N - } - - // Generate query - Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N - - // Set parameter - query.setParameter("id", userId); //NOI18N - - // Try this - try { - User dummy = (User) query.getSingleResult(); - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserIdExists: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N - } catch (final NoResultException ex) { - // Log it - this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserIdExists: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N - - // User name does not exist - return false; - } catch (final PersistenceException ex) { - // Something bad happened - this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N - - // Throw again - throw ex; - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserIdExists: Found userId={1} - EXIT!", this.getClass().getSimpleName(), userId)); //NOI18N - - // Found it - return true; - } - - @Override - public boolean ifUserNameExists (final String userName) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: userName={1} - CALLED!", this.getClass().getSimpleName(), userName)); //NOI18N - - // userId should not be null - if (null == userName) { - // Abort here - throw new NullPointerException("userName is null"); //NOI18N - } else if (userName.isEmpty()) { - // Abort here - throw new NullPointerException("userName is empty"); //NOI18N - } - - // Generate query - Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N - - // Set parameter - query.setParameter("userName", userName); //NOI18N - - // Try this - try { - User dummy = (User) query.getSingleResult(); - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserNameExists: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N - } catch (final NoResultException ex) { - // Log it - this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserNameExists: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N - - // User name does not exist - return false; - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: Found userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N - - // Found it - return true; - } - - @Override - public boolean isEmailAddressRegistered (final User user) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N - - // user should not be null - if (null == user) { - // Abort here - throw new NullPointerException("user is null"); //NOI18N - } - - // Generate query - Query query = this.getEntityManager().createNamedQuery("SearchUserByEmailAddress", LoginUser.class); //NOI18N - - // Set parameter - query.setParameter("emailAddress", user.getUserContact().getContactEmailAddress()); //NOI18N - - // Search for it - try { - User dummy = (User) query.getSingleResult(); - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N - } catch (final NoResultException ex) { - // Log it - this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N - - // Email address does not exist - return false; - } catch (final PersistenceException ex) { - // Something bad happened - this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N - - // Throw again - throw ex; - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N - - // Found it - return true; - } - - @Override - public boolean isUserNameRegistered (final User user) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N - - // user should not be null - if (null == user) { - // Abort here - throw new NullPointerException("user is null"); //NOI18N - } - - // Generate query - Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N - - // Set parameter - query.setParameter("userName", user.getUserName()); //NOI18N - - // Try this - try { - User dummy = (User) query.getSingleResult(); - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isUserNameRegistered: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N - } catch (final NoResultException ex) { - // Log it - this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isUserNameRegistered: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N - - // User name does not exist - return false; - } catch (final PersistenceException ex) { - // Something bad happened - this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N - - // Throw again - throw ex; - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N - - // Found it - return true; - } - - @Override - public User updateUserData (final User user) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N - - // user should not be null - if (null == user) { - // Abort here - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() == null) { - // Throw NPE again - throw new NullPointerException("user.userId is null"); //NOI18N - } else if (user.getUserId() < 1) { - // Not valid - throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N - } else if (user.getUserAccountStatus() == null) { - // Throw NPE again - throw new NullPointerException("user.userAccountStatus is null"); //NOI18N - } else if (!this.ifUserExists(user)) { - // User does not exist - throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N - } - - // Remove contact instance as this is not updated - user.setUserContact(null); - - // Find the instance - User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId()); - - // Should be found! - assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N - - // Merge user - User detachedUser = this.getEntityManager().merge(foundUser); - - // Should be found! - assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N - - // Copy all data - detachedUser.copyAll(user); - - // Set as updated - detachedUser.setUserUpdated(new GregorianCalendar()); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: detachedUser={1} - CALLED!", this.getClass().getSimpleName(), detachedUser)); //NOI18N - - // Return updated instance - return detachedUser; - } - - @Override - public PasswordHistory updateUserPassword (final User user, final String baseUrl) throws UserNotFoundException, UserStatusUnconfirmedException, UserStatusLockedException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N - - // user should not be null - if (null == user) { - // Abort here - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() == null) { - // Throw NPE again - throw new NullPointerException("user.userId is null"); //NOI18N - } else if (user.getUserId() < 1) { - // Not valid - throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N - } else if (user.getUserAccountStatus() == null) { - // Throw NPE again - throw new NullPointerException("user.userAccountStatus is null"); //NOI18N - } else if (!this.ifUserExists(user)) { - // User does not exist - throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N - } else if (null == baseUrl) { - // Abort here - throw new NullPointerException("password is null"); //NOI18N - } else if (baseUrl.isEmpty()) { - // Abort here - throw new IllegalArgumentException("password is empty"); //NOI18N - } - - // Call other method - User updatedUser = this.updateUserData(user); - - // Create history entry - PasswordHistory entry = new UserPasswordHistory(user.getUserEncryptedPassword(), updatedUser); - - // Set created timestamp - entry.setUserPasswordHistoryCreated(new GregorianCalendar()); - - // Persist it - this.getEntityManager().persist(entry); - - // Flush it to get id number back - this.getEntityManager().flush(); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: entry.userPasswordHistoryId={1} - EXIT!", this.getClass().getSimpleName(), entry.getUserPasswordHistoryId())); //NOI18N - - // Return it - return entry; - } - - @Override - public User updateUserPersonalData (final User user) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N - - // user should not be null - if (null == user) { - // Abort here - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() == null) { - // Throw NPE again - throw new NullPointerException("user.userId is null"); //NOI18N - } else if (user.getUserId() < 1) { - // Not valid - throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N - } else if (user.getUserAccountStatus() == null) { - // Throw NPE again - throw new NullPointerException("user.userAccountStatus is null"); //NOI18N - } else if (!this.ifUserExists(user)) { - // User does not exist - throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N - } - - // Find the instance - User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId()); - - // Should be found! - assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N - - // Merge user - User detachedUser = this.getEntityManager().merge(foundUser); - - // Should be found! - assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N - - // Copy all data - detachedUser.copyAll(user); - - // Set as updated - detachedUser.setUserUpdated(new GregorianCalendar()); - detachedUser.getUserContact().setContactUpdated(new GregorianCalendar()); - - // Get contact from it and find it - Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId()); - - // Should be found - assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N - - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N - - // Merge contact instance - Contact detachedContact = this.getEntityManager().merge(foundContact); - - // Copy all - detachedContact.copyAll(user.getUserContact()); - - // Set it back in user - user.setUserContact(detachedContact); - - // Should be found! - assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N - - // Get mobile instance - DialableMobileNumber mobileNumber = detachedContact.getContactMobileNumber(); - - // Is there a mobile instance set? - if (mobileNumber instanceof DialableMobileNumber) { - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobile.phoneId={0} is being updated ...", mobileNumber.getPhoneId())); //NOI18N - - // Then find it, too - DialableMobileNumber foundMobile = this.getEntityManager().find(mobileNumber.getClass(), mobileNumber.getPhoneId()); - - // Should be there - assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getPhoneId()); //NOI18N - - // Then merge it, too - DialableMobileNumber detachedMobile = this.getEntityManager().merge(foundMobile); - - // Should be there - assert (detachedMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", detachedMobile.getPhoneId()); //NOI18N - - // Copy all - detachedMobile.copyAll(user.getUserContact().getContactMobileNumber()); - - // Set it back - detachedContact.setContactMobileNumber(detachedMobile); - } - - // Get mobile instance - DialableFaxNumber fax = detachedContact.getContactFaxNumber(); - - // Is there a fax instance set? - if (fax instanceof DialableFaxNumber) { - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N - - // Then find it, too - DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId()); - - // Should be there - assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N - - // Then merge it, too - DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax); - - // Should be there - assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N - - // Copy all - detachedFax.copyAll(user.getUserContact().getContactFaxNumber()); - - // Set it back - detachedContact.setContactFaxNumber(detachedFax); - } - - // Get mobile instance - DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber(); - - // Is there a fax instance set? - if (landLine instanceof DialableLandLineNumber) { - // Debug message - this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N - - // Then find it, too - DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId()); - - // Should be there - assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N - - // Then merge it, too - DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine); - - // Should be there - assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N - - // Copy all - detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber()); - - // Set it back - detachedContact.setContactLandLineNumber(detachedLandLine); - } - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: entry.detachedUser={1} - EXIT!", this.getClass().getSimpleName(), detachedUser)); //NOI18N - - // Return updated user instance - return detachedUser; - } - -} diff --git a/src/java/org/mxchange/jusercore/model/user/FinancialsAdminUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/FinancialsAdminUserSessionBean.java new file mode 100644 index 0000000..1db5c53 --- /dev/null +++ b/src/java/org/mxchange/jusercore/model/user/FinancialsAdminUserSessionBean.java @@ -0,0 +1,356 @@ +/* + * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. + */ +package org.mxchange.jusercore.model.user; + +import java.text.MessageFormat; +import java.util.GregorianCalendar; +import javax.ejb.EJB; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.mail.Address; +import javax.mail.internet.AddressException; +import javax.mail.internet.InternetAddress; +import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean; +import org.mxchange.jcontacts.contact.Contact; +import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException; +import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException; +import org.mxchange.jusercore.exceptions.UserNotFoundException; +import org.mxchange.jusercore.exceptions.UserStatusConfirmedException; +import org.mxchange.jusercore.exceptions.UserStatusLockedException; +import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException; +import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote; +import org.mxchange.jusercore.model.user.status.UserAccountStatus; + +/** + * An administrative user EJB + * <p> + * @author Roland Häder<roland@mxchange.org> + */ +@Stateless (name = "adminUser", description = "A bean handling the user data") +public class FinancialsAdminUserSessionBean extends BaseFinancialsDatabaseBean implements AdminUserSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 542_145_347_916L; + + /** + * Registration EJB + */ + @EJB + private UserRegistrationSessionBeanRemote registerBean; + + /** + * Regular user bean + */ + @EJB + private UserSessionBeanRemote userBean; + + /** + * Default constructor + */ + public FinancialsAdminUserSessionBean () { + } + + @Override + public User addUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() != null) { + // Not allowed here + throw new IllegalStateException(MessageFormat.format("user.userId must be null, is: {0}", user.getUserId())); //NOI18N + } + + // Check if user is registered + if (this.registerBean.isUserNameRegistered(user)) { + // Abort here + throw new UserNameAlreadyRegisteredException(user); + } else if (this.registerBean.isEmailAddressRegistered(user)) { + // Abort here + throw new EmailAddressAlreadyRegisteredException(user); + } + + // Set created timestamp + user.setUserCreated(new GregorianCalendar()); + user.getUserContact().setContactCreated(new GregorianCalendar()); + + // Update cellphone, land-line and fax instance + this.setAllContactPhoneEntriesCreated(user.getUserContact()); + + // Persist it + this.getEntityManager().persist(user); + + // Flush to get id back + this.getEntityManager().flush(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1},user.userId={2} - EXIT!", this.getClass().getSimpleName(), user, user.getUserId())); //NOI18N + + // Return it + return user; + } + + @Override + public void deleteUser (final User user, final String userDeleteReason) throws UserNotFoundException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Id is set + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Not valid id number + throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N + } else if (user.getUserContact() == null) { + // Throw NPE again + throw new NullPointerException("user.userContact is null"); //NOI18N + } else if (user.getUserContact().getContactId() == null) { + // Throw NPE again + throw new NullPointerException("user.userContact.contactId is null"); //NOI18N + } else if (user.getUserContact().getContactId() < 1) { + // Not valid id number + throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N + } else if (user.getUserAccountStatus() == null) { + // Throw NPE again + throw new NullPointerException("user.userAccountStatus is null"); //NOI18N + } else if (!this.userBean.ifUserExists(user)) { + // Name already found + throw new UserNotFoundException(user); + } + + // Get a managed instance + User managedUser = this.getManagedUser(user); + + // Should be found! + assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N + + // Delete it + this.getEntityManager().remove(managedUser); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: EXIT!", this.getClass().getSimpleName())); //NOI18N + } + + @Override + public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={0} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() instanceof Long) { + // Id is set + throw new IllegalArgumentException("user.userId is not null"); //NOI18N + } else if (user.getUserContact() == null) { + // Throw NPE again + throw new NullPointerException("user.userContact is null"); //NOI18N + } else if (user.getUserContact().getContactId() == null) { + // Throw NPE again + throw new NullPointerException("user.userContact.contactId is null"); //NOI18N + } else if (user.getUserContact().getContactId() < 1) { + // Not valid id number + throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N + } else if (user.getUserAccountStatus() == null) { + // Throw NPE again + throw new NullPointerException("user.userAccountStatus is null"); //NOI18N + } else if (this.userBean.ifUserNameExists(user.getUserName())) { + // Name already found + throw new UserNameAlreadyRegisteredException(user.getUserName()); + } + + // Try to find the contact + Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId()); + + // Set detached object in rexcruiter instance + user.setUserContact(foundContact); + + // Set timestamp + user.setUserCreated(new GregorianCalendar()); + + // Perist it + this.getEntityManager().persist(user); + + // Flush it to get updated instance back + this.getEntityManager().flush(); + + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N + + // Return updated instanc + return user; + } + + @Override + public User lockUserAccount (final User user, final String userLockReason, final String baseUrl) throws UserStatusLockedException, UserStatusUnconfirmedException, UserNotFoundException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},userLockReason={2},baseUrl={3} - CALLED!", this.getClass().getSimpleName(), user, userLockReason, baseUrl)); //NOI18N + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Id is set + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Id is set + throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N + } else if (user.getUserContact() == null) { + // Throw NPE again + throw new NullPointerException("user.userContact is null"); //NOI18N + } else if (user.getUserContact().getContactId() == null) { + // Throw NPE again + throw new NullPointerException("user.userContact.contactId is null"); //NOI18N + } else if (user.getUserContact().getContactId() < 1) { + // Not valid id number + throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N + } else if (user.getUserAccountStatus() == null) { + // Throw NPE again + throw new NullPointerException("user.userAccountStatus is null"); //NOI18N + } else if (!this.userBean.ifUserExists(user)) { + // Name already found + throw new UserNotFoundException(user); + } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) { + // Account is locked + throw new UserStatusLockedException(user); + } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) { + // Account is unconfirmed + throw new UserStatusUnconfirmedException(user); + } else if (null == userLockReason) { + // Throw NPE again + throw new NullPointerException("userLockReason is null"); //NOI18N + } else if (userLockReason.isEmpty()) { + // Is empty + throw new IllegalArgumentException("userLockReason is empty"); //NOI18N + } + + // Remove contact instance as this is not updated + user.setUserContact(null); + + // Set as locked, set timestamp and lock reason + user.setUserAccountStatus(UserAccountStatus.LOCKED); + user.setUserLastLocked(new GregorianCalendar()); + user.setUserLastLockedReason(userLockReason); + + // Update user + User updatedUser = this.userBean.updateUserData(user); + + // @TODO Create user lock history entry + // Init variable + Address emailAddress; + + try { + // Create email address and set + emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress()); + } catch (final AddressException ex) { + // Throw again + throw new EJBException(ex); + } + + // Send out email + // @TODO externalize subject line + this.sendEmail("Account locked", "account_locked", emailAddress, updatedUser, baseUrl); //NOI18N + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N + + // Return detached (and updated) user + return updatedUser; + } + + @Override + public User unlockUserAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusUnconfirmedException, UserNotFoundException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Id is set + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Id is set + throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N + } else if (user.getUserContact() == null) { + // Throw NPE again + throw new NullPointerException("user.userContact is null"); //NOI18N + } else if (user.getUserContact().getContactId() == null) { + // Throw NPE again + throw new NullPointerException("user.userContact.contactId is null"); //NOI18N + } else if (user.getUserContact().getContactId() < 1) { + // Not valid id number + throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N + } else if (user.getUserAccountStatus() == null) { + // Throw NPE again + throw new NullPointerException("user.userAccountStatus is null"); //NOI18N + } else if (!this.userBean.ifUserExists(user)) { + // Name already found + throw new UserNotFoundException(user); + } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) { + // Account is confirmed + throw new UserStatusConfirmedException(user); + } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) { + // Account is unconfirmed + throw new UserStatusUnconfirmedException(user); + } + + // Remove contact instance as this is not updated + user.setUserContact(null); + + // Unlock account + user.setUserAccountStatus(UserAccountStatus.CONFIRMED); + + // Update user + User updatedUser = this.userBean.updateUserData(user); + + // @TODO Create user lock history entry + // Init variable + Address emailAddress; + + try { + // Create email address and set + emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress()); + } catch (final AddressException ex) { + // Throw again + throw new EJBException(ex); + } + + // Send out email + // @TODO externalize subject line + this.sendEmail("Account unlocked", "account_unlocked", emailAddress, updatedUser, baseUrl); //NOI18N + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N + + // Return changed account + return updatedUser; + } + +} diff --git a/src/java/org/mxchange/jusercore/model/user/FinancialsUserSessionBean.java b/src/java/org/mxchange/jusercore/model/user/FinancialsUserSessionBean.java new file mode 100644 index 0000000..bbfabd4 --- /dev/null +++ b/src/java/org/mxchange/jusercore/model/user/FinancialsUserSessionBean.java @@ -0,0 +1,802 @@ +/* + * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. + */ +package org.mxchange.jusercore.model.user; + +import java.text.MessageFormat; +import java.util.GregorianCalendar; +import java.util.List; +import javax.ejb.EJB; +import javax.ejb.EJBException; +import javax.ejb.Stateless; +import javax.mail.Address; +import javax.mail.internet.AddressException; +import javax.mail.internet.InternetAddress; +import javax.persistence.NoResultException; +import javax.persistence.PersistenceException; +import javax.persistence.Query; +import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean; +import org.mxchange.jcontacts.contact.Contact; +import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber; +import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber; +import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber; +import org.mxchange.jusercore.exceptions.UserNotFoundException; +import org.mxchange.jusercore.exceptions.UserStatusConfirmedException; +import org.mxchange.jusercore.exceptions.UserStatusLockedException; +import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException; +import org.mxchange.jusercore.model.register.UserRegistrationSessionBeanRemote; +import org.mxchange.jusercore.model.user.password_history.PasswordHistory; +import org.mxchange.jusercore.model.user.password_history.UserPasswordHistory; +import org.mxchange.jusercore.model.user.profilemodes.ProfileMode; +import org.mxchange.jusercore.model.user.status.UserAccountStatus; + +/** + * A user EJB + * <p> + * @author Roland Häder<roland@mxchange.org> + */ +@Stateless (name = "user", description = "A bean handling the user data") +public class FinancialsUserSessionBean extends BaseFinancialsDatabaseBean implements UserSessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 542_145_347_916L; + + /** + * Registration EJB + */ + @EJB + private UserRegistrationSessionBeanRemote registerBean; + + /** + * Default constructor + */ + public FinancialsUserSessionBean () { + } + + @Override + @SuppressWarnings ("unchecked") + public List<User> allMemberPublicVisibleUsers () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMemberPublicVisibleUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Get named query + Query query = this.getEntityManager().createNamedQuery("AllMemberPublicUsers", LoginUser.class); //NOI18N + + // Set parameters + query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N + query.setParameter("members", ProfileMode.MEMBERS); //NOI18N + query.setParameter("public", ProfileMode.PUBLIC); //NOI18N + + // Get result + List<User> users = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allMemberPublicVisibleUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N + + // Return full list + return users; + } + + @Override + @SuppressWarnings ("unchecked") + public List<User> allPublicUsers () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allPublicUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Get named query + Query query = this.getEntityManager().createNamedQuery("AllPublicUsers", LoginUser.class); //NOI18N + + // Set parameters + query.setParameter("status", UserAccountStatus.CONFIRMED); //NOI18N + query.setParameter("mode", ProfileMode.PUBLIC); //NOI18N + + // Get result + List<User> users = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allPublicUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N + + // Return full list + return users; + } + + @Override + @SuppressWarnings ("unchecked") + public List<User> allUsers () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Get named query + Query query = this.getEntityManager().createNamedQuery("AllUsers", LoginUser.class); //NOI18N + + // Get result + List<User> users = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.allUsers: users.size()={1} - EXIT!", this.getClass().getSimpleName(), users.size())); //NOI18N + + // Return full list + return users; + } + + @Override + public User confirmAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusLockedException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N + + // Parameter must be valid + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Abort here + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Invalid number + throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N + } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) { + // Account is already confirmed + throw new UserStatusConfirmedException(user); + } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) { + // Account is already confirmed + throw new UserStatusLockedException(user); + } else if (user.getUserConfirmKey() == null) { + // Throw NPE + throw new NullPointerException("user.userConfirmKey is null"); //NOI18N + } + + // Update user status and remove confirmation key + user.setUserAccountStatus(UserAccountStatus.CONFIRMED); + user.setUserConfirmKey(null); + user.setUserUpdated(new GregorianCalendar()); + + // Update user account + User updatedUser = this.updateUserData(user); + + // Init variable + Address emailAddress; + + try { + // Create email address and set + emailAddress = new InternetAddress(updatedUser.getUserContact().getContactEmailAddress()); + } catch (final AddressException ex) { + // Throw again + throw new EJBException(ex); + } + + // Send out email + this.sendEmail("Account confirmed", "account_confirmed", emailAddress, updatedUser, baseUrl); //NOI18N + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.confirmAccount: updatedUser={1} - EXIT!", this.getClass().getSimpleName(), updatedUser)); //NOI18N + + // Return updated instance + return updatedUser; + } + + @Override + public User fillUserData (final User user) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fillUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } + + // Try to locate it + Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N + + // Set parameter + query.setParameter("userName", user.getUserName()); //NOI18N + + // Initialize variable + User foundUser = null; + + // Try it + try { + // Try to get single result + foundUser = (User) query.getSingleResult(); + } catch (final NoResultException ex) { + // Log it + this.getLoggerBeanLocal().logException(ex); + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.fillUserData: foundUser={1} - EXIT!", this.getClass().getSimpleName(), foundUser)); //NOI18N + + // Return prepared instance + return foundUser; + } + + @Override + public User findUserById (final Long userId) throws UserNotFoundException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findUserById: userId={1} - CALLED!", this.getClass().getSimpleName(), userId)); //NOI18N + + // Is the parameter valid? + if (null == userId) { + // Throw NPE + throw new NullPointerException("userId is null"); //NOI18N + } else if (userId < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("userId={0} is not valid.", userId)); //NOI18N + } else if (!this.ifUserIdExists(userId)) { + // Does not exist + throw new UserNotFoundException(userId); + } + + // Create query instance + Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N + + // Set user id + query.setParameter("id", userId); //NOI18N + + // Fetch the result, it should be there by now + User user = (User) query.getSingleResult(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.findUserById: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N + + // Return found user + return user; + } + + @Override + public String generateRandomUserName () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateRandomUserName - CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Get full list + List<String> userList = this.getUserNameList(); + + // Init variable + String userName = null; + + // Loop until a user name is found + while ((userName == null) || (userList.contains(userName))) { + // Generate random name + userName = UserUtils.generateRandomUserName(); + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.generateRandomUserName: userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N + + // Found one, so return it + return userName; + } + + @Override + @SuppressWarnings ("unchecked") + public List<String> getEmailAddressList () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Get query + Query query = this.getEntityManager().createNamedQuery("AllEmailAddresses", String.class); //NOI18N + + // Get result list + List<String> emailAddressList = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getEmailAddressList: emailAddressList.size()={1} - EXIT!", this.getClass().getSimpleName(), emailAddressList.size())); //NOI18N + + // Return it + return emailAddressList; + } + + @Override + @SuppressWarnings ("unchecked") + public List<String> getUserNameList () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserNameList: CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Get query + Query query = this.getEntityManager().createNamedQuery("AllUserNames", String.class); //NOI18N + + // Get result list + List<String> userNameList = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserNameList: userNameList.size()={1} - EXIT!", this.getClass().getSimpleName(), userNameList.size())); //NOI18N + + // Return it + return userNameList; + } + + @Override + public boolean ifUserExists (final User user) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N + + // userId should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Abort here + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Invalid number + throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N + } + + // Generate query + Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N + + // Set parameter + query.setParameter("id", user.getUserId()); //NOI18N + + // Try this + try { + User dummy = (User) query.getSingleResult(); + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N + } catch (final NoResultException ex) { + // Log it + this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N + + // User name does not exist + return false; + } catch (final PersistenceException ex) { + // Something bad happened + this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N + + // Throw again + throw ex; + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserExists: Found user {1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N + + // Found it + return true; + } + + @Override + public boolean ifUserIdExists (final Long userId) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserIdExists: userId={1} - CALLED!", this.getClass().getSimpleName(), userId)); //NOI18N + + // userId should not be null + if (null == userId) { + // Abort here + throw new NullPointerException("userId is null"); //NOI18N + } else if (userId < 1) { + // Invalid number + throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", userId)); //NOI18N + } + + // Generate query + Query query = this.getEntityManager().createNamedQuery("SearchUserById", LoginUser.class); //NOI18N + + // Set parameter + query.setParameter("id", userId); //NOI18N + + // Try this + try { + User dummy = (User) query.getSingleResult(); + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserIdExists: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N + } catch (final NoResultException ex) { + // Log it + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserIdExists: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N + + // User name does not exist + return false; + } catch (final PersistenceException ex) { + // Something bad happened + this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user id {0} found.", userId, ex)); //NOI18N + + // Throw again + throw ex; + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserIdExists: Found userId={1} - EXIT!", this.getClass().getSimpleName(), userId)); //NOI18N + + // Found it + return true; + } + + @Override + public boolean ifUserNameExists (final String userName) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: userName={1} - CALLED!", this.getClass().getSimpleName(), userName)); //NOI18N + + // userId should not be null + if (null == userName) { + // Abort here + throw new NullPointerException("userName is null"); //NOI18N + } else if (userName.isEmpty()) { + // Abort here + throw new NullPointerException("userName is empty"); //NOI18N + } + + // Generate query + Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N + + // Set parameter + query.setParameter("userName", userName); //NOI18N + + // Try this + try { + User dummy = (User) query.getSingleResult(); + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserNameExists: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N + } catch (final NoResultException ex) { + // Log it + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.ifUserNameExists: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N + + // User name does not exist + return false; + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.ifUserNameExists: Found userName={1} - EXIT!", this.getClass().getSimpleName(), userName)); //NOI18N + + // Found it + return true; + } + + @Override + public boolean isEmailAddressRegistered (final User user) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } + + // Generate query + Query query = this.getEntityManager().createNamedQuery("SearchUserByEmailAddress", LoginUser.class); //NOI18N + + // Set parameter + query.setParameter("emailAddress", user.getUserContact().getContactEmailAddress()); //NOI18N + + // Search for it + try { + User dummy = (User) query.getSingleResult(); + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N + } catch (final NoResultException ex) { + // Log it + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isEmailAddressRegistered: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N + + // Email address does not exist + return false; + } catch (final PersistenceException ex) { + // Something bad happened + this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N + + // Throw again + throw ex; + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isEmailAddressRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N + + // Found it + return true; + } + + @Override + public boolean isUserNameRegistered (final User user) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } + + // Generate query + Query query = this.getEntityManager().createNamedQuery("SearchUserByName", LoginUser.class); //NOI18N + + // Set parameter + query.setParameter("userName", user.getUserName()); //NOI18N + + // Try this + try { + User dummy = (User) query.getSingleResult(); + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isUserNameRegistered: dummy.userId={1} found.", this.getClass().getSimpleName(), dummy.getUserId())); //NOI18N + } catch (final NoResultException ex) { + // Log it + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.isUserNameRegistered: getSingleResult() returned no result: {1}", this.getClass().getSimpleName(), ex)); //NOI18N + + // User name does not exist + return false; + } catch (final PersistenceException ex) { + // Something bad happened + this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one email address {0} found.", user.getUserContact().getContactEmailAddress()), ex); //NOI18N + + // Throw again + throw ex; + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isUserNameRegistered: Returning true ... - EXIT!", this.getClass().getSimpleName())); //NOI18N + + // Found it + return true; + } + + @Override + public User updateUserData (final User user) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Throw NPE again + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N + } else if (user.getUserAccountStatus() == null) { + // Throw NPE again + throw new NullPointerException("user.userAccountStatus is null"); //NOI18N + } else if (!this.ifUserExists(user)) { + // User does not exist + throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N + } + + // Remove contact instance as this is not updated + user.setUserContact(null); + + // Find the instance + User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId()); + + // Should be found! + assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N + + // Merge user + User detachedUser = this.getEntityManager().merge(foundUser); + + // Should be found! + assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N + + // Copy all data + detachedUser.copyAll(user); + + // Set as updated + detachedUser.setUserUpdated(new GregorianCalendar()); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserData: detachedUser={1} - CALLED!", this.getClass().getSimpleName(), detachedUser)); //NOI18N + + // Return updated instance + return detachedUser; + } + + @Override + public PasswordHistory updateUserPassword (final User user, final String baseUrl) throws UserNotFoundException, UserStatusUnconfirmedException, UserStatusLockedException { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Throw NPE again + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N + } else if (user.getUserAccountStatus() == null) { + // Throw NPE again + throw new NullPointerException("user.userAccountStatus is null"); //NOI18N + } else if (!this.ifUserExists(user)) { + // User does not exist + throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N + } else if (null == baseUrl) { + // Abort here + throw new NullPointerException("password is null"); //NOI18N + } else if (baseUrl.isEmpty()) { + // Abort here + throw new IllegalArgumentException("password is empty"); //NOI18N + } + + // Call other method + User updatedUser = this.updateUserData(user); + + // Create history entry + PasswordHistory entry = new UserPasswordHistory(user.getUserEncryptedPassword(), updatedUser); + + // Set created timestamp + entry.setUserPasswordHistoryCreated(new GregorianCalendar()); + + // Persist it + this.getEntityManager().persist(entry); + + // Flush it to get id number back + this.getEntityManager().flush(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPassword: entry.userPasswordHistoryId={1} - EXIT!", this.getClass().getSimpleName(), entry.getUserPasswordHistoryId())); //NOI18N + + // Return it + return entry; + } + + @Override + public User updateUserPersonalData (final User user) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Throw NPE again + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Not valid + throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N + } else if (user.getUserAccountStatus() == null) { + // Throw NPE again + throw new NullPointerException("user.userAccountStatus is null"); //NOI18N + } else if (!this.ifUserExists(user)) { + // User does not exist + throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N + } + + // Find the instance + User foundUser = this.getEntityManager().find(user.getClass(), user.getUserId()); + + // Should be found! + assert (foundUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N + + // Merge user + User detachedUser = this.getEntityManager().merge(foundUser); + + // Should be found! + assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N + + // Copy all data + detachedUser.copyAll(user); + + // Set as updated + detachedUser.setUserUpdated(new GregorianCalendar()); + detachedUser.getUserContact().setContactUpdated(new GregorianCalendar()); + + // Get contact from it and find it + Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId()); + + // Should be found + assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId())); //NOI18N + + // Merge contact instance + Contact detachedContact = this.getEntityManager().merge(foundContact); + + // Copy all + detachedContact.copyAll(user.getUserContact()); + + // Set it back in user + user.setUserContact(detachedContact); + + // Should be found! + assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N + + // Get mobile instance + DialableMobileNumber mobileNumber = detachedContact.getContactMobileNumber(); + + // Is there a mobile instance set? + if (mobileNumber instanceof DialableMobileNumber) { + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: mobile.phoneId={0} is being updated ...", mobileNumber.getPhoneId())); //NOI18N + + // Then find it, too + DialableMobileNumber foundMobile = this.getEntityManager().find(mobileNumber.getClass(), mobileNumber.getPhoneId()); + + // Should be there + assert (foundMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", foundMobile.getPhoneId()); //NOI18N + + // Then merge it, too + DialableMobileNumber detachedMobile = this.getEntityManager().merge(foundMobile); + + // Should be there + assert (detachedMobile instanceof DialableMobileNumber) : MessageFormat.format("Mobile number with id {0} not found but should be.", detachedMobile.getPhoneId()); //NOI18N + + // Copy all + detachedMobile.copyAll(user.getUserContact().getContactMobileNumber()); + + // Set it back + detachedContact.setContactMobileNumber(detachedMobile); + } + + // Get mobile instance + DialableFaxNumber fax = detachedContact.getContactFaxNumber(); + + // Is there a fax instance set? + if (fax instanceof DialableFaxNumber) { + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId())); //NOI18N + + // Then find it, too + DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId()); + + // Should be there + assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId()); //NOI18N + + // Then merge it, too + DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax); + + // Should be there + assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId()); //NOI18N + + // Copy all + detachedFax.copyAll(user.getUserContact().getContactFaxNumber()); + + // Set it back + detachedContact.setContactFaxNumber(detachedFax); + } + + // Get mobile instance + DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber(); + + // Is there a fax instance set? + if (landLine instanceof DialableLandLineNumber) { + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId())); //NOI18N + + // Then find it, too + DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId()); + + // Should be there + assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId()); //NOI18N + + // Then merge it, too + DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine); + + // Should be there + assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId()); //NOI18N + + // Copy all + detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber()); + + // Set it back + detachedContact.setContactLandLineNumber(detachedLandLine); + } + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateUserPersonalData: entry.detachedUser={1} - EXIT!", this.getClass().getSimpleName(), detachedUser)); //NOI18N + + // Return updated user instance + return detachedUser; + } + +} diff --git a/src/java/org/mxchange/jusercore/model/user/password_history/AddressbookUserPasswordHistorySessionBean.java b/src/java/org/mxchange/jusercore/model/user/password_history/AddressbookUserPasswordHistorySessionBean.java deleted file mode 100644 index ce16c8e..0000000 --- a/src/java/org/mxchange/jusercore/model/user/password_history/AddressbookUserPasswordHistorySessionBean.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. - */ -package org.mxchange.jusercore.model.user.password_history; - -import java.text.MessageFormat; -import java.util.List; -import javax.ejb.Stateless; -import javax.persistence.Query; -import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean; -import org.mxchange.jusercore.model.user.User; - -/** - * A user password history EJB - * <p> - * @author Roland Häder<roland@mxchange.org> - */ -@Stateless (name = "userPasswordHistory", description = "A stateless EJB for user's password history. This bean does return the full user's password history and not limited. The application then needs to limit it to it's purpose.") -public class AddressbookUserPasswordHistorySessionBean extends BaseAddressbookDatabaseBean implements UserPasswordHistorySessionBeanRemote { - - /** - * Serial number - */ - private static final long serialVersionUID = 395_767_546_195_014L; - - /** - * Default constructor - */ - public AddressbookUserPasswordHistorySessionBean () { - } - - @Override - @SuppressWarnings ("unchecked") - public List<PasswordHistory> getUserPasswordHistory (final User user) { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserPasswordHistory(): user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N - - // user should not be null - if (null == user) { - // Abort here - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() == null) { - // Throw NPE again - throw new NullPointerException("user.userId is null"); //NOI18N - } else if (user.getUserId() < 1) { - // Illegal id number - throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not allowed.", user.getUserId())); //NOI18N - } - - // Get named query - Query query = this.getEntityManager().createNamedQuery("AllUsersHistoryEntries", UserPasswordHistory.class); //NOI18N - - // Set parameter - query.setParameter("user", user); //NOI18N - - // Get full history - List<PasswordHistory> history = query.getResultList(); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserPasswordHistory(): history.size()={1} - EXIT !", this.getClass().getSimpleName(), history.size())); //NOI18N - - // Return it - return history; - } - -} diff --git a/src/java/org/mxchange/jusercore/model/user/password_history/FinancialsUserPasswordHistorySessionBean.java b/src/java/org/mxchange/jusercore/model/user/password_history/FinancialsUserPasswordHistorySessionBean.java new file mode 100644 index 0000000..403fd5e --- /dev/null +++ b/src/java/org/mxchange/jusercore/model/user/password_history/FinancialsUserPasswordHistorySessionBean.java @@ -0,0 +1,79 @@ +/* + * Copyright (C) 2016 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 <http://www.gnu.org/licenses/>. + */ +package org.mxchange.jusercore.model.user.password_history; + +import java.text.MessageFormat; +import java.util.List; +import javax.ejb.Stateless; +import javax.persistence.Query; +import org.mxchange.jfinancials.database.BaseFinancialsDatabaseBean; +import org.mxchange.jusercore.model.user.User; + +/** + * A user password history EJB + * <p> + * @author Roland Häder<roland@mxchange.org> + */ +@Stateless (name = "userPasswordHistory", description = "A stateless EJB for user's password history. This bean does return the full user's password history and not limited. The application then needs to limit it to it's purpose.") +public class FinancialsUserPasswordHistorySessionBean extends BaseFinancialsDatabaseBean implements UserPasswordHistorySessionBeanRemote { + + /** + * Serial number + */ + private static final long serialVersionUID = 395_767_546_195_014L; + + /** + * Default constructor + */ + public FinancialsUserPasswordHistorySessionBean () { + } + + @Override + @SuppressWarnings ("unchecked") + public List<PasswordHistory> getUserPasswordHistory (final User user) { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserPasswordHistory(): user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N + + // user should not be null + if (null == user) { + // Abort here + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Throw NPE again + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Illegal id number + throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not allowed.", user.getUserId())); //NOI18N + } + + // Get named query + Query query = this.getEntityManager().createNamedQuery("AllUsersHistoryEntries", UserPasswordHistory.class); //NOI18N + + // Set parameter + query.setParameter("user", user); //NOI18N + + // Get full history + List<PasswordHistory> history = query.getResultList(); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserPasswordHistory(): history.size()={1} - EXIT !", this.getClass().getSimpleName(), history.size())); //NOI18N + + // Return it + return history; + } + +}