BrightSide Workbench Full Report + Source Code
DossierParticipantGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2014 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.dossier.zul.dossier;
20 
21 import java.util.Date;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import org.turro.string.Strings;
25 import org.turro.action.Contacts;
26 import org.turro.action.queue.ConstraintKeys;
27 import org.turro.assistant.Assistant;
28 import org.turro.assistant.AssistantSet;
29 import org.turro.assistant.Assistants;
30 import org.turro.contacts.Contact;
31 import org.turro.contacts.util.ContactCombobox;
32 import org.turro.dossier.dossier.ParticipantSet;
33 import org.turro.dossier.entity.CategoryParticipant;
34 import org.turro.dossier.entity.Dossier;
35 import org.turro.dossier.entity.Participant;
36 import org.turro.dossier.entity.ParticipantRole;
37 import org.turro.elephant.context.ElephantContext;
38 import org.turro.elephant.util.Images;
39 import org.turro.entities.EntityCombobox;
40 import org.turro.i18n.I_;
41 import org.turro.jpa.entity.EntityCollections;
42 import org.turro.mail.queue.QueueManager;
43 import org.turro.plugin.contacts.IContact;
44 import org.turro.zkoss.grid.ChangeValueCallback;
45 import org.turro.zkoss.grid.CollectionGrid;
46 import org.turro.zkoss.grid.EditableCell;
47 import org.turro.zkoss.grid.EditableColumn;
48 import org.zkoss.zk.ui.HtmlBasedComponent;
49 import org.zkoss.zk.ui.event.Event;
50 import org.zkoss.zk.ui.event.Events;
51 import org.zkoss.zk.ui.util.Clients;
52 import org.zkoss.zul.Row;
53 
58 public class DossierParticipantGrid extends CollectionGrid<Participant> {
59 
60  private Dossier dossier;
61 
63  }
64 
65  public DossierParticipantGrid(Dossier dossier) {
66  super(new ParticipantSet<>(dossier.getParticipants()));
67  this.dossier = dossier;
68  addColumns();
69  }
70 
71  public void setDossier(Dossier dossier) {
72  this.dossier = dossier;
74  addColumns();
75  }
76 
77  public void addAttendeesByEntity(EntityCombobox entity, boolean deep) {
78  AssistantSet as = new AssistantSet();
79  Assistants.addAssistantsFromEntity(entity.getEntity(), deep, as, null);
80  for(Assistant a : as) {
81  if(a.relationEntity instanceof Participant) {
82  Participant toClone = (Participant) a.relationEntity;
83  if(!dossier.existsParticipant(toClone)) {
84  Participant p = new Participant();
85  p.setCreation(new Date());
86  p.setDossier(dossier);
87  p.setRole(toClone.getRole());
88  p.setBindingVote(toClone.isBindingVote());
90  p.setIdContact(toClone.getIdContact());
91  p.setName(toClone.getName());
94  p.setShowAllIssues(toClone.isShowAllIssues());
96  p.setDriver(toClone.isDriver());
97  dossier.getParticipants().add(p);
98  }
99  } else if(a.relationEntity instanceof CategoryParticipant) {
100  CategoryParticipant toClone = (CategoryParticipant) a.relationEntity;
101  if(!dossier.existsParticipant(toClone)) {
102  Participant p = new Participant();
103  p.setCreation(new Date());
104  p.setDossier(dossier);
105  p.setRole(toClone.getRole());
106  p.setBindingVote(toClone.isBindingVote());
107  p.setDiscriminator(toClone.getDiscriminator());
108  p.setIdContact(toClone.getIdContact());
109  p.setName(toClone.getName());
112  p.setShowAllIssues(toClone.isShowAllIssues());
114  dossier.getParticipants().add(p);
115  }
116  } else {
117  Participant p = new Participant();
118  p.setCreation(new Date());
119  p.setDossier(dossier);
121  p.setIdContact(a.contact.getId());
122  p.setName(a.contact.getName());
123  if(!dossier.existsParticipant(p)) {
124  dossier.getParticipants().add(p);
125  }
126  }
127  }
130  Events.postEvent(new Event(Events.ON_CHANGE));
131  }
132 
133  @Override
134  protected void initiateRow(Row row, Participant value) {
135  if(value == null) {
136  value = new Participant();
137  value.setCreation(new Date());
138  value.setDossier(dossier);
140  dossier.getParticipants().add(value);
141  }
142  row.setValue(value);
143  }
144 
145  @Override
146  protected boolean deleteRow(Row row) {
147  Participant p = (Participant) row.getValue();
148  EntityCollections.entities(dossier.getParticipants()).remove(p);
149  p.setDossier(null);
150  return true;
151  }
152 
153  @Override
154  protected boolean isValid(Participant v) {
155  return !v.isEmpty();
156  }
157 
158  @Override
159  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
160  if(editableCell.getCellIndex() == 1) {
161  Participant p = (Participant) editableCell.getRow().getValue();
162  Object value = getCellValue(editableCell);
163  ContactCombobox cdc = new ContactCombobox();
164  if(!ParticipantRole.PARTICIPANT_SUBJECT.equals(p.getRole())) {
165  cdc.setRole("dossier:list");
166  }
167  if(value != null) cdc.setIdContact((String) value);
168  return cdc;
169  } else if(editableCell.getCellIndex() == 0) {
170  Object value = getCellValue(editableCell);
172  cit.setMold("select");
173  if(value != null) cit.setObjectValue((ParticipantRole) value);
174  return cit;
175  } else if(editableCell.getCellIndex() == 2) {
176  Object value = getCellValue(editableCell);
178  if(value != null) dcb.setObjectValue((String) value);
179  return dcb;
180  }
181  return super.createEditor(editableCell);
182  }
183 
184  @Override
185  protected String formatCell(EditableCell editableCell, Object value) {
186  if(editableCell.getCellIndex() == 1) {
187  IContact ci = Contacts.getContactById((String) value);
188  if(ci.isValid()) {
189  return ci.getName();
190  }
191  return "";
192  } else if(editableCell.getCellIndex() == 0) {
193  ParticipantRole cit = ((ParticipantRole) value);
194  if(cit != null) {
195  return I_.byKey(cit.toString());
196  }
197  return "";
198  }
199  return super.formatCell(editableCell, value);
200  }
201 
202  @Override
203  protected Object getEditorValue(EditableCell editableCell) {
204  if(editableCell.getCellIndex() == 1) {
205  HtmlBasedComponent hbc = (HtmlBasedComponent) editableCell.getEditor();
206  if(hbc instanceof ContactCombobox) {
207  Contact c = ((ContactCombobox) hbc).getContact();
208  if(c != null) {
209  ((Participant) editableCell.getRow().getValue()).setName(c.getName());
210  return c.getId();
211  }
212  return ((ContactCombobox) hbc).getIdContact();
213  }
214  return null;
215  }
216  return super.getEditorValue(editableCell);
217  }
218 
219  private void addColumns() {
220  try {
221  addColumn(I_.get("Role"), ParticipantRole.class, "role", null, 0, false, false).setHflex("1");
222  EditableColumn col = addColumn(I_.get("Contact"), String.class, "idContact", null, 0, false, false);
224  @Override
225  public boolean check(Object newValue, Object rowValue) {
226  if(!Strings.isBlank((String) newValue) && ((String) newValue).equals(((Participant) rowValue).getIdContact())) {
227  Clients.showNotification(I_.format("%s already exists", ((Participant) rowValue).getName()));
228  return true;
229  }
230  return false;
231  }
232  });
233  col.setHflex("2");
234  addColumn(I_.get("Discriminator"), String.class, "discriminator", null, 0, false, false).setHflex("2");
235  addColumn(Images.getImage("clip"), I_.get("Show all attachments"), "boolean", "showAllAttachments", null, 0, true, false).setWidth("25px");
236  addColumn(Images.getImage("issue"), I_.get("Show all issues"), "boolean", "showAllIssues", null, 0, true, false).setWidth("25px");
237  addColumn(Images.getImage("mail_forward"), I_.get("Receive all emails"), "boolean", "receiveAllEmails", null, 0, true, false).setWidth("25px");
238  addColumn(Images.getImage("contacts"), I_.get("Show participants"), "boolean", "showParticipants", null, 0, true, false).setWidth("25px");
239  addColumn(Images.getImage("accepted"), I_.get("Binding votes"), "boolean", "bindingVote", null, 0, true, false).setWidth("25px");
240  addColumn(Images.getImage("driver"), I_.get("Driver"), "boolean", "driver", null, 0, true, false).setWidth("25px");
241  addColumn(Images.getImage("coordinator"), I_.get("Coordinator"), "boolean", "coordinator", null, 0, true, false).setWidth("25px");
242  addColumn(Images.getImage("beneficiary"), I_.get("Beneficiary"), "boolean", "beneficiary", null, 0, true, false).setWidth("25px");
243  addColumn(Images.getImage("offerer"), I_.get("Offerer"), "boolean", "offerer", null, 0, true, false).setWidth("25px");
244  addColumn(Images.getImage("research"), I_.get("R&D"), "boolean", "research", null, 0, true, false).setWidth("25px");
245  addColumn(Images.getImage("funding"), I_.get("Funding"), "boolean", "funding", null, 0, true, false).setWidth("25px");
246  addColumn(Images.getImage("support"), I_.get("Support"), "boolean", "support", null, 0, true, false).setWidth("25px");
247  addColumn(Images.getImage("consortium"), I_.get("Consortium"), "boolean", "consortium", null, 0, true, false).setWidth("25px");
248  addColumn(Images.getImage("admin"), I_.get("Administrator"), "boolean", "admin", null, 0, true, false).setWidth("25px");
249 // addColumn(I_.get("Show all attachments"), "boolean", "showAllAttachments", null, 0, true, false).setHflex("1");
250 // addColumn(I_.get("Show all issues"), "boolean", "showAllIssues", null, 0, true, false).setHflex("1");
251 // addColumn(I_.get("Receive all emails"), "boolean", "receiveAllEmails", null, 0, true, false).setHflex("1");
252 // addColumn(I_.get("Show participants"), "boolean", "showParticipants", null, 0, true, false).setHflex("1");
253 // addColumn(I_.get("Binding votes"), "boolean", "bindingVote", null, 0, true, false).setHflex("1");
254 // addColumn(I_.get("Driver"), "boolean", "driver", null, 0, true, false).setHflex("1");
255  } catch (ClassNotFoundException ex) {
256  Logger.getLogger(DossierVersionGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
257  }
258  }
259 
260 }
static IContact getContactById(String id)
Definition: Contacts.java:72
static void addAssistantsFromEntity(Object entity, boolean deep, AssistantSet list, Object data)
Definition: Assistants.java:52
Set< Participant > getParticipants()
Definition: Dossier.java:175
boolean existsParticipant(IDossierParticipant participant)
Definition: Dossier.java:413
void setBindingVote(boolean bindingVote)
void setReceiveAllEmails(boolean receiveAllEmails)
void setShowParticipants(boolean showParticipants)
void setDiscriminator(String discriminator)
void setShowAllAttachments(boolean showAllAttachments)
void setIdContact(String idContact)
void setShowAllIssues(boolean showAllIssues)
void setDossier(Dossier dossier)
void setRole(ParticipantRole role)
void addAttendeesByEntity(EntityCombobox entity, boolean deep)
HtmlBasedComponent createEditor(EditableCell editableCell)
String formatCell(EditableCell editableCell, Object value)
static String format(String msg, Object... arguments)
Definition: I_.java:49
static String byKey(String key)
Definition: I_.java:83
static String get(String msg)
Definition: I_.java:41
static EntityCollections entities(Collection values)
void setCollection(Collection< V > collection)
void updateCollection(Collection< V > collection)
void setOnCheckDuplicate(ChangeValueCallback onCheckDuplicate)
EditableColumn addColumn(String label, Class javaClass, String property, String format, int scale, boolean onlyDate, boolean readOnly)
Object getCellValue(EditableCell editableCell)