BrightSide Workbench Full Report + Source Code
ReferenceStatusWrapper.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.workorder;
20 
21 import java.util.Date;
22 import java.util.EnumSet;
23 import org.turro.erp.entity.OrderReference;
24 import org.turro.erp.entity.ReferenceStatus;
25 import org.turro.erp.entity.Task;
26 import org.turro.erp.entity.TaskStatus;
27 import org.turro.status.Status;
28 
33 public class ReferenceStatusWrapper extends Status<ReferenceStatus> {
34 
35  private OrderReference orderReference;
36  private Date now;
37 
38  public ReferenceStatusWrapper(OrderReference orderReference, Date now) {
39  super(null);
40  this.orderReference = orderReference;
41  this.now = now;
42  checkStatus(now);
43  }
44 
45  @Override
46  protected ReferenceStatus doCheckStatus(Date now) {
48  if(orderReference.isDelivered()) {
50  } if(orderReference.hasPendingBudget()) {
52  } else {
53  for(Task task : orderReference.getTasks()) {
54  TaskStatus ts = task.getStatus();
55  if(rs.getStatusOrder() > ts.getStatusOrder()) {
56  rs = ReferenceStatus.values()[ts.getStatusOrder()];
57  }
58  }
59  }
60  return rs;
61  }
62 
63  @Override
65  return newStatus;
66  }
67 
68  @Override
69  public boolean canChangeTo(ReferenceStatus newStatus) {
70  if(current.equals(ReferenceStatus.REFERENCE_PLANNING)) {
71  return EnumSet.of(
74  ReferenceStatus.REFERENCE_FINISHED).contains(newStatus) &&
75  taskCouldChange(newStatus);
76  } else if(current.equals(ReferenceStatus.REFERENCE_READY)) {
77  return EnumSet.of(
79  ReferenceStatus.REFERENCE_FINISHED).contains(newStatus) &&
80  taskCouldChange(newStatus);
81  } else if(current.equals(ReferenceStatus.REFERENCE_ONWORK)) {
82  return EnumSet.of(
83  ReferenceStatus.REFERENCE_FINISHED).contains(newStatus) &&
84  taskCouldChange(newStatus);
85  }
86  return false;
87  }
88 
89  private boolean taskCouldChange(ReferenceStatus newStatus) {
90  boolean delivering = false;
91  if(newStatus.equals(ReferenceStatus.REFERENCE_DELIVERED)) {
92  // Delivered would mean finishing tasks
94  delivering = true;
95  }
96  for(Task task : orderReference.getTasks()) {
97  if(!task.getStatusWrapper(now).canChangeTo(TaskStatus.values()[newStatus.getStatusOrder()])) {
98  return false;
99  }
100  if(delivering && task.isValidationPending()) {
101  return false;
102  }
103  }
104  return true;
105  }
106 
107 }
108 
ReferenceStatusWrapper(OrderReference orderReference, Date now)
ReferenceStatus doChangeTo(ReferenceStatus newStatus)