private PizzaServiceApplication () {
// Init products instance
this.products = new TreeMap<>();
-
+
// Init bundle
this.initBundle();
-
+
// Fill products list
this.fillProductsList();
}
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
// 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;
}
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
// 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?
}
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.
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
// 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 {
// 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
// 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
}
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
// 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
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);
// 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
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
// 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
// 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
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);
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 ...
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
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);
// 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
// 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);
this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getName(), object)); //NOI18N
return (String) object;
}
-
+
/**
* Checks whether given product is already used
*
// Is it found?
return this.products.containsKey(name);
}
-
+
/**
* Checks if the product ordered?
*
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
// 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
* 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();
this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue())); //NOI18N
}
}
-
}
--- /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.");
+ }
+}