]> git.mxchange.org Git - jaddressbook-share-lib.git/commitdiff
Continued:
authorRoland Haeder <roland@mxchange.org>
Thu, 15 Oct 2015 13:14:48 +0000 (15:14 +0200)
committerRoland Haeder <roland@mxchange.org>
Thu, 15 Oct 2015 13:14:48 +0000 (15:14 +0200)
- added event for when an address book has been loaded
- the address book id validator will now fire that event
- added business method getAddressbookById()
- renamed named query to "SearchAddressbookById"
Signed-off-by:Roland Häder <roland@mxchange.org>

src/org/mxchange/addressbook/events/AddressbookLoadedEvent.java [new file with mode: 0644]
src/org/mxchange/addressbook/events/LoadedAddressbookEvent.java [new file with mode: 0644]
src/org/mxchange/addressbook/exceptions/AddressbookNotFoundException.java [new file with mode: 0644]
src/org/mxchange/addressbook/model/addressbook/AddressbookSessionBeanRemote.java
src/org/mxchange/addressbook/model/addressbook/UserAddressbook.java
src/org/mxchange/addressbook/validator/addressbook/AddressbookIdValidator.java

diff --git a/src/org/mxchange/addressbook/events/AddressbookLoadedEvent.java b/src/org/mxchange/addressbook/events/AddressbookLoadedEvent.java
new file mode 100644 (file)
index 0000000..69811f9
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2015 Roland Haeder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.addressbook.events;
+
+import java.io.Serializable;
+import org.mxchange.addressbook.model.addressbook.Addressbook;
+
+/**
+ * An interface for an event if an address book has been loaded
+ * <p>
+ * @author Roland Haeder
+ */
+public interface AddressbookLoadedEvent extends Serializable {
+
+       /**
+        * Getter for address book instance
+        * <p>
+        * @return Address book instance
+        */
+       public Addressbook getAddressbook ();
+}
diff --git a/src/org/mxchange/addressbook/events/LoadedAddressbookEvent.java b/src/org/mxchange/addressbook/events/LoadedAddressbookEvent.java
new file mode 100644 (file)
index 0000000..733cde7
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2015 Roland Haeder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.addressbook.events;
+
+import org.mxchange.addressbook.model.addressbook.Addressbook;
+
+/**
+ * An event fired when an address book has been loaded
+ * <p>
+ * @author Roland Haeder
+ */
+public class LoadedAddressbookEvent implements AddressbookLoadedEvent {
+
+       /**
+        * Serial number
+        */
+       private static final long serialVersionUID = 1L;
+
+       /**
+        * Adress book instance causing this event
+        */
+       private final Addressbook addressbook;
+
+       /**
+        * Constructor with address book instance
+        * <p>
+        * @param addressbook Address book instance
+        */
+       public LoadedAddressbookEvent (final Addressbook addressbook) {
+               this.addressbook = addressbook;
+       }
+
+       @Override
+       public Addressbook getAddressbook () {
+               return this.addressbook;
+       }
+}
diff --git a/src/org/mxchange/addressbook/exceptions/AddressbookNotFoundException.java b/src/org/mxchange/addressbook/exceptions/AddressbookNotFoundException.java
new file mode 100644 (file)
index 0000000..648b4e8
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2015 Roland Haeder
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.addressbook.exceptions;
+
+import java.text.MessageFormat;
+
+/**
+ * An exception thrown when the address book's id number is not valid, so no
+ * address book can be found.
+ * <p>
+ * @author Roland Haeder
+ */
+public class AddressbookNotFoundException extends Exception {
+
+       /**
+        * Serial number
+        */
+       private static final long serialVersionUID = 14_986_976_417_658_920L;
+
+       /**
+        * Creates an exception with given id number that could not be found.
+        * <p>
+        * @param addressbookId Address book id number
+        */
+       public AddressbookNotFoundException (final Long addressbookId) {
+               super(MessageFormat.format("Address book with id number {0} does not exist.", addressbookId));
+       }
+
+}
index 0fe67bba81ead39ebbe4291e31b1d744aca18eb0..fc3231091225e99dafbbf9c32ccad4cdbac4af79 100644 (file)
@@ -20,6 +20,7 @@ import java.io.Serializable;
 import java.util.List;
 import javax.ejb.Remote;
 import org.mxchange.addressbook.exceptions.AddressbookNameAlreadyUsedException;
+import org.mxchange.addressbook.exceptions.AddressbookNotFoundException;
 import org.mxchange.addressbook.model.addressbook.entry.AddressbookEntry;
 import org.mxchange.jusercore.model.user.User;
 
@@ -31,6 +32,19 @@ import org.mxchange.jusercore.model.user.User;
 @Remote
 public interface AddressbookSessionBeanRemote extends Serializable {
 
+       /**
+        * Some getter for an address book instance from given id number. If the
+        * address book is not found, an exception is thrown.
+        * <p>
+        * @param addressbookId Id number for address book instance
+        * @return Address book instance
+        * @throws org.mxchange.addressbook.exceptions.AddressbookNotFoundException
+        * If the address book cannot be found by given id number
+        * @throws NullPointerException If addressbookId is null
+        * @throws IllegalArgumentException If the id number is below 1
+        */
+       public Addressbook getAddressbookById (final Long addressbookId) throws AddressbookNotFoundException;
+
        /**
         * Returns a list of all entries of given address book, whether the assigned
         * user is the "owner" or "sharer" of the entry.
index 2ddd1cb1bf66cbc6c2aea258bcf51800645767f8..81934a51118e31e8bf44f501417c0b817a15fdd2 100644 (file)
@@ -49,7 +49,7 @@ import org.mxchange.jusercore.model.user.User;
                {
                        @NamedQuery (name = "AllUsersAddressbooks", query = "SELECT a FROM addressbooks AS a WHERE a.addressbookUser = :param ORDER BY a.addressbookId ASC"),
                        @NamedQuery (name = "SearchUserAddressbookName", query = "SELECT a FROM addressbooks AS a WHERE a.addressbookUser = :user AND LOWER(a.addressbookName) LIKE LOWER(:name)"),
-                       @NamedQuery (name = "FindAddressbookById", query = "SELECT a FROM addressbooks AS a WHERE a.addressbookId = :id")
+                       @NamedQuery (name = "SearchAddressbookById", query = "SELECT a FROM addressbooks AS a WHERE a.addressbookId = :id")
                }
 )
 public class UserAddressbook implements Addressbook, Comparable<Addressbook> {
index 2b97044d6f171b93a53b53ef85cd6502247e51d9..b9face6643c5cf07616e394b60c420b1b355d061 100644 (file)
@@ -18,11 +18,18 @@ package org.mxchange.addressbook.validator.addressbook;
 
 import java.text.MessageFormat;
 import javax.ejb.EJB;
+import javax.enterprise.event.Event;
+import javax.enterprise.inject.Any;
 import javax.faces.application.FacesMessage;
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
 import javax.faces.validator.FacesValidator;
 import javax.faces.validator.ValidatorException;
+import javax.inject.Inject;
+import org.mxchange.addressbook.events.AddressbookLoadedEvent;
+import org.mxchange.addressbook.events.LoadedAddressbookEvent;
+import org.mxchange.addressbook.exceptions.AddressbookNotFoundException;
+import org.mxchange.addressbook.model.addressbook.Addressbook;
 import org.mxchange.addressbook.model.addressbook.AddressbookSessionBeanRemote;
 import org.mxchange.jcoreee.validator.number.BaseLongValidator;
 
@@ -45,6 +52,13 @@ public class AddressbookIdValidator extends BaseLongValidator {
        @EJB (mappedName = "ejb/stateless-addressbook")
        private AddressbookSessionBeanRemote addressbookBean;
 
+       /**
+        * An event for loading address book data loading
+        */
+       @Inject
+       @Any
+       private Event<AddressbookLoadedEvent> loadedEvent;
+
        @Override
        public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
                // Trace message
@@ -56,12 +70,30 @@ public class AddressbookIdValidator extends BaseLongValidator {
                // Pre-validation (example: not null, not a string, empty string ...)
                super.preValidate(context, component, value, requiredFileds, false);
 
+               // Cast to long
+               Long addressbookId = (Long) value;
+
                // Is the address book id valid?
-               if (!this.addressbookBean.isAddressbookIdUsed((Long) value)) {
+               if (!this.addressbookBean.isAddressbookIdUsed(addressbookId)) {
                        // Is not valid
-                       throw new ValidatorException(new FacesMessage(MessageFormat.format("No address book found with id {0}. Please check your link.", value))); //NOI18N
+                       throw new ValidatorException(new FacesMessage(MessageFormat.format("No address book found with id {0}. Please check your link.", addressbookId))); //NOI18N
                }
 
+               // Init variable
+               Addressbook addressbook;
+
+               // Try it
+               try {
+                       // Get full data
+                       addressbook = this.addressbookBean.getAddressbookById(addressbookId);
+               } catch (final AddressbookNotFoundException ex) {
+                       // Continue to throw
+                       throw new ValidatorException(new FacesMessage(MessageFormat.format("Cannot find address book with id {0}", addressbookId)), ex);
+               }
+
+               // Fire event
+               this.loadedEvent.fire(new LoadedAddressbookEvent(addressbook));
+
                // Trace message
                //this.getLogger().logTrace("validate: EXIT!"); //NOI18N
        }