001: /*
002: The contents of this file are subject to the Common Public Attribution License
003: Version 1.0 (the "License"); you may not use this file except in compliance with
004: the License. You may obtain a copy of the License at
005: http://www.projity.com/license . The License is based on the Mozilla Public
006: License Version 1.1 but Sections 14 and 15 have been added to cover use of
007: software over a computer network and provide for limited attribution for the
008: Original Developer. In addition, Exhibit A has been modified to be consistent
009: with Exhibit B.
010:
011: Software distributed under the License is distributed on an "AS IS" basis,
012: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
013: specific language governing rights and limitations under the License. The
014: Original Code is OpenProj. The Original Developer is the Initial Developer and
015: is Projity, Inc. All portions of the code written by Projity are Copyright (c)
016: 2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018: Alternatively, the contents of this file may be used under the terms of the
019: Projity End-User License Agreeement (the Projity License), in which case the
020: provisions of the Projity License are applicable instead of those above. If you
021: wish to allow use of your version of this file only under the terms of the
022: Projity License and not to allow others to use your version of this file under
023: the CPAL, indicate your decision by deleting the provisions above and replace
024: them with the notice and other provisions required by the Projity License. If
025: you do not delete the provisions above, a recipient may use your version of this
026: file under either the CPAL or the Projity License.
027:
028: [NOTE: The text of this license may differ slightly from the text of the notices
029: in Exhibits A and B of the license at http://www.projity.com/license. You should
030: use the latest text at http://www.projity.com/license for your modifications.
031: You may not remove this license text from the source files.]
032:
033: Attribution Information: Attribution Copyright Notice: Copyright � 2006, 2007
034: Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj,
035: an open source solution from Projity. Attribution URL: http://www.projity.com
036: Graphic Image as provided in the Covered Code as file: openproj_logo.png with
037: alternatives listed on http://www.projity.com/logo
038:
039: Display of Attribution Information is required in Larger Works which are defined
040: in the CPAL as a work which combines Covered Code or portions thereof with code
041: not governed by the terms of the CPAL. However, in addition to the other notice
042: obligations, all copies of the Covered Code in Executable and Source Code form
043: distributed must, as a form of attribution of the original author, include on
044: each user interface screen the "OpenProj" logo visible to all users. The
045: OpenProj logo should be located horizontally aligned with the menu bar and left
046: justified on the top left of the screen adjacent to the File menu. The logo
047: must be at least 100 x 25 pixels. When users click on the "OpenProj" logo it
048: must direct them back to http://www.projity.com.
049: */
050: package com.projity.pm.graphic.chart;
051:
052: import java.io.Serializable;
053: import java.util.ArrayList;
054: import java.util.Iterator;
055: import java.util.List;
056:
057: import org.apache.commons.collections.Closure;
058: import org.jfree.data.xy.AbstractXYDataset;
059: import org.jfree.data.xy.XYDataset;
060: import org.jfree.data.xy.XYSeries;
061: import org.jfree.data.xy.XYSeriesCollection;
062:
063: import com.projity.algorithm.TimeIteratorGenerator;
064: import com.projity.algorithm.buffer.CalculatedValues;
065: import com.projity.algorithm.buffer.GroupedCalculatedValues;
066: import com.projity.algorithm.buffer.NonGroupedCalculatedValues;
067: import com.projity.algorithm.buffer.SeriesCallback;
068: import com.projity.field.Field;
069: import com.projity.options.CalendarOption;
070: import com.projity.pm.assignment.Assignment;
071: import com.projity.pm.assignment.HasAssignments;
072: import com.projity.pm.assignment.HasTimeDistributedData;
073: import com.projity.pm.assignment.TimeDistributedConstants;
074: import com.projity.pm.graphic.timescale.CoordinatesConverter;
075: import com.projity.pm.graphic.views.ChartView;
076: import com.projity.pm.resource.Resource;
077: import com.projity.pm.resource.ResourceImpl;
078: import com.projity.pm.task.Project;
079: import com.projity.timescale.TimeInterval;
080: import com.projity.timescale.TimeIterator;
081: import com.projity.util.Environment;
082:
083: public class ChartModel implements TimeDistributedConstants,
084: Serializable {
085: private static final long serialVersionUID = -1617376166476506096L;
086: private ChartView chartModel;
087: XYSeriesCollection seriesCollection;
088: XYSeriesCollection secondSeriesCollection = null;
089: CoordinatesConverter coord;
090:
091: public ChartModel(CoordinatesConverter coord) {
092: this .coord = coord;
093: }
094:
095: public CalculatedValues computeTrace(Iterator taskIterator,
096: List resources, Object trace, boolean histogram,
097: boolean cumulative) {
098: if (taskIterator == null || !taskIterator.hasNext()) // if no task selected, dont change chart
099: return null;
100: CalculatedValues calculatedValues;
101: if (histogram)
102: calculatedValues = new GroupedCalculatedValues();
103: else
104: calculatedValues = new NonGroupedCalculatedValues(
105: cumulative, coord.getOrigin());
106:
107: TimeIterator timeIterator = null;
108: TimeIteratorGenerator generator;
109: Iterator i = taskIterator;
110: Object current;
111: Assignment assignment;
112: double resourceMaxUnits = 0;
113: ArrayList assignmentResourcesUsed = new ArrayList();
114: boolean hasValues = false;
115: while (i.hasNext()) { //loop thru tasks
116: current = i.next();
117: if (current instanceof HasAssignments) {
118: List list = ((HasAssignments) current).getAssignments();
119:
120: Iterator a = list.iterator();
121: while (a.hasNext()) { // loop through assignments,
122:
123: assignment = (Assignment) a.next();
124: if (histogram) {
125: timeIterator = coord.getProjectTimeIterator();
126: generator = histogram ? TimeIteratorGenerator
127: .getInstance(timeIterator) : null;
128: } else {
129: generator = null;
130: }
131: if (isTaskBased(trace)) {
132: hasValues = true;
133: assignment.calcDataBetween(trace, generator,
134: calculatedValues);
135: break;
136: }
137: if (!assignment.isDefault()) {// skip dummy assignment
138: if (resources == null
139: || resources.contains(assignment
140: .getResource())) {
141: hasValues = true;
142: assignment.calcDataBetween(trace,
143: generator, calculatedValues);
144: }
145: }
146: }
147: }
148: }
149: if (!hasValues) // if nothing processed
150: return null;
151: return calculatedValues;
152: }
153:
154: public CalculatedValues computeAvailability(List resources) {
155: CalculatedValues calculatedValues = new GroupedCalculatedValues();
156: if (resources != null) {
157: Iterator r = resources.iterator();
158: while (r.hasNext()) {
159: Object obj = r.next();
160: if (!(obj instanceof Resource))
161: continue;
162: Resource res = (Resource) obj;
163: TimeIterator timeIterator = coord
164: .getProjectTimeIterator();
165: TimeIteratorGenerator generator = TimeIteratorGenerator
166: .getInstance(timeIterator);
167: Assignment.calcResourceAvailabilityBetween(res,
168: generator, calculatedValues);
169: }
170: }
171: return calculatedValues;
172: }
173:
174: // Other Projects
175: // public CalculatedValues computeOtherProjects(List tasks, List resources) {
176: // if ((resources!=null&&resources.size()>0)||(tasks!=null&&tasks.size()>0)){ //resources can be put in tasks list, is it a bug?
177: // if (resources == null || resources.size() == 0)
178: // return null;
179: // ResourceImpl resource = (ResourceImpl) resources.get(0);
180: // Iterator j=((resources==null||resources.size()==0)?tasks:resources).iterator();
181: // TimeIterator timeIterator = coord.getTimeIteratorFromDates(coord.getOrigin(),coord.getEnd());
182: // TimeIteratorGenerator generator = TimeIteratorGenerator.getInstance(timeIterator);
183: // CalculatedValues values = new GroupedCalculatedValues();
184: // SimpleCalculatedValuesFunctor visitor = SimpleCalculatedValuesFunctor.getInstance(values, generator);
185: // List vList = resource.getGlobalResource().getGlobalWorkVector().getValues();
186: //
187: // DateValueListIntervalGenerator intGen = DateValueListIntervalGenerator.getDateInstance(vList);
188: //
189: // SelectFrom clause = SelectFrom.getInstance();
190: // clause.whereInRange(generator.getStart(),generator.getEnd()); // automatically also adds a generator to limit range
191: // clause.from(intGen);
192: // Query query = Query.getInstance();
193: // query.selectFrom(clause);
194: // query.execute();
195: // return values;
196: // }
197: // return null;
198: // }
199: // Other Projects
200: public CalculatedValues computeOtherProjects(List tasks,
201: List resources) {
202: CalculatedValues calculatedValues = new GroupedCalculatedValues();
203: if ((resources != null && resources.size() > 0)
204: || (tasks != null && tasks.size() > 0)) { //resources can be put in tasks list, is it a bug?
205: Iterator j = ((resources == null || resources.size() == 0) ? tasks
206: : resources).iterator();
207: GroupedCalculatedValues c = (GroupedCalculatedValues) calculatedValues;
208:
209: TimeIterator timeIterator = coord.getProjectTimeIterator();
210: //TODO uncomment when scheduling is corrected
211: for (int k = 0; timeIterator.hasNext(); k++) {
212: TimeInterval interval = timeIterator.next();
213: c.set(k, interval.getStart1(), interval.getEnd1(), 0.0,
214: null);
215: }
216: //return calculatedValues;
217: while (j.hasNext()) {
218: ResourceImpl resource;
219: Object obj = j.next();
220: if (obj instanceof Assignment)
221: resource = (ResourceImpl) ((Assignment) obj)
222: .getResource();
223: else if (obj instanceof ResourceImpl)
224: resource = (ResourceImpl) obj;
225: else
226: continue;
227: GroupedCalculatedValues global = resource
228: .getGlobalResource().getGlobalWorkVector();
229: if (global != null) {
230: global = global.dayByDayConvert();
231: c.mergeIn(global);
232: }
233: }
234: return c;
235: }
236: return calculatedValues;
237: }
238:
239: public void computeHistogram(Project project, List tasks,
240: List resources, Object[] traces) {
241: boolean stackCurrentOnTop = traces == (Environment
242: .getStandAlone() ? HasTimeDistributedData.histogramTypes
243: : HasTimeDistributedData.serverHistogramTypes);
244:
245: // Availability
246: GroupedCalculatedValues availabilityCalculatedValues = (GroupedCalculatedValues) computeAvailability(resources);
247:
248: // Other Projects
249: GroupedCalculatedValues otherProjectsCalculatedValues = null;
250: if (!Environment.getStandAlone())
251: otherProjectsCalculatedValues = (GroupedCalculatedValues) computeOtherProjects(
252: tasks, resources);
253:
254: //This Project
255: GroupedCalculatedValues this ProjectCalculatedValues = (GroupedCalculatedValues) computeTrace(
256: project.getTaskOutlineIterator(), resources, WORK,
257: true, false);
258:
259: //Selected
260: GroupedCalculatedValues selectedCalculatedValues = (GroupedCalculatedValues) computeTrace(
261: tasks == null ? null : tasks.iterator(), resources,
262: WORK, true, false);
263:
264: // stack so that order is (from botom to top) other projects, this project, selected
265: for (int i = 0; i < availabilityCalculatedValues.size(); i++) {
266: double this Project = this ProjectCalculatedValues != null ? this ProjectCalculatedValues
267: .getUnscaledValue(i)
268: : 0D;
269: double allProjects = this Project;
270: if (otherProjectsCalculatedValues != null)
271: allProjects += otherProjectsCalculatedValues
272: .getUnscaledValue(i); // stack
273: double selected = selectedCalculatedValues != null ? selectedCalculatedValues
274: .getUnscaledValue(i)
275: : 0D;
276: if (stackCurrentOnTop) {
277: if (selectedCalculatedValues != null)
278: selectedCalculatedValues.setValue(i, allProjects);
279: if (this ProjectCalculatedValues != null)
280: this ProjectCalculatedValues.setValue(i, allProjects
281: - selected);
282: } else {
283: if (otherProjectsCalculatedValues != null)
284: otherProjectsCalculatedValues.setValue(i,
285: allProjects);
286: }
287: }
288:
289: XYSeries availabilitySeries = buildHistogramSeries(
290: AVAILABILITY, availabilityCalculatedValues);
291: XYSeries otherProjectsSeries = null;
292: if (otherProjectsCalculatedValues != null)
293: otherProjectsSeries = buildHistogramSeries(OTHER_PROJECTS,
294: otherProjectsCalculatedValues);
295: XYSeries this ProjectSeries = buildHistogramSeries(THIS_PROJECT,
296: this ProjectCalculatedValues);
297: XYSeries selectedSeries = buildHistogramSeries(SELECTED,
298: selectedCalculatedValues);
299:
300: seriesCollection = new XYSeriesCollection();
301: if (stackCurrentOnTop) {
302: seriesCollection.addSeries(selectedSeries);
303: seriesCollection.addSeries(this ProjectSeries);
304: if (otherProjectsSeries != null)
305: seriesCollection.addSeries(otherProjectsSeries);
306: } else {
307: if (otherProjectsSeries != null)
308: seriesCollection.addSeries(otherProjectsSeries);
309: seriesCollection.addSeries(this ProjectSeries);
310: seriesCollection.addSeries(selectedSeries);
311: }
312:
313: secondSeriesCollection = new XYSeriesCollection();
314: secondSeriesCollection.addSeries(availabilitySeries);
315: }
316:
317: private XYSeries buildHistogramSeries(Object trace,
318: CalculatedValues values) {
319: if (values == null)
320: return dummySeries(trace);
321: XYSeries series = new XYSeries(trace.toString(), false, true); // dont bother sorting it already is
322: makeSeries(series, trace, false, values);
323: return series;
324: }
325:
326: private XYSeries dummySeries(Object trace) {
327: return new XYSeries(trace.toString(), false, true); // dont bother sorting it already is
328:
329: }
330:
331: private int findTrace(Object[] traces, Object trace) {
332: for (int i = 0; i < traces.length; i++)
333: if (traces[i] == trace)
334: return i;
335: return -1;
336: }
337:
338: public void dumpDataset(Object[] traces) {
339: for (int i = 0; i < seriesCollection.getSeriesCount(); i++) {
340: System.out.println("series " + i + " " + traces[i]);
341: dumpSeries(seriesCollection.getSeries(i));
342: }
343: }
344:
345: public static void dumpSeries(XYSeries series) {
346: for (int i = 0; i < series.getItemCount(); i++) {
347: System.out.println(new java.util.Date(series.getX(i)
348: .longValue())
349: + " " + series.getY(i));
350: }
351: }
352:
353: private boolean isTraceRectilinear(Object trace) {
354: return trace == AVAILABILITY;
355: }
356:
357: private void makeSeries(XYSeries series, final Object trace,
358: boolean cumulative, CalculatedValues calculatedValues) {
359: final double scaleFactor = getScaleFactor(trace);
360: final XYSeries _series = series;
361: if (isTraceRectilinear(trace)) {
362: calculatedValues
363: .makeRectilinearSeries(new SeriesCallback() {
364: public void add(int index, double x, double y) {
365: _series.add(x, y / scaleFactor);
366: }
367: });
368: } else {
369: calculatedValues.makeSeries(cumulative,
370: new SeriesCallback() {
371: public void add(int index, double x, double y) {
372: _series.add(x, y / scaleFactor);
373: }
374: });
375: }
376: }
377:
378: public void computeValues(List tasks, List resources,
379: boolean cumulative, Object[] traces, boolean histogram) {
380: if (tasks == null)
381: return;
382: CalculatedValues valuesArray[] = new CalculatedValues[traces.length];
383: seriesCollection = new XYSeriesCollection();
384:
385: secondSeriesCollection = null;
386: XYSeries series;
387: for (int i = 0; i < traces.length; i++) {
388: //System.out.println("\n trace #"+i);
389: valuesArray[i] = computeTrace(tasks == null ? null : tasks
390: .iterator(), resources, traces[i], histogram,
391: cumulative);
392: }
393:
394: // done in a second step in case traces depend on each other. Right now, there is no case like that
395: for (int i = 0; i < traces.length; i++) {
396: series = new XYSeries(traces[i].toString(), false, true); // dont bother sorting it already is
397: if (valuesArray[i] == null) {
398: System.out.println("skipping null values array "
399: + traces[i]);
400: continue;
401: }
402: makeSeries(series, traces[i], cumulative, valuesArray[i]);
403:
404: seriesCollection.addSeries(series);
405:
406: }
407: }
408:
409: private double getScaleFactor(Object trace) {
410: if (trace instanceof Field
411: && !((Field) trace).isDurationOrWork())
412: return 1.0;
413: double hourScale = CalendarOption.getInstance()
414: .getHoursPerDay() / 24.0D; // need to adjust for number of working hours compared to 24 hours in a day
415: return hourScale * coord.getIntervalDuration();
416: }
417:
418: /**
419: * @return Returns the dataset.
420: */
421: public AbstractXYDataset getDataset() {
422: return seriesCollection;
423: }
424:
425: private final boolean isTaskBased(Object trace) {
426: return (trace == FIXED_COST || trace == ACTUAL_FIXED_COST);
427: }
428:
429: public XYDataset getSecondDataset() {
430: return secondSeriesCollection;
431: }
432:
433: }
|