]> git.mxchange.org Git - pizzaservice-war.git/blobdiff - src/java/org/mxchange/pizzaapplication/beans/BasePizzaController.java
Please rename:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / BasePizzaController.java
index dd426872868bfed6996d53e3af81ea3072b2b520..88e5c23e3db62e446c2738225f651e6524f1aac5 100644 (file)
@@ -17,6 +17,8 @@
 package org.mxchange.pizzaapplication.beans;
 
 import java.io.Serializable;
+import java.text.MessageFormat;
+import javax.faces.application.FacesMessage;
 import javax.faces.context.FacesContext;
 
 /**
@@ -31,6 +33,47 @@ public abstract class BasePizzaController implements Serializable {
         */
        private static final long serialVersionUID = 50_837_597_127_567_140L;
 
+       /**
+        * Returns given property key or throws an exception if not found.
+        * <p>
+        * @param parameterKey Property key
+        * <p>
+        * @return Property value
+        * <p>
+        * @throws NullPointerException If given key is not found
+        * @throws NumberFormatException If no number is given in context parameter
+        */
+       protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
+               // Get context parameter
+               Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
+
+               // Return it
+               return contextValue;
+       }
+
+       /**
+        * Returns given property key or throws an exception if not found.
+        * <p>
+        * @param parameterKey Property key
+        * <p>
+        * @return Property value
+        * <p>
+        * @throws NullPointerException If given key is not found
+        */
+       protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
+               // Get context parameter
+               String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
+
+               // Is it null?
+               if (null == contextValue) {
+                       // Throw NPE
+                       throw new NullPointerException("parameterKey=" + parameterKey + " is not set.");
+               }
+
+               // Return it
+               return contextValue;
+       }
+
        /**
         * Checks whether debug mode is enabled for given controller
         * <p>
@@ -58,4 +101,30 @@ public abstract class BasePizzaController implements Serializable {
                return isEnabled;
        }
 
+       /**
+        * Shows a faces message for given causing exception. The message from the
+        * exception is being inserted into the message.
+        * <p>
+        * @param clientId Client id to send message to
+        * @param cause    Causing exception
+        */
+       protected void showFacesMessage (final String clientId, final Throwable cause) {
+               // Trace message
+               System.out.println(MessageFormat.format("showFacesMessage: clientId={0},cause={1} - CALLED!", clientId, cause));
+
+               // Get context and add message
+               this.showFacesMessage(clientId, cause.getMessage());
+       }
+
+       /**
+        * Shows a faces message with given message.
+        * <p>
+        * @param clientId Client id to send message to
+        * @param message Causing exception
+        */
+       protected void showFacesMessage (final String clientId, final String message) {
+               // Get context and add message
+               FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));
+       }
+
 }