BrightSide Workbench Full Report + Source Code
OrderReferenceGrid.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.erp.reference;
19 
20 import java.util.logging.Level;
21 import java.util.logging.Logger;
22 import org.turro.command.Command;
23 import org.turro.command.Context;
24 import org.turro.elephant.context.Application;
25 import org.turro.elephant.util.Images;
26 import org.turro.erp.entity.CustomerOrder;
27 import org.turro.erp.entity.OrderReference;
28 import org.turro.erp.entity.WorkOrder;
29 import org.turro.erp.clone.EntityCloneGrid;
30 import org.turro.erp.sales.PendingCustomerOrderListbox;
31 import org.turro.financials.contract.ServiceStoreListbox;
32 import org.turro.financials.entity.Contract;
33 import org.turro.financials.entity.Product;
34 import org.turro.financials.product.ProductCombobox;
35 import org.turro.jpa.entity.EntityCollectionUtil;
36 import org.turro.zkoss.dialog.SelectionDialog;
37 import org.turro.zkoss.grid.CollectionGrid;
38 import org.turro.zkoss.grid.EditableCell;
39 import org.turro.zkoss.grid.EditableColumn;
40 import org.turro.zul.frame.Framework;
41 import org.zkoss.zk.ui.HtmlBasedComponent;
42 import org.zkoss.zk.ui.event.Event;
43 import org.zkoss.zk.ui.event.EventListener;
44 import org.zkoss.zk.ui.event.Events;
45 import org.zkoss.zul.Button;
46 import org.zkoss.zul.Image;
47 import org.zkoss.zul.Row;
48 
53 public class OrderReferenceGrid extends CollectionGrid<OrderReference> {
54 
55  private WorkOrder workOrder;
56 
57  public OrderReferenceGrid() {
58  }
59 
60  public OrderReferenceGrid(WorkOrder workOrder) {
61  super(workOrder.getOrderReferences());
62  this.workOrder = workOrder;
63  addColumns();
64  }
65 
66  public void setWorkOrder(WorkOrder workOrder) {
67  this.workOrder = workOrder;
68  setCollection(workOrder.getOrderReferences());
69  addColumns();
70  }
71 
72  public void importFromCustomerOrder() {
74  pcolb.setMultiple(true);
75  pcolb.setCheckmark(true);
76  pcolb.setSelectFirst(false);
77  pcolb.setContract(workOrder.getContract());
78 
80  Framework.getCurrent().getPage(),
81  Application.getString("lCustomerOrders"),
82  pcolb,
83  "80%", "80%", new Command() {
84  @Override
85  public Object execute(Context context) {
86  if(pcolb != null) {
87  for(CustomerOrder cu : pcolb.getObjectValues()) {
88  Row row = appendValue();
89  OrderReference or = (OrderReference) row.getValue();
90  or.initFrom(cu);
91  updateRow(row);
92  }
93  if(isAllowInsertions()) {
94  appendValue();
95  }
96  }
97  return null;
98  }
99  });
100  }
101 
102  @Override
103  protected void initiateRow(Row row, OrderReference value) {
104  if(value == null) {
105  value = new OrderReference();
106  value.setWorkOrder(workOrder);
107  workOrder.getOrderReferences().add(value);
108  value.setOrderRef(getRowCount());
109  if(workOrder.isProductOrder()) {
110  value.setProductId(workOrder.getProductId());
111  }
112  }
113  row.setValue(value);
114  addDetail(row);
115  }
116 
117  @Override
118  protected boolean deleteRow(Row row) {
119  OrderReference or = (OrderReference) row.getValue();
120  EntityCollectionUtil.remove(workOrder.getOrderReferences(), or);
121  or.setWorkOrder(null);
122  return true;
123  }
124 
125  @Override
126  protected boolean isValid(OrderReference v) {
127  return !v.isEmpty();
128  }
129 
130  @Override
131  protected String formatCell(EditableCell editableCell, Object value) {
132  if(value instanceof Contract) {
133  Contract ctc = (Contract) value;
134  return ctc == null ? null : ctc.getPartialDescription();
135  } else if(value instanceof Product) {
136  Product p = (Product) value;
137  return p == null ? null : p.getDescription();
138  }
139  return super.formatCell(editableCell, value);
140  }
141 
142  @Override
143  protected HtmlBasedComponent createEditor(EditableCell editableCell) {
144  if(editableCell.getColumn() instanceof EditableColumn) {
145  EditableColumn ec = (EditableColumn) editableCell.getColumn();
146  if(ec.getJavaClass().equals(Contract.class)) {
147  Object value = getCellValue(editableCell);
149  if(value != null) sslb.setObjectValue((Contract) value);
150  sslb.setMold("select");
151  return sslb;
152  } else if(ec.getJavaClass().equals(Product.class)) {
153  Object value = getCellValue(editableCell);
154  ProductCombobox pcb = new ProductCombobox();
155  if(value != null) pcb.setObjectValue((Product) value);
156  return pcb;
157  }
158  }
159  return super.createEditor(editableCell);
160  }
161 
162 // @Override
163 // protected HtmlBasedComponent createRenderer(EditableCell editableCell) {
164 // if(editableCell.getColumn() instanceof EditableColumn) {
165 // EditableColumn ec = (EditableColumn) editableCell.getColumn();
166 // if(!workOrder.isProductOrder() && ec.getJavaClass().equals(Product.class)) {
167 // final OrderReference or = (OrderReference) editableCell.getRow().getFieldValue();
168 // Button importDef = new Button(Application.getString("lProductOrder"));
169 // final Product product = or.getProduct();
170 // if(product != null) {
171 // importDef.setLabel(product.getProductString());
172 // }
173 // importDef.addEventListener(Events.ON_CLICK, new EventListener() {
174 // @Override
175 // public void onEvent(Event event) throws Exception {
176 // if(or.getId() > 0 && product != null) {
177 // HashMap<String, Object> map = new HashMap<String, Object>();
178 // map.put("orderReference", or);
179 // ErpMenu.showModalZulFile("lProductOrder", "/WEB-INF/_zul/erp/reference/referenceClone.zul", map);
180 // event.stopPropagation();
181 // }
182 // }
183 // });
184 // return importDef;
185 // }
186 // }
187 // return super.createRenderer(editableCell);
188 // }
189 
190  @Override
191  protected HtmlBasedComponent createRenderer(EditableCell editableCell) {
192  if(editableCell.getColumn() instanceof EditableColumn) {
193  final OrderReference or = editableCell.getRow().getValue();
194  if(editableCell.getCellIndex() == 7) {
195  Button edit = new Button(null, Images.getImage("edit"));
196  edit.addEventListener(Events.ON_CLICK, new EventListener() {
197  @Override
198  public void onEvent(Event event) throws Exception {
199  event.stopPropagation();
200  showTree(or);
201  }
202  });
203  return edit;
204  }
205  }
206  return super.createRenderer(editableCell);
207  }
208 
209  private void showTree(OrderReference orderReference) {
210  if(!orderReference.isEmpty()) {
211  EntityCloneGrid orcg = new EntityCloneGrid();
212  orcg.setOrderReference(orderReference);
213  SelectionDialog.showComponent(getPage(), Application.getString("lReference"), orcg, "95%", "600px", null);
214  }
215  }
216 
217  private void addColumns() {
218  try {
219  int fractionDigits = workOrder.getContract() != null ?
220  workOrder.getContract().getCurrency().getDefaultFractionDigits() :
221  2;
222  addDetailColumn(new Image("/_zul/images/reference.png"));
223  addColumn("#", "int", "orderRef", null, 0, false, false).setWidth("40px");
224  addColumn(Application.getString("lQuantity"), "double", "quantity", null, 3, false, false).setWidth("90px");
225  addColumn(Application.getString("lProduct"), Product.class, "product", null, 0, false, workOrder.isProductOrder()).setHflex("2");
226  addColumn(Application.getString("lDescription"), String.class, "description", null, 20, false, false).setHflex("3");
227  addColumn(Application.getString("lPrice"), "double", "price", null, fractionDigits, false, false).setWidth("120px");
228  addColumn(Application.getString("lService"), Contract.class, "service", null, 0, false, false).setHflex("1");
229  addColumn("", String.class, null, null, 2, false, true).setWidth("50px");
230  } catch (ClassNotFoundException ex) {
231  Logger.getLogger(OrderReferenceGrid.class.getName()).log(Level.SEVERE, null, ex);
232  }
233  }
234 
235  private void addDetail(Row row) {
236  row.getChildren().clear();
237  OrderReferenceDetail detail = new OrderReferenceDetail(row, this);
238  row.appendChild(detail);
239  }
240 
241  @Override
242  protected boolean canDeleteRow(Row row) {
243  OrderReference or = (OrderReference) row.getValue();
244  return super.canDeleteRow(row) && !or.hasBugdetStopper();
245  }
246 
247  @Override
248  protected boolean canEditRow(Row row) {
249  OrderReference or = (OrderReference) row.getValue();
250  return super.canEditRow(row) && !or.hasBugdetStopper();
251  }
252 
253 }
static String getString(String key)
static String getImage(String image)
Definition: Images.java:36
void setOrderReference(OrderReference orderReference)
void setWorkOrder(WorkOrder workOrder)
Set< OrderReference > getOrderReferences()
Definition: WorkOrder.java:124
void initiateRow(Row row, OrderReference value)
String formatCell(EditableCell editableCell, Object value)
HtmlBasedComponent createRenderer(EditableCell editableCell)
HtmlBasedComponent createEditor(EditableCell editableCell)
static void showComponent(Page page, String title, Component component, String width, String height, final Command command)
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 setSelectFirst(boolean selectFirst)
static Framework getCurrent()
Definition: Framework.java:203