From 28683f43ce4b162d31331c14f854fb1489b60944 Mon Sep 17 00:00:00 2001
From: Roland Haeder <roland@mxchange.org>
Date: Tue, 13 Oct 2015 12:07:14 +0200
Subject: [PATCH] =?utf8?q?Continued:=20-=20added=20validator=20for=20addre?=
 =?utf8?q?ss=20book=20id.=20This=20validator=20uses=20the=20remote=20addre?=
 =?utf8?q?ss=20book=20bean=20for=20validation=20-=20added=20business=20met?=
 =?utf8?q?hod=20isAddressbookIdUsed()=20Signed-off-by:Roland=20H=C3=A4der?=
 =?utf8?q?=20<roland@mxchange.org>?=
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit

---
 .../AddressbookSessionBeanRemote.java         |  8 ++
 .../addressbook/AddressbookIdValidator.java   | 85 +++++++++++++++++++
 2 files changed, 93 insertions(+)
 create mode 100644 src/org/mxchange/addressbook/validator/addressbook/AddressbookIdValidator.java

diff --git a/src/org/mxchange/addressbook/model/addressbook/AddressbookSessionBeanRemote.java b/src/org/mxchange/addressbook/model/addressbook/AddressbookSessionBeanRemote.java
index de3baa0..44da7a7 100644
--- a/src/org/mxchange/addressbook/model/addressbook/AddressbookSessionBeanRemote.java
+++ b/src/org/mxchange/addressbook/model/addressbook/AddressbookSessionBeanRemote.java
@@ -64,6 +64,14 @@ public interface AddressbookSessionBeanRemote extends Serializable {
 	 */
 	public Addressbook createAddressbook (final Addressbook addressbook) throws AddressbookNameAlreadyUsedException;
 
+	/**
+	 * Checks whether the given address book id is used (means available).
+	 * <p>
+	 * @param addressbookId Address book id to check
+	 * @return Whether the id is valid
+	 */
+	public boolean isAddressbookIdUsed (final Long addressbookId);
+
 	/**
 	 * Checks if the given address book's name is already used by the user.
 	 * <p>
diff --git a/src/org/mxchange/addressbook/validator/addressbook/AddressbookIdValidator.java b/src/org/mxchange/addressbook/validator/addressbook/AddressbookIdValidator.java
new file mode 100644
index 0000000..e57ace4
--- /dev/null
+++ b/src/org/mxchange/addressbook/validator/addressbook/AddressbookIdValidator.java
@@ -0,0 +1,85 @@
+/*
+ * 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.validator.addressbook;
+
+import java.text.MessageFormat;
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.validator.ValidatorException;
+import javax.faces.view.facelets.FaceletException;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import org.mxchange.addressbook.model.addressbook.AddressbookSessionBeanRemote;
+import org.mxchange.jcoreee.validator.number.BaseLongValidator;
+
+/**
+ * A validator for address book id verification
+ * <p>
+ * @author Roland Haeder
+ */
+public class AddressbookIdValidator extends BaseLongValidator {
+
+	/**
+	 * Serial number
+	 */
+	private static final long serialVersionUID = 158_768_467_186_951_809L;
+
+	/**
+	 * Remote bean
+	 */
+	private AddressbookSessionBeanRemote addressbookBean;
+
+	/**
+	 * Default constructor
+	 */
+	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);
+		}
+	}
+
+	@Override
+	public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
+		// Trace message
+		//this.getLogger().logTrace(MessageFormat.format("validate: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
+
+		// All accepted, required fields
+		String[] requiredFileds = {"addressbookId"}; //NOI18N
+
+		// Pre-validation (example: not null, not a string, empty string ...)
+		super.preValidate(context, component, value, requiredFileds, false);
+
+		// Is the address book id valid?
+		if (!this.addressbookBean.isAddressbookIdUsed((Long) value)) {
+			// Is not valid
+			throw new ValidatorException(new FacesMessage(MessageFormat.format("No address book found with id {0}. Please check your link.", value)));
+		}
+
+		// Trace message
+		//this.getLogger().logTrace("validate: EXIT!"); //NOI18N
+	}
+}
-- 
2.39.5