]> git.mxchange.org Git - jfinancials-lib.git/blobdiff - src/org/mxchange/addressbook/validator/addressbook/AddressbookIdValidator.java
Continued:
[jfinancials-lib.git] / src / org / mxchange / addressbook / validator / addressbook / AddressbookIdValidator.java
index 2b97044d6f171b93a53b53ef85cd6502247e51d9..0237bfa682bba847cc3538af30fa4d41f7acc0b4 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.addressbook.AddressbookLoadedEvent;
+import org.mxchange.addressbook.events.addressbook.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
        }