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;
/**
* 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 {
// 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
}