BrightSide Workbench Full Report + Source Code
TechnologyBox.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.Customer;
29 import org.turro.crm.entity.Technology;
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 TechnologyBox extends MultiLabelBox<Technology> {
40 
41  private Customer customer;
42 
43  public void setCustomer(Customer customer) {
44  this.customer = customer;
45  }
46 
47  @Override
48  protected boolean checkVisible() {
49  return true;
50  }
51 
52  @Override
53  protected boolean checkNew() {
54  return true;
55  }
56 
57  @Override
58  protected boolean checkEditable() {
59  return true;
60  }
61 
62  @Override
63  protected boolean checkDelete() {
64  return true;
65  }
66 
67  @Override
68  protected String getNewImage() {
69  return Images.getImage("technology_new");
70  }
71 
72  @Override
73  protected Collection<Technology> getCollection() {
74  return customer.getTechnologies();
75  }
76 
77  @Override
78  protected String convertToString(Technology label) {
79  return label.getName();
80  }
81 
82  @Override
83  protected Collection<String> getLabelChoices() {
84  return new CrmPU().getResultList("select distinct a.name from Technology a");
85  }
86 
87  @Override
88  protected void addChoice(final String choice, final Command command) {
89  for(Technology asl : customer.getTechnologies()) {
90  if(asl.getName().equalsIgnoreCase(choice)) {
91  return;
92  }
93  }
95  "select a from Technology a where a.name = ?",
96  new Object[] { choice });
97  if(as == null) {
98  Messages.confirmAcceptation().add(choice).show(() -> {
99  Technology nas = new Technology();
100  nas.setName(choice);
101  nas = new CrmPU().saveObject(nas);
102  customer.getTechnologies().add(nas);
103  if(command != null) command.execute(null);
104  });
105  } else {
106  customer.getTechnologies().add(as);
107  if(command != null) try {
108  command.execute(null);
109  } catch (Exception ex) {
110  Logger.getLogger(TechnologyBox.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
111  }
112  }
113  }
114 
115  @Override
116  protected void changeChoice(final String oldChoice, String choice, final Command command) {
117  addChoice(choice, new Command() {
118  @Override
119  public Object execute(Context context) {
120  deleteChoice(oldChoice, null);
121  if(command != null) command.execute(null);
122  return true;
123  }
124  });
125  }
126 
127  @Override
128  protected void deleteChoice(String oldChoice, Command command) {
129  Iterator<Technology> it = customer.getTechnologies().iterator();
130  while(it.hasNext()) {
131  if(it.next().getName().equals(oldChoice)) {
132  it.remove();
133  if(command != null) try {
134  command.execute(null);
135  } catch (Exception ex) {
136  Logger.getLogger(TechnologyBox.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
137  }
138  }
139  }
140  }
141 
142 }
143 
Set< Technology > getTechnologies()
Definition: Customer.java:104
void deleteChoice(String oldChoice, Command command)
void addChoice(final String choice, final Command command)
void changeChoice(final String oldChoice, String choice, final Command command)
Collection< Technology > getCollection()
Collection< String > getLabelChoices()
String convertToString(Technology label)
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