--- /dev/null
+/*
+ * 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 ();
+}
--- /dev/null
+/*
+ * 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;
+ }
+}
--- /dev/null
+/*
+ * 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));
+ }
+
+}
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;
@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.
{
@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> {
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
}