BrightSide Workbench Full Report + Source Code
SaleActionRowComposer.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2012 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.sale;
20 
21 import java.util.Date;
22 import java.util.HashSet;
23 import java.util.Set;
24 import org.amic.util.date.CheckDate;
25 import org.turro.string.Strings;
26 import org.turro.calendar.CalendarEventCallback;
27 import org.turro.calendar.CalendarView;
28 import org.turro.contacts.util.AddressListbox;
29 import org.turro.contacts.util.AttendeesList;
30 import org.turro.crm.db.CrmPU;
31 import org.turro.crm.entity.ProspectType;
32 import org.turro.crm.entity.SaleAction;
33 import org.turro.crm.entity.SaleProspect;
34 import org.turro.crm.entity.TouchPoint;
35 import org.turro.crm.zul.touch.TouchPointListbox;
36 import org.turro.crm.zul.touch.TouchPointRadiogroup;
37 import org.turro.crm.zul.vendor.VendorProspectListbox;
38 import org.turro.elephant.context.Application;
39 import org.turro.elephant.util.Messages;
40 import org.turro.jpa.entity.EntityCollections;
41 import org.turro.util.CompareUtil;
42 import org.zkoss.zk.ui.Component;
43 import org.zkoss.zk.ui.Executions;
44 import org.zkoss.zk.ui.WrongValueException;
45 import org.zkoss.zk.ui.event.InputEvent;
46 import org.zkoss.zk.ui.util.Clients;
47 import org.zkoss.zk.ui.util.GenericForwardComposer;
48 import org.zkoss.zul.A;
49 import org.zkoss.zul.Button;
50 import org.zkoss.zul.Cell;
51 import org.zkoss.zul.Datebox;
52 import org.zkoss.zul.Image;
53 import org.zkoss.zul.Radiogroup;
54 import org.zkoss.zul.Row;
55 import org.zkoss.zul.Textbox;
56 
61 public class SaleActionRowComposer extends GenericForwardComposer {
62 
63  private SaleAction saleAction;
64  private SaleProspect saleProspect;
65  private Datebox fromDate, toDate;
66  private Image fromWatch;
67  private Textbox comment;
68  private VendorProspectListbox vendor;
69  private ActionTypeBox type;
70  private AttendeesList attendees;
71  private Cell touchpointcell;
72  private TouchPointListbox touchpoint;
73  private TouchPointRadiogroup touchpointend;
74  private HashSet<TouchPoint> touchPoints = new HashSet<TouchPoint>();
75  private AddressListbox address;
76  private Radiogroup status;
77  private Button saveButton, cancelButton, delButton;
78  private Row row;
79  private SaleActionGrid grid;
80  private Date lastDate;
81  private A calendar;
82 
83  public void onChanging$comment(InputEvent evt) {
84  updateButtons(true, evt.getValue());
85  }
86 
87  public void onSelect$touchpoint() {
88  updateButtons(true);
89  }
90 
91  public void onCheck$touchpointend() {
92  updateButtons(true);
93  }
94 
95  public void onSelect$address() {
96  updateButtons(true);
97  }
98 
99  public void onSelect$vendor() {
100  updateButtons(true);
101  }
102 
103  public void onChange$type() {
104  updateButtons(true);
105  }
106 
107  public void onChange$attendees() {
108  updateButtons(true);
109  }
110 
111  public void onChange$fromDate() {
112  toDate.setValue(CheckDate.addDayDifference(lastDate, fromDate.getValue(), toDate.getValue()));
113  updateDates();
114  updateButtons(true);
115  }
116 
117  public void onChange$toDate() {
118  updateDates();
119  updateButtons(true);
120  }
121 
122  public void onCheck$status() {
123  updateControls();
124  updateDates();
125  updateButtons(true);
126  }
127 
128  public void onClick$calendar() {
130  @Override
131  public void onCreate(Date from, Date to) {
132  fromDate.setValue(from);
133  toDate.setValue(to);
134  updateDates();
135  updateButtons(true);
136  }
137  @Override
138  public void onUpdate(Date from, Date to) {
139  fromDate.setValue(from);
140  toDate.setValue(to);
141  updateDates();
142  updateButtons(true);
143  }
144  @Override
145  public String getPath() {
146  return CrmPU.getObjectPath(saleAction);
147  }
148  });
149  }
150 
151  public void onClick$saveButton() {
152  saleAction.setActionDate(fromDate.getValue());
153  saleAction.setFinalDate(toDate.getValue());
154  saleAction.setComment(comment.getValue());
155  saleAction.setVendorProspect(vendor.getObjectValue());
156  saleAction.setActionType((Set<String>) type.getCollection());
157  saleAction.setAttendees((HashSet<String>) attendees.getAttendees());
158  if(saleProspect.getType() == ProspectType.TRACING) {
159  saleAction.getTouchPoints().clear();
160  } else {
161  saleAction.setProcessTouchPoints((Set<TouchPoint>) touchpoint.getObjectValues());
162  saleAction.setEndingTouchPoint(touchpointend.getObjectValue());
163  }
164  saleAction.setAddress(address.getObjectValue());
165  saleAction.setStatus(status.getSelectedIndex());
166  if(!saleAction.isEmpty()) {
167  saleAction.prepareSave();
168  boolean reload = saleAction.getId() == 0;
169  saleAction = new CrmPU().saveObject(saleAction);
170  if(reload) {
171  grid.refresh();
172  } else {
173  updateButtons(false);
174  }
175  }
176  }
177 
178  public void onClick$cancelButton() {
179  initControls();
180  updateButtons(false);
181  }
182 
183  public void onClick$delButton() {
184  Messages.confirmDeletion().show(() -> {
186  saleAction.setVendorProspect(null);
187  new CrmPU().executeUpdate("delete from SaleAction as sa where sa = ?", new Object[] { saleAction });
188  row.detach();
189  });
190  }
191 
192  @Override
193  public void doAfterCompose(Component comp) throws Exception {
194  super.doAfterCompose(comp);
195  saleAction = (SaleAction) Executions.getCurrent().getAttribute("saleAction");
196  if(saleAction == null) {
197  saleProspect = (SaleProspect) Executions.getCurrent().getAttribute("saleProspect");
198  if(saleProspect != null) {
199  Date from = (Date) Executions.getCurrent().getAttribute("from"),
200  to = (Date) Executions.getCurrent().getAttribute("to");
201  saleAction = new SaleAction();
202  if(from != null && to != null) {
203  saleAction.setActionDate(from);
204  saleAction.setFinalDate(to);
205  } else {
206  saleAction.setActionDate(new Date());
207  }
208  saleAction.setAttendees(saleProspect.getAttendees());
209  SaleAction last = saleProspect.getSaleActions().isEmpty() ? null : saleProspect.getSaleActions().last();
210  if(last != null) {
211  saleAction.setTouchPoints(last.getTouchPoints());
212  }
213  grid = ((SaleActionGrid) Executions.getCurrent().getAttribute("grid"));
214  }
215  } else {
216  saleProspect = saleAction.getVendorProspect().getSaleProspect();
217  row = ((Row) Executions.getCurrent().getAttribute("row"));
218  }
219  initControls();
220  }
221 
222  private void updateDates() throws WrongValueException {
223  if(CompareUtil.compare(fromDate.getValue(), toDate.getValue()) > 0) {
224  toDate.setValue(fromDate.getValue());
225  }
226  fromWatch.setVisible(fromDate.getValue().before(new Date()) && status.getSelectedIndex() == 0);
227  lastDate = fromDate.getValue();
228  }
229 
230  private void updateControls() {
231  fromDate.setDisabled(status.getSelectedIndex() != 0);
232  toDate.setDisabled(status.getSelectedIndex() != 0);
233  comment.setDisabled(status.getSelectedIndex() != 0);
234  vendor.setDisabled(status.getSelectedIndex() != 0);
235  attendees.setReadOnly(status.getSelectedIndex() != 0);
236  attendees.afterCompose();
237  type.setReadOnly(status.getSelectedIndex() != 0);
238  type.afterCompose();
239  touchpoint.setDisabled(status.getSelectedIndex() != 0);
240  touchpointend.setDisabled(status.getSelectedIndex() != 0);
241  address.setDisabled(status.getSelectedIndex() != 0);
242  }
243 
244  private void updateButtons(boolean visible) {
245  updateButtons(visible, null);
246  }
247 
248  private void updateButtons(boolean visible, String value) {
249  saveButton.setVisible(visible && inputIsValid(value));
250  cancelButton.setVisible(visible);
251  delButton.setVisible(saleAction.getId() > 0 && Application.getApplication().isInRole("sale-action:delete"));
252  }
253 
254  private void initControls() throws WrongValueException {
255  fromDate.setValue(saleAction.getActionDate());
256  lastDate = saleAction.getActionDate();
257  toDate.setValue(saleAction.getFinalDate());
258  updateDates();
259  comment.setValue(saleAction.getComment());
260  vendor.setSaleProspect(saleProspect);
261  vendor.setSelectSelf(true);
262  vendor.setObjectValue(saleAction.getVendorProspect());
263  type.setTypes(new HashSet<String>(saleAction.getActionType()));
264  attendees.setProvider(saleProspect.getCustomer().getIContact());
265  attendees.setAttendees(new HashSet<String>(saleAction.getAttendees()));
266  if(saleProspect.getType() == ProspectType.TRACING) {
267  touchpointcell.setVisible(false);
268  touchpoint.setVisible(false);
269  touchpointend.setVisible(false);
270  } else {
271  touchpoint.setCheckmark(true);
272  touchpoint.setMultiple(true);
273  touchPoints.clear();
274  touchPoints.addAll(saleAction.getProcessTouchPoints());
275  touchpoint.setObjectValues(touchPoints);
276  touchpointend.setAllowNone(true);
277  touchpointend.setObjectValue(saleAction.getEndingTouchPoint());
278  }
279  address.setAllowNull(true);
280  address.setProvider(saleProspect.getCustomer().getIContact());
281  address.setObjectValue(saleAction.getAddress());
282  status.setSelectedIndex(saleAction.getStatus());
283  if(saleAction.getId() > 0) {
284  fromDate.setInplace(true);
285  toDate.setInplace(true);
286  comment.setInplace(true);
287  } else {
288  comment.setFocus(true);
289  Clients.scrollIntoView(status.getParent());
290  }
291  updateDates();
292  updateControls();
293  updateButtons(false);
294  }
295 
296  private boolean inputIsValid(String value) {
297  return fromDate.getValue() != null &&
298  toDate.getValue() != null &&
299  (!Strings.isBlank(value) || !Strings.isBlank(comment.getValue())) &&
300  vendor.getObjectValue() != null &&
301  !type.getCollection().isEmpty();
302  }
303 
304 }
static void showCalendar(CalendarEventCallback eventCreateCallback)
void setProvider(IContact provider)
void setAttendees(Collection< String > attendees)
static String getObjectPath(Object object)
Definition: CrmPU.java:43
void setActionType(Set< String > actionType)
Definition: SaleAction.java:70
void setEndingTouchPoint(TouchPoint tp)
void setAttendees(Set< String > attendees)
Definition: SaleAction.java:86
VendorProspect getVendorProspect()
Set< TouchPoint > getProcessTouchPoints()
void setVendorProspect(VendorProspect vendorProspect)
void setComment(String comment)
Definition: SaleAction.java:94
void setFinalDate(Date finalDate)
void setProcessTouchPoints(Set< TouchPoint > stp)
void setActionDate(Date actionDate)
Definition: SaleAction.java:62
Set< TouchPoint > getTouchPoints()
void setTouchPoints(Set< TouchPoint > touchPoints)
void setAddress(Address address)
void setTypes(HashSet< String > types)
Collection< String > getCollection()
static Messages confirmDeletion()
Definition: Messages.java:87
static EntityCollections entities(Collection values)
void setObjectValues(Collection< V > collection)
void setReadOnly(boolean readOnly)