/build/
/dist/
*.properties
+/data/*.*
*/
package org.mxchange.pizzaapplication;
+import java.text.MessageFormat;
+import javax.servlet.ServletContext;
import org.mxchange.jcore.BaseFrameworkSystem;
/**
* @author Roland Haeder
*/
public class BasePizzaServiceSystem extends BaseFrameworkSystem {
-
+ /**
+ * Initializes properties from given servlet configuration
+ * @param context Servlet context instance
+ */
+ protected void initProperties (final ServletContext context) {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("context={0} - CALLED!", context));
+
+ // We need some properties that needs to be set
+ for (final String name : this.getPropertyNames()) {
+ // Debug log
+ this.getLogger().debug(MessageFormat.format("name={0}", name));
+
+ // Get value
+ String value = context.getInitParameter(name).trim();
+
+ // Debug log
+ this.getLogger().debug(MessageFormat.format("{0}={1}", name, value));
+
+ // Set property
+ this.setProperty(name, value);
+ }
+
+ // Trace message
+ this.getLogger().trace("EXIT!");
+ }
}
import org.mxchange.pizzaapplication.BasePizzaServiceSystem;
import org.mxchange.pizzaapplication.customer.Customer;
import org.mxchange.pizzaapplication.customer.PizzaServiceCustomer;
-import org.mxchange.pizzaapplication.product.PizzaProduct;
+import org.mxchange.pizzaapplication.database.frontend.product.PizzaProductDatabaseFrontend;
+import org.mxchange.pizzaapplication.database.frontend.product.ProductFrontend;
import org.mxchange.pizzaapplication.product.Product;
/**
* Some singleton getter for this instance. If the instance is not set in
* given application, it will be created.
*
- * @param application Servlet context
+ * @param context Servlet context
* @return This instance
* @throws javax.servlet.ServletException If object is not set correctly
*/
- public static final PizzaApplication getInstance (final ServletContext application) throws ServletException {
+ public static final PizzaApplication getInstance (final ServletContext context) throws ServletException {
// Check application instance
- if (application == null) {
+ if (context == null) {
// Not set
throw new NullPointerException("application is null"); //NOI18N
}
PizzaApplication instance = null;
// Get instance from servlet application (aka. "application scope")
- Object object = application.getAttribute("app"); //NOI18N
+ Object object = context.getAttribute("app"); //NOI18N
// Is it set?
if (object instanceof PizzaApplication) {
throw new ServletException("app is not set correctly"); //NOI18N
} else {
// "service" is null, so initialize it
- instance = new PizzaServiceApplication();
+ instance = new PizzaServiceApplication(context);
// And set it here
- application.setAttribute("app", instance); //NOI18N
+ context.setAttribute("app", instance); //NOI18N
}
// Trace message
private PizzaServiceApplication () {
// Init products instance
this.products = new TreeMap<>();
+ }
- // Init bundle
- this.initBundle();
+ /**
+ * Constructor with servet configuration
+ * @param context Servlet context
+ */
+ private PizzaServiceApplication (final ServletContext context) {
+ // Call other constructor first
+ this();
- // Fill products list
+ // Initialize properties from config
+ this.initProperties(context);
+
+ // Load available products
this.fillProductsList();
}
/**
* Adds given product to list or throws an exception if name is already found
*
- * @param name Internal name of product
- * @param title Product's title
- * @param price Price
+ * @param product Product instance to add
*/
- private void addProduct (final String name, final String title, final float price) {
+ private void addProduct (final Product product) {
// Trace message
- this.getLogger().trace(MessageFormat.format("name={0},title={1},price={2} - CALLED!", name, title, price)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
+
+ // Get all data from it
+ String name = product.getName();
// Is the name already used?
if (this.isProductNameUsed(name)) {
throw new IllegalArgumentException(MessageFormat.format("product {0} is already used.", name)); //NOI18N
}
- // Instance product
- Product product = new PizzaProduct(name, title, price);
-
// Debug message
this.getLogger().debug(MessageFormat.format("Adding product={0} ...", product)); //NOI18N
// Trace message
this.getLogger().trace("CALLED!"); //NOI18N
- // Add products
- this.addProduct("italia", "Pizza Italia", 5.50f); //NOI18N
- this.addProduct("diablo", "Pizza Diablo", 7.80f); //NOI18N
- this.addProduct("bolognese", "Spagetti Bolognese", 11.95f); //NOI18N
+ // Get a product frontend
+ ProductFrontend frontend = new PizzaProductDatabaseFrontend();
+
+ // Get iterator
+ Iterator<Product> iterator = frontend.getProducts();
+
+ // Get all products
+ while (iterator.hasNext()) {
+ // Get it
+ Product product = iterator.next();
+
+ // Add it
+ this.addProduct(product);
+ }
// Trace message
this.getLogger().trace("EXIT!"); //NOI18N
* Application starter
*/
private void start () {
+ // Init bundle
+ this.initBundle();
+
+ // Init properties
+ this.initProperties();
+
+ // Fill products list
+ this.fillProductsList();
+
// "Walk" over all products
for (final Product product : this.getProducts()) {
// Output data
--- /dev/null
+/*
+ * 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.pizzaapplication.database.frontend.product;
+
+import java.sql.SQLException;
+import java.text.MessageFormat;
+import java.util.Iterator;
+import org.mxchange.jcore.criteria.searchable.SearchCriteria;
+import org.mxchange.jcore.criteria.searchable.SearchableCritera;
+import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend;
+import org.mxchange.jcore.database.result.Result;
+import org.mxchange.jcore.database.storage.Storeable;
+import org.mxchange.jcore.exceptions.BadTokenException;
+import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
+import org.mxchange.pizzaapplication.database.product.PizzaProductDatabaseConstants;
+import org.mxchange.pizzaapplication.product.Product;
+
+/**
+ * Stores and retrieves Contact instances
+ *
+ * @author Roland Haeder
+ */
+public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implements ProductFrontend {
+
+ /**
+ * Default constrcutor
+ */
+ public PizzaProductDatabaseFrontend () {
+ // Trace message
+ this.getLogger().trace("CALLED!"); //NOI18N
+
+ // Set "table" name
+ this.setTableName("products"); //NOI18N
+
+ try {
+ // Initalize backend
+ this.initBackend();
+ } catch (final UnsupportedDatabaseBackendException | SQLException ex) {
+ // Abort program
+ this.abortProgramWithException(ex);
+ }
+ }
+
+ /**
+ * Shuts down the database layer
+ */
+ @Override
+ public void doShutdown () {
+ // Trace message
+ this.getLogger().trace("CALLED!"); //NOI18N
+
+ // Shutdown backend
+ this.getBackend().doShutdown();
+
+ // Trace message
+ this.getLogger().trace("EXIT!"); //NOI18N
+ }
+
+ /**
+ * An iterator on all products
+ *
+ * @return Iterator on all products
+ */
+ @Override
+ @SuppressWarnings ("unchecked")
+ public Iterator<Product> getProducts () {
+ // Trace message
+ this.getLogger().trace("CALLED!"); //NOI18N
+
+ // Instance search criteria
+ SearchableCritera critera = new SearchCriteria();
+
+ // Add criteria
+ critera.addCriteria(PizzaProductDatabaseConstants.COLUMN_AVAILABLE, true);
+
+ // Run the query
+ Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(critera);
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
+
+ // Get iterator
+ Iterator<?> iterator = result.iterator();
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("iterator={0} - EXIT!", iterator)); //NOI18N
+
+ // Get iterator and return it
+ return (Iterator<Product>) iterator;
+ }
+
+ /**
+ * Parses given line from database backend into a Storeable instance. Please
+ * note that not all backends need this.
+ *
+ * @param line Line from database backend
+ * @return A Storeable instance
+ */
+ @Override
+ public Storeable parseLineToStoreable (final String line) throws BadTokenException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("line={0} - CALLED!", line)); //NOI18N
+
+ // Call inner method
+ Product product = this.parseLineToProduct(line);
+
+ // Debug message
+ this.getLogger().trace(MessageFormat.format("product={0} - EXIT!", product)); //NOI18N
+
+ // Return it
+ return product;
+ }
+
+ /**
+ * Parses given line to a Product instance
+ *
+ * @param line
+ * @return A Product instance from given line
+ */
+ private Product parseLineToProduct (final String line) {
+ throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: line={0}", line)); //NOI18N
+ }
+}
--- /dev/null
+/*
+ * Copyright (C) 2015 Roland Häder
+ *
+ * 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.pizzaapplication.database.frontend.product;
+
+import java.util.Iterator;
+import org.mxchange.jcore.database.frontend.DatabaseFrontend;
+import org.mxchange.pizzaapplication.product.Product;
+
+/**
+ * An interface for product database frontends
+ *
+ * @author Roland Häder
+ */
+public interface ProductFrontend extends DatabaseFrontend {
+
+ /**
+ * An iterator on all products
+ *
+ * @return Iterator on all products
+ */
+ public Iterator<Product> getProducts ();
+}
--- /dev/null
+/*
+ * 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.pizzaapplication.database.product;
+
+/**
+ *
+ * @author Roland Haeder
+ */
+public final class PizzaProductDatabaseConstants {
+ /**
+ * Column name for "available"
+ */
+ public static final String COLUMN_AVAILABLE = "available";
+
+ /**
+ * No instance from this class
+ */
+ private PizzaProductDatabaseConstants () {
+ }
+}
--- /dev/null
+/*
+ * 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.pizzaapplication.filter.http;
+
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import org.mxchange.jcore.BaseFrameworkSystem;
+
+/**
+ * A general HTTP servlet filter class
+ * @author Roland Haeder
+ */
+public class BaseServletFilter extends BaseFrameworkSystem {
+ /**
+ * Configuration instance
+ */
+ private FilterConfig config;
+
+ /**
+ * Configuration instance
+ * @return the config
+ */
+ protected FilterConfig getConfig () {
+ return this.config;
+ }
+
+ /**
+ * Configuration instance
+ * @param config the config to set
+ */
+ protected void setConfig (final FilterConfig config) {
+ this.config = config;
+ }
+
+ /**
+ * Destroys this filter
+ */
+ public void destroy () {
+ // Unset config instance
+ this.setConfig(null);
+ }
+
+ /**
+ * Initializes this filter
+ *
+ * @param filterConfig Filter configuration
+ * @throws ServletException
+ */
+ public void init (final FilterConfig filterConfig) throws ServletException {
+ // Set config instance
+ this.setConfig(filterConfig);
+ }
+}
--- /dev/null
+/*
+ * 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.pizzaapplication.filter.http.utf8;
+
+import java.io.IOException;
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import org.mxchange.pizzaapplication.filter.http.BaseServletFilter;
+
+/**
+ * A HTTP filter for setting UTF-8 character encoding.
+ *
+ * @author Roland Haeder
+ */
+public class Utf8HttpFilter extends BaseServletFilter implements Filter {
+ /**
+ * Filter to set UTF-8 encoding
+ *
+ * @param request ServletRequest instance
+ * @param response ServletResponse instance
+ * @param chain FilterChain instance
+ * @throws IOException
+ * @throws ServletException
+ */
+ @Override
+ public void doFilter (final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
+ // Call super method
+ chain.doFilter(request, response);
+
+ // Set response/request both to UTF-8
+ request.setCharacterEncoding("UTF-8");
+ response.setCharacterEncoding("UTF-8");
+ }
+}
*/
package org.mxchange.pizzaapplication.product;
-import org.mxchange.jcore.FrameworkInterface;
+import org.mxchange.jcore.database.storage.Storeable;
/**
*
* @author Roland Haeder
*/
-public interface Product extends FrameworkInterface {
+public interface Product extends Storeable {
/**
* Getter for name, suitable for form fields.
*
+++ /dev/null
-/*
- * 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.pizzaapplicationk.database.frontend.contact;
-
-import java.sql.SQLException;
-import java.text.MessageFormat;
-import org.mxchange.jcore.database.frontend.BaseDatabaseFrontend;
-import org.mxchange.jcore.database.storage.Storeable;
-import org.mxchange.jcore.exceptions.BadTokenException;
-import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
-import org.mxchange.pizzaapplication.product.Product;
-
-/**
- * Stores and retrieves Contact instances
- *
- * @author Roland Haeder
- */
-public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implements ProductFrontend {
-
- /**
- * Basic constrcutor
- */
- protected PizzaProductDatabaseFrontend () {
- // Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
-
- // Set "table" name
- this.setTableName("contacts"); //NOI18N
-
- try {
- // Initalize backend
- this.initBackend();
- } catch (final UnsupportedDatabaseBackendException | SQLException ex) {
- // Abort program
- this.abortProgramWithException(ex);
- }
- }
-
- /**
- * Shuts down the database layer
- */
- @Override
- public void doShutdown () {
- // Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
-
- // Shutdown backend
- this.getBackend().doShutdown();
-
- // Trace message
- this.getLogger().trace("EXIT!"); //NOI18N
- }
-
- /**
- * Parses given line from database backend into a Storeable instance. Please
- * note that not all backends need this.
- *
- * @param line Line from database backend
- * @return A Storeable instance
- */
- @Override
- public Storeable parseLineToStoreable (final String line) throws BadTokenException {
- // Trace message
- this.getLogger().trace(MessageFormat.format("line={0} - CALLED!", line)); //NOI18N
-
- // Call inner method
- Product product = this.parseLineToProduct(line);
-
- // Debug message
- this.getLogger().debug(MessageFormat.format("product={0}", product));
-
- // Return it
- return (Storeable) product;
- }
-
- /**
- * Parses given line to a Product instance
- * @param line
- * @return A Product instance from given line
- */
- private Product parseLineToProduct (final String line) {
- throw new UnsupportedOperationException("Not supported yet.");
- }
-}
+++ /dev/null
-/*
- * Copyright (C) 2015 Roland Häder
- *
- * 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.pizzaapplicationk.database.frontend.contact;
-
-import org.mxchange.jcore.database.frontend.DatabaseFrontend;
-
-/**
- * An interface for product database frontends
- *
- * @author Roland Häder
- */
-public interface ProductFrontend extends DatabaseFrontend {
-}
<description>Keep a copy of the generated servlet class' java code.</description>
</property>
</jsp-config>
- <parameter-encoding default-charset="utf-8" />
</glassfish-web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
+ <context-param>
+ <description>Full-qualified back class name, must implement DatabaseBackend interface</description>
+ <param-name>database.backend.class</param-name>
+ <param-value>org.mxchange.jcore.database.backend.base64.Base64CsvDatabaseBackend</param-value>
+ </context-param>
+ <context-param>
+ <description>Login name for MySQL database, mostly not root</description>
+ <param-name>database.mysql.login</param-name>
+ <param-value>root</param-value>
+ </context-param>
+ <context-param>
+ <description>Password for above login, an empty password can be archived by setting a space</description>
+ <param-name>database.mysql.password</param-name>
+ <param-value>root</param-value>
+ </context-param>
+ <context-param>
+ <description>Hostname or IP address for MySQL server</description>
+ <param-name>database.mysql.host</param-name>
+ <param-value>localhost</param-value>
+ </context-param>
+ <context-param>
+ <description>Name of MySQL catalog</description>
+ <param-name>database.mysql.dbname</param-name>
+ <param-value>test</param-value>
+ </context-param>
+ <filter>
+ <description>A filter for setting character encoding to UTF-8</description>
+ <filter-name>Utf8HttpFilter</filter-name>
+ <filter-class>org.mxchange.pizzaapplication.filter.http.utf8.Utf8HttpFilter</filter-class>
+ </filter>
+ <filter-mapping>
+ <filter-name>Utf8HttpFilter</filter-name>
+ <url-pattern>/*</url-pattern>
+ </filter-mapping>
<session-config>
<session-timeout>
30
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
+ <error-page>
+ <exception-type>Exception</exception-type>
+ <location>/errorHandler.jsp</location>
+ </error-page>
</web-app>