]> git.mxchange.org Git - pizzaservice-war.git/blobdiff - src/java/org/mxchange/pizzaapplication/validator/business/basicdata/PizzaCompanyNameValidator.java
Updated copyright year
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / validator / business / basicdata / PizzaCompanyNameValidator.java
index 155d0d608373eb8e422d1368204cdbe1057c8d55..e9e8b1944099d44bfb0e0a6027540719004b7e0c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * 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;
 
 /**
@@ -30,38 +33,51 @@ 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)) {
@@ -70,22 +86,22 @@ public class PizzaCompanyNameValidator extends BaseStringValidator {
                        }
 
                        // 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