]> git.mxchange.org Git - pizzaservice-mailer-ejb.git/blobdiff - src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java
Continued a bit:
[pizzaservice-mailer-ejb.git] / src / java / org / mxchange / addressbook / model / addressbook / AddressbookSessionBean.java
index c1bd4a5c3785fa49f1457c02ca629a92cc496a6f..a572183089883f710010d2749f74ca4e614708af 100644 (file)
@@ -1,37 +1,41 @@
 /*
- * Copyright (C) 2015 Roland Haeder
+ * Copyright (C) 2016, 2017 Roland Häder
  *
  * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
+ * 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 General Public License for more details.
+ * GNU Affero General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
+ * 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.GregorianCalendar;
 import java.util.List;
 import javax.ejb.Stateless;
 import javax.persistence.NoResultException;
 import javax.persistence.Query;
-import org.mxchange.addressbook.exceptions.AddressbookNameAlreadyUsedException;
-import org.mxchange.addressbook.model.addressbook.entry.AddressbookEntry;
+import org.mxchange.jaddressbook.exceptions.AddressbookNameAlreadyUsedException;
+import org.mxchange.jaddressbook.exceptions.AddressbookNotFoundException;
+import org.mxchange.jaddressbook.model.addressbook.Addressbook;
+import org.mxchange.jaddressbook.model.addressbook.UserAddressbook;
+import org.mxchange.jaddressbook.model.addressbook.entry.AddressbookEntry;
 import org.mxchange.jcoreee.database.BaseDatabaseBean;
 import org.mxchange.jusercore.model.user.User;
 
 /**
  * A stateless bean handling addressbooks
  * <p>
- * @author Roland Haeder
+ * @author Roland Häder<roland@mxchange.org>
  */
-@Stateless (name = "addressbook", mappedName = "ejb/stateless-addressbook", description = "A stateless bean for handling addressbooks")
+@Stateless (name = "addressbook", description = "A stateless bean for handling Addressbook addressbooks")
 public class AddressbookSessionBean extends BaseDatabaseBean implements AddressbookSessionBeanRemote {
 
        /**
@@ -42,13 +46,36 @@ public class AddressbookSessionBean extends BaseDatabaseBean implements Addressb
        @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("AllAddressbookEntries"); //NOI18N
+               Query query = this.getEntityManager().createNamedQuery("SearchUsersAddressbookEntries", List.class); //NOI18N
 
                // Set parameters
                query.setParameter("addressbook", addressbook); //NOI18N
                query.setParameter("owner", addressbook.getAddressbookUser()); //NOI18N
-               query.setParameter("sharer", addressbook.getAddressbookUser()); //NOI18N
 
                // Return it
                return query.getResultList();
@@ -74,6 +101,9 @@ public class AddressbookSessionBean extends BaseDatabaseBean implements Addressb
                        throw new AddressbookNameAlreadyUsedException(addressbook);
                }
 
+               // Add timestamp of creation
+               addressbook.setAddressbookCreated(new GregorianCalendar());
+
                // Persist it now
                this.getEntityManager().persist(addressbook);
 
@@ -83,28 +113,55 @@ public class AddressbookSessionBean extends BaseDatabaseBean implements Addressb
                // 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> getUsersList (final User loggedInUser) {
+       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;
        }
@@ -124,7 +181,7 @@ public class AddressbookSessionBean extends BaseDatabaseBean implements Addressb
                }
 
                // Get query instance
-               Query query = this.getEntityManager().createNamedQuery("FindAddressbookById"); //NOI18N
+               Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById", UserAddressbook.class); //NOI18N
 
                // Set parameter
                query.setParameter("id", addressbookId); //NOI18N
@@ -162,7 +219,13 @@ public class AddressbookSessionBean extends BaseDatabaseBean implements Addressb
                        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
+                       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