]> git.mxchange.org Git - jjobs-mailer-ejb.git/commitdiff
Continued:
authorRoland Haeder <roland@mxchange.org>
Fri, 9 Oct 2015 19:32:27 +0000 (21:32 +0200)
committerRoland Haeder <roland@mxchange.org>
Fri, 9 Oct 2015 19:32:27 +0000 (21:32 +0200)
- added business methods createAddressbook() and isAddressbookNameUsed()
- only the first one is implemented now

src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java

index fb44a417ac85c8b9ce9dcc7345536284004512ca..81b0e08968fa892a80e6d099d515d7a4f4745300 100644 (file)
@@ -20,6 +20,7 @@ import java.util.List;
 import javax.ejb.EJB;
 import javax.ejb.Stateless;
 import javax.persistence.Query;
+import org.mxchange.addressbook.exceptions.AddressbookNameAlreadyUsedException;
 import org.mxchange.jcoreee.database.BaseDatabaseBean;
 import org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote;
 import org.mxchange.jusercore.model.user.User;
@@ -29,7 +30,7 @@ import org.mxchange.jusercore.model.user.User;
  * <p>
  * @author Roland Haeder
  */
-@Stateless (name = "addressbook", mappedName = "ejb/stateless-addressbook", description = "A stateless bean for handling addressbooks")
+@Stateless(name = "addressbook", mappedName = "ejb/stateless-addressbook", description = "A stateless bean for handling addressbooks")
 public class AddressbookSessionBean extends BaseDatabaseBean implements AddressbookSessionBeanRemote {
 
        /**
@@ -44,8 +45,8 @@ public class AddressbookSessionBean extends BaseDatabaseBean implements Addressb
        private UserLoginSessionBeanRemote loginBean;
 
        @Override
-       @SuppressWarnings ("unchecked")
-       public List<Addressbook> getUsersList (final User loggedInUser) {
+       @SuppressWarnings("unchecked")
+       public List<Addressbook> getUsersList(final User loggedInUser) {
                // Get query instance
                Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks"); //NOI18N
 
@@ -58,4 +59,39 @@ public class AddressbookSessionBean extends BaseDatabaseBean implements Addressb
                // Return it
                return addressbooks;
        }
+
+       @Override
+       public Addressbook createAddressbook(final Addressbook addressbook) throws AddressbookNameAlreadyUsedException {
+               // Is it not null?
+               if (null == addressbook) {
+                       // Abort here
+                       throw new NullPointerException("addressbook is null");
+               } else if (addressbook.getAddressbookUser() == null) {
+                       // User instance is null
+                       throw new IllegalArgumentException("addressbook.user should not be null.");
+               } else if (addressbook.getAddressbookName() == null) {
+                       // Address book name not set
+                       throw new IllegalArgumentException("addressbook.addressbookName should not be null");
+               } else if (addressbook.getAddressbookName().isEmpty()) {
+                       // Address book name not set
+                       throw new IllegalArgumentException("addressbook.addressbookName should not be empty");
+               } else if (this.isAddressbookNameUsed(addressbook)) {
+                       // The assigned user already used that name
+                       throw new AddressbookNameAlreadyUsedException(addressbook);
+               }
+
+               // Persist it now
+               this.getEntityManager().persist(addressbook);
+
+               // Flush it to get all data
+               this.getEntityManager().flush();
+
+               // Return it updated
+               return addressbook;
+       }
+
+       @Override
+       public boolean isAddressbookNameUsed(final Addressbook addressbook) {
+               throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+       }
 }