001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.glm.execution.eg;
027:
028: import org.cougaar.glm.execution.common.*;
029: import java.io.IOException;
030: import java.util.GregorianCalendar;
031: import java.util.Calendar;
032: import java.util.Date;
033:
034: public class SteppedInventoryReportSchedule extends Timed implements
035: TimeConstants {
036: public String theSource;
037: protected InventoryScheduleManager theInventoryScheduleManager;
038: public InventoryReportSchedule theInventoryReportSchedule;
039: private Object theKey = null;
040: private int step;
041: private long theTime = -1; // -1 signifies needs to be computed
042:
043: public SteppedInventoryReportSchedule(String aSource,
044: InventoryReportSchedule anInventoryReportSchedule,
045: InventoryScheduleManager anInventoryScheduleManager) {
046: theSource = aSource.intern();
047: theInventoryScheduleManager = anInventoryScheduleManager;
048: if (anInventoryReportSchedule == null) {
049: throw new IllegalArgumentException(
050: "anInventoryReportSchedule is null");
051: }
052: theInventoryReportSchedule = anInventoryReportSchedule;
053: setEnabled(true);
054: }
055:
056: public static Object getKey(String theSource,
057: InventoryReportSchedule irs) {
058: return theSource + "/" + irs.theItemIdentification;
059: }
060:
061: public Object getKey() {
062: if (theKey == null) {
063: theKey = getKey(theSource, theInventoryReportSchedule);
064: }
065: return theKey;
066: }
067:
068: public String getCluster() {
069: return theSource;
070: }
071:
072: /** Crazy code asks for yesterday's inventory report today and stamps it with yesterday's date
073: **/
074: public boolean expired(long time) {
075: Calendar cal = new GregorianCalendar();
076: cal.setTime(new Date(getTime()));
077: cal.set(Calendar.HOUR_OF_DAY, 0);
078: cal.set(Calendar.MINUTE, 0);
079: cal.set(Calendar.SECOND, 0);
080: cal.set(Calendar.MILLISECOND, 0);
081: cal.add(Calendar.MILLISECOND, -1); // Last millisecond of previous day
082: time = cal.getTime().getTime();
083: try {
084: theInventoryScheduleManager.requestInventoryReport(
085: theSource,
086: theInventoryReportSchedule.theItemIdentification,
087: time);
088: } catch (IOException ioe) {
089: ioe.printStackTrace();
090: }
091: step(1);
092: return false;
093: }
094:
095: public void step(int amount) {
096: step += amount;
097: theTime = -1L;
098: }
099:
100: public long getTime() {
101: if (theTime < 0L) {
102: GregorianCalendar cal = (GregorianCalendar) theInventoryReportSchedule.theBase
103: .clone();
104: cal.add(theInventoryReportSchedule.theStep, step);
105: theTime = cal.getTime().getTime();
106: }
107: return theTime;
108: }
109:
110: public String getItem() {
111: return theInventoryReportSchedule.theItemIdentification;
112: }
113: }
|