BrightSide Workbench Full Report + Source Code
ActivityComposer.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.crm.zul.activity;
20 
21 import java.util.Collection;
22 import java.util.HashMap;
23 import org.amic.util.date.CheckDate;
24 import org.turro.action.Contacts;
25 import org.turro.crm.db.CrmPU;
26 import org.turro.crm.entity.Customer;
27 import org.turro.crm.entity.SaleAction;
28 import org.turro.crm.entity.SaleProspect;
29 import org.turro.crm.entity.Vendor;
30 import org.turro.crm.zul.customer.CustomerCombobox;
31 import org.turro.crm.zul.sale.ActionStatus;
32 import org.turro.crm.zul.sale.ActionTypeBox;
33 import org.turro.crm.zul.sale.SaleProspectCombobox;
34 import org.turro.crm.zul.vendor.VendorCombobox;
35 import org.turro.elephant.context.Application;
36 import org.turro.jpa.Dao;
37 import org.turro.plugin.contacts.IContact;
38 import org.turro.vendor.VendorUtil;
39 import org.zkoss.zk.ui.Component;
40 import org.zkoss.zk.ui.Executions;
41 import org.zkoss.zk.ui.util.GenericForwardComposer;
42 import org.zkoss.zul.Chart;
43 import org.zkoss.zul.Datebox;
44 import org.zkoss.zul.SimplePieModel;
45 
50 public class ActivityComposer extends GenericForwardComposer<Component> {
51 
52  private VendorCombobox vendor;
53  private CustomerCombobox customer;
54  private ActionTypeBox type;
55  private SaleProspectCombobox saleProspect;
56  private Datebox begin, end;
57  private ActivityGrid activity;
58  private InactiveProspectGrid inactivity;
59  private Chart byVendor, byCustomer, byParticipant, byType, byStatus;
60 
61  public void onSelect$vendor() {
62  activity.setVendor(vendor.getObjectValue());
63  inactivity.setVendor(activity.getVendor());
64  reload();
65  }
66 
67  public void onSelect$customer() {
68  activity.setCustomer(customer.getObjectValue());
69  reload();
70  }
71 
72  public void onChange$type() {
73  activity.setTypes(type.getTypes());
74  reload();
75  }
76 
77  public void onSelect$saleProspect() {
78  activity.setSaleProspect(saleProspect.getObjectValue());
79  reload();
80  }
81 
82  public void onChange$begin() {
83  activity.setBegin(begin.getValue());
84  reload();
85  }
86 
87  public void onChange$end() {
88  activity.setEnd(end.getValue());
89  reload();
90  }
91 
92  @Override
93  public void doAfterCompose(Component comp) throws Exception {
94  super.doAfterCompose(comp);
95  begin.setValue(new CheckDate().addMonths(-2).getDate());
96  activity.setBegin(begin.getValue());
97  Vendor v = null;
98  if(!Application.getApplication().isInRole("sale-prospect:all")) {
99  v = VendorUtil.getCurrent();
100  vendor.setDisabled(true);
101  } else {
102  v = (Vendor) Executions.getCurrent().getAttribute("vendor");
103  }
104  if(v != null) {
105  vendor.setObjectValue(v);
106  activity.setVendor(v);
107  inactivity.setVendor(v);
108  }
109  Customer c = (Customer) Executions.getCurrent().getAttribute("customer");
110  if(c != null) {
111  customer.setObjectValue(c);
112  activity.setCustomer(c);
113  }
114  SaleProspect sp = (SaleProspect) Executions.getCurrent().getAttribute("saleProspect");
115  if(sp != null) {
116  saleProspect.setObjectValue(sp);
117  activity.setSaleProspect(sp);
118  }
119  reload();
120  }
121 
122  private void reload() {
123  activity.reload();
124  inactivity.reload();
125  fillCharts(activity.getSaleActions());
126  }
127 
128  private void fillCharts(Collection<SaleAction> saleActions) {
129  Dao dao = new CrmPU();
130  HashMap<Long, Long> vendorData = new HashMap<Long, Long>();
131  HashMap<Long, Long> customerData = new HashMap<Long, Long>();
132  HashMap<String, Long> attData = new HashMap<String, Long>();
133  HashMap<String, Long> typeData = new HashMap<String, Long>();
134  HashMap<Integer, Long> statusData = new HashMap<Integer, Long>();
135  for(SaleAction sa : saleActions) {
136  Long vendorCount = vendorData.get(sa.getVendorProspect().getVendor().getId());
137  vendorCount = vendorCount == null ? 1 : vendorCount + 1;
138  vendorData.put(sa.getVendorProspect().getVendor().getId(), vendorCount);
139  Long customerCount = customerData.get(sa.getVendorProspect().getSaleProspect().getCustomer().getId());
140  customerCount = customerCount == null ? 1 : customerCount + 1;
141  customerData.put(sa.getVendorProspect().getSaleProspect().getCustomer().getId(), customerCount);
142  for(String attendee : sa.getAttendees()) {
143  Long attCount = attData.get(attendee);
144  attCount = attCount == null ? 1 : attCount + 1;
145  attData.put(attendee, attCount);
146  }
147  for(String typ : sa.getActionType()) {
148  Long typeCount = typeData.get(typ);
149  typeCount = typeCount == null ? 1 : typeCount + 1;
150  typeData.put(typ, typeCount);
151  }
152  Long statusCount = statusData.get(sa.getStatus());
153  statusCount = statusCount == null ? 1 : statusCount + 1;
154  statusData.put(sa.getStatus(), statusCount);
155  }
156  byVendor.setType(Chart.PIE);
157  SimplePieModel vendorModel = new SimplePieModel();
158  for(Long vid : vendorData.keySet()) {
159  vendorModel.setValue(dao.find(Vendor.class, vid).getName(), vendorData.get(vid));
160  }
161  byVendor.setModel(vendorModel);
162  byCustomer.setType(Chart.PIE);
163  SimplePieModel customerModel = new SimplePieModel();
164  for(Long cid : customerData.keySet()) {
165  customerModel.setValue(dao.find(Customer.class, cid).getName(), customerData.get(cid));
166  }
167  byCustomer.setModel(customerModel);
168  byParticipant.setType(Chart.PIE);
169  IContact contact = Contacts.getEmpty();
170  SimplePieModel attModel = new SimplePieModel();
171  for(String aid : attData.keySet()) {
172  contact.loadById(aid);
173  if(contact.isValid()) {
174  attModel.setValue(contact.getName(), attData.get(aid));
175  }
176  }
177  byParticipant.setModel(attModel);
178  byType.setType(Chart.PIE);
179  SimplePieModel typeModel = new SimplePieModel();
180  for(String tid : typeData.keySet()) {
181  typeModel.setValue(tid, typeData.get(tid));
182  }
183  byType.setModel(typeModel);
184  byStatus.setType(Chart.PIE);
185  SimplePieModel statusModel = new SimplePieModel();
186  for(int sid : statusData.keySet()) {
187  statusModel.setValue(ActionStatus.getStringStatus(sid), statusData.get(sid));
188  }
189  byStatus.setModel(statusModel);
190  }
191 
192 }
Collection< SaleAction > getSaleActions()
void setSaleProspect(SaleProspect saleProspect)
void setTypes(Collection< String > types)
static Vendor getCurrent()
Definition: VendorUtil.java:32