From: Roland Haeder <roland@mxchange.org>
Date: Tue, 11 Aug 2015 12:26:30 +0000 (+0200)
Subject: Added product database frontend class and interface
X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=81ba2433d422f0873800ebfe65d64def6e30ad56;p=pizzaservice-war.git

Added product database frontend class and interface
Signed-off-by:Roland Häder <roland@mxchange.org>
---

diff --git a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java
index 4ce28a5b..b3199752 100644
--- a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java
+++ b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java
@@ -107,10 +107,10 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 	private PizzaServiceApplication () {
 		// Init products instance
 		this.products = new TreeMap<>();
-		
+
 		// Init bundle
 		this.initBundle();
-		
+
 		// Fill products list
 		this.fillProductsList();
 	}
@@ -126,7 +126,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 	public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) {
 		// Trace message
 		this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
-		
+
 		// Is product and session set?
 		if (request == null) {
 			// Not set
@@ -138,27 +138,27 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 
 		// Init total price
 		int totalAmount = 0;
-		
+
 		// "Walk" over all products
 		for (final Product product : this.getProducts()) {
 			// Is this choosen?
 			if (this.isProductChoosen(product, request, session)) {
 				// Then add ordered amount
 				this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getName())); //NOI18N
-				
+
 				// Getting amount
 				String amount = this.getAmountFromSession(product, session);
-				
+
 				// Add it up
 				this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
 				totalAmount += Integer.valueOf(amount);
 			}
 			this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getName(), totalAmount)); //NOI18N
 		}
-		
+
 		// Trace message
 		this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
-		
+
 		// Return total price
 		return totalAmount;
 	}
@@ -174,7 +174,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 	public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) {
 		// Trace message
 		this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
-		
+
 		// Is product and session set?
 		if (request == null) {
 			// Not set
@@ -183,10 +183,10 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 			// Not set
 			throw new NullPointerException("session is null"); //NOI18N
 		}
-		
+
 		// Init total price
 		float totalPrice = 0.00f;
-		
+
 		// "Walk" over all products
 		for (final Product product : this.getProducts()) {
 			// Is this choosen?
@@ -197,24 +197,24 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 			}
 			this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getName(), totalPrice)); //NOI18N
 		}
-		
+
 		// Trace message
 		this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
-		
+
 		// Return total price
 		return totalPrice;
 	}
-	
+
 	@Override
 	public void doBootstrap () {
 		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
 	}
-	
+
 	@Override
 	public void doMainLoop () {
 		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
 	}
-	
+
 	@Override
 	public void doShutdown () {
 		throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
@@ -272,7 +272,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 	public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
 		// Trace message
 		this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
-		
+
 		// Is product and session set?
 		if (product == null) {
 			// Not set
@@ -284,12 +284,12 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 			// Not set
 			throw new NullPointerException("session is null"); //NOI18N
 		}
-		
+
 		// First let's check if the product is choosen
 		if (this.isProductChoosen(product, request, session)) {
 			// Trace message
 			this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
-			
+
 			// Is choosen
 			return "checked=\"checked\""; //NOI18N
 		} else {
@@ -321,10 +321,10 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 			// Not set
 			throw new NullPointerException("session is null"); //NOI18N
 		}
-		
+
 		// Get attribute
 		Object object = this.getValueFromSession(product, session, HTTP_PARAM_CHOOSE);
-		
+
 		// Is the object null?
 		if (object == null) {
 			// Not found
@@ -359,18 +359,18 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 			// Not set
 			throw new NullPointerException("session is null"); //NOI18N
 		}
-		
+
 		// Is something selected?
 		if (this.calculateTotalAmount(request, session) > 0) {
 			// Trace message
 			this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
-			
+
 			// Something has been choosen
 			return ""; //NOI18N
 		} else {
 			// Trace message
 			this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
-			
+
 			// Nothing choosen yet
 			return "disabled=\"disabled\""; //NOI18N
 		}
@@ -443,7 +443,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 	public Object getPrintableValeFromSession (final HttpSession session, final String key) {
 		// Trace message
 		this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
-		
+
 		// Are both parameter not null?
 		if (session == null) {
 			// Abort here
@@ -452,13 +452,13 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 			// Abort here
 			throw new NullPointerException("key is null"); //NOI18N
 		}
-		
+
 		// Now get it
 		Object value = this.getValueFromSession(session, key);
-		
+
 		// Debug message
 		this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
-		
+
 		// Trace message
 		this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
 
@@ -563,7 +563,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 	public Product[] getUnmarkedProducts (final HttpSession session) {
 		// Init array
 		Product[] array = this.getProducts();
-		
+
 		// Unmark are all as ordered
 		for (final Product product : array) {
 			this.unmarkProductAsOrdered(product, session);
@@ -597,10 +597,10 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 			// Not set
 			throw new NullPointerException("session is null"); //NOI18N
 		}
-		
+
 		// Init variabke
 		Object object;
-		
+
 		// Check request method
 		if (!"POST".equals(request.getMethod())) { //NOI18N
 			// Not POST, so get from session
@@ -611,10 +611,10 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 			this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getName())); //NOI18N
 			return "0"; //NOI18N
 		}
-		
+
 		// Get attribute from request
 		object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getName()));
-		
+
 		// Is it set?
 		if (object instanceof String) {
 			// Try to parse it to integer
@@ -711,7 +711,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 			// Not set
 			throw new NullPointerException("session is null"); //NOI18N
 		}
-		
+
 		// Mark it as ordered by setting flag
 		this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getName())); //NOI18N
 		this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, "1"); //NOI18N
@@ -739,7 +739,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 			// Not set
 			throw new NullPointerException("session is null"); //NOI18N
 		}
-		
+
 		// Mark it as ordered by setting flag
 		this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getName())); //NOI18N
 		this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
@@ -835,13 +835,13 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 	private void addProduct (final String name, final String title, final float price) {
 		// Trace message
 		this.getLogger().trace(MessageFormat.format("name={0},title={1},price={2} - CALLED!", name, title, price)); //NOI18N
-		
+
 		// Is the name already used?
 		if (this.isProductNameUsed(name)) {
 			// Something went wrong
 			throw new IllegalArgumentException(MessageFormat.format("product {0} is already used.", name)); //NOI18N
 		}
-		
+
 		// Instance product
 		Product product = new PizzaProduct(name, title, price);
 		
@@ -865,15 +865,15 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 	private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
 		// Trace message
 		this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
-		
+
 		// Clear in session
 		this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getName(), parameter)); //NOI18N
 		this.setValueInSession(product, session, parameter, null);
-		
+
 		// Trace message
 		this.getLogger().trace("EXIT!"); //NOI18N
 	}
-	
+
 	/**
 	 * Fills products list
 	 * @todo Very hard-coded stuff ...
@@ -881,7 +881,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 	private void fillProductsList () {
 		// 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
@@ -925,10 +925,10 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 	private Object getValueFromSession (final HttpSession session, final String key) {
 		// Trace message
 		this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
-		
+
 		// Init value
 		Object value = null;
-		
+
 		// Get it synchronized from session
 		synchronized (session) {
 			value = session.getAttribute(key);
@@ -964,10 +964,10 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 			// Not set
 			throw new NullPointerException("session is null"); //NOI18N
 		}
-		
+
 		// Init variabke
 		Object object;
-		
+
 		// Check request method
 		if (!"POST".equals(request.getMethod())) { //NOI18N
 			// Not POST, so get from session
@@ -986,18 +986,18 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 		// Get reqzest element
 		object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getName()));
 		this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getName(), object)); //NOI18N
-		
+
 		// Is it null?
 		if (object == null) {
 			// Unset session
 			this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getName())); //NOI18N
 			this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
 			this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
-			
+
 			// Return empty string
 			return ""; //NOI18N
 		}
-		
+
 		// Then set it in session
 		this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
 
@@ -1005,7 +1005,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 		this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getName(), object)); //NOI18N
 		return (String) object;
 	}
-	
+
 	/**
 	 * Checks whether given product is already used
 	 *
@@ -1019,7 +1019,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 		// Is it found?
 		return this.products.containsKey(name);
 	}
-	
+
 	/**
 	 * Checks if the product ordered?
 	 *
@@ -1030,7 +1030,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 	private boolean isProductOrdered(final Product product, final HttpSession session) {
 		// Trace message
 		this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
-		
+
 		// Get session
 		Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
 		this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getName(), isOrdered)); //NOI18N
@@ -1068,10 +1068,10 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 			// Output data
 			this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getName(), product.getTitle(), product.getPrice())); //NOI18N
 		}
-		
+
 		// Generate fake Customer instance
 		Customer customer = new PizzaServiceCustomer();
-		
+
 		/*
 		 * Need a least a gender ... :( See, that is why I don't like default
 		 * constructors, you can easily miss something important and bam! You
@@ -1079,10 +1079,10 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 		 * requires all required instances that needs to be set to get a
 		 * consitent object back.
 		 */
-		
+
 		// Gender is MALE now
 		customer.setGender(Gender.MALE);
-		
+
 		// Get iterator on all its fields
 		Iterator<Map.Entry<Field, Object>> it = customer.iterator();
 
@@ -1092,5 +1092,4 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 			this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue())); //NOI18N
 		}
 	}
-
 }
diff --git a/src/java/org/mxchange/pizzaapplicationk/database/frontend/contact/PizzaProductDatabaseFrontend.java b/src/java/org/mxchange/pizzaapplicationk/database/frontend/contact/PizzaProductDatabaseFrontend.java
new file mode 100644
index 00000000..97b19cf0
--- /dev/null
+++ b/src/java/org/mxchange/pizzaapplicationk/database/frontend/contact/PizzaProductDatabaseFrontend.java
@@ -0,0 +1,98 @@
+/*
+ * 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.");
+	}
+}
diff --git a/src/java/org/mxchange/pizzaapplicationk/database/frontend/contact/ProductFrontend.java b/src/java/org/mxchange/pizzaapplicationk/database/frontend/contact/ProductFrontend.java
new file mode 100644
index 00000000..a04499ec
--- /dev/null
+++ b/src/java/org/mxchange/pizzaapplicationk/database/frontend/contact/ProductFrontend.java
@@ -0,0 +1,27 @@
+/*
+ * 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 {
+}