if (object instanceof PizzaApplication) {
// Instance is set, so casting should work
instance = (PizzaApplication) object;
+
+ // Debug message
+ instance.getLogger().debug(MessageFormat.format("Using existing instance {0} ...", object)); //NOI18N
} else if (object instanceof Object) {
// Not correct instance
throw new ServletException("app is not set correctly"); //NOI18N
} else {
try {
// "service" is null, so initialize it
- instance = new PizzaServiceApplication(context);
+ instance = new PizzaServiceApplication();
+ instance.init(context);
+
+ // Debug message
+ instance.getLogger().debug(MessageFormat.format("Created new instance {0} ...", object)); //NOI18N
} catch (final UnsupportedDatabaseBackendException | SQLException | IOException | BadTokenException ex) {
throw new ServletException(ex);
}
new PizzaServiceApplication().start();
}
- /**
- * Constructor with servet configuration
- *
- * @param context Servlet context
- */
- private PizzaServiceApplication (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
+ @Override
+ public void init (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
// Temporary initialize default bundle
// @TODO The JSF may have better internatialization support
this.initBundle();
}
// Get attribute
- Object object = this.getValueFromSession(product, session, HTTP_PARAM_CHOOSE);
+ Object object = this.getValueFromSession(product, session, HTTP_PARAM_ITEM_ID);
// Is the object null?
if (object == null) {
throw new NullPointerException("session is null"); //NOI18N
}
+ // Get "enabled" from request scope
+ Boolean enabled = Boolean.parseBoolean((String) request.getAttribute("enabled")); //NOI18N
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("enabled={0}", enabled)); //NOI18N
+
// Is something selected?
- if (this.calculateTotalAmount(request, session) > 0) {
+ if ((enabled) || (this.calculateTotalAmount(request, session) > 0)) {
// Trace message
this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
// Mark it as ordered by setting flag
this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getId())); //NOI18N
- this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, "1"); //NOI18N
+ this.setValueInSession(product, session, HTTP_PARAM_ITEM_ID, "1"); //NOI18N
// Trace message
this.getLogger().trace("EXIT!"); //NOI18N
// Mark it as ordered by setting flag
this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getId())); //NOI18N
- this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
+ this.clearSessionAttribute(product, session, HTTP_PARAM_ITEM_ID);
// Trace message
this.getLogger().trace("EXIT!"); //NOI18N
}
// Get reqzest element
- object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getId()));
+ 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
// Is it null?
if (object == null) {
// Unset session
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_ITEM_ID);
this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
// Return empty string
}
// Then set it in session
- this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
+ 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
*
* @param product Product instance
* @param session HttpSession instance
- * @return
+ * @return Whether the product has been ordered
*/
private boolean isProductOrdered (final Product product, final HttpSession session) {
// Trace message
try {
// Init properties
this.initProperties();
- } catch (final IOException ex) {
+
+ // And frontends
+ this.initDatabaseFrontends();
+ } catch (final IOException | UnsupportedDatabaseBackendException | SQLException ex) {
// Abort here
this.abortProgramWithException(ex);
}
String available = request.getParameter(PizzaProductDatabaseConstants.COLUMN_AVAILABLE);
// Debug message
- this.getLogger().debug(MessageFormat.format("title={0},price={1},category={2},available={3}", title, price, category, available));
+ this.getLogger().debug(MessageFormat.format("title={0},price={1},category={2},available={3}", title, price, category, available)); //NOI18N
// Variables for converting
Long id = null;
@Override
public String getPrintableProduktCategory (final Product product) throws ServletException {
// Trace message
- this.getLogger().trace("product=" + product + " - CALLED!");
+ this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
// product must not be null
if (product == null) {
// Abort here
- throw new NullPointerException("product is null");
+ throw new NullPointerException("product is null"); //NOI18N
}
// Declare category
}
// Debug message
- this.getLogger().debug("category=" + category);
+ this.getLogger().debug(MessageFormat.format("category={0}", category)); //NOI18N
String title = null;
try {
}
// Trace message
- this.getLogger().trace("title=" + title + " - EXIT!");
+ this.getLogger().trace(MessageFormat.format("title={0} - EXIT!", title)); //NOI18N
// Return it
return title;
*/
private boolean isProductTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// Trace message
- this.getLogger().trace("request=" + request + " - CALLED!");
+ this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
// Init title
String title = request.getParameter(PizzaProductDatabaseConstants.COLUMN_TITLE);
// request must not be null and "title" must be found and non-empty
if (request == null) {
// Abort here
- throw new NullPointerException("request is null");
+ throw new NullPointerException("request is null"); //NOI18N
} else if (title == null) {
// title is not set
throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
boolean isUsed = this.isProductTitleUsed(title);
// Trace message
- this.getLogger().trace("isUsed=" + isUsed + " - EXIT!");
+ this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
// Return it
return isUsed;
@Override
public void doAdminHandleProductForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
// Trace message
- this.getLogger().trace("request=" + request + ",response=" + response + " - CALLED!");
+ this.getLogger().trace(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
// request and response must both be set
if (request == null) {
// request is null
- throw new NullPointerException("request is null");
+ throw new NullPointerException("request is null"); //NOI18N
} else if (response == null) {
// response is null
- throw new NullPointerException("response is null");
+ throw new NullPointerException("response is null"); //NOI18N
}
// Try this operations
// Is it already added?
if (this.isProductTitleUsed(request)) {
// Debug message
- this.getLogger().debug("Already used, redirecting ...");
+ this.getLogger().debug("Already used, redirecting ..."); //NOI18N
// Already added, so redirect here, else a ServletException will be thrown
- response.sendRedirect(request.getContextPath() + "/admin/product.jsp?already=1");
+ response.sendRedirect(String.format("%s/admin/product.jsp?already=1", request.getContextPath())); //NOI18N
} else {
// Add new product
this.doAdminAddProduct(request);
}
// Trace message
- this.getLogger().trace("EXIT!");
+ this.getLogger().trace("EXIT!"); //NOI18N
}
/**
@Override
public void doAdminHandleCategoryForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
// Trace message
- this.getLogger().trace("request=" + request + ",response=" + response + " - CALLED!");
+ this.getLogger().trace(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
// request and response must both be set
if (request == null) {
// request is null
- throw new NullPointerException("request is null");
+ throw new NullPointerException("request is null"); //NOI18N
} else if (response == null) {
// response is null
- throw new NullPointerException("response is null");
+ throw new NullPointerException("response is null"); //NOI18N
}
// Try this operations
// Is the category title already used?
if (this.isCategoryTitleUsed(request)) {
// Debug message
- this.getLogger().debug("Already used, redirecting ...");
+ this.getLogger().debug("Already used, redirecting ..."); //NOI18N
// Already added, so redirect here, else a ServletException will be thrown
- response.sendRedirect(request.getContextPath() + "/admin/category.jsp?already=1");
+ response.sendRedirect(String.format("%s/admin/category.jsp?already=1", request.getContextPath())); //NOI18N
} else {
// Add new category
this.doAdminAddCategory(request);
}
// Trace message
- this.getLogger().trace("EXIT!");
+ this.getLogger().trace("EXIT!"); //NOI18N
}
/**
*/
private boolean isCategoryTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
// Trace message
- this.getLogger().trace("request=" + request + " - CALLED!");
+ this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
// Init title
String title = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE);
// request must not be null and "title" must be found and non-empty
if (request == null) {
// Abort here
- throw new NullPointerException("request is null");
+ throw new NullPointerException("request is null"); //NOI18N
} else if (title == null) {
// title is not set
throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
boolean isUsed = this.isCategoryTitleUsed(title);
// Trace message
- this.getLogger().trace("isUsed=" + isUsed + " - EXIT!");
+ this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
// Return it
return isUsed;