]> git.mxchange.org Git - pizzaservice-war.git/commitdiff
Continued with project:
authorRoland Haeder <roland@mxchange.org>
Fri, 7 Aug 2015 12:54:53 +0000 (14:54 +0200)
committerRoland Haeder <roland@mxchange.org>
Fri, 7 Aug 2015 12:54:53 +0000 (14:54 +0200)
- 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 <roland@mxchange.org>

src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java
src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java
src/java/org/mxchange/pizzaapplication/beans/CustomerBean.java
src/java/org/mxchange/pizzaapplication/customer/bean/PizzaServiceCustomerBean.java
web/finished.jsp
web/order.jsp

index a01c56d8c28fb76c7ddafbc521fd70ae289fe4a3..9cc5ba6d20acdd94ba5c8e767fa9c7345941db39 100644 (file)
@@ -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);
 }
index da4e399166f97bb93e858c59d05181e91834fa27..27afd3217dac325d622534df9734b23db223e7f5 100644 (file)
@@ -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<Object> it = customer.iterator();
+               Iterator<Map.Entry<Field, Object>> it = customer.iterator();
 
                // Output it
                while (it.hasNext()) {
-                       Object field = it.next();
-                       this.getLogger().debug("field=" + field);
+                       Map.Entry<Field, Object> 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);
+       }
 }
index 0902343160cb86c46304c755f4d700fcc62e2b48..4826a9b3315d5d9f7e59ce9c6eed19cede38bcc0 100644 (file)
@@ -23,5 +23,5 @@ import org.mxchange.pizzaapplication.customer.Customer;
  *
  * @author Roland Haeder
  */
-public interface CustomerBean extends Customer, Iterable<Object> {
+public interface CustomerBean extends Customer {
 }
index 931880a510d8f25b44f8ba7f92457b67df9bd4ae..98a5a6a9d2e0ce0fc583a7e26c7bad1f3dedb951 100644 (file)
@@ -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<Object> iterator () {
+       public Iterator<Map.Entry<Field, Object>> iterator () {
                // Deligate to "hidden" object
                return this.getContact().iterator();
        }
index 6adaf51a49490820a668134982069c5b7ed0505f..ab7d949842e876e49a6e5a5ba58198766edb27c3 100644 (file)
@@ -4,6 +4,7 @@
        Author     : Roland Haeder
 --%>
 
+<%@page import="java.lang.reflect.Field"%>
 <%--<%@page errorPage="errorHandler.jsp" %>--%>
 <%@page contentType="text/html" pageEncoding="UTF-8"%>
 
                                                <tr>
                                                        <td colspan="5" class="table_header">
                                                                <%
-                                                               Iterator<Object> fieldIterator = customer.iterator();
+                                                               Iterator<Map.Entry<Field, Object>> fieldIterator = customer.iterator();
                                                                %>
                                                                <ul>
                                                                <%
                                                                while (fieldIterator.hasNext()) {
-                                                               %>
-                                                                       <li><%=fieldIterator.next()%></li>
-                                                               <%
+                                                                       Map.Entry<Field, Object> entry = fieldIterator.next();
+                                                                       %>
+                                                                       <li><%=entry.getKey().getName()%> set to: <%=entry.getValue()%></li>
+                                                                       <%
+                                                                       // Set it in session
+                                                                       synchronized (session) {
+                                                                               session.setAttribute(entry.getKey().getName(), entry.getValue());
+                                                                       }
                                                                }
                                                                %>
                                                                </ul>
                                                                        out.print("Ihre Bestellung ist eingegangen.");
                                                                        synchronized(session) {
                                                                                //session.invalidate();
+                                                                               out.print("<div>Zu Demo-Zwecken wird die Sitzung nicht gekillt. Bitte 'Bestellung' aufrufen, um zu gucken, dass die Daten da sind.</div>");
                                                                        }
                                                                } else {
                                                                        // Nothing ordered
index 77324721f5ca21618a1b78565f5383527ee274ad..35d6ec23f95febf5cfa82ba27514ec90b36aa706 100644 (file)
                                                                                                Gender[] genders = Gender.values();
                                                                                                for (final Gender gender : genders) {
                                                                                                        %>
-                                                                                                       <option value="<%=gender.name()%>"><%=gender%></option>
+                                                                                                       <option value="<%=gender.name()%>"<%
+                                                                                                       synchronized (session) {
+                                                                                                               // Get session value
+                                                                                                               Object value = session.getAttribute("gender");
+                                                                                                               if ((value instanceof Gender) && (value.equals(gender))) {
+                                                                                                                       // Output selected="selected"
+                                                                                                                       out.print("selected=\"selected\"");
+                                                                                                               }
+                                                                                                       }
+                                                                                                       %>><%=gender%></option>
                                                                                                        <%
                                                                                                }
-                                                                                               %>
+                                                                                               %> 
                                                                                        </select>
                                                                                </div>
                                                                                <div class="clear"></div>
                                                                                        <label for="companyname">Firmenname:</label>
                                                                                </div>
                                                                                <div class="data_right">
-                                                                                       <input type="text" id="companyname" name="companyname" size="10" maxlength="255" <%=app.getDisabledHtmlFromSession(request, session)%> />
+                                                                                       <input type="text" id="companyname" name="companyname" size="10" maxlength="255" value="<%=app.getPrintableValeFromSession(session, "companyname")%>" <%=app.getDisabledHtmlFromSession(request, session)%> />
                                                                                </div>
                                                                                <div class="clear"></div>
                                                                        </div>
                                                                                        <label for="surname">Vorname:</label>
                                                                                </div>
                                                                                <div class="data_right">
-                                                                                       <input type="text" id="surname" name="surname" size="10" maxlength="255" <%=app.getDisabledHtmlFromSession(request, session)%> />
+                                                                                       <input type="text" id="surname" name="surname" size="10" maxlength="255" value="<%=app.getPrintableValeFromSession(session, "surname")%>" <%=app.getDisabledHtmlFromSession(request, session)%> />
                                                                                </div>
                                                                                <div class="clear"></div>
                                                                        </div>
                                                                                        <label for="familyname">Nachname:</label>
                                                                                </div>
                                                                                <div class="data_right">
-                                                                                       <input type="text" id="familyname" name="familyname" size="10" maxlength="255" <%=app.getDisabledHtmlFromSession(request, session)%> />
+                                                                                       <input type="text" id="familyname" name="familyname" size="10" maxlength="255" value="<%=app.getPrintableValeFromSession(session, "familyname")%>" <%=app.getDisabledHtmlFromSession(request, session)%> />
                                                                                </div>
                                                                                <div class="clear"></div>
                                                                        </div>
                                                                                        <label for="street">Straße:</label>
                                                                                </div>
                                                                                <div class="data_right">
-                                                                                       <input type="text" id="street" name="street" size="20" maxlength="255" <%=app.getDisabledHtmlFromSession(request, session)%> />
+                                                                                       <input type="text" id="street" name="street" size="20" maxlength="255" value="<%=app.getPrintableValeFromSession(session, "street")%>" <%=app.getDisabledHtmlFromSession(request, session)%> />
                                                                                </div>
                                                                                <div class="clear"></div>
                                                                        </div>
                                                                                        <label for="houseNumber">Hausnummer:</label>
                                                                                </div>
                                                                                <div class="data_right">
-                                                                                       <input type="text" id="houseNumber" name="houseNumber" size="3" maxlength="5" <%=app.getDisabledHtmlFromSession(request, session)%> />
+                                                                                       <input type="text" id="houseNumber" name="houseNumber" size="3" maxlength="5" value="<%=app.getPrintableValeFromSession(session, "houseNumber")%>" <%=app.getDisabledHtmlFromSession(request, session)%> />
                                                                                </div>
                                                                                <div class="clear"></div>
                                                                        </div>
                                                                                        <label for="zipCode">Postleitzahl:</label>
                                                                                </div>
                                                                                <div class="data_right">
-                                                                                       <input type="text" id="zipCode" name="zipCode" size="5" maxlength="6" <%=app.getDisabledHtmlFromSession(request, session)%> />
+                                                                                       <input type="text" id="zipCode" name="zipCode" size="5" maxlength="6" value="<%=app.getPrintableValeFromSession(session, "zipCode")%>" <%=app.getDisabledHtmlFromSession(request, session)%> />
                                                                                </div>
                                                                                <div class="clear"></div>
                                                                        </div>
                                                                                        <label for="city">Ort:</label>
                                                                                </div>
                                                                                <div class="data_right">
-                                                                                       <input type="text" id="city" name="city" size="10" maxlength="255" <%=app.getDisabledHtmlFromSession(request, session)%> />
+                                                                                       <input type="text" id="city" name="city" size="10" maxlength="255" value="<%=app.getPrintableValeFromSession(session, "city")%>" <%=app.getDisabledHtmlFromSession(request, session)%> />
                                                                                </div>
                                                                                <div class="clear"></div>
                                                                        </div>
                                                                                        <label for="phoneNumber">Telefon:</label>
                                                                                </div>
                                                                                <div class="data_right">
-                                                                                       <input type="text" id="phoneNumber" name="phoneNumber" size="20" maxlength="255" <%=app.getDisabledHtmlFromSession(request, session)%> />
+                                                                                       <input type="text" id="phoneNumber" name="phoneNumber" size="20" maxlength="255" value="<%=app.getPrintableValeFromSession(session, "phoneNumber")%>" <%=app.getDisabledHtmlFromSession(request, session)%> />
                                                                                </div>
                                                                                <div class="clear"></div>
                                                                        </div>
                                                                                        <label for="faxNumber">Faxnummer:</label>
                                                                                </div>
                                                                                <div class="data_right">
-                                                                                       <input type="text" id="faxNumber" name="faxNumber" size="20" maxlength="255" <%=app.getDisabledHtmlFromSession(request, session)%> />
+                                                                                       <input type="text" id="faxNumber" name="faxNumber" size="20" maxlength="255" value="<%=app.getPrintableValeFromSession(session, "faxNumber")%>" <%=app.getDisabledHtmlFromSession(request, session)%> />
                                                                                </div>
                                                                                <div class="clear"></div>
                                                                        </div>
                                                                                        <label for="cellphoneNumber">Handy:</label>
                                                                                </div>
                                                                                <div class="data_right">
-                                                                                       <input type="text" id="cellphoneNumber" name="cellphoneNumber" size="20" maxlength="255" <%=app.getDisabledHtmlFromSession(request, session)%> />
+                                                                                       <input type="text" id="cellphoneNumber" name="cellphoneNumber" size="20" maxlength="255" value="<%=app.getPrintableValeFromSession(session, "cellphoneNumber")%>" <%=app.getDisabledHtmlFromSession(request, session)%> />
                                                                                </div>
                                                                                <div class="clear"></div>
                                                                        </div>