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:
027: package org.cougaar.mlm.plugin.ldm;
028:
029: import java.util.Calendar;
030: import java.util.Date;
031:
032: import org.cougaar.planning.ldm.asset.Asset;
033: import org.cougaar.planning.ldm.asset.NewTypeIdentificationPG;
034: import org.cougaar.planning.ldm.asset.TypeIdentificationPG;
035: import org.cougaar.planning.ldm.plan.NewRoleSchedule;
036: import org.cougaar.planning.ldm.plan.NewSchedule;
037:
038: /** AggregateAssetCreator implements QueryHandler to create sql queries
039: * and process the query results by creating aggregateassets.
040: */
041:
042: public class SQLAggregateAssetCreator extends PeriodicQuery {
043: public SQLAggregateAssetCreator() {
044: }
045:
046: public String getQuery() {
047: return (String) getParameter("query");
048: }
049:
050: public void processRow(Object[] data) {
051: //System.out.println("I see "+data[1]+" of "+data[0]);
052:
053: String nsn = (String) data[0];
054: Number count = (Number) data[1];
055: String nomenclature = (String) data[2];
056:
057: String tid = "NSN/" + nsn;
058: // System.out.println(myMessageAddress.getAddress() + ": " +
059: // "Creating aggregate asset : " + tid + " " +
060: // count + " " + nomenclature);
061:
062: Asset proto = findPrototype(tid, nomenclature);
063: Asset newaggasset = createAggregateAsset(proto, count
064: .intValue());
065: setupAvailableSchedule(newaggasset);
066: publishAdd(newaggasset);
067: }
068:
069: /** find or create a prototype asset suitable for using in an
070: * aggregate asset.
071: **/
072: private Asset findPrototype(String tid, String nomenclature) {
073: // find it if it already exists
074: Asset proto = ldm.getPrototype(tid);
075:
076: // if it wasn't found, make one and cache it
077: if (proto == null) {
078: proto = ldmf.createPrototype("Asset", tid);
079: ldm.cachePrototype(tid, proto);
080: }
081:
082: // set the nomenclature if needed (the prototypeProvider should be doing this)
083: TypeIdentificationPG tip = proto.getTypeIdentificationPG();
084: if (tip.getNomenclature() == null
085: && tip instanceof NewTypeIdentificationPG) {
086: ((NewTypeIdentificationPG) tip)
087: .setNomenclature(nomenclature);
088: }
089:
090: return proto;
091: }
092:
093: private Asset createAsset(String prototype, String nomenclature) {
094: return ldmf.createInstance(prototype, nomenclature);
095: }
096:
097: public Asset createAggregateAsset(Asset asset, int qty) {
098: // wrong:
099: // return asset.createAggregate(qty);
100: // right:
101: return ldmf.createAggregate(asset, qty);
102: }
103:
104: private void setupAvailableSchedule(Asset asset) {
105: Calendar mycalendar = Calendar.getInstance();
106: // set the start date of the available schedule to 01/01/1990
107: mycalendar.set(1990, 0, 1, 0, 0, 0);
108: Date start = mycalendar.getTime();
109: // set the end date of the available schedule to 01/01/2010
110: mycalendar.set(2010, 0, 1, 0, 0, 0);
111: Date end = mycalendar.getTime();
112: NewSchedule availsched = ldmf.newSimpleSchedule(start, end);
113: // set the available schedule
114: ((NewRoleSchedule) asset.getRoleSchedule())
115: .setAvailableSchedule(availsched);
116: }
117:
118: }
|