BrightSide Workbench Full Report + Source Code
WorkOrder.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.entity;
19 
20 import java.util.Date;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.Set;
24 import javax.persistence.*;
25 import org.amic.util.string.MultipleString;
26 import org.turro.string.Strings;
27 import org.turro.contacts.Contact;
28 import org.turro.contacts.db.ContactsPU;
29 import org.turro.elephant.db.IdUtils;
30 import org.turro.elephant.impl.util.StringParser;
31 import org.turro.erp.db.ErpPU;
32 import org.turro.financials.db.FinancialsPU;
33 import org.turro.financials.entity.Contract;
34 import org.turro.financials.entity.Product;
35 import org.turro.financials.entity.RegisterView;
36 import org.turro.jpa.Dao;
37 import org.turro.jpa.entity.CachedEntity;
38 
43 @Entity
44 public class WorkOrder implements java.io.Serializable {
45 
46  @Id
47  @GeneratedValue(strategy=GenerationType.IDENTITY)
48  @Column(name="IDENTIFIER")
49  private long id;
50 
51  private long productId;
52 
53  @Column(name="WORKORDER_DATE")
54  @Temporal(value = TemporalType.DATE)
55  private Date workOrderDate;
56 
57  @Lob
58  @Column(length=4096)
59  private String description;
60 
61  private long workOrderId;
62 
63  private long contractId;
64 
65  private String petitionerId;
66 
67  private long viewId;
68 
69  private boolean draft;
70 
71  @ManyToOne
72  private OwnedAptitude supervised;
73 
74  @ManyToOne
75  private WorkOrder parent;
76 
77  @OneToMany(mappedBy = "parent", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
78  private Set<WorkOrder> extensions = new HashSet<WorkOrder>();
79 
80  @OneToMany(mappedBy = "workOrder", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
81  @OrderBy(value="orderRef")
82  private Set<OrderReference> orderReferences = new HashSet<OrderReference>();
83 
84  public long getContractId() {
85  return contractId;
86  }
87 
88  public void setContractId(long contractId) {
89  this.contractId = contractId;
90  }
91 
92  public String getDescription() {
93  return description;
94  }
95 
96  public void setDescription(String description) {
97  this.description = description;
98  }
99 
100  public boolean isDraft() {
101  return draft;
102  }
103 
104  public void setDraft(boolean draft) {
105  this.draft = draft;
106  }
107 
108  public Set<WorkOrder> getExtensions() {
109  return extensions;
110  }
111 
112  public void setExtensions(Set<WorkOrder> extensions) {
113  this.extensions = extensions;
114  }
115 
116  public long getId() {
117  return id;
118  }
119 
120  public void setId(long id) {
121  this.id = id;
122  }
123 
124  public Set<OrderReference> getOrderReferences() {
125  return orderReferences;
126  }
127 
128  public void setOrderReferences(Set<OrderReference> orderReferences) {
129  this.orderReferences = orderReferences;
130  }
131 
132  public WorkOrder getParent() {
133  return parent;
134  }
135 
136  public void setParent(WorkOrder parent) {
137  this.parent = parent;
138  }
139 
140  public String getPetitionerId() {
141  return petitionerId;
142  }
143 
144  public void setPetitionerId(String petitionerId) {
145  this.petitionerId = petitionerId;
146  }
147 
148  public long getProductId() {
149  return productId;
150  }
151 
152  public void setProductId(long productId) {
153  this.productId = productId;
154  }
155 
157  return supervised;
158  }
159 
160  public void setSupervised(OwnedAptitude supervised) {
161  this.supervised = supervised;
162  }
163 
164  public long getViewId() {
165  return viewId;
166  }
167 
168  public void setViewId(long viewId) {
169  this.viewId = viewId;
170  }
171 
172  public Date getWorkOrderDate() {
173  return workOrderDate;
174  }
175 
176  public void setWorkOrderDate(Date workOrderDate) {
177  this.workOrderDate = workOrderDate;
178  }
179 
180  public long getWorkOrderId() {
181  return workOrderId;
182  }
183 
184  public void setWorkOrderId(long workOrderId) {
185  this.workOrderId = workOrderId;
186  }
187 
188  /* Financials Helpers */
189 
190  private transient CachedEntity<Product, Long> product =
191  new CachedEntity<Product, Long>() {
192  @Override
193  protected Dao createDao() {
194  return new FinancialsPU();
195  }
196  };
197 
198  public Product getProduct() {
199  return product.getEntity(productId);
200  }
201 
202  public void setProduct(Product product) {
203  productId = this.product.setEntity(product);
204  }
205 
206  private transient CachedEntity<Contract, Long> contract =
207  new CachedEntity<Contract, Long>() {
208  @Override
209  protected Dao createDao() {
210  return new FinancialsPU();
211  }
212  };
213 
215  return contract.getEntity(contractId);
216  }
217 
218  public void setContract(Contract contract) {
219  contractId = this.contract.setEntity(contract);
220  }
221 
222  private transient CachedEntity<Contact, String> petitioner =
223  new CachedEntity<Contact, String>() {
224  @Override
225  protected Dao createDao() {
226  return new ContactsPU();
227  }
228  };
229 
231  return petitioner.getEntity(petitionerId);
232  }
233 
234  public void setPetitioner(Contact petitioner) {
235  petitionerId = this.petitioner.setEntity(petitioner);
236  }
237 
238  private transient CachedEntity<RegisterView, Long> view =
239  new CachedEntity<RegisterView, Long>() {
240  @Override
241  protected Dao createDao() {
242  return new FinancialsPU();
243  }
244  };
245 
247  return view.getEntity(viewId);
248  }
249 
250  public void setView(RegisterView view) {
251  viewId = this.view.setEntity(view);
252  }
253 
254  /* Description Helper */
255 
256  public String getSomeDescription() {
257  if(productId > 0) {
258  Product p = getProduct();
259  return p != null ? p.getProductString() : "";
260  } else {
261  if(!Strings.isBlank(description)) {
262  return description;
263  } else {
264  MultipleString ms = new MultipleString();
265  for(OrderReference or : getOrderReferences()) {
266  ms.add(or.getSomeDescription());
267  }
268  return ms.getString(100);
269  }
270  }
271  }
272 
273  public String getHtmlDescription() {
275  }
276 
277  /* Helpers */
278 
279  public boolean isProductOrder() {
280  return productId > 0;
281  }
282 
283  public boolean isEmpty() {
284  boolean empty = workOrderDate == null;
285  if(!empty) {
286  empty = true;
287  for(OrderReference or : orderReferences) {
288  if(!or.isEmpty()) {
289  empty = false;
290  break;
291  }
292  }
293  }
294  return empty;
295  }
296 
297  public void clearEmptyLines() {
298  Iterator<OrderReference> it = orderReferences.iterator();
299  while(it.hasNext()) {
300  OrderReference or = it.next();
301  if(or.isEmpty()) {
302  or.setWorkOrder(null);
303  it.remove();
304  }
305  }
306  }
307 
308  public void prepareForSaving() {
309  clearEmptyLines();
310  for(WorkOrder wo : extensions) {
311  wo.prepareForSaving();
312  }
313  for(OrderReference or : orderReferences) {
314  or.prepareForSaving();
315  }
316  if(workOrderId == 0 && !draft) {
317  workOrderId = IdUtils.getMaxLongIdFromLong(
318  new ErpPU(), "WorkOrder", "workOrderId");
319  }
320  }
321 
322  public String getFullDescription() {
323  return (isDraft() ? "" : workOrderId + " - ") +
324  Strings.truncateAndWarn(getSomeDescription(), 100);
325  }
326 
327  public Set<OrderReference> getSavedOrderReferences() {
328  HashSet<OrderReference> set = new HashSet<OrderReference>();
329  for(OrderReference or : orderReferences) {
330  if(or.getId() > 0) {
331  set.add(or);
332  }
333  }
334  return set;
335  }
336 
337  public long getNextBudgetNumber() {
338  long num = 1;
339  for(OrderReference or : getOrderReferences()) {
340  if(!or.isEmpty()) {
341  for(Budget b : or.getBudgets()) {
342  num = Math.max(num, b.getBudgetNumber() + 1);
343  }
344  }
345  }
346  return num;
347  }
348 
349  public double getPrice() {
350  double price = 0;
351  for(OrderReference or : orderReferences) {
352  price += or.getPrice();
353  }
354  return price;
355  }
356 
357  public double getCost() {
358  double cost = 0;
359  for(OrderReference or : orderReferences) {
360  cost += or.getCost();
361  }
362  return cost;
363  }
364 
365  public static WorkOrder getByWorkOrderId(long value) {
366  if(value > 0) {
367  Dao dao = new ErpPU();
368  return (WorkOrder) dao.getSingleResultOrNull(
369  "select wo from WorkOrder as wo " +
370  "where wo.workOrderId = ?",
371  new Object[] { value });
372  }
373  return null;
374  }
375 
376 }
static long getMaxLongIdFromLong(Dao dao, String table, String field)
Definition: IdUtils.java:91
static String toHTML(String value)
void setWorkOrder(WorkOrder workOrder)
void setPetitionerId(String petitionerId)
Definition: WorkOrder.java:144
void setView(RegisterView view)
Definition: WorkOrder.java:250
void setOrderReferences(Set< OrderReference > orderReferences)
Definition: WorkOrder.java:128
void setExtensions(Set< WorkOrder > extensions)
Definition: WorkOrder.java:112
void setSupervised(OwnedAptitude supervised)
Definition: WorkOrder.java:160
Set< WorkOrder > getExtensions()
Definition: WorkOrder.java:108
void setDescription(String description)
Definition: WorkOrder.java:96
void setContract(Contract contract)
Definition: WorkOrder.java:218
void setProduct(Product product)
Definition: WorkOrder.java:202
void setWorkOrderDate(Date workOrderDate)
Definition: WorkOrder.java:176
void setParent(WorkOrder parent)
Definition: WorkOrder.java:136
void setContractId(long contractId)
Definition: WorkOrder.java:88
Set< OrderReference > getSavedOrderReferences()
Definition: WorkOrder.java:327
void setWorkOrderId(long workOrderId)
Definition: WorkOrder.java:184
void setPetitioner(Contact petitioner)
Definition: WorkOrder.java:234
static WorkOrder getByWorkOrderId(long value)
Definition: WorkOrder.java:365
void setProductId(long productId)
Definition: WorkOrder.java:152
Set< OrderReference > getOrderReferences()
Definition: WorkOrder.java:124
void setViewId(long viewId)
Definition: WorkOrder.java:168
void setDraft(boolean draft)
Definition: WorkOrder.java:104
Object getSingleResultOrNull(SqlClause sc)
Definition: Dao.java:419