Coverage Report - org.paneris.rimauresq.Trolley
 
Classes in this File Line Coverage Branch Coverage Complexity
Trolley
0%
0/90
0%
0/16
2
Trolley$1
0%
0/3
N/A
2
Trolley$2
0%
0/3
N/A
2
Trolley$Item
0%
0/11
N/A
2
 
 1  
 /*
 2  
  * Created on 07-Jan-2006
 3  
  */
 4  
 package org.paneris.rimauresq;
 5  
 import java.io.File;
 6  
 import java.text.DateFormat;
 7  
 import java.util.Date;
 8  
 import java.util.Enumeration;
 9  
 import java.util.Hashtable;
 10  
 import java.util.Vector;
 11  
 import org.melati.Melati;
 12  
 import org.melati.servlet.Form;
 13  
 import org.melati.PoemContext;
 14  
 import org.melati.poem.AccessPoemException;
 15  
 import org.melati.poem.AccessToken;
 16  
 import org.melati.poem.ExecutingSQLPoemException;
 17  
 import org.melati.poem.Initialiser;
 18  
 import org.melati.poem.Persistent;
 19  
 import org.melati.poem.PoemTask;
 20  
 import org.melati.poem.PoemThread;
 21  
 import org.melati.poem.Table;
 22  
 import org.melati.poem.ValidationPoemException;
 23  
 import org.melati.servlet.PathInfoException;
 24  
 import org.melati.template.ServletTemplateContext;
 25  
 import org.melati.util.Email;
 26  
 import org.melati.util.MelatiStringWriter;
 27  
 import org.paneris.rimauresq.model.RimauresqDatabase;
 28  
 import org.paneris.rimauresq.model.User;
 29  
 /**
 30  
  * A simple trolley, a cut down version of MelatiShopping,
 31  
  * with underlying data structures compliant with MelatiShopping,
 32  
  * such that upgrade should be feasible.
 33  
  *
 34  
  * @author timp
 35  
  */
 36  
 public class Trolley extends RimauresqServlet {
 37  
 
 38  
   /** Stop Eclipse wingeing. */
 39  
   private static final long serialVersionUID = 1L;
 40  
 
 41  
   /**
 42  
    * A container class to hold an item's data.
 43  
    * @author timp
 44  
    */
 45  
   public class Item {
 46  
     protected Integer productId;
 47  
     protected Integer quantity;
 48  
     
 49  
     /**
 50  
      * Constructor.
 51  
      */
 52  0
     Item(Integer productId, Integer quantity) {
 53  0
       super();
 54  0
       setProductId(productId);
 55  0
       setQuantity(quantity);
 56  0
     }
 57  
     /**
 58  
      * @return Returns the quantity.
 59  
      */
 60  
     public Integer getQuantity() {
 61  0
       return quantity;
 62  
     }
 63  
     /**
 64  
      * @param quantity
 65  
      *          The quantity to set.
 66  
      */
 67  
     protected void setQuantity(Integer quantity) {
 68  0
       this.quantity = quantity;
 69  0
     }
 70  
     /**
 71  
      * @return Returns the productId.
 72  
      */
 73  
     public Integer getProductId() {
 74  0
       return productId;
 75  
     }
 76  
     /**
 77  
      * @param productId The productId to set.
 78  
      */
 79  
     protected void setProductId(Integer productId) {
 80  0
       this.productId = productId;
 81  0
     }
 82  
   }
 83  
 
 84  
   User u;
 85  
 
 86  
   /**
 87  
    * Constructor.
 88  
    */
 89  
   public Trolley() {
 90  0
     super();
 91  0
   }
 92  
 
 93  
   /*
 94  
    * (non-Javadoc)
 95  
    *
 96  
    * @see org.paneris.rimauresq.RimauresqServlet#reallyDoTemplateRequest(org.melati.Melati,
 97  
    *      org.melati.template.ServletTemplateContext)
 98  
    */
 99  
   protected String doRimauresqTemplateRequest(Melati melati,
 100  
       ServletTemplateContext templateContext) throws Exception {
 101  
 
 102  0
     templateContext.put("object", ((RimauresqDatabase)melati.getDatabase()).getThanksPage());
 103  
 
 104  
     
 105  0
     Vector orderedItems = new Vector();
 106  0
     Hashtable items = new Hashtable();
 107  0
     final Table t = melati.getTable();
 108  0
     final ServletTemplateContext tc = templateContext;
 109  
     try {
 110  0
       PoemThread.withAccessToken(AccessToken.root, new PoemTask() {
 111  0
         public void run() {
 112  0
           u = (User)create(t, tc);
 113  0
         }
 114  
       });
 115  0
     } catch (ExecutingSQLPoemException e) {
 116  0
       templateContext.put("error",
 117  
         "Problem creating user:" + e);
 118  0
       return "thanks";
 119  0
   }
 120  0
     for (Enumeration e = melati.getRequest().getParameterNames(); e
 121  0
         .hasMoreElements();) {
 122  0
       String name = (String)e.nextElement();
 123  0
       if (name.startsWith("quantity")) {
 124  0
         String id = name.substring("quantity".length());
 125  0
         Integer idInt = new Integer(id);
 126  0
         Integer quantity = new Integer(1);
 127  0
         String quantityString = Form.getFormNulled(
 128  
                 melati.getServletTemplateContext(), name);
 129  0
         if (quantityString != null) {
 130  0
           quantity = new Integer(quantityString);
 131  0
           Item it = new Item(idInt, quantity);
 132  0
           orderedItems.add(it);
 133  0
           items.put(idInt, quantity);
 134  
         }
 135  
       }
 136  0
     }
 137  0
     templateContext.put("user", u);
 138  
     
 139  0
     if (items.size() > 0) {
 140  0
       templateContext.put("itemsOrdered", Boolean.TRUE);
 141  
     } else {
 142  0
       if (!u.getNews().booleanValue()) {
 143  0
         templateContext.put("illogical", Boolean.TRUE);
 144  0
         return "thanks";
 145  
       }
 146  
     }
 147  0
     templateContext.put("items", items);
 148  0
     templateContext.put("orderedItems", orderedItems);
 149  0
     templateContext.put("comment",
 150  
                         Form.getFormNulled(
 151  
             melati.getServletTemplateContext(), "comment"));
 152  0
     String smtpServer = getSetting(melati,"SMTPServer");
 153  0
     String websiteFromName = getSetting(melati,"WebsiteFromName");
 154  0
     String websiteFromEmail = getSetting(melati,"WebsiteFromEmail");
 155  0
     String from = mailAddress(websiteFromName, websiteFromEmail);
 156  
 
 157  0
     String salesAdminName = getSetting(melati,"SalesAdminName");
 158  0
     String salesAdminEmail = getSetting(melati,"SalesAdminEmail");
 159  0
     String newsAdminName = getSetting(melati,"NewsAdminName");
 160  0
     String newsAdminEmail = getSetting(melati,"NewsAdminEmail");
 161  0
     String to = null;
 162  0
     String subject = null;
 163  0
     if (orderedItems.size() > 0) {
 164  0
       to = mailAddress(salesAdminName, salesAdminEmail);
 165  0
       subject = "Rimauresq Website order";
 166  
     }
 167  0
     if (u.getNews().booleanValue()) {
 168  0
       if (to != null) {
 169  0
         to += ", ";
 170  0
         subject += " and ";
 171  
       } else {
 172  0
         to = "";
 173  0
         subject = "";
 174  
       }
 175  0
       to += mailAddress(newsAdminName, newsAdminEmail);
 176  0
       to += ", ";
 177  0
       to += mailAddress("Tim Pizey", "timp@paneris.org");
 178  0
       subject += "News subscription";
 179  
     }
 180  0
     String replyTo = "";
 181  
     try {
 182  0
       Date now = new Date();
 183  
 
 184  0
       DateFormat df =  DateFormat.getDateInstance();
 185  0
       templateContext.put("now",  df.format(now));
 186  0
       templateContext.put("servlet",this);
 187  0
       templateContext.put("servlet",this);
 188  0
       templateContext.put("from",from);
 189  0
       templateContext.put("to",to);
 190  0
       templateContext.put("replyTo",replyTo);
 191  0
       templateContext.put("subject",subject);
 192  0
       String templateName = "org/paneris/rimauresq/view/Email.wm";
 193  0
       MelatiStringWriter sw = templateEngine.getStringWriter();
 194  0
       templateEngine.expandTemplate(sw,
 195  
                                     templateName,
 196  
                                     templateContext);
 197  0
       String message = sw.toString();
 198  
 
 199  0
       templateName = "org/paneris/rimauresq/view/HtmlEmail.wm";
 200  0
       sw = templateEngine.getStringWriter();
 201  0
       templateEngine.expandTemplate(sw,
 202  
                                     templateName,
 203  
                                     templateContext);
 204  0
       String htmlMessage = sw.toString();
 205  0
       File[] empty = {};
 206  0
       Email.sendAsHtmlWithAttachments(smtpServer, from,
 207  
               to, replyTo,
 208  
               subject,
 209  
               message,
 210  
               htmlMessage,
 211  
               empty, empty);
 212  0
     } catch (Exception e) {
 213  0
       e.printStackTrace(System.err);
 214  0
       templateContext.put("error",
 215  
           "Unexpected error: " + e);
 216  0
     }
 217  
 
 218  0
     return "thanks";
 219  
   }
 220  
   /**
 221  
    * @return a fancy email address
 222  
    */
 223  
   private String mailAddress(String name, String email) {
 224  0
     return name + " <" + email + ">";
 225  
   }
 226  
 
 227  
   /**
 228  
    * Creates a row for a table using field data in a template context.
 229  
    */
 230  
   protected Persistent create(Table table,
 231  
       final ServletTemplateContext context) {
 232  0
     Persistent result = table.create(new Initialiser() {
 233  0
       public void init(Persistent object) throws AccessPoemException,
 234  
           ValidationPoemException {
 235  0
         Form.extractFields(context, object);
 236  0
       }
 237  
     });
 238  0
     result.postEdit(true);
 239  0
     return result;
 240  
   }
 241  
   
 242  
   /**
 243  
    * Hardcode the table.
 244  
    * 
 245  
    * @see org.melati.servlet.ConfigServlet#melatiContext(org.melati.Melati)
 246  
    */
 247  
   protected PoemContext poemContext(Melati melati) throws PathInfoException {
 248  0
     PoemContext it = super.poemContext(melati);
 249  0
     it.setTable("user");
 250  0
     return it;
 251  
   }
 252  
 
 253  
 }