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.plugin.inventory;
028:
029: import org.cougaar.core.logging.NullLoggingServiceImpl;
030: import org.cougaar.glm.ldm.asset.Organization;
031: import org.cougaar.glm.ldm.plan.GeolocLocation;
032: import org.cougaar.planning.ldm.asset.AggregateAsset;
033: import org.cougaar.planning.ldm.asset.Asset;
034: import org.cougaar.planning.ldm.asset.LocationSchedulePG;
035: import org.cougaar.planning.ldm.asset.TypeIdentificationPG;
036: import org.cougaar.planning.ldm.plan.LocationScheduleElement;
037: import org.cougaar.planning.ldm.plan.Relationship;
038: import org.cougaar.planning.ldm.plan.RelationshipSchedule;
039: import org.cougaar.planning.ldm.plan.Role;
040: import org.cougaar.planning.ldm.plan.Schedule;
041: import org.cougaar.util.MutableTimeSpan;
042: import org.cougaar.util.NewTimeSpan;
043: import org.cougaar.util.TimeSpan;
044: import org.cougaar.util.log.Logger;
045:
046: import java.util.Collection;
047: import java.util.Enumeration;
048: import java.util.Iterator;
049: import java.util.Vector;
050:
051: /** Provides convenience methods. */
052: public class AssetUtils {
053:
054: private transient Logger logger;
055: private transient UtilsProvider utilProvider;
056:
057: public AssetUtils(UtilsProvider provider) {
058: utilProvider = provider;
059: if (utilProvider == null) {
060: logger = NullLoggingServiceImpl.getLoggingService();
061: } else {
062: logger = (Logger) utilProvider.getLoggingService(this );
063: }
064: }
065:
066: public AssetUtils(Logger aLogger) {
067: utilProvider = null;
068: logger = aLogger;
069: if (logger == null) {
070: logger = NullLoggingServiceImpl.getLoggingService();
071: }
072: }
073:
074: public String assetDesc(Asset asset) {
075: String nsn = getAssetIdentifier(asset);
076: return nsn + "(" + getPartNomenclature(asset) + ")";
077: }
078:
079: public static String getPartNomenclature(Asset part) {
080: String nomen = "Unknown part name";
081: TypeIdentificationPG tip = part.getTypeIdentificationPG();
082: if (tip != null) {
083: nomen = tip.getNomenclature();
084: }
085: return nomen;
086: }
087:
088: public String getAssetIdentifier(Asset asset) {
089: if (asset == null) {
090: return null;
091: } else {
092: TypeIdentificationPG tip = asset.getTypeIdentificationPG();
093: if (tip != null) {
094: return tip.getTypeIdentification();
095: } else {
096: logger.error("asset: " + asset
097: + " has null getTypeIdentificationPG()");
098: return null;
099: }
100: }
101: }
102:
103: public Enumeration getSupportingOrgs(Organization myOrg, Role role,
104: long time) {
105: RelationshipSchedule rel_sched = myOrg
106: .getRelationshipSchedule();
107: Collection c = rel_sched.getMatchingRelationships(role, time);
108: Vector support_orgs = new Vector();
109: Iterator i = c.iterator();
110: Relationship r;
111: while (i.hasNext()) {
112: r = (Relationship) i.next();
113: support_orgs.add(rel_sched.getOther(r));
114: }
115: return support_orgs.elements();
116: }
117:
118: public Enumeration getSupportingOrgs(Organization myOrg, Role role,
119: long start, long end) {
120: TimeSpan timespan = new MutableTimeSpan();
121: ((NewTimeSpan) timespan).setTimeSpan(start, end);
122: RelationshipSchedule rel_sched = myOrg
123: .getRelationshipSchedule();
124: Collection c = rel_sched.getMatchingRelationships(role,
125: timespan);
126: Vector support_orgs = new Vector();
127: Iterator i = c.iterator();
128: Relationship r;
129: while (i.hasNext()) {
130: r = (Relationship) i.next();
131: support_orgs.add(rel_sched.getOther(r));
132: }
133: return support_orgs.elements();
134: }
135:
136: public Enumeration getGeolocLocationAtTime(Organization org,
137: long time) {
138: LocationSchedulePG lspg = org.getLocationSchedulePG();
139: Vector geolocs = new Vector();
140: try {
141: Schedule ls = lspg.getSchedule();
142: Iterator i = ls.getScheduleElementsWithTime(time)
143: .iterator();
144: while (i.hasNext()) {
145: LocationScheduleElement lse = (LocationScheduleElement) i
146: .next();
147: geolocs.add((GeolocLocation) lse.getLocation());
148: }
149: } catch (NullPointerException npe) {
150: // Not all organizations have LocationSchedulePG's
151: logger
152: .info("AssetUtils AssetGeolocLocationAtTime(), LocationSchedulePG NOT found on "
153: + org);
154: }
155: return geolocs.elements();
156: }
157:
158: public boolean isLevel2Asset(Asset asset) {
159: Asset actualAsset = asset;
160: if (asset instanceof AggregateAsset) {
161: actualAsset = ((AggregateAsset) asset).getAsset();
162: }
163: return (getAssetIdentifier(actualAsset).startsWith("Level2"));
164: }
165:
166: public long getQuantity(Asset asset) {
167: long qty = 0;
168: if (asset instanceof AggregateAsset) {
169: AggregateAsset aa = (AggregateAsset) asset;
170: qty = aa.getQuantity();
171: } else {
172: qty = 1;
173: }
174: return qty;
175: }
176:
177: }
|