import java.text.MessageFormat;
import java.util.Iterator;
import java.util.Map;
-import java.util.SortedMap;
-import java.util.TreeMap;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.mxchange.jcore.exceptions.BadTokenException;
import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
import org.mxchange.pizzaapplication.BasePizzaServiceSystem;
+import org.mxchange.pizzaapplication.category.Category;
import org.mxchange.pizzaapplication.customer.Customer;
import org.mxchange.pizzaapplication.customer.PizzaServiceCustomer;
import org.mxchange.pizzaapplication.database.frontend.product.PizzaProductDatabaseFrontend;
*/
public static final String MAIN_TITLE = "Pizza-Service";
+ /**
+ * Frontend for products
+ */
+ private ProductFrontend productFrontend;
+
/**
* Some singleton getter for this instance. If the instance is not set in
* given application, it will be created.
new PizzaServiceApplication().start();
}
- /**
- * Product list
- */
- private final SortedMap<String, Product> products;
-
- /**
- * Private constructor
- */
- private PizzaServiceApplication () {
- // Init products instance
- this.products = new TreeMap<>();
- }
-
/**
* Constructor with servet configuration
*
* @param context Servlet context
*/
private PizzaServiceApplication (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
- // Call other constructor first
- this();
-
// Temporary initialize default bundle
// @TODO The JSF may have better internatialization support
this.initBundle();
// Initialize properties from config
this.initProperties(context);
- // Load available products
- this.fillProductsList();
+ // Init database frontends
+ this.initDatabaseFrontends();
+ }
+
+ /**
+ * Default constructor
+ */
+ private PizzaServiceApplication () {
}
/**
// Is this choosen?
if (this.isProductChoosen(product, request, session)) {
// Then add ordered amount
- this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getName())); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getId())); //NOI18N
// Getting amount
String amount = this.getAmountFromSession(product, session);
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
+ this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getId(), totalAmount)); //NOI18N
}
// Trace message
// Is this choosen?
if (this.isProductChoosen(product, request, session)) {
// Then add product's total price
- this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getName())); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getId())); //NOI18N
totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
}
- this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getName(), totalPrice)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getId(), totalPrice)); //NOI18N
}
// Trace message
// Is the object null?
if (object == null) {
// Not found
- this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getName())); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getId())); //NOI18N
return ""; //NOI18N
}
}
// Get element
- this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getName(), request, session)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
String choosen = this.handleChooseFromRequestSession(product, request, session);
- this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
// Must not be null
assert(choosen instanceof String): "choosen is null"; //NOI18N
// Get amount
String amount = this.handleAmountFromRequestSession(product, request, session);
- this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getName(), amount)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
// Must not be null
assert(amount instanceof String): "amount is null"; //NOI18N
/**
* Some "getter" for a an array of all products
*
- * @return Unmarked products
+ * @return All products
*/
@Override
- public Product[] getProducts () {
- return this.products.values().toArray(new Product[this.products.size()]);
+ public Iterable<Product> getProducts () {
+ throw new UnsupportedOperationException("Needs refacturing ...");
+ }
+
+ /**
+ * Some "getter" for a an array of all categories
+ *
+ * @return All categories
+ */
+ @Override
+ public Iterable<Category> getCategories () {
+ throw new UnsupportedOperationException("Needs refacturing ...");
}
/**
}
// Get choosen
- this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getName(), request, session)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
String choosen = this.handleChooseFromRequestSession(product, request, session);
- this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
// Must not be null
assert(choosen instanceof String): "choosen is null"; //NOI18N
// Is it set?
if (choosen.isEmpty()) {
// Is empty
- this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getName(), choosen)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getId(), choosen)); //NOI18N
return 0.00f;
}
// Get amount
String amount = this.handleAmountFromRequestSession(product, request, session);
- this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getName(), amount)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
// Must not be null
assert(amount instanceof String): "amount is null"; //NOI18N
// Is it empty?
if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
// Is empty
- this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getName(), amount)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getId(), amount)); //NOI18N
return 0.00f;
}
float price = (product.getPrice() * value);
// Trace message
- this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getName(), price)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getId(), price)); //NOI18N
// Then multiply it with price
return price;
}
- /**
- * Gets an array of products from product iterator and unmarks them as ordered
- *
- * @param session HttpSession instance
- * @return Unmarked products
- */
- @Override
- 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);
- }
-
- // Return finished array
- return array;
- }
-
/**
* Handler for amount from request or session
*
} else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
// Not choosen
this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
- this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getName())); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getId())); //NOI18N
return "0"; //NOI18N
}
// Get attribute from request
- object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getName()));
+ object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getId()));
// Is it set?
if (object instanceof String) {
}
// Get choosen
- this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getName(), request, session)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
String choosen = this.handleChooseFromRequestSession(product, request, session);
- this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
// Must not be null
assert(choosen instanceof String): "choosen is null"; //NOI18N
}
// Mark it as ordered by setting flag
- this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getName())); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getId())); //NOI18N
this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, "1"); //NOI18N
// Trace message
}
// Mark it as ordered by setting flag
- this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getName())); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getId())); //NOI18N
this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
// Trace message
}
// Mark it as ordered by setting flag
- this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getName())); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getId())); //NOI18N
this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
// Trace message
}
// Mark it as ordered by setting flag
- this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getName())); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getId())); //NOI18N
this.clearSessionAttribute(product, session, SESSION_ORDERED);
// Trace message
this.getLogger().trace("EXIT!"); //NOI18N
}
- /**
- * Adds given product to list or throws an exception if name is already found
- *
- * @param product Product instance to add
- */
- private void addProduct (final Product product) {
- // Trace message
- 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)) {
- // Something went wrong
- throw new IllegalArgumentException(MessageFormat.format("product {0} is already used.", name)); //NOI18N
- }
-
- // Debug message
- this.getLogger().debug(MessageFormat.format("Adding product={0} ...", product)); //NOI18N
-
- // Add it
- this.products.put(product.getName(), product);
-
- // Trace message
- this.getLogger().trace("EXIT!"); //NOI18N
- }
-
/**
* Clears given parameter for product in session
*
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.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getId(), 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 () throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
- // Trace message
- this.getLogger().trace("CALLED!"); //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
- }
-
/**
* Some getter for value from session
*
this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
// Init variable
- Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getName()));
+ Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getId()));
- this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getName(), attribute, value)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getId(), attribute, value)); //NOI18N
// Trace message
this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
// Check request method
if (!"POST".equals(request.getMethod())) { //NOI18N
// Not POST, so get from session
- this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getName(), session));
+ this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session));
return this.getChooseFromSession(product, session);
} else if (this.isProductOrdered(product, session)) {
// Product is ordered
- this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getName(), session));
+ this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session));
return this.getChooseFromSession(product, session);
} else if (!this.getChooseFromSession(product, session).isEmpty()) {
// Found in session
- this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getName(), session));
+ this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session));
return this.getChooseFromSession(product, 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
+ object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getId()));
+ this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getId(), object)); //NOI18N
// Is it null?
if (object == null) {
// Unset session
- this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getName())); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getId())); //NOI18N
this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
// Cast to string and return it
- this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getName(), object)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getId(), object)); //NOI18N
return (String) object;
}
/**
- * Checks whether given product is already used
- *
- * @param name Name of product
- * @return Whether the given product's name is already used
+ * Initializes database frontends.
*/
- private boolean isProductNameUsed(final String name) {
- // Trace message
- this.getLogger().trace(MessageFormat.format("name={0} - CALLED!", name)); //NOI18N
-
- // Is it found?
- return this.products.containsKey(name);
+ private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException {
+ this.productFrontend = new PizzaProductDatabaseFrontend();
}
/**
* Checks if the product ordered?
*
- * @param product
- * @param session
+ * @param product Product instance
+ * @param session HttpSession instance
* @return
*/
private boolean isProductOrdered(final Product product, final HttpSession session) {
// Get session
Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
- this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getName(), isOrdered)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getId(), isOrdered)); //NOI18N
// Return result
return ("true".equals(isOrdered)); //NOI18N
this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
// Set it synced
- this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getName(), keyPart)); //NOI18N
- this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getName()), value);
+ this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getId(), keyPart)); //NOI18N
+ this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getId()), value);
// Trace message
this.getLogger().trace("EXIT!"); //NOI18N
try {
// Init properties
this.initProperties();
-
- // Fill products list
- this.fillProductsList();
- } catch (final IOException | UnsupportedDatabaseBackendException | SQLException | BadTokenException ex) {
+ } catch (final IOException ex) {
// Abort here
this.abortProgramWithException(ex);
}
// "Walk" over all products
for (final Product product : this.getProducts()) {
// Output data
- this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getName(), product.getTitle(), product.getPrice())); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getId(), product.getTitle(), product.getPrice())); //NOI18N
}
// Generate fake Customer instance