001: /*
002: * <copyright>
003: *
004: * Copyright 2000-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.tools.csmart.society.cdata;
027:
028: import org.cougaar.tools.csmart.core.cdata.AgentAssetData;
029: import org.cougaar.tools.csmart.core.cdata.AgentComponentData;
030: import org.cougaar.tools.csmart.core.cdata.ComponentData;
031: import org.cougaar.tools.csmart.core.cdata.PropGroupData;
032: import org.cougaar.tools.csmart.core.cdata.RelationshipData;
033: import org.cougaar.tools.csmart.core.property.ModifiableConfigurableComponent;
034: import org.cougaar.tools.csmart.core.property.Property;
035: import org.cougaar.tools.csmart.society.AssetComponent;
036: import org.cougaar.tools.csmart.society.ContainerBase;
037: import org.cougaar.tools.csmart.society.PropGroupBase;
038: import org.cougaar.tools.csmart.society.RelationshipBase;
039: import org.cougaar.tools.csmart.ui.viewer.CSMART;
040: import org.cougaar.util.TimeSpan;
041:
042: import java.text.DateFormat;
043: import java.text.ParseException;
044: import java.util.Collection;
045: import java.util.Date;
046: import java.util.Iterator;
047:
048: /**
049: * ConfigurableComponent that represents an <code>AssetComponentData</code>
050: */
051: public class AssetCDataComponent extends
052: ModifiableConfigurableComponent implements AssetComponent {
053:
054: /** Property Definitions **/
055:
056: /** Type Property Definition **/
057: // public static final String PROP_TYPE = "Asset Type";
058: /** Type Description Definition **/
059: // public static final String PROP_TYPE_DESC = "Type of Asset";
060: /** Class Definition **/
061: public static final String PROP_CLASS = "Asset Class";
062:
063: /** Class Description Definition **/
064: public static final String PROP_CLASS_DESC = "Class of the Asset";
065:
066: /** UID Definition **/
067: public static final String PROP_UID = "UID";
068:
069: /** UID Description Definition **/
070: public static final String PROP_UID_DESC = "UID of the Asset";
071:
072: /** Unit Name Definition **/
073: public static final String PROP_UNITNAME = "Unit Name";
074:
075: /** Unit Name Description Definition **/
076: public static final String PROP_UNITNAME_DESC = "Unit Name of the Asset";
077:
078: /** UIC Definition **/
079: public static final String PROP_UIC = "UIC";
080:
081: /** UID Description Definition **/
082: public static final String PROP_UIC_DESC = "UIC of the Asset";
083:
084: // private Property propAssetType;
085: private Property propAssetClass;
086: private Property propUniqueID;
087: private Property propUnitName;
088: private Property propUIC;
089: private AgentAssetData createdFromData;
090:
091: /**
092: * Creates a new <code>AssetCDataComponent</code> instance.
093: *
094: * @param data <code>AgentAssetData</code> to create from
095: */
096: public AssetCDataComponent(AgentAssetData data) {
097: super ("AssetData");
098: createdFromData = data;
099: }
100:
101: /**
102: * Init properties from AgentAssetData
103: */
104:
105: public void initProperties() {
106: // propAssetType = addProperty(PROP_TYPE,
107: // new Integer(createdFromData.getType()));
108: // propAssetType.setToolTip(PROP_TYPE_DESC);
109: if (createdFromData == null)
110: return;
111: propAssetClass = addProperty(PROP_CLASS, createdFromData
112: .getAssetClass());
113: propAssetClass.setToolTip(PROP_CLASS_DESC);
114: if (createdFromData.getUniqueID() != null) {
115: propUniqueID = addProperty(PROP_UID, createdFromData
116: .getUniqueID());
117: propUniqueID.setToolTip(PROP_UID_DESC);
118: }
119: // Unit name is allowed to be null, if it is, give an empty string.
120: String unitname = (createdFromData.getUnitName() == null) ? ""
121: : createdFromData.getUnitName();
122: propUnitName = addProperty(PROP_UNITNAME, unitname);
123: propUnitName.setToolTip(PROP_UNITNAME_DESC);
124:
125: if (createdFromData.getUIC() != null) {
126: propUIC = addProperty(PROP_UIC, createdFromData.getUIC());
127: propUIC.setToolTip(PROP_UIC_DESC);
128: }
129:
130: addPropGroups(createdFromData);
131: addRelationships(createdFromData.getRelationshipData());
132: }
133:
134: /**
135: * Add component data for asset properties, relationships,
136: * and property groups. Only additions are made in this method,
137: * no modifications to existing data.
138: *
139: * @param data Pointer to the global <code>ComponentData</code> tree
140: * @return an updated <code>ComponentData</code> value
141: */
142: public ComponentData addComponentData(ComponentData data) {
143: AgentAssetData assetData = new AgentAssetData(
144: (AgentComponentData) data);
145: // assetData.setType(((Integer)propAssetType.getValue()).intValue());
146: assetData.setAssetClass((String) propAssetClass.getValue());
147: assetData.setUniqueID((String) propUniqueID.getValue());
148: assetData.setUnitName((String) propUnitName.getValue());
149: assetData.setUIC((String) propUIC.getValue());
150:
151: // Add Relationships.
152: Iterator iter = ((Collection) getDescendentsOfClass(ContainerBase.class))
153: .iterator();
154: while (iter.hasNext()) {
155: ContainerBase container = (ContainerBase) iter.next();
156: if (container.getShortName().equals("Relationships")) {
157: for (int i = 0; i < container.getChildCount(); i++) {
158: RelationshipBase rel = (RelationshipBase) container
159: .getChild(i);
160: RelationshipData rData = new RelationshipData();
161: if (rel != null) {
162: if (rel.getProperty(RelationshipBase.PROP_TYPE) == null) {
163: if (log.isErrorEnabled()) {
164: log
165: .error(this
166: + " using CSMART "
167: + CSMART.writeDebug()
168: + " got null relationship type for "
169: + data.getName()
170: + " in rel #"
171: + i
172: + " with role="
173: + rel
174: .getProperty(RelationshipBase.PROP_ROLE)
175: + ", ItemId="
176: + rel
177: .getProperty(RelationshipBase.PROP_ITEM)
178: + ", please report this on bug# 1304");
179: }
180: } else {
181: rData.setType((String) rel.getProperty(
182: RelationshipBase.PROP_TYPE)
183: .getValue());
184: }
185: rData.setRole((String) rel.getProperty(
186: RelationshipBase.PROP_ROLE).getValue());
187: rData.setItemId((String) rel.getProperty(
188: RelationshipBase.PROP_ITEM).getValue());
189: rData.setTypeId((String) rel.getProperty(
190: RelationshipBase.PROP_TYPEID)
191: .getValue());
192: rData.setSupported((String) rel.getProperty(
193: RelationshipBase.PROP_SUPPORTED)
194: .getValue());
195:
196: DateFormat df = DateFormat.getInstance();
197: try {
198: Date start = df
199: .parse((String) rel
200: .getProperty(
201: RelationshipBase.PROP_STARTTIME)
202: .getValue());
203: Date end = df
204: .parse((String) rel
205: .getProperty(
206: RelationshipBase.PROP_STOPTIME)
207: .getValue());
208: rData.setStartTime(start.getTime());
209: rData.setEndTime(end.getTime());
210: } catch (ParseException pe) {
211: if (log.isErrorEnabled()) {
212: log
213: .error(
214: "Caught Exception parsing Date, using default dates.",
215: pe);
216: }
217: rData.setStartTime(TimeSpan.MIN_VALUE);
218: rData.setEndTime(TimeSpan.MAX_VALUE);
219: }
220:
221: assetData.addRelationship(rData);
222: } else {
223: // null relationship?
224: if (log.isErrorEnabled()) {
225: log.error(this + " Got null relationship #"
226: + i);
227: }
228: }
229: }
230: }
231: }
232:
233: // Add Property Groups.
234: iter = ((Collection) getDescendentsOfClass(ContainerBase.class))
235: .iterator();
236: while (iter.hasNext()) {
237: ContainerBase container = (ContainerBase) iter.next();
238: if (container.getShortName().equals("Property Groups")) {
239: for (int i = 0; i < container.getChildCount(); i++) {
240: PropGroupBase pg = (PropGroupBase) container
241: .getChild(i);
242: assetData.addPropertyGroup(pg.getPropGroupData());
243: }
244: }
245: }
246:
247: data.addAgentAssetData(assetData);
248: return data;
249: }
250:
251: private void addRelationships(RelationshipData[] rel) {
252: ContainerBase relContainer = new ContainerBase("Relationships");
253: relContainer.initProperties();
254: addChild(relContainer);
255: for (int i = 0; i < rel.length; i++) {
256: RelationshipBase newR = new RelationshipBase(rel[i]);
257: newR.initProperties();
258: relContainer.addChild(newR);
259: }
260: }
261:
262: private void addPropGroups(AgentAssetData aad) {
263: ContainerBase pgContainer = new ContainerBase("Property Groups");
264: pgContainer.initProperties();
265: addChild(pgContainer);
266: Iterator iter = aad.getPropGroupsIterator();
267: while (iter.hasNext()) {
268: PropGroupData pgd = (PropGroupData) iter.next();
269: PropGroupBase newPG = new PropGroupBase(pgd);
270: if (log.isDebugEnabled()) {
271: log.debug("Adding: " + pgd.getName());
272: }
273: newPG.initProperties();
274: pgContainer.addChild(newPG);
275: }
276: }
277:
278: }
|