From 9527ce19be444ab3ea72e12d2362b64387473d69 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Fri, 7 Aug 2015 14:54:53 +0200 Subject: [PATCH] =?utf8?q?Continued=20with=20project:=20-=20rewrote=20iter?= =?utf8?q?ation=20over=20Contact=20fields=20=20with=20an=20iterator=20from?= =?utf8?q?=20Map.Entry=20-=20added=20default=20values=20in=20orderjsp=20(t?= =?utf8?q?o=20output=20session-stored=20values)=20-=20introduced=20getPrin?= =?utf8?q?tableValueFromSession()=20which=20returns=20a=20value=20from=20s?= =?utf8?q?ession=20key=20or=20an=20empty=20string=20for=20null=20-=20intro?= =?utf8?q?duced=20getValueFromSession()=20which=20is=20a=20low-level=20met?= =?utf8?q?hod=20to=20get=20a=20value=20from=20session=20key=20(but=20sychr?= =?utf8?q?onized)=20Signed-off-by:Roland=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../application/PizzaApplication.java | 9 +++ .../application/PizzaServiceApplication.java | 73 +++++++++++++++++-- .../pizzaapplication/beans/CustomerBean.java | 2 +- .../bean/PizzaServiceCustomerBean.java | 4 +- web/finished.jsp | 15 +++- web/order.jsp | 33 ++++++--- 6 files changed, 110 insertions(+), 26 deletions(-) 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)%> />
-- 2.39.5