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.planning.plugin.asset;
028:
029: import java.util.Arrays;
030:
031: import org.cougaar.planning.ldm.asset.NewPropertyGroup;
032: import org.cougaar.planning.service.AssetInitializerService;
033: import org.cougaar.util.log.Logger;
034: import org.cougaar.util.log.Logging;
035:
036: /**
037: * Populates an "Asset" from the config database.
038: **/
039: public class AssetDataDBReader implements AssetDataReader {
040: private static Logger logger = Logging
041: .getLogger(AssetDataDBReader.class);
042:
043: private AssetDataCallback cb;
044: private String agentId;
045: AssetInitializerService assetInitService;
046:
047: public AssetDataDBReader(AssetInitializerService ais) {
048: assetInitService = ais;
049: }
050:
051: /**
052: *
053: */
054: public void readAsset(String aId, AssetDataCallback cb) {
055: this .cb = cb;
056:
057: if (assetInitService == null) {
058: logger.fatal("AssetInitializerService is null."
059: + " Unable to create local asset for " + aId);
060: return;
061: }
062: try {
063: agentId = aId;
064: cb.createMyLocalAsset(assetInitService
065: .getAgentPrototype(aId));
066: String[] pgNames = assetInitService
067: .getAgentPropertyGroupNames(aId);
068: for (int i = 0; i < pgNames.length; i++) {
069: String pgName = pgNames[i];
070: NewPropertyGroup pg = cb.createPropertyGroup(pgName);
071: cb.addPropertyToAsset(pg);
072: Object[][] props = assetInitService.getAgentProperties(
073: aId, pgName);
074: for (int j = 0; j < props.length; j++) {
075: Object[] prop = props[j];
076: String attributeName = (String) prop[0];
077: String attributeType = (String) prop[1];
078: Object attributeValue = prop[2];
079: if (attributeType.startsWith("query")) {
080: String v = ((String) attributeValue).trim();
081: Object[] r = assetInitService
082: .translateAttributeValue(attributeType,
083: v);
084: attributeType = (String) r[0];
085: attributeValue = r[1];
086: }
087: if (attributeType.equals("FixedLocation")) {
088: String v = ((String) attributeValue).trim();
089: if (v.startsWith("("))
090: v = v.substring(1);
091: if (v.endsWith(")"))
092: v = v.substring(0, v.length() - 1);
093: int ix = v.indexOf(',');
094: String latStr = v.substring(0, ix);
095: String lonStr = v.substring(ix + 1);
096: cb.setLocationSchedule(latStr.trim(), lonStr
097: .trim());
098: } else {
099: if (attributeValue.getClass().isArray()) {
100: String[] rv = (String[]) attributeValue;
101: Object[] pv = new Object[rv.length];
102: for (int k = 0; k < rv.length; k++) {
103: pv[k] = cb.parseExpr(attributeType,
104: rv[k]);
105: }
106: Object[] args = { Arrays.asList(pv) };
107: cb.callSetter(pg, "set" + attributeName,
108: "Collection", args);
109: } else {
110: Object[] args = { cb.parseExpr(
111: attributeType,
112: (String) attributeValue) };
113: cb.callSetter(pg, "set" + attributeName, cb
114: .getType(attributeType), args);
115: }
116: }
117: }
118: }
119: String[][] relationships = assetInitService
120: .getAgentRelationships(aId);
121: for (int i = 0; i < relationships.length; i++) {
122: String[] r = relationships[i];
123: long start = cb.getDefaultStartTime();
124: long end = cb.getDefaultEndTime();
125: try {
126: start = cb.parseDate(r[4]);
127: } catch (java.text.ParseException pe) {
128: logger.error("Unable to parse: " + r[4]
129: + ". Start time defaulting to " + start);
130: }
131: try {
132: if (r[5] != null) {
133: end = cb.parseDate(r[5]);
134: }
135: } catch (java.text.ParseException pe) {
136: logger.error("Unable to parse: " + r[5]
137: + ". End time defaulted to " + end);
138: }
139: cb.addRelationship(r[2], // Type id
140: r[1], // Item id
141: r[3], // Other agent
142: r[0], // Role
143: start, // Start time
144: end); // End time
145: }
146: } catch (Exception e) {
147: e.printStackTrace();
148: throw new RuntimeException(e.toString());
149: }
150: }
151: }
|