BrightSide Workbench Full Report + Source Code
CompanyGrid.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 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 package org.turro.financials.business;
19 
20 import java.util.Iterator;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import org.turro.string.Strings;
24 import org.turro.action.Contacts;
25 import org.turro.contacts.Contact;
26 import org.turro.contacts.util.ContactCombobox;
27 import org.turro.elephant.context.ElephantContext;
28 import org.turro.financials.db.FinancialsPU;
29 import org.turro.financials.entity.Company;
30 import org.turro.financials.entity.Department;
31 import org.turro.financials.entity.Headquarters;
32 import org.turro.financials.entity.Service;
33 import org.turro.i18n.I_;
34 import org.turro.jpa.grid.JpaGrid;
35 import org.turro.plugin.contacts.IContact;
36 import org.turro.zkoss.grid.EditableCell;
37 import org.zkoss.zk.ui.HtmlBasedComponent;
38 import org.zkoss.zul.Row;
39 
44 public class CompanyGrid extends JpaGrid<Company> {
45 
46  public CompanyGrid() {
47  super(new FinancialsPU(),
48  "select company from Company as company " +
49  "order by company.name", false);
50  setDefaultBehavior(false);
51  addColumns();
52  }
53 
54  @Override
55  public void save() {
56  for(Company c : getValues()) {
57  Iterator<Service> its = c.getServices().iterator();
58  while(its.hasNext()) {
59  Service s = its.next();
60  if(Strings.isBlank(s.getName())) {
61  its.remove();
62  }
63  }
64  Iterator<Headquarters> ith = c.getHeadquarters().iterator();
65  while(ith.hasNext()) {
66  Headquarters h = ith.next();
67  if(Strings.isBlank(h.getIdContact())) {
68  ith.remove();
69  } else {
70  Iterator<Department> itd = h.getDepartments().iterator();
71  while(itd.hasNext()) {
72  Department d = itd.next();
73  if(Strings.isBlank(d.getName())) {
74  itd.remove();
75  }
76  }
77  }
78  }
79  }
80  super.save();
81  }
82 
83  @Override
84  protected void initiateRow(Row row, Company value) {
85  if(value == null) value = new Company();
86  row.setValue(value);
87  addDetail(row);
88  }
89 
90  @Override
91  protected boolean isValid(Company v) {
92  return !Strings.isBlank(v.getName());
93  }
94 
95  @Override
96  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
97  if(editableCell.getCellIndex() == 1) {
98  Object value = getCellValue(editableCell);
99  ContactCombobox cdc = new ContactCombobox();
100  if(value != null) cdc.setIdContact(((Company) editableCell.getRow().getValue()).getIdContact());
101  return cdc;
102  }
103  return super.createEditor(editableCell);
104  }
105 
106  @Override
107  protected Object getEditorValue(EditableCell editableCell) {
108  if(editableCell.getCellIndex() == 1) {
109  HtmlBasedComponent hbc = (HtmlBasedComponent) editableCell.getEditor();
110  if(hbc instanceof ContactCombobox) {
111  Contact c = ((ContactCombobox) hbc).getContact();
112  if(c != null) {
113  ((Company) editableCell.getRow().getValue()).setName(c.getName());
114  return c.getId();
115  }
116  }
117  return null;
118  }
119  return super.getEditorValue(editableCell);
120  }
121 
122  @Override
123  protected String formatCell(EditableCell editableCell, Object value) {
124  if(editableCell.getCellIndex() == 1) {
125  IContact ci = Contacts.getContactById((String) value);
126  if(ci.isValid()) {
127  return ci.getName();
128  }
129  return "";
130  }
131  return super.formatCell(editableCell, value);
132  }
133 
134  private void addColumns() {
135  try {
136  addDetailColumn();
137  addColumn(I_.get("Company"), String.class,
138  "idContact", null, 0, false, false).setImage("/_zul/images/b_company.png");
139  addColumn(I_.get("Structure margin"), "double",
140  "structureMargin", "0.00", 1, false, false).setWidth("160px");
141  addColumn(I_.get("Profit margin"), "double",
142  "profitMargin", "0.00", 1, false, false).setWidth("160px");
143  addColumn(I_.get("Always apply"), "boolean",
144  "alwaysApply", null, 0, false, false).setWidth("160px");
145  } catch (ClassNotFoundException ex) {
146  Logger.getLogger(CompanyGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
147  }
148  }
149 
150  private void addDetail(Row row) {
151  row.getChildren().clear();
152  CompanyDetail detail = new CompanyDetail(row, this);
153  row.appendChild(detail);
154  }
155 
156 }
static IContact getContactById(String id)
Definition: Contacts.java:72
Object getEditorValue(EditableCell editableCell)
void initiateRow(Row row, Company value)
HtmlBasedComponent createEditor(EditableCell editableCell)
String formatCell(EditableCell editableCell, Object value)
static String get(String msg)
Definition: I_.java:41
EditableColumn addColumn(String label, Class javaClass, String property, String format, int scale, boolean onlyDate, boolean readOnly)
Object getCellValue(EditableCell editableCell)
void setDefaultBehavior(boolean defaultBehavior)