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;
@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
// 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
}