BrightSide Workbench Full Report + Source Code
UsageGrid.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.erp.task.usage;
20 
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import org.turro.elephant.context.Application;
24 import org.turro.elephant.util.DecimalFormats;
25 import org.turro.elephant.util.Images;
26 import org.turro.erp.entity.RequiredUsage;
27 import org.turro.erp.entity.Resource;
28 import org.turro.erp.entity.Task;
29 import org.turro.erp.humanres.HumanResourceAptitudeCombobox;
30 import org.turro.erp.resource.ResourceCombobox;
31 import org.turro.financials.contract.ProviderCombobox;
32 import org.turro.financials.entity.Contract;
33 import org.turro.financials.entity.LineType;
34 import org.turro.financials.linetype.BillLineTypeListbox;
35 import org.turro.financials.model.document.LineTypeDescription;
36 import org.turro.financials.product.IProduct;
37 import org.turro.financials.product.ProductExtendedCombobox;
38 import org.turro.hr.humanres.HumanResourceAptitude;
39 import org.turro.jpa.entity.EntityCollectionUtil;
40 import org.turro.zkoss.dialog.SelectionDialog;
41 import org.turro.zkoss.grid.CollectionGrid;
42 import org.turro.zkoss.grid.EditableCell;
43 import org.turro.zkoss.grid.EditableColumn;
44 import org.turro.zkoss.input.Percentbox;
45 import org.zkoss.zk.ui.HtmlBasedComponent;
46 import org.zkoss.zk.ui.event.Event;
47 import org.zkoss.zk.ui.event.EventListener;
48 import org.zkoss.zk.ui.event.Events;
49 import org.zkoss.zul.Button;
50 import org.zkoss.zul.Row;
51 
56 public class UsageGrid extends CollectionGrid<RequiredUsage> {
57 
58  private boolean product;
59 
60  private Task task;
61 
62  public boolean isProduct() {
63  return product;
64  }
65 
66  public void setProduct(boolean product) {
67  this.product = product;
68  }
69 
70  public Task getTask() {
71  return task;
72  }
73 
74  public void setTask(Task task) {
75  this.task = task;
76  }
77 
78  @Override
79  protected void initiateRow(Row row, RequiredUsage value) {
80  if(value == null) {
81  value = new RequiredUsage();
82  value.setTask(task);
83  task.getRequiredUsages().add(value);
84  if(product) {
85  value.prepareProduct();
86  } else {
87  value.prepareHumanResource();
88  }
89  }
90  row.setValue(value);
91  }
92 
93  @Override
94  protected boolean deleteRow(Row row) {
95  RequiredUsage ru = (RequiredUsage) row.getValue();
96  if(ru.getOrderItems().isEmpty()) {
97  EntityCollectionUtil.remove(task.getRequiredUsages(), ru);
98  ru.setTask(null);
99  return true;
100  } else {
101  return false;
102  }
103  }
104 
105  @Override
106  protected boolean isValid(RequiredUsage v) {
107  return !v.isEmpty();
108  }
109 
110  @Override
111  public void afterCompose() {
112  addColumns();
114  super.afterCompose();
115  }
116 
117  @Override
118  protected boolean filterValue(RequiredUsage v) {
119  return (v.isProductType() && product) || (v.isHumanResourceType() && !product);
120  }
121 
122  @Override
123  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
124  if(editableCell.getColumn() instanceof EditableColumn) {
125  EditableColumn ec = (EditableColumn) editableCell.getColumn();
126  RequiredUsage ru = editableCell.getRow().getValue();
127  if(ec.getJavaClass().equals(HumanResourceAptitude.class)) {
128  Object value = getCellValue(editableCell);
130  hrac.setTask(task);
131  if(value != null) hrac.setObjectValue((HumanResourceAptitude) value);
132  return hrac;
133  } else if(ec.getJavaClass().equals(Resource.class)) {
134  Object value = getCellValue(editableCell);
136  if(value != null) rcb.setObjectValue((Resource) value);
137  return rcb;
138  } else if(ec.getJavaClass().equals(Contract.class)) {
139  Object value = getCellValue(editableCell);
141  if(value != null) pcb.setObjectValue((Contract) value);
142  return pcb;
143  } else if(ec.getJavaClass().equals(IProduct.class)) {
144  Object value = getCellValue(editableCell);
146  pecb.setContractorId(ru.getProviderId());
147  if(value != null) pecb.setObjectValue((IProduct) value);
148  return pecb;
149  } else if(ec.getJavaClass().equals(LineType.class)) {
150  Object value = getCellValue(editableCell);
152  bltlb.setMold("select");
154  return bltlb;
155  } else if(editableCell.getColumn().getLabel().equals(Application.getString("lDedication"))) {
156  Object value = getCellValue(editableCell);
157  Percentbox pb = new Percentbox();
158  if(value != null) pb.setValue((Double) value);
159  return pb;
160  }
161  }
162  return super.createEditor(editableCell);
163  }
164 
165  @Override
166  protected HtmlBasedComponent createRenderer(EditableCell editableCell) {
167  if(editableCell.getColumn() instanceof EditableColumn) {
168  EditableColumn ec = (EditableColumn) editableCell.getColumn();
169  final RequiredUsage ru = editableCell.getRow().getValue();
170  if(ec.getJavaClass().equals(String.class)) {
171  Button edit = new Button(null, Images.getImage("edit"));
172  edit.addEventListener(Events.ON_CLICK, new EventListener() {
173  @Override
174  public void onEvent(Event event) throws Exception {
175  event.stopPropagation();
176  showTree(ru);
177  }
178  });
179  return edit;
180  }
181  }
182  return super.createRenderer(editableCell);
183  }
184 
185  @Override
186  protected String formatCell(EditableCell editableCell, Object value) {
187  if(value instanceof HumanResourceAptitude) {
189  return res.getRealName();
190  } else if(value instanceof Resource) {
191  Resource res = ((Resource) value);
192  return res.getName();
193  } else if(value instanceof Contract) {
194  Contract ctc = ((Contract) value);
195  return ctc.getPartialDescription();
196  } else if(value instanceof IProduct) {
197  IProduct p = ((IProduct) value);
198  return p.getProductString();
199  } else if(value instanceof LineType) {
200  RequiredUsage ru = editableCell.getRow().getValue();
202  } else if(editableCell.getColumn().getLabel().equals(Application.getString("lDedication"))) {
203  return DecimalFormats.format(((Double) value) * 100.0) + "%";
204  }
205  return super.formatCell(editableCell, value);
206  }
207 
208  @Override
209  protected Object getEditorValue(EditableCell editableCell) {
210  if(editableCell.getJavaClass().equals(LineType.class)) {
211  RequiredUsage ru = editableCell.getRow().getValue();
212  BillLineTypeListbox bltlb = (BillLineTypeListbox) editableCell.getEditor();
213  LineType lt = bltlb.getObjectValue();
214  if(lt != null) {
215  ru.setLineTypeId(lt.getId());
216  if(lt.getContractPreference() != null) {
218  }
219  }
220  return lt;
221  }
222  return super.getEditorValue(editableCell);
223  }
224 
225  private void addColumns() {
226  try {
227  if(!product) {
228  addColumn(Application.getString("lHumanResource"), HumanResourceAptitude.class, "humanResourceAptitude", null, 2, false, false).setHflex("1");
229  addColumn(Application.getString("lVariable"), "boolean", "canChange", null, 0, false, false).setWidth("70px");
230  addColumn(Application.getString("lResource"), Resource.class, "resource", null, 2, false, false).setHflex("1");
231  addColumn(Application.getString("lHours"), "double", "units", null, 2, false, false).setWidth("60px");
232  addColumn(Application.getString("lDedication"), "double", "dedication", null, 2, false, false).setWidth("80px");
233  addColumn(Application.getString("lCost"), "double", "cost", null, 3, false, false).setWidth("90px");
234  addColumn(Application.getString("lMaximum"), "boolean", "costMaximum", null, 0, false, false).setWidth("70px");
235  } else {
236  addColumn(Application.getString("lProvider"), Contract.class, "provider", null, 2, false, false).setHflex("2");
237  addColumn(Application.getString("lFixed"), "boolean", "providerFixed", null, 0, false, false).setWidth("70px");
238  addColumn(Application.getString("lProduct"), IProduct.class, "IProduct", null, 2, false, false).setHflex("2");
239  addColumn(Application.getString("lType"), LineType.class, "lineType", null, 2, false, false).setHflex("1");
240  addColumn(Application.getString("lQuantity"), "double", "units", null, 3, false, false).setWidth("70px");
241  addColumn(Application.getString("lCost"), "double", "cost", null, 3, false, false).setWidth("90px");
242  }
243  addColumn("", String.class, null, null, 2, false, true).setWidth("50px");
244  } catch (ClassNotFoundException ex) {
245  Logger.getLogger(UsageGrid.class.getName()).log(Level.SEVERE, null, ex);
246  }
247  }
248 
249  private void showTree(RequiredUsage ru) {
250  if(!ru.isEmpty()) {
251  UsageTree usageTree = new UsageTree();
252  usageTree.setRequiredUsage(ru);
253  SelectionDialog.showComponent(getPage(), Application.getString("lRequiredUsages"), usageTree, "95%", "600px", null);
254  }
255  }
256 
257 }
static String getString(String key)
static String format(Number value, String pattern)
static String getImage(String image)
Definition: Images.java:36
void setContractPreferenceId(long contractPreferenceId)
void setLineTypeId(long lineTypeId)
void initiateRow(Row row, RequiredUsage value)
Definition: UsageGrid.java:79
HtmlBasedComponent createEditor(EditableCell editableCell)
Definition: UsageGrid.java:123
boolean filterValue(RequiredUsage v)
Definition: UsageGrid.java:118
boolean isValid(RequiredUsage v)
Definition: UsageGrid.java:106
Object getEditorValue(EditableCell editableCell)
Definition: UsageGrid.java:209
void setProduct(boolean product)
Definition: UsageGrid.java:66
HtmlBasedComponent createRenderer(EditableCell editableCell)
Definition: UsageGrid.java:166
String formatCell(EditableCell editableCell, Object value)
Definition: UsageGrid.java:186
ContractPreference getContractPreference()
Definition: LineType.java:77
void setData(Contract contract, long lineTypeId, long contractPreferenceId)
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)