From a30751a6b4560bd5c3d3a4dd31794c7114accac3 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Thu, 15 Oct 2015 15:14:48 +0200 Subject: [PATCH] =?utf8?q?Continued:=20-=20added=20event=20for=20when=20an?= =?utf8?q?=20address=20book=20has=20been=20loaded=20-=20the=20address=20bo?= =?utf8?q?ok=20id=20validator=20will=20now=20fire=20that=20event=20-=20add?= =?utf8?q?ed=20business=20method=20getAddressbookById()=20-=20renamed=20na?= =?utf8?q?med=20query=20to=20"SearchAddressbookById"=20Signed-off-by:Rolan?= =?utf8?q?d=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../events/AddressbookLoadedEvent.java | 35 +++++++++++++ .../events/LoadedAddressbookEvent.java | 51 +++++++++++++++++++ .../AddressbookNotFoundException.java | 43 ++++++++++++++++ .../AddressbookSessionBeanRemote.java | 14 +++++ .../model/addressbook/UserAddressbook.java | 2 +- .../addressbook/AddressbookIdValidator.java | 36 ++++++++++++- 6 files changed, 178 insertions(+), 3 deletions(-) create mode 100644 src/org/mxchange/addressbook/events/AddressbookLoadedEvent.java create mode 100644 src/org/mxchange/addressbook/events/LoadedAddressbookEvent.java create mode 100644 src/org/mxchange/addressbook/exceptions/AddressbookNotFoundException.java diff --git a/src/org/mxchange/addressbook/events/AddressbookLoadedEvent.java b/src/org/mxchange/addressbook/events/AddressbookLoadedEvent.java new file mode 100644 index 0000000..69811f9 --- /dev/null +++ b/src/org/mxchange/addressbook/events/AddressbookLoadedEvent.java @@ -0,0 +1,35 @@ +/* + * 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 . + */ +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 + *

+ * @author Roland Haeder + */ +public interface AddressbookLoadedEvent extends Serializable { + + /** + * Getter for address book instance + *

+ * @return Address book instance + */ + public Addressbook getAddressbook (); +} diff --git a/src/org/mxchange/addressbook/events/LoadedAddressbookEvent.java b/src/org/mxchange/addressbook/events/LoadedAddressbookEvent.java new file mode 100644 index 0000000..733cde7 --- /dev/null +++ b/src/org/mxchange/addressbook/events/LoadedAddressbookEvent.java @@ -0,0 +1,51 @@ +/* + * 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 . + */ +package org.mxchange.addressbook.events; + +import org.mxchange.addressbook.model.addressbook.Addressbook; + +/** + * An event fired when an address book has been loaded + *

+ * @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 + *

+ * @param addressbook Address book instance + */ + public LoadedAddressbookEvent (final Addressbook addressbook) { + this.addressbook = addressbook; + } + + @Override + public Addressbook getAddressbook () { + return this.addressbook; + } +} diff --git a/src/org/mxchange/addressbook/exceptions/AddressbookNotFoundException.java b/src/org/mxchange/addressbook/exceptions/AddressbookNotFoundException.java new file mode 100644 index 0000000..648b4e8 --- /dev/null +++ b/src/org/mxchange/addressbook/exceptions/AddressbookNotFoundException.java @@ -0,0 +1,43 @@ +/* + * 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 . + */ +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. + *

+ * @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. + *

+ * @param addressbookId Address book id number + */ + public AddressbookNotFoundException (final Long addressbookId) { + super(MessageFormat.format("Address book with id number {0} does not exist.", addressbookId)); + } + +} diff --git a/src/org/mxchange/addressbook/model/addressbook/AddressbookSessionBeanRemote.java b/src/org/mxchange/addressbook/model/addressbook/AddressbookSessionBeanRemote.java index 0fe67bb..fc32310 100644 --- a/src/org/mxchange/addressbook/model/addressbook/AddressbookSessionBeanRemote.java +++ b/src/org/mxchange/addressbook/model/addressbook/AddressbookSessionBeanRemote.java @@ -20,6 +20,7 @@ import java.io.Serializable; 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; @@ -31,6 +32,19 @@ 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. + *

+ * @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. diff --git a/src/org/mxchange/addressbook/model/addressbook/UserAddressbook.java b/src/org/mxchange/addressbook/model/addressbook/UserAddressbook.java index 2ddd1cb..81934a5 100644 --- a/src/org/mxchange/addressbook/model/addressbook/UserAddressbook.java +++ b/src/org/mxchange/addressbook/model/addressbook/UserAddressbook.java @@ -49,7 +49,7 @@ import org.mxchange.jusercore.model.user.User; { @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 { diff --git a/src/org/mxchange/addressbook/validator/addressbook/AddressbookIdValidator.java b/src/org/mxchange/addressbook/validator/addressbook/AddressbookIdValidator.java index 2b97044..b9face6 100644 --- a/src/org/mxchange/addressbook/validator/addressbook/AddressbookIdValidator.java +++ b/src/org/mxchange/addressbook/validator/addressbook/AddressbookIdValidator.java @@ -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.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; @@ -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 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 } -- 2.39.5