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.algorithm;
051:
052: import java.util.Collection;
053: import java.util.LinkedList;
054: import org.apache.commons.collections.Closure;
055: import org.apache.commons.collections.Predicate;
056: import org.apache.commons.collections.functors.ChainedClosure;
057: import org.apache.commons.collections.functors.FalsePredicate;
058: import org.apache.commons.collections.functors.TruePredicate;
059: import com.projity.pm.time.HasStartAndEnd;
060:
061: /**
062: * The part of a query that will apply visitors over generators
063: */
064: public class SelectFrom implements HasStartAndEnd {
065: long start;
066: long end;
067: boolean finished = false;
068: private IntervalGenerator generator = null;
069: boolean mustProcessAll = false;
070: Closure fieldVisitors = null;
071: CalculationVisitor[] fieldVisitorArray = null;
072: Predicate wherePredicate = TruePredicate.INSTANCE;
073:
074: public static LinkedList selectFromListInstance() {
075: return new LinkedList();
076: }
077:
078: public static LinkedList listInstance(SelectFrom a) {
079: LinkedList list = new LinkedList();
080: list.add(a);
081: return list;
082: }
083:
084: public static LinkedList listInstance(SelectFrom a, SelectFrom b) {
085: LinkedList list = new LinkedList();
086: list.add(a);
087: list.add(b);
088: return list;
089: }
090:
091: public static LinkedList listInstance(SelectFrom a, SelectFrom b,
092: SelectFrom c) {
093: LinkedList list = new LinkedList();
094: list.add(a);
095: list.add(b);
096: list.add(c);
097: return list;
098: }
099:
100: public static LinkedList listInstance(SelectFrom a, SelectFrom b,
101: SelectFrom c, SelectFrom d) {
102: LinkedList list = new LinkedList();
103: list.add(a);
104: list.add(b);
105: list.add(c);
106: list.add(d);
107: return list;
108: }
109:
110: protected IntervalGeneratorSet fromGenerators = null;
111:
112: public Collection getFromIntervalGenerators() {
113: return fromGenerators.getGenerators();
114: }
115:
116: public SelectFrom select(CalculationVisitor[] fieldVisitorArray) {
117: this .fieldVisitorArray = fieldVisitorArray;
118: this .fieldVisitors = new ChainedClosure(fieldVisitorArray);
119: return this ;
120: }
121:
122: public SelectFrom select(CalculationVisitor fieldVisitor) {
123: if (fieldVisitorArray != null) { // if already something there, add to it
124: CalculationVisitor[] newArray = new CalculationVisitor[fieldVisitorArray.length + 1];
125: System.arraycopy(fieldVisitorArray, 0, newArray, 0,
126: fieldVisitorArray.length);
127: newArray[fieldVisitorArray.length] = fieldVisitor; // add new one to end
128: return select(newArray);
129: }
130: fieldVisitorArray = new CalculationVisitor[] { fieldVisitor }; //make one element array
131: this .fieldVisitors = fieldVisitor; // no need to make chained closure since only one element
132: return this ;
133: }
134:
135: public SelectFrom all() {
136: mustProcessAll = true; // must be set after from generators are set!
137: return this ;
138: }
139:
140: public SelectFrom from(LinkedList fromGeneratorList) {
141: if (fromGenerators == null)
142: this .fromGenerators = IntervalGeneratorSet
143: .getInstance(fromGeneratorList);
144: else
145: this .fromGenerators.getGenerators().addAll(
146: fromGeneratorList);
147: return this ;
148: }
149:
150: public SelectFrom from(IntervalGenerator fromGenerator) {
151: if (fromGenerators == null)
152: this .fromGenerators = IntervalGeneratorSet
153: .getInstance(fromGenerator);
154: else
155: fromGenerators.getGenerators().add(fromGenerator);
156: return this ;
157: }
158:
159: public SelectFrom where(Predicate wherePredicate) {
160: this .wherePredicate = wherePredicate;
161: return this ;
162: }
163:
164: public SelectFrom whereInRange(long start, long end) {
165: if (start <= end) { // if non backwards range
166: // If there is already a range, intersect with it
167: if (wherePredicate != null
168: && wherePredicate instanceof DateInRangePredicate) {
169: DateInRangePredicate range = (DateInRangePredicate) wherePredicate;
170: range.limitTo(start, end);
171: start = range.getStart();
172: end = range.getEnd();
173: } else {
174: wherePredicate = DateInRangePredicate.getInstance(
175: start, end);
176: }
177: from(RangeIntervalGenerator.betweenInstance(start, end)); // add a generator assuring the endpoints are treated corrctly
178: } else { // take care in cases where range is invalid
179: wherePredicate = FalsePredicate.INSTANCE;
180: }
181:
182: return this ;
183: }
184:
185: /**
186: * Initializes all calculation totals for active field visitors. This will set all non-cumulative ones to 0s
187: * Cumulative ones are not initialized
188: *
189: */
190: public void initializeCalculations() {
191: if (fieldVisitorArray == null)
192: return;
193: for (int i = 0; i < fieldVisitorArray.length; i++)
194: fieldVisitorArray[i].initialize();
195: }
196:
197: /**
198: * Put fields back to their 0 state. This is used when the clause is used up. Cumulative fields as well.
199: *
200: */
201: public void resetCalculations() {
202: if (fieldVisitorArray == null)
203: return;
204: for (int i = 0; i < fieldVisitorArray.length; i++)
205: fieldVisitorArray[i].reset();
206: }
207:
208: /**
209: * Calculate values in a range of times by calling each visitor on subranges until the range is complete.
210: * @param groupByStart start of calculation range. currently unused!
211: * @param groupByEnd end of calculation range
212: * @return true if all of the from generators are still active, false if one of them has been used up.
213: */
214: public boolean calculate(long groupByStart, long groupByEnd) {
215: if (finished) {// if the last item of a generator was processed in previous call
216: resetCalculations(); // since it is no longer active, should always return 0s from now on
217: return false;
218: }
219: while (true) {
220: if (generator == null) // will be null on first call, and after the previously active generator has been evaluated
221: generator = fromGenerators.earliestEndingGenerator();
222:
223: if (generator == null) { //could be case if there are no from generatros at all TODO is this test needed?
224: finished = true;
225: break;
226: }
227:
228: start = Math.max(start, generator.currentStart()); // if current generator was interrupted by ending a range, we need to start at point left off
229: end = Math.min(groupByEnd, generator.currentEnd());
230: if (end >= start) { // in cases where a clause starts in the middle, such as remaining work, end may be less than start at first
231: // System.out.println("SelectFrom start" + new java.util.Date(start) + " end " + new java.util.Date(end) + " " + generator);
232: // evaluate fields
233: boolean whereConditionMet = wherePredicate
234: .evaluate(this );
235: if (fieldVisitors != null) {
236: for (int i = 0; i < fieldVisitorArray.length; i++) {
237: // if we are in the calculation range, or if the functor is cumulative
238: if (whereConditionMet
239: || fieldVisitorArray[i].isCumulative()) {
240: fieldVisitorArray[i].execute(this );
241: }
242: }
243: }
244: }
245: start = end; // for next iteration, shift start to current end
246:
247: if (end == groupByEnd) // at end of groupBy.
248: break;
249:
250: if (!generator.evaluate(this )) {
251: if (mustProcessAll) { // if all froms must be treated
252: fromGenerators.remove(generator);
253: finished = fromGenerators.isEmpty(); // any left?
254: } else {
255: finished = true; // The next time calculate is called, it should return false
256: }
257: if (finished)
258: break;
259: }
260: generator = null; // The current generator has been finished. Will need to find earliest next time
261: }
262: return true;
263: }
264:
265: /**
266: *
267: */
268: private SelectFrom() {
269: super ();
270: // TODO Auto-generated constructor stub
271: }
272:
273: /**
274: * Factory method
275: * @return
276: */
277: public static SelectFrom getInstance() {
278: return new SelectFrom();
279: }
280:
281: /**
282: * @return Returns the end.
283: */
284: public long getEnd() {
285: return end;
286: }
287:
288: /**
289: * @return Returns the start.
290: */
291: public long getStart() {
292: return start;
293: }
294:
295: public String toString() {
296: return "Select From where is " + wherePredicate;
297: }
298:
299: public static final SelectFrom[] NOTHING = new SelectFrom[] {};
300:
301: }
|