BrightSide Workbench Full Report + Source Code
RelationGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2019 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.contacts.form;
20 
21 import java.util.Date;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import org.turro.collections.KeyValueMap;
25 import org.turro.contacts.BusinessRelation;
26 import org.turro.contacts.Contact;
27 import org.turro.contacts.ContactType;
28 import org.turro.contacts.organigram.RelationType;
29 import org.turro.contacts.zul.organigram.OrganigramListbox;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.entities.EntityCombobox;
32 import org.turro.i18n.I_;
33 import org.turro.jpa.entity.EntityCollections;
34 import org.turro.plugin.contacts.ContactRelations;
35 import org.turro.zkoss.grid.CollectionGrid;
36 import org.turro.zkoss.grid.EditableCell;
37 import org.zkoss.zk.ui.HtmlBasedComponent;
38 import org.zkoss.zk.ui.ext.AfterCompose;
39 import org.zkoss.zk.ui.util.Clients;
40 import org.zkoss.zul.Row;
41 
46 public class RelationGrid extends CollectionGrid<BusinessRelation> implements AfterCompose {
47 
48  private Contact contact;
49 
50  public void setContact(Contact contact) {
51  this.contact = contact;
53  }
54 
55  @Override
56  protected boolean isValid(BusinessRelation v) {
57  return !v.isEmpty();
58  }
59 
60  @Override
61  protected void initiateRow(Row row, BusinessRelation value) {
62  if(value == null) {
63  value = new BusinessRelation();
64  value.setContact(contact);
65  contact.getBusinessRelations().add(value);
66  }
67  row.setValue(value);
68  }
69 
70  @Override
71  protected boolean deleteRow(Row row) {
72  BusinessRelation a = (BusinessRelation) row.getValue();
73  EntityCollections.entities(contact.getBusinessRelations()).remove(a);
74  a.setContact(contact);
75  return true;
76  }
77 
78  @Override
79  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
80  if(editableCell.getCellIndex() == 0) {
81  Object value = getCellValue(editableCell);
82  EntityCombobox cdc = new EntityCombobox();
83  cdc.setRoot("contact");
84  if(ContactType.CONTACT_INTERNAL.equals(contact.getType())) {
85  cdc.setParameters(KeyValueMap.of("type", ContactType.CONTACT_INTERNAL.toString()));
86  } else {
87  cdc.setParameters(KeyValueMap.of("type", ContactType.getJuridical()));
88  }
89  cdc.setObjectValue(value);
90  return cdc;
91  } else if(editableCell.getCellIndex() == 1) {
92  Object value = getCellValue(editableCell);
94  ol.setAllowNull(true);
95  ol.setMold("select");
97  ol.setObjectValue(RelationType.getFromString((String) value));
98  return ol;
99  }
100  return super.createEditor(editableCell);
101  }
102 
103  @Override
104  protected Object getEditorValue(EditableCell editableCell) {
105  if(editableCell.getCellIndex() == 0) {
106  EntityCombobox ol = (EntityCombobox) editableCell.getEditor();
107  return checkBusiness(ol.getObjectValue()) ? ol.getObjectValue() : null;
108  } else if(editableCell.getCellIndex() == 1) {
109  OrganigramListbox ol = (OrganigramListbox) editableCell.getEditor();
110  RelationType type = ol.getObjectValue();
111  if(type != null) {
112  return type.getRelationKey();
113  }
114  }
115  return super.getEditorValue(editableCell);
116  }
117 
118  @Override
119  protected String formatCell(EditableCell editableCell, Object value) {
120  if(value instanceof Contact) {
121  return ((Contact) value).getFullName();
122  } else if(editableCell.getCellIndex() == 1) {
123  if(value != null) {
124  return I_.byKey(RelationType.getFromString((String) value).toString());
125  }
126  }
127  return super.formatCell(editableCell, value);
128  }
129 
130  @Override
131  public void afterCompose() {
132  addColumns();
133  super.afterCompose();
134  }
135 
136  private void addColumns() {
137  try {
138  addColumn(I_.get("Company") + "/" + I_.get("Headquarters") +
139  "/" + I_.get("Department"), Contact.class,
140  "business", null, 0, false, false).setHflex("2");
141  addColumn(I_.get("Type"), String.class,
142  "relationType", null, 0, false, false).setHflex("2");
143  addColumn(I_.get("Description"), String.class,
144  "description", null, 0, false, false).setHflex("2");
145  addColumn(I_.get("Start date"), Date.class,
146  "startDate", null, 0, true, false).setHflex("1");
147  addColumn(I_.get("End date"), Date.class,
148  "endDate", null, 0, true, false).setHflex("1");
149  addColumn(I_.get("Preferential"), "boolean",
150  "preferential", null, 0, false, false).setHflex("1");
151  addColumn(I_.get("Validated"), "boolean",
152  "validated", null, 0, false, false).setHflex("1");
153  } catch (ClassNotFoundException ex) {
154  Logger.getLogger(RelationGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
155  }
156  }
157 
158  private boolean checkBusiness(Object business) {
159  if(business instanceof Contact) {
160  boolean right = contact.getId() == null || ContactRelations.checkBusiness(contact.getId(), ((Contact) business).getId());
161  if(!right) Clients.showNotification(I_.get("Wrong relation"));
162  return right;
163  }
164  return false;
165  }
166 
167 }
168 
169 
Set< BusinessRelation > getBusinessRelations()
Definition: Contact.java:379
Object getEditorValue(EditableCell editableCell)
HtmlBasedComponent createEditor(EditableCell editableCell)
boolean isValid(BusinessRelation v)
String formatCell(EditableCell editableCell, Object value)
void initiateRow(Row row, BusinessRelation value)
void setParameters(KeyValueMap kvm)
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)
EditableColumn addColumn(String label, Class javaClass, String property, String format, int scale, boolean onlyDate, boolean readOnly)
Object getCellValue(EditableCell editableCell)
void setCollection(Collection< V > collection)
static RelationType getFromString(String relation)
static Collection< RelationType > getAllowedTops()