/*
- * Copyright (C) 2017 Roland Häder
+ * Copyright (C) 2024 Free Software Foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
package org.mxchange.pizzaapplication.validator.business.basicdata;
import java.text.MessageFormat;
-import javax.ejb.EJB;
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.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import org.mxchange.jcontactsbusiness.model.basicdata.BasicCompanyDataSessionBeanRemote;
import org.mxchange.jcoreee.validator.string.BaseStringValidator;
/**
* <p>
* @author Roland Häder<roland@mxchange.org>
*/
-@FacesValidator (value = "CompanyNameValidator", managed = true)
+@FacesValidator ("CompanyNameValidator")
public class PizzaCompanyNameValidator extends BaseStringValidator {
/**
- * Serial number
+ * Business contact EJB
*/
- private static final long serialVersionUID = 57_283_657_476_561L;
+ private static BasicCompanyDataSessionBeanRemote BASIC_DATA_BEAN;
/**
- * Business contact EJB
+ * Serial number
*/
- @EJB (lookup = "java:global/jfinancials-ejb/businessData!org.mxchange.jcontactsbusiness.basicdata.BusinessDataSessionBeanRemote")
- private BusinessDataSessionBeanRemote basicDataBean;
+ private static final long serialVersionUID = 57_283_657_476_561L;
@Override
public void validate (final FacesContext context, final UIComponent component, final Object value) throws ValidatorException {
+ // Is the instance there?
+ if (BASIC_DATA_BEAN == null) {
+ try {
+ // Not yet, attempt lookup
+ final Context initial = new InitialContext();
+
+ // Lookup EJB
+ BASIC_DATA_BEAN = (BasicCompanyDataSessionBeanRemote) initial.lookup("java:global/pizzaservice-ejb/basicCompanyData!org.mxchange.jcontactsbusiness.model.basicdata.BasicCompanyDataSessionBeanRemote");
+ } catch (final NamingException ex) {
+ // Throw it again
+ throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Cannot lookup EJB", ex.getMessage()), ex);
+ }
+ }
+
// All accepted, required fields
- String[] requiredFields = {"companyName"}; //NOI18N
+ final String[] requiredFields = {"companyName"}; //NOI18N
// Pre-validation (example: not null, not a string, empty string ...)
- super.preValidate(context, component, value, requiredFields, false);
+ super.preValidate(context, component, value, requiredFields, Boolean.FALSE);
// Convert name to string (now securely checked in BaseStringValidator)
- String companyName = (String) value;
+ final String companyName = (String) value;
// Default is to check on existing names
Boolean checkExisting = Boolean.TRUE;
- // Is attribute "allowEmptyValue" set?
+ // Is attribute "checkExisting" set?
if (component.getAttributes().containsKey("checkExisting")) { //NOI18N
// Get attribute
- Object attribute = component.getAttributes().get("checkExisting"); //NOI18N
+ final Object attribute = component.getAttributes().get("checkExisting"); //NOI18N
// Make sure, it is Boolean as no String is accepted anymore
if (!(attribute instanceof String)) {
}
// Securely cast it
- checkExisting = Boolean.parseBoolean((String) attribute);
+ checkExisting = Boolean.valueOf((String) attribute);
}
// Check if name is already used
- Boolean nameExists = this.basicDataBean.isCompanyNameUsed(companyName);
+ final Boolean nameExists = BASIC_DATA_BEAN.isCompanyNameUsed(companyName);
// Is the user id valid?
if ((!nameExists) && (checkExisting)) {
// Format message
- String message = MessageFormat.format("No basic data found with comany name {0}.", companyName);
+ final String message = MessageFormat.format("No basic data found with comany name {0}.", companyName);
// Name does not exist
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message)); //NOI18N
} else if ((nameExists) && (!checkExisting)) {
// Format message
- String message = MessageFormat.format("Found basic data with comany name {0}.", companyName);
+ final String message = MessageFormat.format("Found basic data with comany name {0}.", companyName);
// Name already exists
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_WARN, message, message)); //NOI18N