BrightSide Workbench Full Report + Source Code
bserp-core/src/main/java/org/turro/erp/task/WorkloadSet.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;
20 
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Date;
24 import java.util.List;
25 import java.util.TreeSet;
26 import org.turro.elephant.db.WhereClause;
27 import org.turro.erp.db.ErpPU;
28 import org.turro.erp.entity.HumanResource;
29 import org.turro.erp.entity.OrderItem;
30 import org.turro.erp.entity.RequiredUsage;
31 import org.turro.erp.entity.Resource;
32 import org.turro.erp.entity.Task;
33 import org.turro.erp.entity.TaskStatus;
34 import org.turro.jpa.Dao;
35 
40 public class WorkloadSet extends TreeSet<Workload> {
41 
42  private HumanResource humanResource;
43  private Resource resource;
44  private boolean assigned;
45 
46  public WorkloadSet(HumanResource humanResource, boolean assigned) {
47  this.assigned = assigned;
48  setHumanResource(humanResource);
49  }
50 
51  public WorkloadSet(Resource resource, boolean assigned) {
52  this.assigned = assigned;
53  setResource(resource);
54  }
55 
57  return humanResource;
58  }
59 
60  public void setHumanResource(HumanResource humanResource) {
61  this.humanResource = humanResource;
62  this.resource = null;
63  generateList();
64  }
65 
66  public Resource getResource() {
67  return resource;
68  }
69 
70  public void setResource(Resource resource) {
71  this.resource = resource;
72  this.humanResource = null;
73  generateList();
74  }
75 
76  public Collection<RequiredUsage> getRequiredUsages() {
77  ArrayList<RequiredUsage> list = new ArrayList<RequiredUsage>();
78  for(Workload wl : this) {
79  list.add(wl.getRequiredUsage());
80  }
81  return list;
82  }
83 
84  public Collection<OrderItem> getOrderItems() {
85  ArrayList<OrderItem> list = new ArrayList<OrderItem>();
86  for(Workload wl : this) {
87  list.add(wl.getOrderItem());
88  }
89  return list;
90  }
91 
92  private void generateList() {
93 
94  clear();
95 
96  if(assigned) {
97  WhereClause wc = new WhereClause();
98  wc.addClause("select distinct oi from OrderItem as oi");
99  wc.addClause("where oi.requiredUsage.task.status <> :status1");
100  wc.addClause("and oi.requiredUsage.task.status <> :status2");
101  if(humanResource != null) {
102  wc.addClause("and oi.humanResource = :humanRes1");
103  wc.addNamedValue("humanRes1", humanResource);
104  } else if(resource != null) {
105  wc.addClause("and oi.related.resource = :resource1");
106  wc.addNamedValue("resource1", resource);
107  }
108  wc.addNamedValue("status1", TaskStatus.TASK_PLANNING);
109  wc.addNamedValue("status2", TaskStatus.TASK_FINISHED);
110  Date now = new Date();
111  Dao dao = new ErpPU();
112  for(OrderItem oi : (List<OrderItem>) dao.getResultList(wc)) {
113  Task task = oi.getRequiredUsage().getTask();
114  task.checkStatus(now);
115  if(TaskStatus.isOnWork(task.getStatus())) {
116  if(resource != null) {
117  add(new Workload(oi.getRelated()));
118  } else {
119  add(new Workload(oi));
120  }
121  }
122  }
123  } else {
124  WhereClause wc = new WhereClause();
125  wc.addClause("select distinct ru from RequiredUsage as ru");
126  wc.addClause("where ru.task.status <> :status1");
127  wc.addClause("and ru.task.status <> :status2");
128  wc.addNamedValue("status1", TaskStatus.TASK_PLANNING);
129  wc.addNamedValue("status2", TaskStatus.TASK_FINISHED);
130  if(humanResource != null) {
131  wc.addClause("and (ru.humanResource = :humanRes1");
132  wc.addNamedValue("humanRes1", humanResource);
133  wc.addClause("or ru.canChange = TRUE");
134  wc.addClause(")");
135  } else if(resource != null) {
136  wc.addClause("and ru.resource = :resource1");
137  wc.addNamedValue("resource1", resource);
138  } else {
139  wc.addClause("and (ru.humanResource is not null or ru.resource is not null)");
140  }
141 
142  Date now = new Date();
143  Dao dao = new ErpPU();
144  for(RequiredUsage ru : (List<RequiredUsage>) dao.getResultList(wc)) {
145  if(ru.getEstimatedUnits() > ru.getExpectedUnits()) {
146  Task task = ru.getTask();
147  task.checkStatus(now);
148  if(TaskStatus.isOnWork(task.getStatus()) && (humanResource == null || ru.fits(humanResource))) {
149  add(new Workload(ru));
150  }
151  }
152  }
153  }
154 
155  }
156 
157 }
void addNamedValue(String name, Object value)
WorkloadSet(HumanResource humanResource, boolean assigned)
RequiredUsage getRequiredUsage()
Definition: Workload.java:53