]> 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 e57ace42a13d93f821dab000d80edbdcde243d86..0237bfa682bba847cc3538af30fa4d41f7acc0b4 100644 (file)
 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.faces.view.facelets.FaceletException;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
+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;
 
@@ -33,6 +38,7 @@ import org.mxchange.jcoreee.validator.number.BaseLongValidator;
  * <p>
  * @author Roland Haeder
  */
+@FacesValidator (value = "AddressbookIdValidator")
 public class AddressbookIdValidator extends BaseLongValidator {
 
        /**
@@ -43,24 +49,15 @@ public class AddressbookIdValidator extends BaseLongValidator {
        /**
         * Remote bean
         */
+       @EJB (mappedName = "ejb/stateless-addressbook")
        private AddressbookSessionBeanRemote addressbookBean;
 
        /**
-        * Default constructor
+        * An event for loading address book data loading
         */
-       public AddressbookIdValidator () {
-               // Try it
-               try {
-                       // Get context
-                       Context context = new InitialContext();
-
-                       // Try to to lookup the bean
-                       this.addressbookBean = (AddressbookSessionBeanRemote) context.lookup("ejb/stateless-addressbook");
-               } catch (final NamingException ex) {
-                       // Throw again
-                       throw new FaceletException(ex);
-               }
-       }
+       @Inject
+       @Any
+       private Event<AddressbookLoadedEvent> loadedEvent;
 
        @Override
        public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
@@ -73,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)));
+                       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
        }