*/
package org.mxchange.addressbook.beans.addressbook;
+import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.mxchange.addressbook.beans.login.UserLoginWebController;
+import org.mxchange.addressbook.exceptions.AddressbookNameAlreadyUsedException;
import org.mxchange.addressbook.model.addressbook.Addressbook;
import org.mxchange.addressbook.model.addressbook.AddressbookSessionBeanRemote;
+import org.mxchange.addressbook.model.addressbook.UserAddressbook;
+import org.mxchange.addressbook.model.addressbook.status.AddressbokStatus;
/**
* A user bean (controller)
* <p>
* @author Roland Haeder<roland@mxchange.org>
*/
-@Named ("addressbookController")
+@Named("addressbookController")
@SessionScoped
public class AddressbookWebBean implements AddressbookWebController {
/////////////////////// Properties /////////////////////
/**
- * A list of all user's addressbooks
+ * A list of all user's address books
*/
private List<Addressbook> addressbookList;
private String addressbookName;
/**
- * Remote addressbook bean
+ * Remote address book bean
*/
private final AddressbookSessionBeanRemote addressbookBean;
/**
* Default constructor
*/
- public AddressbookWebBean () {
+ public AddressbookWebBean() {
// Try it
try {
// Get initial context
}
@PostConstruct
- public void init () {
+ public void init() {
// Init list
this.addressbookList = new ArrayList<>(0);
// Is the user logged-in?
- if (this.loginController.isLoggedIn()) {
+ if (this.loginController.isUserLoggedIn()) {
// Fill list with entries
this.addressbookList = this.addressbookBean.getUsersList(this.loginController.getLoggedInUser());
}
}
@Override
- public boolean hasCreatedAddressbooks () {
+ public boolean hasCreatedAddressbooks() {
// Is the user logged in?
- if (!this.loginController.isLoggedIn()) {
+ if (!this.loginController.isUserLoggedIn()) {
// Not logged in
- throw new FaceletException("This method can only be called as logged-in user.");
+ throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
}
// Check if the list is filled
@Override
public void addAddressbook() {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ // Is this name already used?
+ if (!this.loginController.isUserLoggedIn()) {
+ // Not logged in
+ throw new FaceletException("This method can only be called as logged-in user."); //NOI18N
+ } else if (this.getAddressbookName() == null) {
+ // Address book name is null
+ throw new IllegalStateException("addressbookName is null");
+ } else if (this.getAddressbookName().isEmpty()) {
+ // Address book name is empty
+ throw new IllegalStateException("addressbookName is empty.");
+ } else if (!this.isAddressbookNameUsed(this.getAddressbookName())) {
+ // Already used by this user
+ throw new FaceletException(MessageFormat.format("Address book name {0} already used.", this.getAddressbookName())); //NOI18N
+ }
+
+ // Create address book instance with name
+ Addressbook addressbook = new UserAddressbook(this.getAddressbookName());
+
+ // Set default status to UNLOCKED and owner
+ addressbook.setAddressbookStatus(AddressbokStatus.UNLOCKED);
+ addressbook.setAddressbookUser(this.loginController.getLoggedInUser());
+
+ try {
+ // Register this address book
+ Addressbook updatedAddressbook = this.addressbookBean.createAddressbook(addressbook);
+
+ // Remove name
+ this.setAddressbookName(null);
+
+ // Add address book entry to list
+ this.addressbookList.add(updatedAddressbook);
+ } catch (final AddressbookNameAlreadyUsedException ex) {
+ // Throw again as cause
+ throw new FaceletException(ex);
+ }
}
@Override
public void setAddressbookName(final String addressbookName) {
this.addressbookName = addressbookName;
}
+
+ @Override
+ public boolean isAddressbookNameUsed(final String addressbookName) {
+ // Is it zero size?
+ if (null == addressbookName) {
+ // Is null
+ throw new NullPointerException("addressbookName is null"); //NOI18N
+ } else if (this.addressbookList.isEmpty()) {
+ // Not found!
+ return false;
+ }
+
+ // Default is not found
+ boolean isFound = false;
+
+ // Check all entries
+ for (final Addressbook addressbook : this.addressbookList) {
+ // Is the name same?
+ if (addressbook.getAddressbookName().equals(addressbookName)) {
+ // Found a match
+ isFound = true;
+ break;
+ }
+ }
+
+ // Return status
+ return isFound;
+ }
}