From: Roland Haeder Date: Fri, 7 Aug 2015 12:54:53 +0000 (+0200) Subject: Continued with project: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=9527ce19be444ab3ea72e12d2362b64387473d69;p=pizzaservice-war.git Continued with project: - rewrote iteration over Contact fields with an iterator from Map.Entry - added default values in orderjsp (to output session-stored values) - introduced getPrintableValueFromSession() which returns a value from session key or an empty string for null - introduced getValueFromSession() which is a low-level method to get a value from session key (but sychronized) Signed-off-by:Roland Häder --- diff --git a/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java b/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java index a01c56d8..9cc5ba6d 100644 --- a/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java +++ b/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java @@ -176,4 +176,13 @@ public interface PizzaApplication extends Application { * @param session Session instance */ public void unmarkProductAsOrdered(final Product product, final HttpSession session); + + /** + * Some getter for printable value from session or an empty string for null. + * + * @param session Session instance + * @param key Key to get + * @return Value from key, empty string for null + */ + public Object getPrintableValeFromSession (final HttpSession session, final String key); } diff --git a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java index da4e3991..27afd321 100644 --- a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java +++ b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java @@ -16,6 +16,7 @@ */ package org.mxchange.pizzaapplication.application; +import java.lang.reflect.Field; import java.text.MessageFormat; import java.util.Iterator; import java.util.Map; @@ -209,15 +210,36 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P 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, product.getName(), attribute)); + + this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getName(), attribute, value)); //NOI18N + + // Trace message + this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N + + // Return it + return value; + } + + /** + * Some getter for value from session + * + * @param session Session instance + * @param key Key to get value from + * @return Value from session + */ + private Object getValueFromSession (final HttpSession session, final String key) { + // Trace message + this.getLogger().trace(MessageFormat.format("session={043},key={1} - CALLED!", session, key)); //NOI18N + + // Init value Object value = null; - // Get it synced + // Get it synchronized from session synchronized (session) { - value = session.getAttribute(String.format(HTTP_PARAM_MASK, product.getName(), attribute)); + value = session.getAttribute(key); } - this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getName(), attribute, value)); //NOI18N - // Trace message this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N @@ -313,7 +335,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P Customer customer = new PizzaServiceCustomer(); /* - * Need a least a gender :( See, that is why I don't like default + * Need a least a gender ... :( See, that is why I don't like default * constructors, you can easily miss something important and bam! You * get an NPE. The fix here is, to have construtors (or factories) which * requires all required instances that needs to be set to get a @@ -324,17 +346,18 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P customer.setGender(Gender.MALE); // Get iterator on all its fields - Iterator it = customer.iterator(); + Iterator> it = customer.iterator(); // Output it while (it.hasNext()) { - Object field = it.next(); - this.getLogger().debug("field=" + field); + Map.Entry entry = it.next(); + this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue())); } } /** * Some "getter" for amount from session + * * @param product Product instance * @param session Session instance * @return Amount as string @@ -960,4 +983,38 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P // Trace message this.getLogger().trace("EXIT!"); //NOI18N } + + /** + * Some getter for printable value from session or an empty string for null. + * + * @param session Session instance + * @param key Key to get + * @return Value from key, empty string for null + */ + @Override + public Object getPrintableValeFromSession (final HttpSession session, final String key) { + // Trace message + this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); + + // Are both parameter not null? + if (session == null) { + // Abort here + throw new NullPointerException("session is null"); + } else if (key == null) { + // Abort here + throw new NullPointerException("key is null"); + } + + // Now get it + Object value = this.getValueFromSession(session, key); + + // Debug message + this.getLogger().debug(MessageFormat.format("value={0}", value)); + + // Trace message + this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); + + // Return actual value + return this.convertNullToEmpty(value); + } } diff --git a/src/java/org/mxchange/pizzaapplication/beans/CustomerBean.java b/src/java/org/mxchange/pizzaapplication/beans/CustomerBean.java index 09023431..4826a9b3 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/CustomerBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/CustomerBean.java @@ -23,5 +23,5 @@ import org.mxchange.pizzaapplication.customer.Customer; * * @author Roland Haeder */ -public interface CustomerBean extends Customer, Iterable { +public interface CustomerBean extends Customer { } diff --git a/src/java/org/mxchange/pizzaapplication/customer/bean/PizzaServiceCustomerBean.java b/src/java/org/mxchange/pizzaapplication/customer/bean/PizzaServiceCustomerBean.java index 931880a5..98a5a6a9 100644 --- a/src/java/org/mxchange/pizzaapplication/customer/bean/PizzaServiceCustomerBean.java +++ b/src/java/org/mxchange/pizzaapplication/customer/bean/PizzaServiceCustomerBean.java @@ -16,7 +16,9 @@ */ package org.mxchange.pizzaapplication.customer.bean; +import java.lang.reflect.Field; import java.util.Iterator; +import java.util.Map; import org.mxchange.jcore.client.Client; import org.mxchange.jcore.contact.Contact; import org.mxchange.jcore.contact.Gender; @@ -233,7 +235,7 @@ public class PizzaServiceCustomerBean extends BasePizzaServiceSystem implements } @Override - public Iterator iterator () { + public Iterator> iterator () { // Deligate to "hidden" object return this.getContact().iterator(); } diff --git a/web/finished.jsp b/web/finished.jsp index 6adaf51a..ab7d9498 100644 --- a/web/finished.jsp +++ b/web/finished.jsp @@ -4,6 +4,7 @@ Author : Roland Haeder --%> +<%@page import="java.lang.reflect.Field"%> <%--<%@page errorPage="errorHandler.jsp" %>--%> <%@page contentType="text/html" pageEncoding="UTF-8"%> @@ -128,14 +129,19 @@ <% - Iterator fieldIterator = customer.iterator(); + Iterator> fieldIterator = customer.iterator(); %>
    <% while (fieldIterator.hasNext()) { - %> -
  • <%=fieldIterator.next()%>
  • - <% + Map.Entry entry = fieldIterator.next(); + %> +
  • <%=entry.getKey().getName()%> set to: <%=entry.getValue()%>
  • + <% + // Set it in session + synchronized (session) { + session.setAttribute(entry.getKey().getName(), entry.getValue()); + } } %>
@@ -149,6 +155,7 @@ out.print("Ihre Bestellung ist eingegangen."); synchronized(session) { //session.invalidate(); + out.print("
Zu Demo-Zwecken wird die Sitzung nicht gekillt. Bitte 'Bestellung' aufrufen, um zu gucken, dass die Daten da sind.
"); } } else { // Nothing ordered diff --git a/web/order.jsp b/web/order.jsp index 77324721..35d6ec23 100644 --- a/web/order.jsp +++ b/web/order.jsp @@ -138,10 +138,19 @@ Gender[] genders = Gender.values(); for (final Gender gender : genders) { %> - + <% } - %> + %>
@@ -152,7 +161,7 @@
- /> + " <%=app.getDisabledHtmlFromSession(request, session)%> />
@@ -162,7 +171,7 @@
- /> + " <%=app.getDisabledHtmlFromSession(request, session)%> />
@@ -172,7 +181,7 @@
- /> + " <%=app.getDisabledHtmlFromSession(request, session)%> />
@@ -182,7 +191,7 @@
- /> + " <%=app.getDisabledHtmlFromSession(request, session)%> />
@@ -192,7 +201,7 @@
- /> + " <%=app.getDisabledHtmlFromSession(request, session)%> />
@@ -202,7 +211,7 @@
- /> + " <%=app.getDisabledHtmlFromSession(request, session)%> />
@@ -212,7 +221,7 @@
- /> + " <%=app.getDisabledHtmlFromSession(request, session)%> />
@@ -222,7 +231,7 @@
- /> + " <%=app.getDisabledHtmlFromSession(request, session)%> />
@@ -232,7 +241,7 @@
- /> + " <%=app.getDisabledHtmlFromSession(request, session)%> />
@@ -242,7 +251,7 @@
- /> + " <%=app.getDisabledHtmlFromSession(request, session)%> />