1 package org.paneris.jammyjoes.controller; 2 3 4 import java.util.Date; 5 import java.util.Vector; 6 7 import org.melati.Melati; 8 import org.melati.MelatiConfig; 9 import org.melati.poem.AccessPoemException; 10 import org.melati.poem.AccessToken; 11 import org.melati.poem.Capability; 12 import org.melati.poem.PoemThread; 13 import org.melati.servlet.Form; 14 import org.melati.template.ServletTemplateContext; 15 import org.melati.util.ContextUtil; 16 import org.paneris.jammyjoes.model.JammyjoesDatabase; 17 import org.paneris.jammyjoes.model.Product; 18 import org.paneris.jammyjoes.model.StockTransaction; 19 import org.paneris.jammyjoes.servlet.JammyJoesMelatiServlet; 20 21 22 public class StockEntry extends JammyJoesMelatiServlet { 23 24 private static final long serialVersionUID = 1L; 25 26 protected String jammyjoesRequest(Melati melati, ServletTemplateContext context) 27 throws Exception { 28 29 JammyjoesDatabase db = (JammyjoesDatabase) melati.getDatabase(); 30 ServletTemplateContext tc = melati.getServletTemplateContext(); 31 Capability admin = db.getCanAdminister(); 32 AccessToken token = PoemThread.accessToken(); 33 if (!token.givesCapability(admin)) 34 throw new AccessPoemException(token,admin); 35 String template = "view/StockEntry.wm"; 36 String submit = Form.getFormNulled(tc,"submit"); 37 int itemsperpage = Form.getIntegerField(tc,"itemsperpage",new Integer(10)).intValue(); 38 Vector items = new Vector(); 39 boolean allgood = true; 40 41 if (submit == null) { 42 allgood = false; 43 for (int i=0; i<itemsperpage; i++) { 44 items.add(new Item()); 45 } 46 } else { 47 if (submit.equals("Check")) { 48 for (int i=0; i<itemsperpage; i++) { 49 Integer id = Form.getIntegerField(tc,i+""); 50 if (id == null) { 51 items.add(new Item()); 52 } else { 53 Product p = (Product)db.getProductTable().getIdColumn().firstWhereEq(id); 54 if (p == null) { 55 allgood = false; 56 items.add(new Item(id)); 57 } else { 58 items.add(new Item(p)); 59 } 60 } 61 } 62 } 63 if (submit.equals("Confirm")) { 64 allgood = false; 65 for (int i=0; i<itemsperpage; i++) { 66 items.add(new Item()); 67 Integer id = Form.getIntegerField(tc,i+""); 68 if (id != null) { 69 Product p = (Product)db.getProductTable().getIdColumn().firstWhereEq(id); 70 Integer quantity = Form.getIntegerField(tc,"q"+id); 71 java.sql.Date date = (java.sql.Date)MelatiConfig.getSimpleDateAdaptor().rawFrom(melati.getServletTemplateContext(),"field_date"); 72 StockTransaction st = p.newStockTransaction(date, db.getStockTransactionTypeTable().getShopSale(),quantity); 73 db.getStockTransactionTable().create(st); 74 } 75 } 76 } 77 } 78 context.put("allgood",new Boolean(allgood)); 79 context.put("items",items); 80 context.put("util", new ContextUtil()); 81 StockTransaction dummyst = (StockTransaction)db.getStockTransactionTable().newPersistent(); 82 if (Form.getFormNulled(tc,"field_date") == null) { 83 dummyst.setDate(new java.sql.Date(new Date().getTime())); 84 } else { 85 dummyst.setDate((java.sql.Date)MelatiConfig.getSimpleDateAdaptor().rawFrom(melati.getServletTemplateContext(),"field_date")); 86 } 87 context.put("st", dummyst); 88 return template; 89 } 90 91 public class Item { 92 93 private Integer id; 94 private Product product; 95 96 private Item() { 97 id = null; 98 product = null; 99 } 100 101 private Item(Integer id) { 102 this.id = id; 103 product = null; 104 } 105 106 private Item(Product p) { 107 product = p; 108 id = p.getTroid(); 109 } 110 111 public Integer getId() { 112 return id; 113 } 114 115 public Product getProduct() { 116 return product; 117 } 118 } 119 120 }