BrightSide Workbench Full Report + Source Code
ActivitySectorBox.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.sector;
20 
21 import java.util.Collection;
22 import java.util.Iterator;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import org.turro.command.Command;
26 import org.turro.command.Context;
27 import org.turro.crm.db.CrmPU;
28 import org.turro.crm.entity.ActivitySector;
29 import org.turro.crm.entity.Customer;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.elephant.util.Images;
32 import org.turro.elephant.util.Messages;
33 import org.turro.zkoss.input.MultiLabelBox;
34 
39 public class ActivitySectorBox extends MultiLabelBox<ActivitySector> {
40 
41  private Customer customer;
42  private boolean selfSectors = false;
43 
44  public void setCustomer(Customer customer) {
45  this.customer = customer;
46  }
47 
48  public void setSelfSectors(boolean selfSectors) {
49  this.selfSectors = selfSectors;
50  }
51 
52  @Override
53  protected boolean checkVisible() {
54  return true;
55  }
56 
57  @Override
58  protected boolean checkNew() {
59  return true;
60  }
61 
62  @Override
63  protected boolean checkEditable() {
64  return true;
65  }
66 
67  @Override
68  protected boolean checkDelete() {
69  return true;
70  }
71 
72  @Override
73  protected String getNewImage() {
74  return Images.getImage("sector_new");
75  }
76 
77  @Override
78  protected Collection<ActivitySector> getCollection() {
79  return (selfSectors ? customer.getCustomerSectors() : customer.getActivitySectors());
80  }
81 
82  @Override
83  protected String convertToString(ActivitySector label) {
84  return label.getName();
85  }
86 
87  @Override
88  protected Collection<String> getLabelChoices() {
89  return new CrmPU().getResultList("select distinct a.name from ActivitySector a");
90  }
91 
92  @Override
93  protected void addChoice(final String choice, final Command command) {
94  for(ActivitySector asl : (selfSectors ? customer.getCustomerSectors() : customer.getActivitySectors())) {
95  if(asl.getName().equalsIgnoreCase(choice)) {
96  return;
97  }
98  }
100  "select a from ActivitySector a where a.name = ?",
101  new Object[] { choice });
102  if(as == null) {
103  Messages.confirmAcceptation().add(choice).show(() -> {
104  ActivitySector nas = new ActivitySector();
105  nas.setName(choice);
106  nas = new CrmPU().saveObject(nas);
107  if(selfSectors) {
108  customer.getCustomerSectors().add(nas);
109  } else {
110  customer.getActivitySectors().add(nas);
111  }
112  if(command != null) command.execute(null);
113  });
114  } else {
115  if(selfSectors) {
116  customer.getCustomerSectors().add(as);
117  } else {
118  customer.getActivitySectors().add(as);
119  }
120  try {
121  if(command != null) command.execute(null);
122  } catch (Exception ex) {
123  Logger.getLogger(ActivitySectorBox.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
124  }
125  }
126  }
127 
128  @Override
129  protected void changeChoice(final String oldChoice, String choice, final Command command) {
130  addChoice(choice, new Command() {
131  @Override
132  public Object execute(Context context) {
133  deleteChoice(oldChoice, null);
134  if(command != null) command.execute(null);
135  return true;
136  }
137  });
138  }
139 
140  @Override
141  protected void deleteChoice(String oldChoice, Command command) {
142  Iterator<ActivitySector> it = (selfSectors ?
143  customer.getCustomerSectors().iterator() :
144  customer.getActivitySectors().iterator());
145  while(it.hasNext()) {
146  if(it.next().getName().equals(oldChoice)) {
147  it.remove();
148  if(command != null) try {
149  command.execute(null);
150  return;
151  } catch (Exception ex) {
152  Logger.getLogger(ActivitySectorBox.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
153  }
154  }
155  }
156  }
157 
158 }
Set< ActivitySector > getActivitySectors()
Definition: Customer.java:46
Set< ActivitySector > getCustomerSectors()
Definition: Customer.java:54
String convertToString(ActivitySector label)
void changeChoice(final String oldChoice, String choice, final Command command)
void deleteChoice(String oldChoice, Command command)
Collection< ActivitySector > getCollection()
void addChoice(final String choice, final Command command)
static String getImage(String image)
Definition: Images.java:36
static Messages confirmAcceptation()
Definition: Messages.java:103
Messages add(String word)
Definition: Messages.java:50
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419