BrightSide Workbench Full Report + Source Code
contract/ProductGrid.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.contract;
19 
20 import java.util.logging.Level;
21 import java.util.logging.Logger;
22 import org.amic.util.db.Converter;
23 import org.turro.string.Strings;
24 import org.turro.elephant.context.ElephantContext;
25 import org.turro.financials.db.FinancialsPU;
26 import org.turro.financials.entity.Contract;
27 import org.turro.financials.entity.Product;
28 import org.turro.financials.entity.ProductByContractor;
29 import org.turro.financials.product.ProductCombobox;
30 import org.turro.i18n.I_;
31 import org.turro.zkoss.grid.CollectionGrid;
32 import org.turro.zkoss.grid.EditableCell;
33 import org.zkoss.zk.ui.HtmlBasedComponent;
34 import org.zkoss.zk.ui.ext.AfterCompose;
35 import org.zkoss.zul.Row;
36 
41 public class ProductGrid extends CollectionGrid<ProductByContractor> implements AfterCompose {
42 
43  private Contract contract;
44 
45  public ProductGrid() {
46  super();
47  addColumns();
48  }
49 
50  public Contract getContract() {
51  return contract;
52  }
53 
54  public void setContract(Contract contract) {
55  this.contract = contract;
56  setCollection(new FinancialsPU().getResultList(
57  "select pbc from ProductByContractor pbc " +
58  "where pbc.contract = ?",
59  new Object[] { contract }
60  ));
61  }
62 
63  @Override
64  protected void initiateRow(Row row, ProductByContractor value) {
65  if(value == null) {
66  value = new ProductByContractor();
67  value.setContract(contract);
68  }
69  row.setValue(value);
70  }
71 
72  @Override
73  protected boolean deleteRow(Row row) {
74  Object count = new FinancialsPU().getSingleResultOrNull(
75  "select count(dl) from DocumentLine dl " +
76  "where dl.productByContractor = ?",
77  new Object[] { row.getValue() }
78  );
79  return new Converter(count).getLong() > 0 ? false : true;
80  }
81 
82  @Override
83  protected boolean isValid(ProductByContractor v) {
84  return v.getProduct() != null && ! Strings.isBlank(v.getContractorCode());
85  }
86 
87  @Override
88  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
89  if(editableCell.getCellIndex() == 0) {
90  Object value = getCellValue(editableCell);
91  ProductCombobox cdc = new ProductCombobox();
92  if(value != null) cdc.setObjectValue((Product) value);
93  return cdc;
94  }
95  return super.createEditor(editableCell);
96  }
97 
98  @Override
99  protected String formatCell(EditableCell editableCell, Object value) {
100  if(editableCell.getCellIndex() == 0) {
101  Product prod = ((Product) value);
102  if(prod != null) {
103  return prod.getProductString();
104  }
105  return "";
106  }
107  return super.formatCell(editableCell, value);
108  }
109 
110  private void addColumns() {
111  try {
112  addColumn(I_.get("Product"), Product.class,
113  "product", null, 0, false, false).setWidth("40%");
114  addColumn(I_.get("Identifier"), String.class,
115  "contractorCode", null, 0, false, false).setWidth("20%");
116  addColumn(I_.get("Tax"), "double",
117  "tax", null, 0, false, false).setWidth("20%");
118  addColumn(I_.get("Price"), "double",
119  "price", null, 0, false, false).setWidth("20%");
120  } catch (ClassNotFoundException ex) {
121  Logger.getLogger(ProductGrid.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
122  }
123  }
124 
125 }
void initiateRow(Row row, ProductByContractor value)
String formatCell(EditableCell editableCell, Object value)
HtmlBasedComponent createEditor(EditableCell editableCell)
static String get(String msg)
Definition: I_.java:41
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419
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)