BrightSide Workbench Full Report + Source Code
BrightSide/bserp-core/src/main/java/org/turro/erp/entity/Task.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.*;
21 import javax.persistence.*;
22 import org.turro.string.Strings;
23 import org.turro.elephant.impl.util.StringParser;
24 import org.turro.erp.task.TaskStatusWrapper;
25 import org.turro.financials.db.FinancialsPU;
26 import org.turro.financials.entity.Contract;
27 import org.turro.jpa.Dao;
28 import org.turro.jpa.entity.DaoEntity;
29 import org.turro.plugin.contacts.IContact;
30 
35 @Entity
36 public class Task implements java.io.Serializable {
37 
38  @Id
39  @GeneratedValue(strategy=GenerationType.IDENTITY)
40  @Column(name="IDENTIFIER")
41  private long id;
42 
43  @Column(name="TASK_NAME", nullable = false)
44  private String name;
45 
46  @Lob
47  @Column(length=4096)
48  private String description;
49 
50  @ManyToOne
51  private OwnedAptitude supervised;
52 
53  @ManyToOne
54  private AptitudeDegree aptitudeDegree;
55 
56  @Column(name="START_DATE")
57  @Temporal(value = TemporalType.TIMESTAMP)
58  private Date startDate;
59 
60  @Column(name="END_DATE")
61  @Temporal(value = TemporalType.TIMESTAMP)
62  private Date endDate;
63 
64  @Column(name="DELIVERY_DATE")
65  @Temporal(value = TemporalType.TIMESTAMP)
66  private Date delivery;
67 
68  private boolean validated, milestone, waiting;
69 
70  private TaskStatus status;
71 
72  @ManyToOne
73  private org.turro.erp.entity.OrderReference orderReference;
74 
75  private long departmentId;
76 
77  @OneToMany(mappedBy = "task", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
78  @OrderBy("type")
79  private Set<Predecessor> predecessors = new HashSet<Predecessor>();
80 
81  @OneToMany(mappedBy = "task", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
82  private Set<RequiredUsage> requiredUsages = new HashSet<RequiredUsage>();
83 
84  public Date getDelivery() {
85  return delivery;
86  }
87 
88  public void setDelivery(Date delivery) {
89  this.delivery = delivery;
90  }
91 
92  public long getDepartmentId() {
93  return departmentId;
94  }
95 
96  public void setDepartmentId(long departmentId) {
97  this.departmentId = departmentId;
98  }
99 
100  public String getDescription() {
101  return description;
102  }
103 
104  public void setDescription(String description) {
105  this.description = description;
106  }
107 
108  public Date getEndDate() {
109  return endDate;
110  }
111 
112  public void setEndDate(Date endDate) {
113  this.endDate = endDate;
114  }
115 
116  public Set<RequiredUsage> getRequiredUsages() {
117  return requiredUsages;
118  }
119 
120  public void setRequiredUsages(Set<RequiredUsage> requiredUsages) {
121  this.requiredUsages = requiredUsages;
122  }
123 
124  public long getId() {
125  return id;
126  }
127 
128  public void setId(long id) {
129  this.id = id;
130  }
131 
132  public boolean isMilestone() {
133  return milestone;
134  }
135 
136  public void setMilestone(boolean milestone) {
137  this.milestone = milestone;
138  }
139 
140  public String getName() {
141  return name;
142  }
143 
144  public void setName(String name) {
145  this.name = name;
146  }
147 
149  return aptitudeDegree;
150  }
151 
152  public void setAptitudeDegree(AptitudeDegree aptitudeDegree) {
153  this.aptitudeDegree = aptitudeDegree;
154  }
155 
157  return orderReference;
158  }
159 
160  public void setOrderReference(OrderReference orderReference) {
161  this.orderReference = orderReference;
162  }
163 
164  public Set<Predecessor> getPredecessors() {
165  return predecessors;
166  }
167 
168  public void setPredecessors(Set<Predecessor> predecessors) {
169  this.predecessors = predecessors;
170  }
171 
172  public Date getStartDate() {
173  return startDate;
174  }
175 
176  public void setStartDate(Date startDate) {
177  this.startDate = startDate;
178  }
179 
181  return status;
182  }
183 
184  public void setStatus(TaskStatus status) {
185  this.status = status;
186  }
187 
189  return supervised;
190  }
191 
192  public void setSupervised(OwnedAptitude supervised) {
193  this.supervised = supervised;
194  }
195 
196  public boolean isValidated() {
197  return validated;
198  }
199 
200  public void setValidated(boolean validated) {
201  this.validated = validated;
202  }
203 
204  public boolean isWaiting() {
205  return waiting;
206  }
207 
208  public void setWaiting(boolean waiting) {
209  this.waiting = waiting;
210  }
211 
212  /* Helpers */
213 
214  public boolean isEmpty() {
215  clearEmptyLines();
216  return Strings.isBlank(name) ||
217  departmentId == 0;
218  }
219 
220  public void clearEmptyLines() {
221  Iterator<Predecessor> itp = predecessors.iterator();
222  while(itp.hasNext()) {
223  Predecessor p = itp.next();
224  if(p.isEmpty()) {
225  p.setTask(null);
226  itp.remove();
227  }
228  }
229  Iterator<RequiredUsage> ith = requiredUsages.iterator();
230  while(ith.hasNext()) {
231  RequiredUsage hru = ith.next();
232  if(hru.isEmpty()) {
233  hru.setTask(null);
234  ith.remove();
235  }
236  }
237  }
238 
239  public void prepareForSaving() {
240  clearEmptyLines();
241  checkStatus(new Date());
242  }
243 
245  return new TaskStatusWrapper(this, new Date());
246  }
247 
249  return new TaskStatusWrapper(this, now);
250  }
251 
252  public double getEstimatedDuration() {
253  double estimatedDuration = 0.0;
254 
255  for(RequiredUsage hru : requiredUsages) {
256  estimatedDuration = Math.max(hru.getEstimatedDuration(), estimatedDuration);
257  }
258 
259  if(estimatedDuration == 0.0) {
260  if(startDate != null && endDate != null) {
261  estimatedDuration = (endDate.getTime() - startDate.getTime()) * (1000.0 * 60.0 * 60.0);
262  estimatedDuration = ((estimatedDuration / 24.0) / 7.0) * 5.0 * 8.0;
263  }
264  }
265 
266  return estimatedDuration;
267  }
268 
269  public double getRealDuration() {
270  double realDuration = 0.0;
271 
272  for(RequiredUsage hru : requiredUsages) {
273  realDuration = Math.max(hru.getRealDuration(), realDuration);
274  }
275 
276  return realDuration;
277  }
278 
279  public Collection<AptitudeDegree> getAptitudeDegrees() {
280  ArrayList<AptitudeDegree> list = new ArrayList<AptitudeDegree>();
281  if(aptitudeDegree != null) {
282  list.add(aptitudeDegree);
283  }
284  return list;
285  }
286 
287  public boolean checkStatus(Date now) {
288  return new TaskStatusWrapper(this, now).isChanged();
289  }
290 
291  /* Description Helper */
292 
293  public String getFullDescription() {
294  return id + " - " + Strings.truncateAndWarn(name + (description == null ? "" : " - " + description), 50);
295  }
296 
297  public String getHtmlDescription() {
298  return StringParser.toHTML(description);
299  }
300 
301  /* Financials Helpers */
302 
304  return new DaoEntity<Contract, Long>() {
305  @Override
306  protected Dao createDao() {
307  return new FinancialsPU();
308  }
309  @Override
310  protected boolean shouldLog() {
311  return false;
312  }
313  }.find(departmentId);
314  }
315 
316  public void setDepartment(Contract contract) {
317  if(contract != null) {
318  departmentId = contract.getId();
319  } else {
320  departmentId = 0;
321  }
322  }
323 
324  public boolean fits(IContact contact) {
325  for(RequiredUsage ru : getRequiredUsages()) {
326  if(ru.getHumanResource() != null && ru.getHumanResource().fits(contact)) {
327  return true;
328  }
329  }
330  if(supervised != null && supervised.getHumanResource() != null &&
331  supervised.getHumanResource().fits(contact)) {
332  return true;
333  }
334  return false;
335  }
336 
337  public boolean isValidationPending() {
338  return supervised != null && status.equals(TaskStatus.TASK_FINISHED) && !validated;
339  }
340 
341  public boolean canStart() {
342  if(orderReference.getWorkOrder().isDraft()) {
343  return false;
344  }
345  if(orderReference.getStatusWrapper().getCurrent().equals(ReferenceStatus.REFERENCE_PENDING)) {
346  return false;
347  }
348  return !waiting;
349  }
350 
351  public boolean isProductOrder() {
352  return orderReference != null &&
353  orderReference.getWorkOrder() != null &&
354  orderReference.getWorkOrder().isProductOrder();
355  }
356 
357  public boolean fits(HumanResource humanResource) {
358  for(RequiredUsage ru : requiredUsages) {
359  if(ru.fits(humanResource)) {
360  return true;
361  }
362  }
363  return false;
364  }
365 
366  public double getCost() {
367  double cost = 0;
368  for(RequiredUsage ru : getRequiredUsages()) {
369  cost += ru.getMostRealCost();
370  }
371  return cost;
372  }
373 
374 }
static String toHTML(String value)
ReferenceStatusWrapper getStatusWrapper()