BrightSide Workbench Full Report + Source Code
ActivityGrid.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.text.DateFormat;
22 import java.util.Collection;
23 import java.util.Date;
24 import org.amic.util.date.DateFormats;
25 import org.turro.contacts.zul.label.ContactInfo;
26 import org.turro.crm.db.CrmPU;
27 import org.turro.crm.entity.Customer;
28 import org.turro.crm.entity.SaleAction;
29 import org.turro.crm.entity.SaleProspect;
30 import org.turro.crm.entity.TouchPoint;
31 import org.turro.crm.entity.Vendor;
32 import org.turro.crm.zul.sale.ActionStatus;
33 import org.turro.elephant.context.Application;
34 import org.turro.elephant.db.SQLHelper;
35 import org.turro.elephant.db.WhereClause;
36 import org.turro.elephant.util.DurationFormats;
37 import org.turro.i18n.I_;
38 import org.turro.jpa.Dao;
39 import org.turro.zkoss.grid.GroupExtended;
40 import org.turro.zkoss.grid.PagingGrid;
41 import org.turro.zkoss.label.LabelTypes;
42 import org.turro.zkoss.label.PreLabel;
43 import org.zkoss.zk.ui.ext.AfterCompose;
44 import org.zkoss.zul.Column;
45 import org.zkoss.zul.Columns;
46 import org.zkoss.zul.Group;
47 import org.zkoss.zul.Hlayout;
48 import org.zkoss.zul.Label;
49 import org.zkoss.zul.Row;
50 import org.zkoss.zul.Rows;
51 import org.zkoss.zul.Space;
52 import org.zkoss.zul.Vlayout;
53 
58 public class ActivityGrid extends PagingGrid implements AfterCompose {
59 
60  private Customer customer;
61  private SaleProspect saleProspect;
62  private Vendor vendor;
63  private Collection<String> types, attendees;
64  private Date begin, end;
65  private Collection<SaleAction> saleActions;
66 
67  public Customer getCustomer() {
68  return customer;
69  }
70 
71  public void setCustomer(Customer customer) {
72  this.customer = customer;
73  }
74 
76  return saleProspect;
77  }
78 
79  public void setSaleProspect(SaleProspect saleProspect) {
80  this.saleProspect = saleProspect;
81  }
82 
83  public Vendor getVendor() {
84  return vendor;
85  }
86 
87  public void setVendor(Vendor vendor) {
88  this.vendor = vendor;
89  }
90 
91  public Collection<String> getTypes() {
92  return types;
93  }
94 
95  public void setTypes(Collection<String> types) {
96  this.types = types;
97  }
98 
99  public Collection<String> getAttendees() {
100  return attendees;
101  }
102 
103  public void setAttendees(Collection<String> attendees) {
104  this.attendees = attendees;
105  }
106 
107  public Date getBegin() {
108  return begin;
109  }
110 
111  public void setBegin(Date begin) {
112  this.begin = begin;
113  }
114 
115  public Date getEnd() {
116  return end;
117  }
118 
119  public void setEnd(Date end) {
120  this.end = end;
121  }
122 
123  public Collection<SaleAction> getSaleActions() {
124  return saleActions;
125  }
126 
127  public void reload() {
128  if(getRows() != null) {
129  getRows().detach();
130  }
131  addRows();
132  }
133 
134  @Override
135  public void afterCompose() {
136  addColumns();
137  }
138 
139  private void addRows() {
140  saleActions = getActivity();
141 
142  Rows rows = new Rows();
143  appendChild(rows);
144 
145  SaleProspect lastSaleProspect = null;
146 
147  for(final SaleAction sa : saleActions) {
148  if(lastSaleProspect == null || sa.getVendorProspect().getSaleProspect().getId() != lastSaleProspect.getId()) {
149  lastSaleProspect = sa.getVendorProspect().getSaleProspect();
150  Group group = new GroupExtended("#" + lastSaleProspect.getId() + " " + lastSaleProspect.getFullDescription());
151  rows.appendChild(group);
152  }
153  Row row = new Row();
154  row.setValign("top");
155  rows.appendChild(row);
156  Vlayout vbox = new Vlayout();
157  Hlayout hbox = new Hlayout();
158  hbox.setSpacing("10px");
159  hbox.appendChild(new Label(DateFormats.format(sa.getActionDate(),
160  DateFormat.SHORT, DateFormat.SHORT, Application.getUsedLocale())));
161  hbox.appendChild(LabelTypes.getSoftLabel(DurationFormats.format(sa.getActionDate(), sa.getFinalDate())));
162  for(String actionType : sa.getActionType()) {
163  hbox.appendChild(LabelTypes.getSoftLabel(actionType));
164  }
165  hbox.appendChild(LabelTypes.getSoftLabel(ActionStatus.getStringStatus(sa)));
166  vbox.appendChild(hbox);
167  vbox.appendChild(new Space());
168  vbox.appendChild(new ContactInfo(sa.getVendorProspect().getVendor().getIdContact()));
169  for(String attendee : sa.getAttendees()) {
170  vbox.appendChild(new ContactInfo(attendee));
171  }
172  row.appendChild(vbox);
173  vbox = new Vlayout();
174  vbox.appendChild(new PreLabel(sa.getComment()));
175  vbox.appendChild(new Space());
176  for(TouchPoint tp : sa.getTouchPoints()) {
177  vbox.appendChild(LabelTypes.getSoftLabel(tp.getName()));
178  }
179  row.appendChild(vbox);
180  }
181 
182  setRowCount(saleActions.size());
183  }
184 
185  private void addColumns() {
186  Columns cols = new Columns();
187  appendChild(cols);
188 
189  Column col = new Column(I_.get("Participants"), null, "40%");
190  cols.appendChild(col);
191 
192  col = new Column(I_.get("Description"), null, "60%");
193  cols.appendChild(col);
194 
195  }
196 
197  private Collection<SaleAction> getActivity() {
198  Dao dao = new CrmPU();
199  return dao.getResultList(getCriteria());
200  }
201 
202  private WhereClause getCriteria() {
203  WhereClause wc = new WhereClause();
204  wc.addClause("select distinct sa from SaleAction sa");
205  wc.addClause("left join sa.vendorProspect vp");
206  wc.addClause("left join vp.saleProspect sp");
207  wc.addClause("left join sa.actionType at");
208  wc.addClause("left join sa.attendees pa");
209  wc.addClause("where 1=1");
210  if(begin != null) {
211  wc.addClause("and sa.actionDate >= :begin");
212  wc.addNamedValue("begin", begin);
213  }
214  if(end != null) {
215  wc.addClause("and sa.actionDate <= :end");
216  wc.addNamedValue("end", end);
217  }
218  if(customer != null) {
219  wc.addClause("and sp.customer = :customer");
220  wc.addNamedValue("customer", customer);
221  }
222  if(vendor != null) {
223  wc.addClause("and vp.vendor = :vendor");
224  wc.addNamedValue("vendor", vendor);
225  }
226  if(saleProspect != null) {
227  wc.addClause("and sp = :saleProspect");
228  wc.addNamedValue("saleProspect", saleProspect);
229  }
230  if(types != null && !types.isEmpty()) {
231  wc.addClause("and at in (" + SQLHelper.convertToIn(types) + ")");
232  }
233  if(attendees != null && !attendees.isEmpty()) {
234  wc.addClause("and pa in (" + SQLHelper.convertToIn(attendees) + ")");
235  }
236  wc.addClause("order by sa.actionDate");
237  return wc;
238  }
239 
240 }
241 
void setAttendees(Collection< String > attendees)
Collection< SaleAction > getSaleActions()
void setSaleProspect(SaleProspect saleProspect)
void setTypes(Collection< String > types)
Rows getRows(boolean create)