- Had to renamed id -> itemId and categoryId accordingly to allow databased- baskets
- Implemented almost all methods in basket database frontend class
- Added constants class for above frontend
- Updated jcore
Signed-off-by:Roland Häder <roland@mxchange.org>
public static final String HTTP_PARAM_AMOUNT = "amount"; //NOI18N
/**
- * HTTP parameter "id"
+ * HTTP parameter "item_id"
*/
- public static final String HTTP_PARAM_ITEM_ID = "id"; //NOI18N
+ public static final String HTTP_PARAM_ITEM_ID = "item_id"; //NOI18N
/**
* HTTP parameter "type"
// Is this choosen?
if (this.isProductChoosen(product, request, session)) {
// Then add ordered amount
- this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getId())); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getItemId())); //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.getId(), totalAmount)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getItemId(), 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.getId())); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getItemId())); //NOI18N
totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
}
- this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getId(), totalPrice)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getItemId(), totalPrice)); //NOI18N
}
// Trace message
this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
// Is product and session set?
- if (product == null) {
+ if (null == product) {
// Not set
throw new NullPointerException("product is null"); //NOI18N
} else if (session == null) {
this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
// Is product and session set?
- if (product == null) {
+ if (null == product) {
// Not set
throw new NullPointerException("product is null"); //NOI18N
} else if (request == null) {
this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
// Is product and session set?
- if (product == null) {
+ if (null == product) {
// Not set
throw new NullPointerException("product is null"); //NOI18N
} else if (session == null) {
// Is the object null?
if (object == null) {
// Not found
- this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getId())); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getItemId())); //NOI18N
return ""; //NOI18N
}
this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
// Is product and session set?
- if (product == null) {
+ if (null == product) {
// Not set
throw new NullPointerException("product is null"); //NOI18N
} else if (request == null) {
}
// Get element
- this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getItemId(), request, session)); //NOI18N
String choosen = this.handleChooseFromRequestSession(product, request, session);
- this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getItemId(), 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.getId(), amount)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getItemId(), amount)); //NOI18N
// Must not be null
assert(amount instanceof String): "amount is null"; //NOI18N
this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
// Is it null?
- if (product == null) {
+ if (null == product) {
// Should not be null
throw new NullPointerException("product is null"); //NOI18N
}
this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
// Is product and session set?
- if (product == null) {
+ if (null == product) {
// Not set
throw new NullPointerException("product is null"); //NOI18N
} else if (request == null) {
}
// Get choosen
- this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getItemId(), request, session)); //NOI18N
String choosen = this.handleChooseFromRequestSession(product, request, session);
- this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getItemId(), 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.getId(), choosen)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getItemId(), choosen)); //NOI18N
return 0.00f;
}
// Get amount
String amount = this.handleAmountFromRequestSession(product, request, session);
- this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getItemId(), 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.getId(), amount)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getItemId(), amount)); //NOI18N
return 0.00f;
}
float price = (product.getPrice() * value);
// Trace message
- this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getId(), price)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getItemId(), price)); //NOI18N
// Then multiply it with price
return price;
this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
// Is product and session set?
- if (product == null) {
+ if (null == product) {
// Not set
throw new NullPointerException("product is null"); //NOI18N
} else if (request == null) {
} 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.getId())); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getItemId())); //NOI18N
return "0"; //NOI18N
}
// Get attribute from request
- object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getId()));
+ object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getItemId()));
// Is it set?
if (object instanceof String) {
this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
// Is product and session set?
- if (product == null) {
+ if (null == product) {
// Not set
throw new NullPointerException("product is null"); //NOI18N
} else if (request == null) {
}
// Get choosen
- this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getItemId(), request, session)); //NOI18N
String choosen = this.handleChooseFromRequestSession(product, request, session);
- this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getItemId(), choosen)); //NOI18N
// Must not be null
assert(choosen instanceof String): "choosen is null"; //NOI18N
this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
// Is product and session set?
- if (product == null) {
+ if (null == product) {
// Not set
throw new NullPointerException("product is null"); //NOI18N
} else if (session == null) {
}
// Mark it as ordered by setting flag
- this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getId())); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getItemId())); //NOI18N
this.setValueInSession(product, session, HTTP_PARAM_ITEM_ID, "1"); //NOI18N
// Trace message
this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
// Is product and session set?
- if (product == null) {
+ if (null == product) {
// Not set
throw new NullPointerException("product is null"); //NOI18N
} else if (session == null) {
}
// Mark it as ordered by setting flag
- this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getId())); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getItemId())); //NOI18N
this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
// Trace message
this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
// Is product and session set?
- if (product == null) {
+ if (null == product) {
// Not set
throw new NullPointerException("product is null"); //NOI18N
} else if (session == null) {
}
// Mark it as ordered by setting flag
- this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getId())); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getItemId())); //NOI18N
this.clearSessionAttribute(product, session, HTTP_PARAM_ITEM_ID);
// Trace message
this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
// Is product and session set?
- if (product == null) {
+ if (null == product) {
// Not set
throw new NullPointerException("product is null"); //NOI18N
} else if (session == null) {
}
// Mark it as ordered by setting flag
- this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getId())); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getItemId())); //NOI18N
this.clearSessionAttribute(product, session, SESSION_ORDERED);
// 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.getId(), parameter)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getItemId(), parameter)); //NOI18N
this.setValueInSession(product, session, parameter, null);
// Trace message
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.getId()));
+ Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getItemId()));
- this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getId(), attribute, value)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getItemId(), attribute, value)); //NOI18N
// Trace message
this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
// Is product and session set?
- if (product == null) {
+ if (null == product) {
// Not set
throw new NullPointerException("product is null"); //NOI18N
} else if (request == null) {
// 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.getId(), session)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getItemId(), session)); //NOI18N
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.getId(), session)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getItemId(), session)); //NOI18N
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.getId(), session)); //NOI18N
+ this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getItemId(), session)); //NOI18N
return this.getChooseFromSession(product, session);
}
// Get reqzest element
- object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_ITEM_ID, product.getId()));
- this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getId(), object)); //NOI18N
+ object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_ITEM_ID, product.getItemId()));
+ this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getItemId(), object)); //NOI18N
// Is it null?
if (object == null) {
// Unset session
- this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getId())); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getItemId())); //NOI18N
this.clearSessionAttribute(product, session, HTTP_PARAM_ITEM_ID);
this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
this.setValueInSession(product, session, HTTP_PARAM_ITEM_ID, object);
// Cast to string and return it
- this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getId(), object)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getItemId(), object)); //NOI18N
return (String) object;
}
// Get session
Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
- this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getId(), isOrdered)); //NOI18N
+ this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getItemId(), 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.getId(), keyPart)); //NOI18N
- this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getId()), value);
+ this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getItemId(), keyPart)); //NOI18N
+ this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getItemId()), value);
// Trace message
this.getLogger().trace("EXIT!"); //NOI18N
Product product = iterator.next();
// Output data
- this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getId(), product.getTitle(), product.getPrice())); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getItemId(), product.getTitle(), product.getPrice())); //NOI18N
}
// Generate fake Customer instance
this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
// product must not be null
- if (product == null) {
+ if (null == product) {
// Abort here
throw new NullPointerException("product is null"); //NOI18N
}
*/
package org.mxchange.pizzaapplication.basket;
+import java.io.IOException;
+import java.io.ObjectInputStream;
import java.io.Serializable;
+import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.text.MessageFormat;
import org.mxchange.jcore.BaseFrameworkSystem;
+import org.mxchange.jcore.exceptions.BadTokenException;
+import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
import org.mxchange.pizzaapplication.database.frontend.basket.BasketDatabaseFrontend;
import org.mxchange.pizzaapplication.database.frontend.basket.BasketFrontend;
*/
private static final long serialVersionUID = 784396762230845717L;
+ /**
+ * Session id assigned with this basket, or any other unique identifier
+ */
+ private String sessionId;
+
/**
* Protected constructor with session instance
*
}
@Override
- public void addItem (final T item) {
+ public void addItem (final T item) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
// Trace call
this.getLogger().trace(MessageFormat.format("item={0} - CALLED!", item)); //NOI18N
// item must not be null
- if (item == null) {
+ if (null == item) {
// Then abort here
throw new NullPointerException("item is null"); //NOI18N
} else if (this.isItemAdded(item)) {
}
// Add item to database
- ((BasketFrontend) this.getFrontend()).addItem(item);
+ ((BasketFrontend) this.getFrontend()).addItem(item, this.getSessionId());
// Trace call
this.getLogger().trace("EXIT!"); //NOI18N
}
@Override
- public AddableBasketItem getItem (final Product product) {
+ public AddableBasketItem getItem (final Product product) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
// Trace message
this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
// product must not be null
- if (product == null) {
+ if (null == product) {
// Abort here
throw new NullPointerException("product is null"); //NOI18N
}
// Debug message
- this.getLogger().debug("Product " + product.getTitle() + " has id " + product.getId()); //NOI18N
+ this.getLogger().debug(MessageFormat.format("Product {0} has id {1},frontend={2}", product.getTitle(), product.getItemId(), this.getFrontend())); //NOI18N
// Get item instance from map by product's id number (both are the same)
- AddableBasketItem item = ((BasketFrontend) this.getFrontend()).getItem(product);
+ AddableBasketItem item = ((BasketFrontend) this.getFrontend()).getItem(product, this.getSessionId());
// Is it null?
- if (item == null) {
+ if (null == item) {
// Trace message
this.getLogger().trace("item=null - EXIT!"); //NOI18N
} else {
// Trace message
- this.getLogger().trace(MessageFormat.format("item={0} with id={1},amount={2} - EXIT!", item, item.getId(), item.getAmount())); //NOI18N
+ this.getLogger().trace(MessageFormat.format("item={0} with id={1},amount={2} - EXIT!", item, item.getItemId(), item.getAmount())); //NOI18N
}
// Return it
}
@Override
- public boolean isItemAdded (final T item) {
+ public boolean isItemAdded (final T item) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
// Trace call
this.getLogger().trace(MessageFormat.format("item={0} - CALLED!", item)); //NOI18N
// item must not be null
- if (item == null) {
+ if (null == item) {
// Then abort here
throw new NullPointerException("item is null"); //NOI18N
}
// Call map's method
- boolean isAdded = ((BasketFrontend) this.getFrontend()).isItemAdded(item);
+ boolean isAdded = ((BasketFrontend) this.getFrontend()).isItemAdded(item, this.getSessionId());
// Trace message
this.getLogger().trace(MessageFormat.format("isAdded={0} - EXIT!", isAdded)); //NOI18N
// Return it
return isAdded;
}
+
+ /**
+ * Setter for session id
+ *
+ * @param sessionId Session id
+ */
+ protected final void setSessionId (final String sessionId) {
+ this.sessionId = sessionId;
+ }
+
+ /**
+ * Getter for session id
+ *
+ * @return Session id
+ */
+ protected final String getSessionId () {
+ return this.sessionId;
+ }
+
+ /**
+ * Called when fields are set on deserialization
+ *
+ * @param in Object input stream
+ * @throws IOException If any IO error occurs
+ * @throws ClassNotFoundException If the class is not found
+ */
+ private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("in={0} - CALLED!", in));
+
+ try {
+ // Init frontend
+ BasketFrontend frontend = new BasketDatabaseFrontend();
+
+ // Set it here
+ this.setFrontend(frontend);
+ } catch (final UnsupportedDatabaseBackendException | SQLException ex) {
+ // Continue to throw
+ throw new IOException(ex);
+ }
+ }
}
*/
package org.mxchange.pizzaapplication.basket;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.sql.SQLException;
import org.mxchange.jcore.FrameworkInterface;
+import org.mxchange.jcore.exceptions.BadTokenException;
+import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
import org.mxchange.pizzaapplication.item.AddableBasketItem;
import org.mxchange.pizzaapplication.product.Product;
/**
* Adds given item instance to this basket
* @param item Item instance to add
+ * @throws java.io.IOException If an IO error occurs
+ * @throws java.sql.SQLException If an SQL error occurs
+ * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found
+ * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If a corrupted database file was found
+ * @throws java.lang.NoSuchMethodException If a method was not found
+ * @throws java.lang.IllegalAccessException If the invoked method is not public
+ * @throws java.lang.reflect.InvocationTargetException If anything else happened?
*/
- public void addItem (final T item);
+ public void addItem (final T item) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException;
/**
* Checks whether the given item as already been added. Mostly
*
* @param item Item instance to check
* @return Whether the given item has been found
+ * @throws java.io.IOException If an IO error occurs
+ * @throws java.sql.SQLException If an SQL error occurs
+ * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found
+ * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If a corrupted database file was found
+ * @throws java.lang.NoSuchMethodException If a method was not found
+ * @throws java.lang.IllegalAccessException If the invoked method is not public
+ * @throws java.lang.reflect.InvocationTargetException If anything else happened?
*/
- public boolean isItemAdded (final T item);
+ public boolean isItemAdded (final T item) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException;
/**
* Returns an item for given product instance or null if not found.
*
* @param product Product instance
* @return Item instance or null
+ * @throws java.io.IOException If an IO error occurs
+ * @throws java.sql.SQLException If an SQL error occurs
+ * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found
+ * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If a corrupted database file was found
+ * @throws java.lang.NoSuchMethodException If a method was not found
+ * @throws java.lang.IllegalAccessException If the invoked method is not public
+ * @throws java.lang.reflect.InvocationTargetException If anything else happened?
*/
- public AddableBasketItem getItem (final Product product);
+ public AddableBasketItem getItem (final Product product) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException;
}
package org.mxchange.pizzaapplication.basket.item;
import java.sql.SQLException;
+import java.text.MessageFormat;
import javax.servlet.http.HttpSession;
+import org.mxchange.jcore.BaseFrameworkSystem;
import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
import org.mxchange.pizzaapplication.basket.BaseBasket;
import org.mxchange.pizzaapplication.basket.Basket;
*/
@SuppressWarnings ("unchecked")
public final static Basket<AddableBasketItem> getInstance (final HttpSession session) throws UnsupportedDatabaseBackendException, SQLException {
+ // Trace message
+ BaseFrameworkSystem.getInstance().getLogger().trace(MessageFormat.format("session={0} - CALLED!", session)); //NOI18N
+
// Get instance
Basket<AddableBasketItem> basket = (Basket<AddableBasketItem>) session.getAttribute("basket"); //NOI18N
+ // Debug message
+ BaseFrameworkSystem.getInstance().getLogger().debug(MessageFormat.format("basket={0}", basket));
+
// Is the basket already created?
- if (!(basket instanceof BaseBasket)) {
+ if (!(basket instanceof Basket)) {
// Not yet, so create it
- basket = new ItemBasket<>();
+ basket = new ItemBasket<>(session);
// Add it in session
session.setAttribute("basket", basket); //NOI18N
/**
* Default constructor to be able to throw exceptions from super constructor
+ *
+ * @param session HttpSession instance
*/
- private ItemBasket () throws UnsupportedDatabaseBackendException, SQLException {
+ private ItemBasket (final HttpSession session) throws UnsupportedDatabaseBackendException, SQLException {
+ // Call super constructor
+ super();
+
// Trace message
- this.getLogger().trace("CALLED!"); //NOI18N
+ this.getLogger().trace(MessageFormat.format("session={0} - CALLED!", session)); //NOI18N
+
+ // Session must be set
+ if (null == session) {
+ // Session is null
+ throw new NullPointerException("session is null");
+ }
+
+ // Set session id here
+ this.setSessionId(session.getId());
}
}
/**
* Id number of category
*/
- private Long id;
+ private Long categoryId;
/**
- * Parent category id
+ * Parent category categoryId
*/
private Long parent;
* Constructor which accepts all database fields
* @param id Id number of database record
* @param title Category title
- * @param parent Parent id
+ * @param parent Parent categoryId
*/
protected BaseCategory (final Long id, final String title, final Long parent) {
// Set all here
- this.setId(id);
+ this.setCategoryId(id);
this.setTitle(title);
this.setParent(parent);
}
}
// Debug message
- this.getLogger().debug(MessageFormat.format("this.id={0},category.id={1}", this.getId(), category.getId())); //NOI18N
+ this.getLogger().debug(MessageFormat.format("this.id={0},category.id={1}", this.getCategoryId(), category.getCategoryId())); //NOI18N
- // Is the id the same?
- if (Objects.equals(this.getId(), category.getId())) {
- // Same id, means same category
+ // Is the categoryId the same?
+ if (Objects.equals(this.getCategoryId(), category.getCategoryId())) {
+ // Same categoryId, means same category
return 0;
- } else if (this.getId() > category.getId()) {
- // This id is larger than compared to
+ } else if (this.getCategoryId() > category.getCategoryId()) {
+ // This categoryId is larger than compared to
return -1;
}
- // The other id is larger
+ // The other categoryId is larger
return 1;
}
/**
* Id number of category
- * @return the id
+ * @return the categoryId
*/
@Override
- public final Long getId () {
- return this.id;
+ public final Long getCategoryId () {
+ return this.categoryId;
}
/**
* Id number of category
- * @param id the id to set
+ * @param categoryId the categoryId to set
*/
@Override
- public final void setId (final Long id) {
- this.id = id;
+ public final void setCategoryId (final Long categoryId) {
+ this.categoryId = categoryId;
}
/**
- * Parent category id
+ * Parent category categoryId
* @return the parent
*/
@Override
}
/**
- * Parent category id
+ * Parent category categoryId
* @param parent the parent to set
*/
@Override
* Id number of category
* @return the id
*/
- public Long getId ();
+ public Long getCategoryId ();
/**
* Id number of category
* @param id the id to set
*/
- public void setId (final Long id);
+ public void setCategoryId (final Long id);
/**
* Parent category id
--- /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.basket;
+
+/**
+ * Constants for table "basket".
+ *
+ * @author Roland Haeder
+ */
+public final class BasketDatabaseConstants {
+ /**
+ * Column for id
+ */
+ public static final String COLUMN_ID = "id"; //NOI18N
+
+ /**
+ * Column for amount
+ */
+ public static final String COLUMN_AMOUNT = "amount"; //NOI18N
+
+ /**
+ * Column for product id
+ */
+ public static final String COLUMN_ITEM_ID = "item_id"; //NOI18N
+
+ /**
+ * Column for product type
+ */
+ public static final String COLUMN_PRODUCT_TYPE = "item_type"; //NOI18N
+
+ /**
+ * Column for session id or any unique identifier
+ */
+ public static final String COLUMN_SESSION_ID = "session_id"; //NOI18N
+
+ /**
+ * No instance from this class
+ */
+ private BasketDatabaseConstants () {
+ }
+}
/**
* Column name for "id"
*/
- public static final String COLUMN_ID = "id"; //NOI18N
+ public static final String COLUMN_ID = "category_id"; //NOI18N
/**
* Column name for "parent"
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.text.MessageFormat;
+import java.util.Iterator;
import java.util.Map;
+import org.mxchange.jcore.criteria.searchable.SearchCriteria;
+import org.mxchange.jcore.criteria.searchable.SearchableCriteria;
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.CorruptedDatabaseFileException;
import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
+import org.mxchange.pizzaapplication.database.basket.BasketDatabaseConstants;
import org.mxchange.pizzaapplication.item.AddableBasketItem;
+import org.mxchange.pizzaapplication.item.basket.BasketItem;
import org.mxchange.pizzaapplication.product.Product;
/**
}
@Override
- public void addItem (final AddableBasketItem item) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ public void addItem (final AddableBasketItem item, final String sessionId) throws SQLException, IOException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("item={0},sessionId={1} - CALLED!", item, sessionId));
+
+ // Both must not be null
+ if (null == item) {
+ // Product is null
+ throw new NullPointerException("item is null"); //NOI18N
+ } else if (null == sessionId) {
+ // Session id is null
+ throw new NullPointerException("sessionId is null"); //NOI18N
+ } else if (sessionId.isEmpty()) {
+ // Session id should not be empty
+ throw new IllegalArgumentException("sessionId is empty."); //NOI18N
+ }
+
+ // Clear previous data set
+ this.clearDataSet();
+
+ // Add item it and session
+ this.addToDataSet(BasketDatabaseConstants.COLUMN_ITEM_ID, item.getItemId());
+ this.addToDataSet(BasketDatabaseConstants.COLUMN_SESSION_ID, sessionId);
+ this.addToDataSet(BasketDatabaseConstants.COLUMN_AMOUNT, item.getAmount());
+ this.addToDataSet(BasketDatabaseConstants.COLUMN_PRODUCT_TYPE, item.getItemType());
+
+ // Handle this over to the backend
+ // @todo Nothing is done yet!
+ Result<? extends Storeable> result = this.doInsertDataSet();
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
+
+ // Trace message
+ this.getLogger().trace("EXIT!"); //NOI18N
}
/**
@Override
public String getIdName () {
- throw new UnsupportedOperationException("Not supported yet.");
+ return BasketDatabaseConstants.COLUMN_ID;
}
@Override
- public AddableBasketItem getItem (final Product product) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ public AddableBasketItem getItem (final Product product, final String sessionId) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("product={0},sessionId={1} - CALLED!", product, sessionId)); //NOI18N
+
+ // Now search for it
+ Result<? extends Storeable> result = this.getResultFromProduct(product, sessionId);
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("result({0})={1}", result.size(), result)); //NOI18N
+
+ // Init item variable
+ AddableBasketItem item = null;
+
+ // Was an entry found?
+ if (result.hasNext()) {
+ // Found one entry
+ Storeable storeable = result.next();
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("storeable={0}", storeable)); //NOI18N
+
+ // Is it instance of Category?
+ if (storeable instanceof AddableBasketItem) {
+ // Then cast it
+ item = (AddableBasketItem) storeable;
+ }
+ }
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("item={0} - EXIT!", item)); //NOI18N
+
+ // Return item
+ return item;
}
+/*
+ // Clear data set
+ this.clearDataSet();
+
+ // Add data to set
+ this.addToDataSet(BasketDatabaseConstants.COLUMN_ITEM_ID, id);
+ this.addToDataSet(BasketDatabaseConstants.COLUMN_SESSION_ID, sessionId);
+*/
+
@Override
public Storeable getStoreableAtRow (final int rowIndex) {
- throw new UnsupportedOperationException("Not supported yet: rowIndex=" + rowIndex);
+ throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: rowIndex={0}", rowIndex));
}
@Override
- public boolean isItemAdded (final AddableBasketItem item) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
+ public boolean isItemAdded (final AddableBasketItem item, final String sessionId) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("item={0},sessionId={1} - CALLED!", item, sessionId)); //NOI18N
+
+ // Get result back
+ Result<? extends Storeable> result = this.getResultFromItem(item, sessionId);
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("result={0}", result)); //NOI18N
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("result.hasNext()={0} - EXIT!", result.hasNext())); //NOI18N
+
+ // Return hasNext()
+ return result.hasNext();
}
@Override
public Storeable toStoreable (final Map<String, String> map) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
- throw new UnsupportedOperationException("Not supported yet: map=" + map);
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("map={0} - CALLED!", map)); //NOI18N
+
+ // Is map null?
+ if (map == null) {
+ // Is null
+ throw new NullPointerException("map is null"); //NOI18N
+ } else if (map.isEmpty()) {
+ // Map is empty
+ throw new IllegalArgumentException("map is empty."); //NOI18N
+ }
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("Has to handle {0} entries", map.size())); //NOI18N
+
+ // Get iterator on all entries
+ Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
+
+ // Init object instance
+ Storeable instance = new BasketItem();
+
+ // Iterate over all
+ while (iterator.hasNext()) {
+ // Get next entry
+ Map.Entry<String, String> entry = iterator.next();
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("entry:{0}={1}", entry.getKey(), entry.getValue())); //NOI18N
+
+ // Try to set value
+ instance.setValueFromColumn(entry.getKey(), entry.getValue());
+ }
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N
+
+ // Return it
+ return instance;
+ }
+
+ /**
+ * Some "getter" for a result instance from given product and session id
+ *
+ * @param product Product instance
+ * @param sessionId Session id or any unique identifier
+ * @return Result instance
+ * @throws java.io.IOException If an IO error occurs
+ * @throws java.sql.SQLException If an SQL error occurs
+ * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found
+ * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If a corrupted database file was found
+ * @throws java.lang.NoSuchMethodException If a method was not found
+ * @throws java.lang.IllegalAccessException If the invoked method is not public
+ * @throws java.lang.reflect.InvocationTargetException If anything else happened?
+ */
+ private Result<? extends Storeable> getResultFromProduct (final Product product, final String sessionId) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("product={0},sessionId={1} - CALLED!", product, sessionId)); //NOI18N
+
+ // Both must not be null
+ if (null == product) {
+ // Product is null
+ throw new NullPointerException("product is null"); //NOI18N
+ } else if (null == sessionId) {
+ // Session id is null
+ throw new NullPointerException("sessionId is null"); //NOI18N
+ } else if (sessionId.isEmpty()) {
+ // Session id should not be empty
+ throw new IllegalArgumentException("sessionId is empty."); //NOI18N
+ }
+
+ // Get product id
+ Long id = product.getItemId();
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("id={0}", id)); //NOI18N
+
+ // Id should be > 0 here
+ assert(id > 0) : MessageFormat.format("Id for product {0} is below zero: {1}", product, id); //NOI18N
+
+ // Prepare search instance
+ SearchableCriteria criteria = new SearchCriteria();
+
+ // Add session id and product id and limit to 1
+ criteria.addCriteria(BasketDatabaseConstants.COLUMN_ITEM_ID, id);
+ criteria.addCriteria(BasketDatabaseConstants.COLUMN_SESSION_ID, sessionId);
+ criteria.setLimit(1);
+
+ // Now search for it
+ Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(criteria);
+
+ // Trace message
+ this.getLogger().debug(MessageFormat.format("result={0} - EXIT!", result)); //NOI18N
+
+ // Return it
+ return result;
+ }
+
+ /**
+ * Some "getter" for a result instance from given item and session id
+ *
+ * @param item Item instance
+ * @param sessionId Session id or any unique identifier
+ * @return Result instance
+ * @throws java.io.IOException If an IO error occurs
+ * @throws java.sql.SQLException If an SQL error occurs
+ * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found
+ * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If a corrupted database file was found
+ * @throws java.lang.NoSuchMethodException If a method was not found
+ * @throws java.lang.IllegalAccessException If the invoked method is not public
+ * @throws java.lang.reflect.InvocationTargetException If anything else happened?
+ */
+ private Result<? extends Storeable> getResultFromItem (final AddableBasketItem item, final String sessionId) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("item={0},sessionId={1} - CALLED!", item, sessionId)); //NOI18N
+
+ // Both must not be null
+ if (null == item) {
+ // Item is null
+ throw new NullPointerException("item is null"); //NOI18N
+ } else if (null == sessionId) {
+ // Session id is null
+ throw new NullPointerException("sessionId is null"); //NOI18N
+ } else if (sessionId.isEmpty()) {
+ // Session id should not be empty
+ throw new IllegalArgumentException("sessionId is empty."); //NOI18N
+ }
+
+ // Get product id
+ Long id = item.getItemId();
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("id={0}", id)); //NOI18N
+
+ // Id should be > 0 here
+ assert(id > 0) : MessageFormat.format("Id for item {0} is below zero: {1}", item, id); //NOI18N
+
+ // Prepare search instance
+ SearchableCriteria criteria = new SearchCriteria();
+
+ // Add session id and product id and limit to 1
+ criteria.addCriteria(BasketDatabaseConstants.COLUMN_ITEM_ID, id);
+ criteria.addCriteria(BasketDatabaseConstants.COLUMN_SESSION_ID, sessionId);
+ criteria.setLimit(1);
+
+ // Now search for it
+ Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(criteria);
+
+ // Trace message
+ this.getLogger().debug(MessageFormat.format("result={0} - EXIT!", result)); //NOI18N
+
+ // Return it
+ return result;
}
}
*/
package org.mxchange.pizzaapplication.database.frontend.basket;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.sql.SQLException;
import org.mxchange.jcore.database.frontend.DatabaseFrontend;
+import org.mxchange.jcore.exceptions.BadTokenException;
+import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
import org.mxchange.pizzaapplication.item.AddableBasketItem;
import org.mxchange.pizzaapplication.product.Product;
* comes from product table.
*
* @param item Item instance to add
+ * @param sessionId Session id or any other unique identifier
+ * @throws java.io.IOException If an IO error occurs
+ * @throws java.sql.SQLException If an SQL error occurs
*/
- public void addItem (final AddableBasketItem item);
+ public void addItem (final AddableBasketItem item, final String sessionId) throws IOException, SQLException;
/**
* Gets an item instance for given Product instance
*
* @param product Prodduct instance
+ * @param sessionId Session id or any other unique identifier
* @return An item instance
+ * @throws java.io.IOException If an IO error occurs
+ * @throws java.sql.SQLException If an SQL error occurs
+ * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found
+ * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If a corrupted database file was found
+ * @throws java.lang.NoSuchMethodException If a method was not found
+ * @throws java.lang.IllegalAccessException If the invoked method is not public
+ * @throws java.lang.reflect.InvocationTargetException If anything else happened?
*/
- public AddableBasketItem getItem (final Product product);
+ public AddableBasketItem getItem (final Product product, final String sessionId) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException;
/**
* Checks if given item instance is found in basket by its id number.
*
* @param item Item instance to check
+ * @param sessionId Session id or any other unique identifier
* @return Whether the item is already added to the basket
+ * @throws java.io.IOException If an IO error occurs
+ * @throws java.sql.SQLException If an SQL error occurs
+ * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found
+ * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If a corrupted database file was found
+ * @throws java.lang.NoSuchMethodException If a method was not found
+ * @throws java.lang.IllegalAccessException If the invoked method is not public
+ * @throws java.lang.reflect.InvocationTargetException If anything else happened?
*/
- public boolean isItemAdded (final AddableBasketItem item);
+ public boolean isItemAdded (final AddableBasketItem item, final String sessionId) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException;
}
public interface CategoryFrontend extends DatabaseFrontend {
/**
- * Adds given category title as new category, parent may be null if not selected.
+ * Adds given category title as new category, parent may be null if not
+ * selected.
*
* @param title Title of category
* @param parent Parent id or null if not selected
* @author Roland Haeder
*/
public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implements CategoryFrontend {
+
/**
* Default constrcutor
+ *
* @throws org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException If the configured backend is not supported
* @throws java.sql.SQLException If any SQL error occurs
*/
}
/**
- * Adds given category title as new category, parent may be null if not selected.
+ * Adds given category title as new category, parent may be null if not
+ * selected.
*
* @param title Title of category
* @param parent Parent id or null if not selected
/**
* Shuts down the database layer
+ *
* @throws java.sql.SQLException If an SQL error occurs
* @throws java.io.IOException If any IO error occurs
*/
public Category getCategory (final Product product) throws IOException, BadTokenException, CorruptedDatabaseFileException, SQLException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// Trace message
this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
-
+
// product must not be null
- if (product == null) {
+ if (null == product) {
// Abort here
throw new NullPointerException("product is null"); //NOI18N
}
-
+
// Get category id from it
Long id = product.getCategory();
-
+
// Debug message
this.getLogger().debug(MessageFormat.format("id={0}", id)); //NOI18N
-
+
// It should be >0 here
- assert(id > 0) : MessageFormat.format("id={0} must be larger zero", id); //NOI18N
-
+ assert (id > 0) : MessageFormat.format("id={0} must be larger zero", id); //NOI18N
+
// Then construct a search instance
SearchableCriteria criteria = new SearchCriteria();
-
+
// Add id to it
criteria.addCriteria(PizzaCategoryDatabaseConstants.COLUMN_ID, id);
-
+
// Only one entry is find
criteria.setLimit(1);
-
+
// Run it on backend
Result<? extends Storeable> result = this.getBackend().doSelectByCriteria(criteria);
-
+
// Debug log
this.getLogger().debug(MessageFormat.format("result({0})={1}", result, result.size())); //NOI18N
-
+
// Init category instance
Category category = null;
-
+
// Is there one entry?
if (result.hasNext()) {
// Read result from it
Storeable storeable = result.next();
-
+
// Debug message
this.getLogger().debug(MessageFormat.format("storeable={0}", storeable)); //NOI18N
-
+
// Is it instance of Category?
if (storeable instanceof Category) {
// Then cast it
category = (Category) storeable;
}
}
-
+
// Trace message
this.getLogger().trace(MessageFormat.format("category={0} - EXIT!", category)); //NOI18N
-
+
// Return it
return category;
}
}
/**
- * Converts the given map into a Storeable instance, depending on which class implements it. All
- * keys are being interpreted as class fields/attributes and their respective setters are being searched for. As
- * this method may fail to find one or access it, this method throws some exception.
+ * Converts the given map into a Storeable instance, depending on which
+ * class implements it. All keys are being interpreted as class
+ * fields/attributes and their respective setters are being searched for. As
+ * this method may fail to find one or access it, this method throws some
+ * exception.
*
* @param map Map instance to convert to Storeable
* @return An instance of a Storeable implementation
@Override
public String getIdName () {
// Return column id
- return PizzaProductDatabaseConstants.COLUMN_ID;
+ return PizzaProductDatabaseConstants.COLUMN_ITEM_ID;
}
/**
// "Walk" through all entries
while (resultSet.next()) {
// Get id, title and parent id
- Long id = resultSet.getLong(PizzaProductDatabaseConstants.COLUMN_ID);
+ Long id = resultSet.getLong(PizzaProductDatabaseConstants.COLUMN_ITEM_ID);
String title = resultSet.getString(PizzaProductDatabaseConstants.COLUMN_TITLE);
Float price = resultSet.getFloat(PizzaProductDatabaseConstants.COLUMN_PRICE);
Long category = resultSet.getLong(PizzaProductDatabaseConstants.COLUMN_CATEGORY);
public static final String COLUMN_CATEGORY = "category"; //NOI18N
/**
- * Column name for "id"
+ * Column name for "item_id"
*/
- public static final String COLUMN_ID = "id"; //NOI18N
+ public static final String COLUMN_ITEM_ID = "item_id"; //NOI18N
/**
* Column name for "price"
package org.mxchange.pizzaapplication.filter.servlet.basket;
import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.text.MessageFormat;
import javax.servlet.Filter;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
+import org.mxchange.jcore.exceptions.BadTokenException;
+import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
import org.mxchange.pizzaapplication.basket.Basket;
import org.mxchange.pizzaapplication.basket.item.ItemBasket;
// Get basket instance
basket = ItemBasket.getInstance(session);
- } catch (final UnsupportedDatabaseBackendException | SQLException ex) {
- // Continue to throw
- throw new ServletException(ex);
- }
- // Is the item already added?
- if ((item.getAmount() == null) || (item.getAmount() == 0)) {
- // Debug message
- this.getLogger().debug("Amount for item " + item + " is null");
+ // Is the item already added?
+ if ((item.getAmount() == null) || (item.getAmount() == 0)) {
+ // Debug message
+ this.getLogger().debug("Amount for item " + item + " is null");
- // Amount is not entered
- return;
- } else if (basket.isItemAdded(item)) {
- // Yes, then throw exception here
- throw new ServletException(MessageFormat.format("item id={0} has already been added.", item.getId()));
- }
+ // Amount is not entered
+ return;
+ } else if (basket.isItemAdded(item)) {
+ // Yes, then throw exception here
+ throw new ServletException(MessageFormat.format("item id={0} has already been added.", item.getItemId()));
+ }
- // Register item with it
- basket.addItem(item);
+ // Register item with it
+ basket.addItem(item);
+ } catch (final UnsupportedDatabaseBackendException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
+ // Continue to throw
+ throw new ServletException(ex);
+ }
// Trace message
this.getLogger().trace("EXIT!"); //NOI18N
*/
package org.mxchange.pizzaapplication.item;
-import org.mxchange.jcore.FrameworkInterface;
+import org.mxchange.jcore.database.storage.Storeable;
/**
* An interface for addable basket items
*
* @author Roland Haeder
*/
-public interface AddableBasketItem extends FrameworkInterface {
+public interface AddableBasketItem extends Storeable, Comparable<AddableBasketItem> {
/**
* Item amount
public void setAmount (final Long amount);
/**
+ * Entry id (from database backend)
* @return the id
*/
public Long getId ();
/**
+ * Entry id (from database backend)
* @param id the id to set
*/
public void setId (final Long id);
+ /**
+ * @return the id
+ */
+ public Long getItemId ();
+
+ /**
+ * @param id the id to set
+ */
+ public void setItemId (final Long id);
+
/**
* @return the type
*/
- public String getType ();
+ public String getItemType ();
/**
* @param type the type to set
*/
- public void setType (final String type);
+ public void setItemType (final String type);
+
+ /**
+ * Session id
+ * @return the sessionId
+ */
+ public String getSessionId ();
+
+ /**
+ * Session id
+ * @param sessionId the sessionId to set
+ */
+ public void setSessionId (final String sessionId);
+
+ /**
+ * Compare method
+ * @param item Item to compare to
+ * @return Comparison value
+ */
+ @Override
+ public int compareTo (final AddableBasketItem item);
}
*/
package org.mxchange.pizzaapplication.item;
+import java.lang.reflect.InvocationTargetException;
+import java.text.MessageFormat;
+import java.util.Objects;
import org.mxchange.jcore.BaseFrameworkSystem;
/**
* @author Roland Haeder
*/
public class BaseItem extends BaseFrameworkSystem implements AddableBasketItem {
+ /**
+ * Entry id (from database backend)
+ */
+ private Long id;
+
/**
* Item amount
*/
/**
* Item id number
*/
- private Long id;
+ private Long itemId;
/**
* Item type
*/
- private String type;
+ private String itemType;
+
+ /**
+ * Session id
+ */
+ private String sessionId;
+
+ @Override
+ public int compareTo (final AddableBasketItem item) {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("item={0} - CALLED!", item)); //NOI18N
+
+ // item should not be null
+ if (null == item) {
+ throw new NullPointerException("item is null"); //NOI18N
+ }
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("this.id={0},item.id={1}", this.getItemId(), item.getItemId())); //NOI18N
+
+ // Is the id the same?
+ if (Objects.equals(this.getItemId(), item.getItemId())) {
+ // Same id, means same item
+ return 0;
+ } else if (this.getItemId() > item.getItemId()) {
+ // This id is larger than compared to
+ return -1;
+ }
+
+ // The other id is larger
+ return 1;
+ }
/**
* Item amount
}
/**
+ * Entry id (from database backend)
* @return the id
*/
@Override
}
/**
+ * Entry id (from database backend)
* @param id the id to set
*/
@Override
this.id = id;
}
+ /**
+ * @return the id
+ */
+ @Override
+ public final Long getItemId () {
+ return this.itemId;
+ }
+
+ /**
+ * @param itemId the id to set
+ */
+ @Override
+ public final void setItemId( final Long itemId) {
+ this.itemId = itemId;
+ }
+
/**
* @return the type
*/
@Override
- public final String getType () {
- return this.type;
+ public final String getItemType () {
+ return this.itemType;
+ }
+
+ /**
+ * @param itemType the type to set
+ */
+ @Override
+ public final void setItemType (final String itemType) {
+ this.itemType = itemType;
+ }
+
+ /**
+ * Session id
+ * @return the sessionId
+ */
+ @Override
+ public final String getSessionId () {
+ return this.sessionId;
}
/**
- * @param type the type to set
+ * Session id
+ * @param sessionId the sessionId to set
*/
@Override
- public final void setType (final String type) {
- this.type = type;
+ public final void setSessionId (final String sessionId) {
+ this.sessionId = sessionId;
+ }
+
+ @Override
+ public Object getValueFromColumn (final String columnName) throws IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName)); //NOI18N
+
+ // Call super method
+ Object value = this.getValueInStoreableFromColumn(this, "BaseItem", columnName); //NOI18N
+
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
+
+ // Return value
+ return value;
+ }
+
+ @Override
+ public void setValueFromColumn (final String columnName, final Object value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+ // Trace message
+ this.getLogger().trace(MessageFormat.format("columnName={0},value={1} - CALLED!", columnName, value)); //NOI18N
+
+ // Call super method
+ this.setValueInStoreableFromColumn(this, "BaseItem", columnName, value); //NOI18N
+
+ // Trace message
+ this.getLogger().trace("EXIT!"); //NOI18N
}
}
this.getLogger().debug(MessageFormat.format("product={0} - CALLED!", product));
// product must not be null
- if (product == null) {
+ if (null == product) {
// Abort here
throw new NullPointerException("product is null");
}
// Copy all neccessary values
- this.setId(product.getId());
- this.setType("Product"); //NOI18N
+ this.setItemId(product.getItemId());
+ this.setItemType("Product"); //NOI18N
}
}
private Long category;
/**
- * Id number of product
+ * Id number of product item
*/
- private Long id;
+ private Long itemId;
/**
* Price of product
this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
// category should not be null
- if (product == null) {
+ if (null == product) {
throw new NullPointerException("product is null"); //NOI18N
}
// Debug message
- this.getLogger().debug(MessageFormat.format("this.id={0},product.id={1}", this.getId(), product.getId())); //NOI18N
+ this.getLogger().debug(MessageFormat.format("this.id={0},product.id={1}", this.getItemId(), product.getItemId())); //NOI18N
// Is the id the same?
- if (Objects.equals(this.getId(), product.getId())) {
+ if (Objects.equals(this.getItemId(), product.getItemId())) {
// Same id, means same category
return 0;
- } else if (this.getId() > product.getId()) {
+ } else if (this.getItemId() > product.getItemId()) {
// This id is larger than compared to
return 1;
}
* @return the name
*/
@Override
- public final Long getId () {
- return this.id;
+ public final Long getItemId () {
+ return this.itemId;
}
/**
* Id number of product
- * @param id the id number to set
+ * @param itemId the id number to set
*/
@Override
- public final void setId (final Long id) {
- this.id = id;
+ public final void setItemId (final Long itemId) {
+ this.itemId = itemId;
}
/**
*
* @return Id number of product
*/
- public Long getId ();
+ public Long getItemId ();
/**
* Id number of product
* @param id the id number to set
*/
- public void setId (final Long id);
+ public void setItemId (final Long id);
/**
* Getter for title.
this.getLogger().trace(MessageFormat.format("id={0},title={1},price={2},category={3},available={4} - CALLED!", id, title, price, category, available)); //NOI18N
// Set all here
- this.setId(id);
+ this.setItemId(id);
this.setTitle(title);
this.setPrice(price);
this.setCategory(category);
<c:forEach var="category" items="<%=app.getCategories()%>">
<tr>
<td>
- <input class="input" type="checkbox" name="<%=PizzaCategoryDatabaseConstants.COLUMN_ID%>[${category.getId()}]" value="1" />
+ <input class="input" type="checkbox" name="<%=PizzaCategoryDatabaseConstants.COLUMN_ID%>[${category.getCategoryId()}]" value="1" />
</td>
<td>
${category.decodedTitle()}
<c:forEach var="product" items="<%=app.getAllProducts()%>">
<tr>
<td>
- ${product.getId()}:
- <input class="input" type="checkbox" name="product[${product.getId()}]" value="1" />
+ ${product.getItemId()}:
+ <input class="input" type="checkbox" name="product[${product.getItemId()}]" value="1" />
</td>
<td>
${product.getTitle()}
<tr>
<td class="table_data_column">
<input class="submit" type="submit" name="add" value="Hinzufügen" />
- <input class="input" type="hidden" name="<%=PizzaApplication.HTTP_PARAM_ITEM_ID%>" value="<%=product.getId()%>" />
+ <input class="input" type="hidden" name="<%=PizzaApplication.HTTP_PARAM_ITEM_ID%>" value="<%=product.getItemId()%>" />
<input class="input" type="hidden" name="<%=PizzaApplication.HTTP_PARAM_ITEM_TYPE%>" value="Product" />
</td>
<td class="table_data_column">
<h:panelGrid class="table" columns="4">
<h:column class="table_header_column">
<h:commandButton action="#{path}/form_handler/add_item.jsp" class="submit" value="Hinzufügen" />
- <input class="input" type="hidden" name="<%=PizzaApplication.HTTP_PARAM_ITEM_ID%>" value="<%=product.getId()%>" />
+ <input class="input" type="hidden" name="<%=PizzaApplication.HTTP_PARAM_ITEM_ID%>" value="<%=product.getItemId()%>" />
<input class="input" type="hidden" name="<%=PizzaApplication.HTTP_PARAM_ITEM_TYPE%>" value="Product" />
</h:column>
<h:column class="table_header_column">
<select class="select" name="category" size="1">
<c:forEach var="category" items="<%=app.getCategories()%>">
- <option value="${category.getId()}">${category.decodedTitle()}</option>
+ <option value="${category.getCategoryId()}">${category.decodedTitle()}</option>
</c:forEach>
</select>
<select class="select" name="<%=PizzaCategoryDatabaseConstants.COLUMN_PARENT%>" size="1">
<option value="">Ist oberste Kategorie</option>
<c:forEach var="category" items="<%=app.getCategories()%>">
- <option value="${category.getId()}">${category.decodedTitle()}</option>
+ <option value="${category.getCategoryId()}">${category.decodedTitle()}</option>
</c:forEach>
</select>