BrightSide Workbench Full Report + Source Code
bserp-www/src/main/java/org/turro/erp/task/usage/WorkloadGantt.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.usage;
20 
21 import java.util.HashMap;
22 import java.util.List;
23 import org.turro.erp.db.ErpPU;
24 import org.turro.erp.entity.HumanResource;
25 import org.turro.erp.entity.OrderItem;
26 import org.turro.erp.entity.RequiredUsage;
27 import org.turro.erp.entity.Resource;
28 import org.turro.erp.task.Workload;
29 import org.turro.erp.task.WorkloadSet;
30 import org.turro.jpa.Dao;
31 import org.turro.zkoss.svg.GanttResource;
32 import org.turro.zkoss.svg.GanttResourceData;
33 import org.turro.zkoss.svg.GanttResourceItem;
34 import org.zkoss.zk.ui.ext.AfterCompose;
35 
40 public class WorkloadGantt extends GanttResource implements AfterCompose {
41 
42  private String selectItem;
43  private double scale = 1.0;
44 
45  public WorkloadGantt() {
46  }
47 
48  public String getSelectItem() {
49  return selectItem;
50  }
51 
52  public void setSelectItem(String selectItem) {
53  this.selectItem = selectItem;
54  }
55 
56  public double getScale() {
57  return scale;
58  }
59 
60  public void setScale(double scale) {
61  this.scale = scale;
62  }
63 
64  public void reload() {
65  loadData();
66  repaint();
67  }
68 
69  @Override
70  public void afterCompose() {
71  loadData();
72  repaint();
73  }
74 
75  public void repaint() {
76  setVisible(false);
77  getChildren().clear();
78  clear();
79  startComposing(selectItem, null, null, scale);
80  generateContent(null);
81  setVisible(true);
82  }
83 
84  private void loadData() {
85  setData(fillModel());
86  }
87 
88  private GanttResourceData fillModel() {
89  Dao dao = new ErpPU();
90  List<HumanResource> humanResources = (List<HumanResource>) dao.getResultList("select x from HumanResource x");
91  List<Resource> resources = (List<Resource>) dao.getResultList("select x from Resource x");
92  HashMap<Long, GanttResourceItem> map = new HashMap<Long, GanttResourceItem>();
93  for(HumanResource humanResource : humanResources) {
94  if(humanResource.isActive()) {
95  GanttResourceItem gri = new GanttResourceItem(1, "" + humanResource.getId(), humanResource.getName());
96  map.put(humanResource.getId() + 1000000000000L, gri);
97  }
98  }
99  for(Resource resource : resources) {
100  if(resource.isActive()) {
101  GanttResourceItem gri = new GanttResourceItem(2, "" + resource.getId(), resource.getName());
102  map.put(resource.getId() + 2000000000000L, gri);
103  }
104  }
105  WorkloadSet wls = new WorkloadSet((HumanResource) null, true);
106  for(Workload wl : wls) {
107  OrderItem oi = wl.getOrderItem();
108  HumanResource humanResource = oi.getHumanResource();
109  Resource resource = oi.getRelated() != null ? oi.getRelated().getResource() : null;
110  if(humanResource != null && humanResource.isActive()) {
111  GanttResourceItem gri = map.get(humanResource.getId() + 1000000000000L);
112  if(gri != null) {
113  gri.setExpected(Math.max(oi.getExpectedUnits() - oi.getRealUnits(), 0.0d) + gri.getExpected());
114  }
115  }
116  if(resource != null && resource.isActive()) {
117  oi = oi.getRelated();
118  GanttResourceItem gri = map.get(resource.getId() + 2000000000000L);
119  if(gri != null) {
120  gri.setExpected(Math.max(oi.getExpectedUnits() - oi.getRealUnits(), 0.0d) + gri.getExpected());
121  }
122  }
123  }
124  wls = new WorkloadSet((HumanResource) null, false);
125  for(Workload wl : wls) {
126  RequiredUsage ru = wl.getRequiredUsage();
127  for(HumanResource humanResource : humanResources) {
128  if(humanResource != null && humanResource.isActive() && ru.fits(humanResource)) {
129  GanttResourceItem gri = map.get(humanResource.getId() + 1000000000000L);
130  if(gri != null) {
131  gri.setVariable(Math.max((ru.getUnits() * ru.getDedication()) - getExpectedUnits(ru), 0.0d) + gri.getVariable());
132  }
133  }
134  }
135  Resource resource = ru.getResource();
136  if(resource != null && resource.isActive()) {
137  GanttResourceItem gri = map.get(resource.getId() + 2000000000000L);
138  if(gri != null) {
139  gri.setVariable(Math.max((ru.getUnits()) - getRelatedExpectedUnits(ru), 0.0d) + gri.getVariable());
140  }
141  }
142  }
143 
144 // for(HumanResource humanResource : (List<HumanResource>) dao.getResultList("select x from HumanResource x")) {
145 // if(humanResource.isActive()) {
146 // WorkloadSet wls = new WorkloadSet(humanResource, true);
147 // double value = 0.0d;
148 // for(Workload wl : wls) {
149 // OrderItem oi = wl.getOrderItem();
150 // value += Math.max(oi.getExpectedUnits() - oi.getRealUnits(), 0.0d);
151 // }
152 // GanttResourceItem gri = map.get(humanResource);
153 // if(gri == null) {
154 // gri = new GanttResourceItem(1, humanResource.getId(), humanResource.getName());
155 // map.put(humanResource, gri);
156 // }
157 // gri.setExpected(value);
158 // wls = new WorkloadSet(humanResource, false);
159 // value = 0.0d;
160 // for(Workload wl : wls) {
161 // RequiredUsage ru = wl.getRequiredUsage();
162 // value += Math.max((ru.getUnits() * ru.getDedication()) - getExpectedUnits(ru), 0.0d);
163 // }
164 // gri = map.get(humanResource);
165 // if(gri == null) {
166 // gri = new GanttResourceItem(1, humanResource.getId(), humanResource.getName());
167 // map.put(humanResource, gri);
168 // }
169 // gri.setVariable(value);
170 // }
171 // }
172 // for(Resource resource : (List<Resource>) dao.getResultList("select x from Resource x")) {
173 // if(resource.isActive()) {
174 // WorkloadSet wls = new WorkloadSet(resource, true);
175 // double value = 0.0d;
176 // for(Workload wl : wls) {
177 // OrderItem oi = wl.getOrderItem();
178 // value += Math.max(oi.getExpectedUnits() - oi.getRealUnits(), 0.0d);
179 // }
180 // GanttResourceItem gri = map.get(resource);
181 // if(gri == null) {
182 // gri = new GanttResourceItem(2, resource.getId(), resource.getName());
183 // map.put(resource, gri);
184 // }
185 // gri.setExpected(value);
186 // wls = new WorkloadSet(resource, false);
187 // value = 0.0d;
188 // for(Workload wl : wls) {
189 // RequiredUsage ru = wl.getRequiredUsage();
190 // value += Math.max((ru.getUnits() * ru.getDedication()) - getExpectedUnits(ru), 0.0d);
191 // }
192 // gri = map.get(resource);
193 // if(gri == null) {
194 // gri = new GanttResourceItem(2, resource.getId(), resource.getName());
195 // map.put(resource, gri);
196 // }
197 // gri.setVariable(value);
198 // }
199 // }
200  GanttResourceData grd = new GanttResourceData();
201  for(GanttResourceItem gri : map.values()) {
202  grd.add(gri);
203  gri.setSet(grd);
204  }
205  return grd;
206  }
207 
208  private double getExpectedUnits(RequiredUsage ru) {
209  double q = 0.0d;
210  for(OrderItem oi : ru.getOrderItems()) {
211  q += oi.getUnits() * oi.getDedication();
212  }
213  return q;
214  }
215 
216  private double getRelatedExpectedUnits(RequiredUsage ru) {
217  double q = 0.0d;
218  for(OrderItem oi : ru.getOrderItems()) {
219  if(oi.getRelated() != null) {
220  q += oi.getRelated().getUnits() * oi.getRelated().getDedication();
221  }
222  }
223  return q;
224  }
225 
226 }
void setData(GanttResourceData data)
void startComposing(String onUserEvent, String width, String height, double scale)
void generateContent(Map args)
Definition: Svg.java:73