1 package org.paneris.jammyjoes.controller; 2 3 import java.text.ParsePosition; 4 import java.text.SimpleDateFormat; 5 import java.util.Calendar; 6 import java.util.Date; 7 import java.util.GregorianCalendar; 8 9 import org.melati.Melati; 10 import org.melati.servlet.Form; 11 import org.melati.poem.AccessPoemException; 12 import org.melati.poem.Capability; 13 import org.melati.poem.PoemThread; 14 import org.melati.template.ServletTemplateContext; 15 import org.paneris.jammyjoes.model.JammyjoesDatabase; 16 import org.paneris.jammyjoes.model.User; 17 import org.paneris.jammyjoes.mvp.AffiliateStatsPresenter; 18 import org.paneris.jammyjoes.mvp.HitsSelection; 19 import org.paneris.jammyjoes.mvp.OrderSelection; 20 import org.paneris.jammyjoes.mvp.Presenter; 21 import org.paneris.jammyjoes.mvp.Selection; 22 import org.paneris.jammyjoes.mvp.TimesliceFactory; 23 import org.paneris.jammyjoes.servlet.JammyJoesMelatiServlet; 24 25 public class AffiliateHome extends JammyJoesMelatiServlet { 26 27 private static final long serialVersionUID = 1L; 28 29 protected String jammyjoesRequest(Melati melati, ServletTemplateContext context) throws Exception { 30 31 JammyjoesDatabase db = (JammyjoesDatabase) melati.getDatabase(); 32 Capability affiliate = db.getAffiliateCapability(); 33 User user = (User)PoemThread.accessToken(); 34 if (!user.givesCapability(affiliate)) 35 throw new AccessPoemException(user,affiliate); 36 37 context.put("affiliate", user.getAffiliate()); 38 39 if (Form.getBooleanField(context,"materials").booleanValue()) { 40 return "view/AffiliateMaterials.wm"; 41 } 42 43 String template = "AffiliateHome.wm"; 44 context.put("date", Form.getFormNulled(context,"date")); 45 46 boolean detail = Form.getBooleanField(context,"detail").booleanValue(); 47 48 TimesliceFactory factory = null; 49 Date startDate = rawFrom(context, "date"); 50 if (startDate == null) { 51 Calendar calendar = new GregorianCalendar(); 52 calendar.setTime(new Date()); 53 calendar.add(Calendar.YEAR, -1); 54 startDate = calendar.getTime(); 55 factory = new MonthlyTimesliceFactory(startDate); 56 } else { 57 factory = new DailyTimesliceFactory(startDate); 58 template = "AffiliateHomeDay.wm"; 59 } 60 61 if (detail) { 62 template = "AffiliateDayDetail.wm"; 63 } 64 65 Selection hitsSelection = new HitsSelection(db.getAffiliateTransactionTable(), user.getAffiliate(), factory.getStartDate(), factory.getEndDate()); 66 Presenter hitsPresenter = 67 new AffiliateStatsPresenter("hits", context, factory, hitsSelection, detail); 68 hitsPresenter.handleInteraction(hitsPresenter.createSelection(), hitsPresenter.createCommand()); 69 70 Selection ordersSelection = new OrderSelection(db.getShopOrderTable(),db.getOrderStatusTable().paidStatuses(), user.getAffiliate(), factory.getStartDate(), factory.getEndDate()); 71 Presenter ordersPresenter = 72 new AffiliateStatsPresenter("orders", context, factory, ordersSelection, detail); 73 ordersPresenter.handleInteraction(ordersPresenter.createSelection(), ordersPresenter.createCommand()); 74 75 return "view/" + template; 76 } 77 78 private static SimpleDateFormat dateFormatter = 79 new SimpleDateFormat("MM/yyyy"); 80 81 public Date rawFrom(ServletTemplateContext context, String fieldName) { 82 String value = Form.getFormNulled(context,fieldName); 83 if (value == null) return null; 84 Date date = dateFormatter.parse(value, new ParsePosition(0)); 85 return new Date(date.getTime()); 86 } 87 88 }