From ce2e5892162856f616dd9a7610b4202fd581e966 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Fri, 9 Oct 2015 21:32:27 +0200 Subject: [PATCH] Continued: - added business methods createAddressbook() and isAddressbookNameUsed() - only the first one is implemented now --- .../addressbook/AddressbookSessionBean.java | 42 +++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java b/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java index fb44a41..81b0e08 100644 --- a/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java +++ b/src/java/org/mxchange/addressbook/model/addressbook/AddressbookSessionBean.java @@ -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; *

* @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 getUsersList (final User loggedInUser) { + @SuppressWarnings("unchecked") + public List 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. + } } -- 2.39.5