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;
* <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 {
/**
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
// 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.
+ }
}