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 | 0 | 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 | 0 | JammyjoesDatabase db = (JammyjoesDatabase) melati.getDatabase(); |
32 | 0 | Capability affiliate = db.getAffiliateCapability(); |
33 | 0 | User user = (User)PoemThread.accessToken(); |
34 | 0 | if (!user.givesCapability(affiliate)) |
35 | 0 | throw new AccessPoemException(user,affiliate); |
36 | |
|
37 | 0 | context.put("affiliate", user.getAffiliate()); |
38 | |
|
39 | 0 | if (Form.getBooleanField(context,"materials").booleanValue()) { |
40 | 0 | return "view/AffiliateMaterials.wm"; |
41 | |
} |
42 | |
|
43 | 0 | String template = "AffiliateHome.wm"; |
44 | 0 | context.put("date", Form.getFormNulled(context,"date")); |
45 | |
|
46 | 0 | boolean detail = Form.getBooleanField(context,"detail").booleanValue(); |
47 | |
|
48 | 0 | TimesliceFactory factory = null; |
49 | 0 | Date startDate = rawFrom(context, "date"); |
50 | 0 | if (startDate == null) { |
51 | 0 | Calendar calendar = new GregorianCalendar(); |
52 | 0 | calendar.setTime(new Date()); |
53 | 0 | calendar.add(Calendar.YEAR, -1); |
54 | 0 | startDate = calendar.getTime(); |
55 | 0 | factory = new MonthlyTimesliceFactory(startDate); |
56 | 0 | } else { |
57 | 0 | factory = new DailyTimesliceFactory(startDate); |
58 | 0 | template = "AffiliateHomeDay.wm"; |
59 | |
} |
60 | |
|
61 | 0 | if (detail) { |
62 | 0 | template = "AffiliateDayDetail.wm"; |
63 | |
} |
64 | |
|
65 | 0 | Selection hitsSelection = new HitsSelection(db.getAffiliateTransactionTable(), user.getAffiliate(), factory.getStartDate(), factory.getEndDate()); |
66 | 0 | Presenter hitsPresenter = |
67 | |
new AffiliateStatsPresenter("hits", context, factory, hitsSelection, detail); |
68 | 0 | hitsPresenter.handleInteraction(hitsPresenter.createSelection(), hitsPresenter.createCommand()); |
69 | |
|
70 | 0 | Selection ordersSelection = new OrderSelection(db.getShopOrderTable(),db.getOrderStatusTable().paidStatuses(), user.getAffiliate(), factory.getStartDate(), factory.getEndDate()); |
71 | 0 | Presenter ordersPresenter = |
72 | |
new AffiliateStatsPresenter("orders", context, factory, ordersSelection, detail); |
73 | 0 | ordersPresenter.handleInteraction(ordersPresenter.createSelection(), ordersPresenter.createCommand()); |
74 | |
|
75 | 0 | return "view/" + template; |
76 | |
} |
77 | |
|
78 | 0 | private static SimpleDateFormat dateFormatter = |
79 | |
new SimpleDateFormat("MM/yyyy"); |
80 | |
|
81 | |
public Date rawFrom(ServletTemplateContext context, String fieldName) { |
82 | 0 | String value = Form.getFormNulled(context,fieldName); |
83 | 0 | if (value == null) return null; |
84 | 0 | Date date = dateFormatter.parse(value, new ParsePosition(0)); |
85 | 0 | return new Date(date.getTime()); |
86 | |
} |
87 | |
|
88 | |
} |