*/
package org.mxchange.addressbook.model.addressbook;
+import java.text.MessageFormat;
import java.util.List;
-import javax.ejb.EJB;
import javax.ejb.Stateless;
+import javax.persistence.NoResultException;
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;
/**
*/
private static final long serialVersionUID = 129_857_871_287_691L;
- /**
- * Login bean
- */
- @EJB (mappedName = "ejb/stateless-login")
- private UserLoginSessionBeanRemote loginBean;
-
@Override
@SuppressWarnings ("unchecked")
public List<Addressbook> getUsersList (final User loggedInUser) {
+ // Trace message
+ this.getLoggerBeanLocal().logTrace(MessageFormat.format("getUsersList: loggedInUser={0} - CALLED!", loggedInUser));
+
+ // Is the user instance null?
+ if (null == loggedInUser) {
+ // Abort here
+ throw new NullPointerException("loggedInUser is null");
+ }
+
// Get query instance
- Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks"); //NOI18N
+ Query query = this.getEntityManager().createNamedQuery("AllUsersAddressbooks", List.class); //NOI18N
// Set parameter
query.setParameter("param", loggedInUser); //NOI18N
// Is it not null?
if (null == addressbook) {
// Abort here
- throw new NullPointerException("addressbook is null");
+ throw new NullPointerException("addressbook is null"); //NOI18N
} else if (addressbook.getAddressbookUser() == null) {
// User instance is null
- throw new IllegalArgumentException("addressbook.user should not be null.");
+ throw new NullPointerException("addressbook.user should not be null."); //NOI18N
} else if (addressbook.getAddressbookName() == null) {
// Address book name not set
- throw new IllegalArgumentException("addressbook.addressbookName should not be null");
+ 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");
+ 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);
@Override
public boolean isAddressbookNameUsed (final Addressbook addressbook) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ // 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
+ }
+
+ // Get query instance
+ Query query = this.getEntityManager().createNamedQuery("SearchUserAddressbookName", Addressbook.class);
+
+ // Set parameter
+ query.setParameter("user", addressbook.getAddressbookUser());
+ query.setParameter("name", addressbook.getAddressbookName());
+
+ // 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));
+
+ // Found one
+ isUsed = true;
+ } catch (final NoResultException ex) {
+ // No result found, so log it away
+ this.getLoggerBeanLocal().logDebug("isAddressbookNameUsed: getSingleResult() did not return a result: " + ex);
+ }
+
+ // Return result
+ return isUsed;
}
}