From: Roland Häder Date: Mon, 19 Mar 2018 00:01:58 +0000 (+0100) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=35286461209a44c7f09c1b0c4edd24275be746ff;p=jcore-utils.git Continued: - jcore-utils was splitted out from jcoreee to reduce project centralization (means you have to that whole fat bunch of classes but only use a few) - these utility classes may also be used in JavaSE projects so better split them out as they are generic enough - rename CoreNumberUtils to SafeNumberUtils as this class provides null-safe methods for any kind of Java numbers Signed-off-by: Roland Häder --- diff --git a/build.xml b/build.xml index 8c40280..31a3559 100644 --- a/build.xml +++ b/build.xml @@ -7,8 +7,8 @@ - - Builds, tests, and runs the project jcoreee. + + Builds, tests, and runs the project jcore-utils. - + @@ -468,7 +468,7 @@ is divided into following sections: - + @@ -619,7 +619,7 @@ is divided into following sections: - + @@ -911,7 +911,7 @@ is divided into following sections: - + @@ -1396,7 +1396,7 @@ is divided into following sections: - + diff --git a/nbproject/project.properties b/nbproject/project.properties index 38ebe6b..46c4512 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -3,7 +3,7 @@ annotation.processing.enabled.in.editor=true annotation.processing.processors.list= annotation.processing.run.all.processors=true annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output -application.title=jcoreee +application.title=jcore-utils application.vendor=Roland H\u00e4der auxiliary.org-netbeans-spi-editor-hints-projects.perProjectHintSettingsFile=nbproject/cfg_hints.xml build.classes.dir=${build.dir}/classes @@ -26,17 +26,15 @@ debug.test.classpath=\ dist.archive.excludes= # This directory is removed when the project is cleaned: dist.dir=dist -dist.jar=${dist.dir}/jcoreee.jar +dist.jar=${dist.dir}/jcore-utils.jar dist.javadoc.dir=${dist.dir}/javadoc endorsed.classpath= excludes= -file.reference.jcore-logger-lib.jar=lib/jcore-logger-lib.jar includes=** jar.archive.disabled=${jnlp.enabled} jar.compress=false jar.index=${jnlp.enabled} javac.classpath=\ - ${file.reference.jcore-logger-lib.jar}:\ ${libs.javaee-api-7.0.classpath}:\ ${libs.cache.classpath} # Space-separated list of extra javac options @@ -91,6 +89,5 @@ run.test.classpath=\ ${javac.test.classpath}:\ ${build.test.classes.dir} source.encoding=UTF-8 -source.reference.jcore-logger-lib.jar=../jcore-logger-lib/src/ src.dir=src test.src.dir=test diff --git a/nbproject/project.xml b/nbproject/project.xml index 8e62b15..22b5c58 100644 --- a/nbproject/project.xml +++ b/nbproject/project.xml @@ -3,7 +3,7 @@ org.netbeans.modules.java.j2seproject - jcoreee + jcore-utils diff --git a/src/org/mxchange/jcoreee/bean/ejb/BaseEnterpriseBean.java b/src/org/mxchange/jcoreee/bean/ejb/BaseEnterpriseBean.java deleted file mode 100644 index a08610c..0000000 --- a/src/org/mxchange/jcoreee/bean/ejb/BaseEnterpriseBean.java +++ /dev/null @@ -1,208 +0,0 @@ -/* - * Copyright (C) 2016 - 2018 Free Software Foundation - * - * 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.jcoreee.bean.ejb; - -import java.io.Serializable; -import java.text.MessageFormat; -import javax.faces.FacesException; -import javax.jms.Connection; -import javax.jms.JMSException; -import javax.jms.MessageProducer; -import javax.jms.ObjectMessage; -import javax.jms.Queue; -import javax.jms.QueueConnectionFactory; -import javax.jms.Session; -import javax.naming.Context; -import javax.naming.InitialContext; -import javax.naming.NamingException; -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; -import org.mxchange.jcoreeelogger.beans.local.logger.Log; -import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal; - -/** - * A helper class for beans that access the database. - *

- * @author Roland Häder - */ -public abstract class BaseEnterpriseBean implements Serializable { - - /** - * Serial number - */ - private static final long serialVersionUID = 217_687_175_985_875L; - - /** - * Connection - */ - private Connection connection; - - /** - * Entity manager - */ - @PersistenceContext - private EntityManager entityManager; - - /** - * Logger instance - */ - @Log - private LoggerBeanLocal loggerBeanLocal; - - /** - * Message producer - */ - private MessageProducer messageProducer; - - /** - * Mailer message queue - */ - private Queue queue; - - /** - * Session instance - */ - private Session session; - - /** - * This class' default protected constructor. Please invoke - * super("jms/project-queue-factory", "jms/project-email-queue"); if you - * need to send emails. - */ - protected BaseEnterpriseBean () { - // Call super constructor - super(); - - // Init logger instance - this.initLoggerInstance(); - } - - /** - * Constructor with queue factory JNDI and queue JNDI names - *

- * @param factoryJndi JNDI name for queue factory - * @param queueJndi JNDI name for email queue - */ - protected BaseEnterpriseBean (final String factoryJndi, final String queueJndi) { - // Call default constructor - this(); - - // Try it out - try { - // Get initial context - Context context = new InitialContext(); - - // Get factory from JMS resource - QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup(factoryJndi); - - // Lookup queue - this.queue = (Queue) context.lookup(queueJndi); - - // Create connection - this.connection = connectionFactory.createConnection(); - - // Init session instance - this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE); - - // And message producer - this.messageProducer = this.session.createProducer(this.queue); - } catch (final NamingException | JMSException e) { - // Continued to throw - throw new FacesException(e); - } - } - - /** - * Initializes logger instance - */ - private void initLoggerInstance () { - try { - // Get initial context - Context context = new InitialContext(); - - // Lookup logger - this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N - } catch (final NamingException ex) { - // Continue to throw - throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N - } - } - - /** - * Getter for connection instance - *

- * @return Connection instance - */ - protected EntityManager getEntityManager () { - return this.entityManager; - } - - /** - * Getter for loggerBeanLocal - *

- * @return Logger instance - */ - protected LoggerBeanLocal getLoggerBeanLocal () { - return this.loggerBeanLocal; - } - - /** - * Getter for configured message producer instance - *

- * @return Message producer - */ - protected MessageProducer getMessageProducer () { - return this.messageProducer; - } - - /** - * Getter for configured session instance - *

- * @return Session - */ - protected Session getSession () { - return this.session; - } - - /** - * Sends given message to configured queue - *

- * @param message Message to send - *

- * @throws JMSException if something went wrong - */ - protected void sendMessage (final ObjectMessage message) throws JMSException { - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N - - // The parameter should be valid - if (null == message) { - // Throw NPE - throw new NullPointerException("message is null"); //NOI18N - } else if (this.getMessageProducer() == null) { - // Throw NPE again - throw new NullPointerException("this.messageProvider is null"); //NOI18N - } - - // Send it - this.getMessageProducer().send(message); - - // Trace message - this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N - } - -} diff --git a/src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java b/src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java deleted file mode 100644 index 6101943..0000000 --- a/src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java +++ /dev/null @@ -1,272 +0,0 @@ -/* - * Copyright (C) 2017, 2018 Free Software Foundation - * - * 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.jcoreee.bean.faces; - -import java.io.Serializable; -import java.security.Principal; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; -import java.util.MissingResourceException; -import java.util.ResourceBundle; -import javax.faces.application.FacesMessage; -import javax.faces.context.FacesContext; - -/** - * An abstract bean for faces (web) projects. - *

- * @author Roland Häder - */ -public abstract class BaseFacesBean implements Serializable { - - /** - * Loaded resource bundles ("cached") - */ - private static final List RESOURCE_BUNDLES; - - /** - * Serial number - */ - private static final long serialVersionUID = 18_605_498_672_261L; - - /** - * Static initializer - */ - static { - // Init resource bundle list - RESOURCE_BUNDLES = new ArrayList<>(3); - } - - /** - * Removes all bundles from web application. Typically you want to invoke - * this method in a ServletContextListener implemetation on the - * contextDestroyed() method. - */ - public static void removeBundles () { - // Clear bundles - RESOURCE_BUNDLES.clear(); - } - - /** - * Getter for resource bundle list - *

- * @return Resource bundle list - */ - @SuppressWarnings ("ReturnOfCollectionOrArrayField") - protected static List getBundles () { - return RESOURCE_BUNDLES; - } - - /** - * Protected constructor - */ - protected BaseFacesBean () { - // Call super constructor - super(); - } - - /** - * Determines principal's name or returns null if no principal (security) is - * set. - *

- * @return Principal's name or null - */ - protected String determinePrincipalName () { - // Get principal - final Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal(); - - // Init with null - String principalName = null; - - // Is the principal set? - if (userPrincipal instanceof Principal) { - // Get principal's name - principalName = userPrincipal.getName(); - } - - // Return it - return principalName; - } - - /** - * Returns given property key or throws an exception if not found. - *

- * @param parameterKey Property key - *

- * @return Property value - */ - protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException { - // Get context parameter - final Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey)); - - // Return it - return contextValue; - } - - /** - * Returns a message based on given i18nKey or puts it into three question - * marks each side when not found. - *

- * @param i18nKey I18n key - *

- * @return Localized message - *

- * @throws NullPointerException If the parameter is null - * @throws IllegalArgumentException If the parameter is empty - */ - protected String getMessageFromBundle (final String i18nKey) { - // Validate parameter - if (null == i18nKey) { - // Throw NPE - throw new NullPointerException("i18nKey is null"); //NOI18N - } else if (i18nKey.isEmpty()) { - // Is empty - throw new IllegalArgumentException("i18nKey is empty"); //NOI18N - } - - // Get current locale - final Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale(); - - // Get bundle bundle - this.loadResourceBundles(locale); - - // Default is i18nKey - String message = MessageFormat.format("???{0}???", i18nKey); //NOI18N - - // Loop through all - for (final ResourceBundle bundle : getBundles()) { - // Found message? - // Try it - try { - // Get message - message = bundle.getString(i18nKey); - break; - } catch (final MissingResourceException ex) { - // Did not find it, ignored - } - } - - // Return it - return message; - } - - /** - * Returns given property key or throws an exception if not found. - *

- * @param parameterKey Property key - *

- * @return Property value - *

- * @throws NullPointerException If given key is not found - */ - protected String getStringContextParameter (final String parameterKey) throws NullPointerException { - // Get context parameter - final String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey); - - // Is it null? - if (null == contextValue) { - // Throw NPE - throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N - } - - // Return it - return contextValue; - } - - /** - * Checks whether debug mode is enabled for given controller - *

- * @param controllerName Name of controller - *

- * @return Whether debug mode is enabled - */ - protected boolean isDebugModeEnabled (final String controllerName) { - // Parameters should be valid - if (null == controllerName) { - // Throw NPE - throw new NullPointerException("controllerName is null"); //NOI18N - } else if (controllerName.isEmpty()) { - // Is empty - throw new IllegalArgumentException("controllerName is empty"); //NOI18N - } - - // Try to get context parameter - final String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N - - // Is it set and true? - final boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE; - - // Return it - return isEnabled; - } - - /** - * Loads resource bundles for given locale. This must be implemented per - * project so all projects can still customize their methods. Calling - * ResourceBundleloadBundle() in this class means that also the bundle files - * must be present here. - *

- * @param locale Locale from e.g. FacesContext - */ - protected abstract void loadResourceBundles (final Locale locale); - - /** - * Shows a faces message for given causing exception. The message from the - * exception is being inserted into the message. - *

- * @param clientId Client id to send message to - * @param cause Causing exception - */ - protected void showFacesMessage (final String clientId, final Throwable cause) { - // Get context and add message - this.showFacesMessage(clientId, cause.getMessage()); - } - - /** - * Shows a faces message with given message (i18n) key. - *

- * @param clientId Client id to send message to - * @param i18nKey Message key - *

- * @throws NullPointerException If clientId or i18nKey is null - * @throws IllegalArgumentException If clientId or i18nKey is empty - */ - protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException { - // Both parameter must be valid - if (null == clientId) { - // Throw NPE - throw new NullPointerException("clientId is null"); //NOI18N - } else if (clientId.isEmpty()) { - // Is empty - throw new IllegalArgumentException("clientId is null"); //NOI18N - } else if (null == i18nKey) { - // Throw NPE - throw new NullPointerException("i18nKey is null"); //NOI18N - } else if (i18nKey.isEmpty()) { - // Is empty - throw new IllegalArgumentException("i18nKey is null"); //NOI18N - } - - // Get message from bundle - final String message = this.getMessageFromBundle(i18nKey); - - // Get context and add message - FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message)); - } - -} diff --git a/src/org/mxchange/jcoreee/events/helper/clear/HelperCleanupEvent.java b/src/org/mxchange/jcoreee/events/helper/clear/HelperCleanupEvent.java deleted file mode 100644 index 3912253..0000000 --- a/src/org/mxchange/jcoreee/events/helper/clear/HelperCleanupEvent.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) 2017, 2018 Free Software Foundation - * - * 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.jcoreee.events.helper.clear; - -/** - * An event being fired when a helper bean should be cleaned. - *

- * @author Roland Häder - */ -public class HelperCleanupEvent implements ObservableHelperCleanupEvent { - - /** - * Serial number - */ - private static final long serialVersionUID = 19_686_371_541_676L; - - /** - * Default constructor - */ - public HelperCleanupEvent () { - } - -} diff --git a/src/org/mxchange/jcoreee/events/helper/clear/ObservableHelperCleanupEvent.java b/src/org/mxchange/jcoreee/events/helper/clear/ObservableHelperCleanupEvent.java deleted file mode 100644 index 3a7cb43..0000000 --- a/src/org/mxchange/jcoreee/events/helper/clear/ObservableHelperCleanupEvent.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) 2017, 2018 Free Software Foundation - * - * 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.jcoreee.events.helper.clear; - -import java.io.Serializable; - -/** - * An event being fired when a helper bean should be cleaned. - *

- * @author Roland Häder - */ -public interface ObservableHelperCleanupEvent extends Serializable { - -} diff --git a/src/org/mxchange/jcoreee/events/locale/LocaleChangeEvent.java b/src/org/mxchange/jcoreee/events/locale/LocaleChangeEvent.java deleted file mode 100644 index 3851b90..0000000 --- a/src/org/mxchange/jcoreee/events/locale/LocaleChangeEvent.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (C) 2017, 2018 Free Software Foundation - * - * 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.jcoreee.events.locale; - -import java.util.Locale; - -/** - * An event being fired when a helper bean should be cleaned. - *

- * @author Roland Häder - */ -public class LocaleChangeEvent implements ObservableLocaleChangeEvent { - - /** - * Serial number - */ - private static final long serialVersionUID = 14_689_371_541_856L; - - /** - * New locale instance - */ - private final Locale locale; - - /** - * Default constructor with new locale - *

- * @param locale New locale instance - */ - public LocaleChangeEvent (final Locale locale) { - this.locale = locale; - } - - @Override - public Locale getLocale () { - return this.locale; - } - -} diff --git a/src/org/mxchange/jcoreee/events/locale/ObservableLocaleChangeEvent.java b/src/org/mxchange/jcoreee/events/locale/ObservableLocaleChangeEvent.java deleted file mode 100644 index abeb998..0000000 --- a/src/org/mxchange/jcoreee/events/locale/ObservableLocaleChangeEvent.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (C) 2017, 2018 Free Software Foundation - * - * 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.jcoreee.events.locale; - -import java.io.Serializable; -import java.util.Locale; - -/** - * An event being fired when a helper bean should be cleaned. - *

- * @author Roland Häder - */ -public interface ObservableLocaleChangeEvent extends Serializable { - - /** - * Getter for locale instance - *

- * @return Locale instance - */ - Locale getLocale (); - -} diff --git a/src/org/mxchange/jcoreee/exceptions/CustomExceptionHandler.java b/src/org/mxchange/jcoreee/exceptions/CustomExceptionHandler.java deleted file mode 100644 index 37716a1..0000000 --- a/src/org/mxchange/jcoreee/exceptions/CustomExceptionHandler.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright (C) 2016 - 2018 Free Software Foundation - * - * 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.jcoreee.exceptions; - -import java.text.MessageFormat; -import java.util.Iterator; -import java.util.Map; -import javax.faces.FacesException; -import javax.faces.application.NavigationHandler; -import javax.faces.context.ExceptionHandler; -import javax.faces.context.ExceptionHandlerWrapper; -import javax.faces.context.FacesContext; -import javax.faces.event.ExceptionQueuedEvent; -import javax.faces.event.ExceptionQueuedEventContext; -import javax.naming.Context; -import javax.naming.InitialContext; -import javax.naming.NamingException; -import org.mxchange.jcoreeelogger.beans.local.logger.Log; -import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal; - -/** - * A custom exception handler for nice output. This code is heavily based on - * this [1] example. - *

- * 1: https://wmarkito.wordpress.com/2012/04/05/adding-global-exception-handling-using-jsf-2-x-exceptionhandler/ - *

- * @author Roland Häder - */ -public class CustomExceptionHandler extends ExceptionHandlerWrapper { - - /** - * Logger instance - */ - @Log - private LoggerBeanLocal loggerBeanLocal; - - /** - * Exception handler - */ - private ExceptionHandler wrapped = null; - - /** - * Constructor with exception handler to be wrapped - *

- * @param exceptionHandler Wrapped exception handler - */ - public CustomExceptionHandler (final ExceptionHandler exceptionHandler) { - // Call default constructor - this(); - - // Set handler here - this.wrapped = exceptionHandler; - } - - /** - * Default constructor - */ - public CustomExceptionHandler () { - try { - // Get initial context - Context context = new InitialContext(); - - // Lookup logger - this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N - } catch (final NamingException ex) { - // Continue to throw - throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N - } - } - - @Override - public ExceptionHandler getWrapped () { - return this.wrapped; - } - - @Override - public void handle () throws FacesException { - - final Iterator iterator = this.getUnhandledExceptionQueuedEvents().iterator(); - - while (iterator.hasNext()) { - ExceptionQueuedEvent event = iterator.next(); - ExceptionQueuedEventContext context = (ExceptionQueuedEventContext) event.getSource(); - - // get the exception from context - Throwable t = context.getException(); - - final FacesContext facesContext = FacesContext.getCurrentInstance(); - final Map requestMap = facesContext.getExternalContext().getRequestMap(); - final NavigationHandler nav = facesContext.getApplication().getNavigationHandler(); - - //here you do what ever you want with exception - try { - - //log error ? - this.loggerBeanLocal.logFatal("Critical Exception.", t); //NOI18N - - //redirect error page - requestMap.put("exceptionMessage", t.getMessage()); //NOI18N - nav.handleNavigation(facesContext, null, "exception"); //NOI18N - facesContext.renderResponse(); - - // remove the comment below if you want to report the error in a jsf error message - // @TODO: JsfUtil.addErrorMessage(t.getMessage()); - } finally { - //remove it from queue - iterator.remove(); - } - } - - //parent hanle - this.getWrapped().handle(); - } - -} diff --git a/src/org/mxchange/jcoreee/exceptions/CustomExceptionHandlerFactory.java b/src/org/mxchange/jcoreee/exceptions/CustomExceptionHandlerFactory.java deleted file mode 100644 index 9335645..0000000 --- a/src/org/mxchange/jcoreee/exceptions/CustomExceptionHandlerFactory.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2016 - 2018 Free Software Foundation - * - * 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.jcoreee.exceptions; - -import javax.faces.context.ExceptionHandler; -import javax.faces.context.ExceptionHandlerFactory; - -/** - * A custom factory for exception handler. This class is heavily based on this - * [1] example. - *

- * 1: https://wmarkito.wordpress.com/2012/04/05/adding-global-exception-handling-using-jsf-2-x-exceptionhandler/ - *

- * @author Roland Häder - */ -public class CustomExceptionHandlerFactory extends ExceptionHandlerFactory { - - /** - * Parent exception handler - */ - private final ExceptionHandlerFactory parent; - - public CustomExceptionHandlerFactory (final ExceptionHandlerFactory parent) { - // Set it here - this.parent = parent; - } - - @Override - public ExceptionHandler getExceptionHandler () { - - ExceptionHandler handler = new CustomExceptionHandler(this.parent.getExceptionHandler()); - - return handler; - } - -} diff --git a/src/org/mxchange/jcoreee/utils/Comparables.java b/src/org/mxchange/jcoreee/utils/Comparables.java deleted file mode 100644 index b5287f0..0000000 --- a/src/org/mxchange/jcoreee/utils/Comparables.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright (C) 2018 Free Software Foundation - * - * 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.jcoreee.utils; - -import java.io.Serializable; - -/** - * An utilities class for comparison of objects/entities - *

- * @author Roland Häder - */ -public class Comparables implements Serializable { - - /** - * Serial number - */ - private static final long serialVersionUID = 19_286_457_671_390L; - - /** - * Checks all comparison values if they are not zero and returns proper - * value. These comparators should come from an implementation of - * Comparable.compareTo(). - *

- * @param comparators An array of at least one comparator - *

- * @return Comparison value - */ - public static int checkAll (final int[] comparators) { - // Is array empty? - if (comparators.length == 0) { - // Should not happen - throw new IllegalArgumentException("comparators is empty."); //NOI18N - } - - // Loop through all - for (int i = 0; i < comparators.length; i++) { - // Is it smaller or bigger? - if (comparators[i] < 0) { - return -1; - } else if (comparators[i] > 0) { - return 1; - } - } - - // Assume euqality - return 0; - } - - /** - * Utility classes should not have instances - */ - private Comparables () { - } - -} diff --git a/src/org/mxchange/jcoreee/utils/CoreNumberUtils.java b/src/org/mxchange/jcoreee/utils/CoreNumberUtils.java deleted file mode 100644 index 956660e..0000000 --- a/src/org/mxchange/jcoreee/utils/CoreNumberUtils.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Copyright (C) 2018 Free Software Foundation - * - * 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.jcoreee.utils; - -import java.io.Serializable; -import java.util.Objects; - -/** - * Null-safe utilities class for numbers - *

- * @author Roland Häder - */ -public class CoreNumberUtils implements Serializable { - - /** - * Serial number - */ - private static final long serialVersionUID = 153_746_766_826_120L; - - /** - * Compares both Integer instances null-safe following the rules of - * Comparable.compareTo(). - *

- * @param integer1 Integer instance 1 - * @param integer2 Integer instance 2 - *

- * @return Comparison value - */ - public static int compare (final Integer integer1, final Integer integer2) { - // Check instances - if (Objects.equals(integer1, integer2)) { - // Both objects are equal - return 0; - } else if (null == integer1) { - // First number is null (means is smaller than 0) - return -1; - } else if (null == integer2) { - // Second number is null (means is smaller than 0) - return 1; - } - - // Compare vaules null-safe - return Integer.compare(integer1, integer2); - } - - /** - * Compares both Long instances null-safe following the rules of - * Comparable.compareTo(). - *

- * @param long1 Long instance 1 - * @param long2 Long instance 2 - *

- * @return Comparison value - */ - public static int compare (final Long long1, final Long long2) { - // Check instances - if (Objects.equals(long1, long2)) { - // Both objects are equal - return 0; - } else if (null == long1) { - // First number is null (means is smaller than 0) - return -1; - } else if (null == long2) { - // Second number is null (means is smaller than 0) - return 1; - } - - // Compare vaules null-safe - return Long.compare(long1, long2); - } - - /** - * Compares both Short instances null-safe following the rules of - * Comparable.compareTo(). - *

- * @param short1 Short instance 1 - * @param short2 Short instance 2 - *

- * @return Comparison value - */ - public static int compare (final Short short1, final Short short2) { - // Check instances - if (Objects.equals(short1, short2)) { - // Both objects are equal - return 0; - } else if (null == short1) { - // First number is null (means is smaller than 0) - return -1; - } else if (null == short2) { - // Second number is null (means is smaller than 0) - return 1; - } - - // Compare vaules null-safe - return Short.compare(short1, short2); - } - - /** - * Utilities classes should not have instances - */ - private CoreNumberUtils () { - // Private constructor - } - -} diff --git a/src/org/mxchange/jcoreee/utils/FacesUtils.java b/src/org/mxchange/jcoreee/utils/FacesUtils.java deleted file mode 100644 index 35e0b28..0000000 --- a/src/org/mxchange/jcoreee/utils/FacesUtils.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright (C) 2016 - 2018 Free Software Foundation - * - * 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.jcoreee.utils; - -import java.io.Serializable; -import javax.faces.context.ExternalContext; -import javax.faces.context.FacesContext; - -/** - * An utilities class for JavaEE applications, entities and EJBs and more - *

- * @author Roland Häder - */ -public class FacesUtils implements Serializable { - - /** - * Serial number - */ - private static final long serialVersionUID = 19_863_546_716_250L; - - /** - * Generates a "base URL" for for example mail templates. For JSF - * pages/templates this is not needed. - *

- * @return Base URL - */ - public static String generateBaseUrl () { - // Get external context - ExternalContext context = FacesContext.getCurrentInstance().getExternalContext(); - - // Get request scheme and such ... - String scheme = context.getRequestScheme(); - String serverName = context.getRequestServerName(); - String contextPath = context.getRequestContextPath(); - String servletPath = context.getRequestServletPath(); - int port = context.getRequestServerPort(); - - // Is the path null? - if (null == scheme) { - // Throw NPE - throw new NullPointerException("context.requestScheme is null"); //NOI18N - } else if (null == serverName) { - // And throw again ... - throw new NullPointerException("context.requestServerName is null"); //NOI18N - } - - if (null == contextPath) { - // Set to empty string - contextPath = ""; //NOI18N - } - - if (null == servletPath) { - // Set to empty string - servletPath = ""; //NOI18N - } - - // Init variable - String baseUrl; - - // Unusual port found? - if ((port != 80) && (port != 443)) { - // Construct full URL - baseUrl = String.format("%s://%s:%d%s%s", scheme, serverName, port, contextPath, servletPath); //NOI18N - } else { - // Construct full URL - baseUrl = String.format("%s://%s%s%s", scheme, serverName, contextPath, servletPath); //NOI18N - } - - // Return it - return baseUrl; - } - - /** - * No instances from this class are required - */ - private FacesUtils () { - // Is this null? - if (FacesContext.getCurrentInstance() == null) { - // Okay, don't allow any usage - throw new NullPointerException("Cannot access FacesContext, maybe you have tried to use this class from an EJB?"); //NOI18N - } - } - -} diff --git a/src/org/mxchange/jcoreee/validator/BaseObjectValidator.java b/src/org/mxchange/jcoreee/validator/BaseObjectValidator.java deleted file mode 100644 index 2e9d26f..0000000 --- a/src/org/mxchange/jcoreee/validator/BaseObjectValidator.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (C) 2016 - 2018 Free Software Foundation - * - * 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.jcoreee.validator; - -import java.io.Serializable; -import java.text.MessageFormat; -import javax.faces.application.FacesMessage; -import javax.faces.component.UIComponent; -import javax.faces.context.FacesContext; -import javax.faces.validator.Validator; -import javax.faces.validator.ValidatorException; - -/** - * A general object validation class. Please implement - * javax.faces.validator.Validator (with import line!) and call preValidate(). - * You also may want to try out some other BaseFooValidator classes before - * directly inheriting from this class. - *

- * @param Any object that needs validation - *

- * @author Roland Häder - */ -public abstract class BaseObjectValidator implements Validator, Serializable { - - /** - * Serial number - */ - private static final long serialVersionUID = 48_574_878_176_939_512L; - - /** - * The method pre-validates the given value. It makes sure that the - * component's id is found in requiredFields and is not null. Once the - * component's id has been found, it stops iteration on requiredFields - * (which saves execution time). - *

- * @param context FacesContext instance - * @param component UIComponent instance - * @param value Value to check - * @param requiredFields Array of required field names (ending with) - * @param allowNull Whether null or empty values are allowed - *

- * @throws ValidatorException If something more horrible went wrong - */ - protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, Boolean allowNull) throws ValidatorException { - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N - - // Init message and key - String requiredMessage = null; - - // Get client id - final String clientId = component.getClientId(); - - // Check component's id against required fields and find a match - for (final String field : requiredFields) { - // Get logger - //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N - - // Is it the same? - if (clientId.endsWith(field)) { - // Is it null? - if ((!allowNull) && (null == value)) { - // Value it null - requiredMessage = MessageFormat.format("Field {0} is null.", field); //NOI18N - } - - // Abort here - break; - } - } - - // Debug message - //* NOISY-DEBUG: */ this.getLogger().logDebug(MessageFormat.format("preValidate: requiredMessage={0}", requiredMessage)); //NOI18N - // Is it not null? - if (null != requiredMessage) { - throw new ValidatorException(new FacesMessage(MessageFormat.format("Value {0} for clientId={1} is not valid/unexpected.", value, clientId))); - } - - // Trace message - //* NOISY-DEBUG: */ System.out.println("preValidate: EXIT!"); //NOI18N - } - -} diff --git a/src/org/mxchange/jcoreee/validator/bool/BaseBooleanValidator.java b/src/org/mxchange/jcoreee/validator/bool/BaseBooleanValidator.java deleted file mode 100644 index 9844daf..0000000 --- a/src/org/mxchange/jcoreee/validator/bool/BaseBooleanValidator.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright (C) 2016 - 2018 Free Software Foundation - * - * 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.jcoreee.validator.bool; - -import java.text.MessageFormat; -import javax.faces.application.FacesMessage; -import javax.faces.component.UIComponent; -import javax.faces.component.UIInput; -import javax.faces.component.ValueHolder; -import javax.faces.context.FacesContext; -import javax.faces.validator.ValidatorException; -import org.mxchange.jcoreee.validator.BaseObjectValidator; - -/** - * A general boolean value validator. - *

- * @author BalusC - * @author Roland Häder - */ -public abstract class BaseBooleanValidator extends BaseObjectValidator { - - /** - * Serial number - */ - private static final long serialVersionUID = 42_378_178_715_910_689L; - - @Override - public void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, Boolean allowNull) throws ValidatorException { - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N - - // Pre-validate - super.preValidate(context, component, value, requiredFields, allowNull); - - // Get client id and init message + key - String clientId = component.getClientId(); - String requiredMessage = null; - - // So far all fine, no check if the field is fine - for (final String field : requiredFields) { - // Debug message - //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N - - // Is it the same? - if (clientId.endsWith(field)) { - // Compare value's type - if (!(value instanceof Boolean)) { - // Generate message - requiredMessage = MessageFormat.format("Field {0} is not Boolean: {1}", field, value); //NOI18N - - // Value is not right type - break; - } - - // Cast to string - Boolean bool = (Boolean) value; - - // Is it false? - if (bool.equals(Boolean.FALSE)) { - // Default message - requiredMessage = ((UIInput) component).getRequiredMessage(); - - if (null == requiredMessage) { - Object label = component.getAttributes().get("label"); //NOI18N - - // Check if label is null, or zero length - if ((null == label) || (label instanceof CharSequence && ((CharSequence) label).length() == 0)) { - label = component.getValueExpression("label"); //NOI18N - } - - // Label is still null? - if (null == label) { - label = component.getClientId(context); - } - - // Set message - requiredMessage = MessageFormat.format(UIInput.REQUIRED_MESSAGE_ID, label); - - // Set value to false in UI component - ((ValueHolder) component).setValue(Boolean.FALSE); - } - - // Abort processing here - break; - } - } - } - - // Is facesMessage set? - if (null != requiredMessage) { - // Abort here - throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage)); - } - } - -} diff --git a/src/org/mxchange/jcoreee/validator/date/BaseDateValidator.java b/src/org/mxchange/jcoreee/validator/date/BaseDateValidator.java deleted file mode 100644 index db01cfe..0000000 --- a/src/org/mxchange/jcoreee/validator/date/BaseDateValidator.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (C) 2016 - 2018 Free Software Foundation - * - * 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.jcoreee.validator.date; - -import java.text.MessageFormat; -import java.util.Date; -import javax.faces.application.FacesMessage; -import javax.faces.component.UIComponent; -import javax.faces.context.FacesContext; -import javax.faces.validator.ValidatorException; -import org.mxchange.jcoreee.validator.BaseObjectValidator; - -/** - * A generic validator class for dates - *

- * @author Roland Häder - */ -public abstract class BaseDateValidator extends BaseObjectValidator { - - /** - * Serial number - */ - private static final long serialVersionUID = 57_341_298_601_276L; - - @Override - public void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, Boolean allowNull) throws ValidatorException { - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N - - // Pre-validate - super.preValidate(context, component, value, requiredFields, allowNull); - - // Get client id and init message + key - String clientId = component.getClientId(); - String requiredMessage = null; - - // So far all fine, no check if the field is fine - for (final String field : requiredFields) { - // Debug message - //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N - - // Is it the same? - if (clientId.endsWith(field)) { - // Compare value's type - if (!(value instanceof Date)) { - // Generate message - requiredMessage = MessageFormat.format("Field {0} is not Date: {1}", field, value); //NOI18N - - // Value is not right type - break; - } - - // Cast to date - Date date = (Date) value; - - // @TODO Continue here? - } - } - - // Is facesMessage set? - if (null != requiredMessage) { - // Abort here - throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage)); - } - } - -} diff --git a/src/org/mxchange/jcoreee/validator/number/BaseNumberValidator.java b/src/org/mxchange/jcoreee/validator/number/BaseNumberValidator.java deleted file mode 100644 index 23664ed..0000000 --- a/src/org/mxchange/jcoreee/validator/number/BaseNumberValidator.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright (C) 2016 - 2018 Free Software Foundation - * - * 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.jcoreee.validator.number; - -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 org.mxchange.jcoreee.validator.BaseObjectValidator; - -/** - * A general number value validator. - *

- * @author Roland Häder - */ -public abstract class BaseNumberValidator extends BaseObjectValidator { - - /** - * Serial number - */ - private static final long serialVersionUID = 25_481_878_590_589_321L; - - @Override - public void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, Boolean allowNull) throws ValidatorException { - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},requiredFields={3},allowNull={4} - CALLED!", context, component, value, Arrays.toString(requiredFields, allowNull))); //NOI18N - - // Pre-validate - super.preValidate(context, component, value, requiredFields, allowNull); - - // Get client id and init message + key - String clientId = component.getClientId(); - String requiredMessage = null; - - // So far all fine, no check if the field is fine - for (final String field : requiredFields) { - // Debug message - //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N - - // Is it the same? - if (clientId.endsWith(field)) { - // Init variables - Number number = null; - - // Compare value's type, supported: String, Long, Integer - if (value instanceof String) { - // Is a string, then try to parse it as Long, largest range - try { - number = Long.valueOf((String) value); - } catch (final NumberFormatException ex) { - // Cannot parse string to long - requiredMessage = MessageFormat.format("Field {0} cannot be parsed to long: {1}, exception:{2}", field, value, ex); - - // Abort processing here - break; - } - } else if ((value instanceof Integer) || (value instanceof Long) || (value instanceof Short)) { - // Is any used number - number = (Number) value; - } - - // Is the number below zero? Casting again largest range will not result in unexpected comparison - if ((!allowNull) && ((number instanceof Number) && ((Long) number < 0))) { - // Generate message - requiredMessage = MessageFormat.format("Value {0} for field {1} is below zero.", number, field); //NOI18N - - // Abort processing here - break; - } - } - } - - // Is facesMessage set? - if (null != requiredMessage) { - // Abort here - throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, requiredMessage, requiredMessage)); - } - } - -} diff --git a/src/org/mxchange/jcoreee/validator/string/BaseStringValidator.java b/src/org/mxchange/jcoreee/validator/string/BaseStringValidator.java deleted file mode 100644 index f49c44b..0000000 --- a/src/org/mxchange/jcoreee/validator/string/BaseStringValidator.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (C) 2016 - 2018 Free Software Foundation - * - * 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.jcoreee.validator.string; - -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 org.mxchange.jcoreee.validator.BaseObjectValidator; - -/** - * A general string validation class. You normally want to inherit from this - * class for many form fields, e.g. surname, street name, city name and such. - *

- * @author Roland Häder - */ -public abstract class BaseStringValidator extends BaseObjectValidator { - - /** - * Serial number - */ - private static final long serialVersionUID = 15_484_578_781_760_287L; - - @Override - protected void preValidate (final FacesContext context, final UIComponent component, final Object value, final String[] requiredFields, Boolean allowNull) throws ValidatorException { - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("preValidate: context={0},component={1},value={2},fields={3} - CALLED!", context, component, value, Arrays.toString(requiredFields))); //NOI18N - - // Pre-validate (example: on null) - super.preValidate(context, component, value, requiredFields, allowNull); - - // Get client id and init message + key - String clientId = component.getClientId(); - String requiredMessage = null; - - // So far all fine, no check if the field is fine - for (final String field : requiredFields) { - // Debug message - //this.getLogger().logDebug(MessageFormat.format("preValidate: field={0},clientId={1}", field, clientId)); //NOI18N - - // Is it the same? - if (clientId.endsWith(field)) { - // Compare value's type - if ((!allowNull) && (!(value instanceof String))) { - // Value is empty - requiredMessage = MessageFormat.format("Field {0} is not String: {1}", field, value); //NOI18N - } - - // Cast to string - String str = (String) value; - - // Is it empty? - if ((!allowNull) && (null == str)) { - // Empty strings are currently not accepted, may indicate flaw in invoker - requiredMessage = MessageFormat.format("Field {0} is null.", field); //NOI18N - } else if ((!allowNull) && (str.isEmpty())) { - // Empty strings are currently not accepted, may indicate flaw in invoker - requiredMessage = MessageFormat.format("Field {0} is empty.", field); //NOI18N - } - } - } - - // Debug message - //this.getLogger().logDebug(MessageFormat.format("preValidate: requiredMessage={0}", requiredMessage)); //NOI18N - - // Is it not null? - if (null != requiredMessage) { - // Then there was something wrong with it - throw new ValidatorException(new FacesMessage(requiredMessage)); - } - - // Trace message - //* NOISY-DEBUG: */ System.out.println("preValidate: EXIT!"); //NOI18N - } -} diff --git a/src/org/mxchange/jcoreutils/Comparables.java b/src/org/mxchange/jcoreutils/Comparables.java new file mode 100644 index 0000000..4c5e2ef --- /dev/null +++ b/src/org/mxchange/jcoreutils/Comparables.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2018 Free Software Foundation + * + * 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.jcoreutils; + +import java.io.Serializable; + +/** + * An utilities class for comparison of objects/entities + *

+ * @author Roland Häder + */ +public class Comparables implements Serializable { + + /** + * Serial number + */ + private static final long serialVersionUID = 19_286_457_671_390L; + + /** + * Checks all comparison values if they are not zero and returns proper + * value. These comparators should come from an implementation of + * Comparable.compareTo(). + *

+ * @param comparators An array of at least one comparator + *

+ * @return Comparison value + */ + public static int checkAll (final int[] comparators) { + // Is array empty? + if (comparators.length == 0) { + // Should not happen + throw new IllegalArgumentException("comparators is empty."); //NOI18N + } + + // Loop through all + for (int i = 0; i < comparators.length; i++) { + // Is it smaller or bigger? + if (comparators[i] < 0) { + return -1; + } else if (comparators[i] > 0) { + return 1; + } + } + + // Assume euqality + return 0; + } + + /** + * Utility classes should not have instances + */ + private Comparables () { + } + +} diff --git a/src/org/mxchange/jcoreutils/SafeNumberUtils.java b/src/org/mxchange/jcoreutils/SafeNumberUtils.java new file mode 100644 index 0000000..1adc599 --- /dev/null +++ b/src/org/mxchange/jcoreutils/SafeNumberUtils.java @@ -0,0 +1,146 @@ +/* + * Copyright (C) 2018 Free Software Foundation + * + * 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.jcoreutils; + +import java.io.Serializable; +import java.math.BigDecimal; +import java.util.Objects; + +/** + * Null-safe utilities class for numbers + *

+ * @author Roland Häder + */ +public class SafeNumberUtils implements Serializable { + + /** + * Serial number + */ + private static final long serialVersionUID = 153_746_766_826_120L; + + /** + * Compares both Integer instances null-safe following the rules of + * Comparable.compareTo(). + *

+ * @param integer1 Integer instance 1 + * @param integer2 Integer instance 2 + *

+ * @return Comparison value + */ + public static int compare (final Integer integer1, final Integer integer2) { + // Check instances + if (Objects.equals(integer1, integer2)) { + // Both objects are equal + return 0; + } else if (null == integer1) { + // First number is null (means is smaller than 0) + return -1; + } else if (null == integer2) { + // Second number is null (means is bigger than 0) + return 1; + } + + // Compare vaules null-safe + return Integer.compare(integer1, integer2); + } + + /** + * Compares both Long instances null-safe following the rules of + * Comparable.compareTo(). + *

+ * @param long1 Long instance 1 + * @param long2 Long instance 2 + *

+ * @return Comparison value + */ + public static int compare (final Long long1, final Long long2) { + // Check instances + if (Objects.equals(long1, long2)) { + // Both objects are equal + return 0; + } else if (null == long1) { + // First number is null (means is smaller than 0) + return -1; + } else if (null == long2) { + // Second number is null (means is smaller than 0) + return 1; + } + + // Compare vaules null-safe + return Long.compare(long1, long2); + } + + /** + * Compares both Short instances null-safe following the rules of + * Comparable.compareTo(). + *

+ * @param short1 Short instance 1 + * @param short2 Short instance 2 + *

+ * @return Comparison value + */ + public static int compare (final Short short1, final Short short2) { + // Check instances + if (Objects.equals(short1, short2)) { + // Both objects are equal + return 0; + } else if (null == short1) { + // First number is null (means is smaller than 0) + return -1; + } else if (null == short2) { + // Second number is null (means is smaller than 0) + return 1; + } + + // Compare vaules null-safe + return Short.compare(short1, short2); + } + + /** + * Compares both BigDecimal instances null-safe following the rules of + * Comparable.compareTo(). + *

+ * @param decimal1 BigDecimal instance 1 + * @param decimal2 BigDecimal instance 2 + *

+ * @return Comparison value + */ + public static int compare (final BigDecimal decimal1, final BigDecimal decimal2) { + // Check instances + if (Objects.equals(decimal1, decimal2)) { + // Both objects are equal + return 0; + } else if (null == decimal1) { + // First number is null (means is smaller than 0) + return -1; + } else if (null == decimal2) { + // Second number is null (means is smaller than 0) + return 1; + } + + // Compare vaules null-safe + return decimal1.compareTo(decimal2); + } + + /** + * Utilities classes should not have instances + */ + private SafeNumberUtils () { + // Private constructor + } + +}