]> git.mxchange.org Git - addressbook-war.git/commitdiff
Continued:
authorRoland Haeder <roland@mxchange.org>
Fri, 9 Oct 2015 19:35:09 +0000 (21:35 +0200)
committerRoland Haeder <roland@mxchange.org>
Fri, 9 Oct 2015 19:35:09 +0000 (21:35 +0200)
- Bean method addAddressbook() should be "basicly finished"
- Same for isAddressbookNameUsed()
- renamed isLoggedIn() to isUserLoggedIn() to make it more clear

src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebBean.java

index 5661a9259d95c2e03a838780c963b38deaa83c6d..8ea688602284914fcf1b7df02440f45132197883 100644 (file)
@@ -16,6 +16,7 @@
  */
 package org.mxchange.addressbook.beans.addressbook;
 
+import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.List;
 import javax.annotation.PostConstruct;
@@ -27,15 +28,18 @@ import javax.naming.Context;
 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 {
 
@@ -46,7 +50,7 @@ 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;
 
@@ -56,7 +60,7 @@ public class AddressbookWebBean implements AddressbookWebController {
        private String addressbookName;
 
        /**
-        * Remote addressbook bean
+        * Remote address book bean
         */
        private final AddressbookSessionBeanRemote addressbookBean;
 
@@ -69,7 +73,7 @@ public class AddressbookWebBean implements AddressbookWebController {
        /**
         * Default constructor
         */
-       public AddressbookWebBean () {
+       public AddressbookWebBean() {
                // Try it
                try {
                        // Get initial context
@@ -84,23 +88,23 @@ public class AddressbookWebBean implements AddressbookWebController {
        }
 
        @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
@@ -109,7 +113,41 @@ public class AddressbookWebBean implements AddressbookWebController {
 
        @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
@@ -121,4 +159,32 @@ public class AddressbookWebBean implements AddressbookWebController {
        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;
+       }
 }