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.logistics.ldm.asset;
028:
029: import org.cougaar.core.service.LoggingService;
030: import org.cougaar.logistics.ldm.MEIPrototypeProvider;
031: import org.cougaar.planning.ldm.asset.AggregateAsset;
032: import org.cougaar.planning.ldm.asset.Asset;
033:
034: import java.math.BigDecimal;
035: import java.util.Collection;
036: import java.util.Enumeration;
037: import java.util.HashMap;
038: import java.util.Vector;
039:
040: public class Level2FuelConsumerBG extends FuelConsumerBG {
041: public final static String LEVEL2BULKPOL = "Level2BulkPOL";
042: private transient LoggingService logger = LoggingService.NULL;
043: private String orgName = null;
044:
045: public Level2FuelConsumerBG(FuelConsumerPG pg) {
046: super (pg);
047: }
048:
049: public void initialize(MEIPrototypeProvider plugin) {
050: super .initialize(plugin);
051: logger = parentPlugin.getLoggingService(this );
052: }
053:
054: public Collection getConsumed() {
055: if (orgName == null) {
056: if (parentPlugin != null)
057: orgName = parentPlugin.getMyOrg()
058: .getItemIdentificationPG()
059: .getItemIdentification();
060: else {
061: if (logger != null)
062: logger.error("getConsumed - parentPlugin is null!");
063: else
064: System.err
065: .println("Lvl2FuelConsBG.getConsumed null parent / logger");
066: }
067: }
068: if (consumptionRates == null) {
069: synchronized (cachedDBValues) {
070: Asset asset = myPG.getMei();
071: if (asset instanceof AggregateAsset) {
072: asset = ((AggregateAsset) asset).getAsset();
073: }
074: String typeId = asset.getTypeIdentificationPG()
075: .getTypeIdentification();
076: consumptionRates = (HashMap) cachedDBValues.get(orgName
077: + typeId);
078: if (consumptionRates == null) {
079: Vector result = null;
080: if (parentPlugin != null)
081: result = parentPlugin
082: .lookupLevel2AssetConsumptionRate(
083: orgName, asset, supplyType);
084: else {
085: if (logger != null)
086: logger
087: .error("getConsumed - null parentPlugin for "
088: + asset);
089: else
090: System.err
091: .println("Lvl2FuelConsBG.getConsumed null parent / logger for "
092: + asset);
093: }
094: if (result == null) {
095: if (logger.isDebugEnabled())
096: logger
097: .debug("getConsumed(): Database query returned EMPTY result set for "
098: + myPG.getMei()
099: + ", "
100: + supplyType
101: + " "
102: + LEVEL2BULKPOL);
103: } else {
104: consumptionRates = parseResults(result);
105: cachedDBValues.put(orgName + typeId,
106: consumptionRates);
107: }
108: }
109: }
110: }
111: if (consumptionRates == null) {
112: if (logger.isDebugEnabled())
113: logger
114: .debug("No consumption rates for "
115: + myPG.getMei()
116: + ((parentPlugin != null) ? (" at " + parentPlugin
117: .getMyOrg()
118: .getItemIdentificationPG()
119: .getItemIdentification())
120: : ""));
121: consumptionRates = new HashMap();
122: }
123: return consumptionRates.keySet();
124: }
125:
126: protected HashMap parseResults(Vector result) {
127: String optempo = null;
128: double dcr = 0.0;
129: Asset newAsset = null;
130: HashMap map = null, ratesMap = new HashMap();
131:
132: Enumeration results = result.elements();
133: Object row[];
134: while (results.hasMoreElements()) {
135: row = (Object[]) results.nextElement();
136: if (logger.isDebugEnabled())
137: logger.debug("BulkPOL: parsing results for Level2MEI ");
138: newAsset = parentPlugin.getPrototype(LEVEL2BULKPOL);
139: if (newAsset != null) {
140: optempo = (String) row[0];
141: dcr = ((BigDecimal) row[1]).doubleValue();
142: map = (HashMap) ratesMap.get(newAsset);
143: if (map == null) {
144: map = new HashMap();
145: ratesMap.put(newAsset, map);
146: }
147: map.put(optempo.toUpperCase(), new Double(dcr));
148: if (logger.isDebugEnabled())
149: logger.debug("parseResult() for " + newAsset
150: + ", Level2MEI " + ", DCR " + dcr
151: + ", Optempo " + optempo);
152: } else {
153: logger
154: .error("parseResults() Unable to get prototype for "
155: + LEVEL2BULKPOL);
156: }
157: }
158: if (ratesMap.isEmpty()) {
159: System.out.println(" rates map empty for "
160: + newAsset.getTypeIdentificationPG()
161: .getTypeIdentification());
162: }
163: return ratesMap;
164: }
165: }
|