import javax.persistence.NoResultException;
import javax.persistence.Query;
import org.mxchange.addressbook.exceptions.AddressbookNameAlreadyUsedException;
+import org.mxchange.addressbook.exceptions.AddressbookNotFoundException;
import org.mxchange.addressbook.model.addressbook.entry.AddressbookEntry;
import org.mxchange.jcoreee.database.BaseDatabaseBean;
import org.mxchange.jusercore.model.user.User;
// 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"); //NOI18N
+
+ // Set parameter
+ query.setParameter("id", addressbookId); //NOI18N
+
+ // Return it
+ return (Addressbook) query.getSingleResult();
+ }
+
@Override
@SuppressWarnings ("unchecked")
public List<Addressbook> getUsersList (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;
}
}
// Get query instance
- Query query = this.getEntityManager().createNamedQuery("FindAddressbookById"); //NOI18N
+ Query query = this.getEntityManager().createNamedQuery("SearchAddressbookById"); //NOI18N
// Set parameter
query.setParameter("id", addressbookId); //NOI18N