001: /*
002: * <copyright>
003: *
004: * Copyright 2001-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.lib.vishnu.client.custom;
027:
028: import org.cougaar.planning.ldm.asset.Asset;
029: import org.cougaar.planning.ldm.plan.PlanElement;
030: import org.cougaar.planning.ldm.plan.RoleSchedule;
031: import org.cougaar.planning.ldm.plan.Schedule;
032: import org.cougaar.planning.ldm.plan.NamedPosition;
033: import org.cougaar.planning.ldm.plan.LatLonPoint;
034: import org.cougaar.util.TimeSpan;
035: import org.cougaar.util.log.Logger;
036: import org.w3c.dom.Document;
037: import org.w3c.dom.Element;
038:
039: import java.text.SimpleDateFormat;
040: import java.util.Calendar;
041: import java.util.Collection;
042: import java.util.Date;
043: import java.util.Iterator;
044:
045: /**
046: * Fills in DOM document nodes given Cougaar Objects.
047: *
048: */
049: public class XMLDataHelper implements DataHelper {
050: /**
051: * Sets up endOfWorld variable -- used in createRoleScheduleListField
052: *
053: * @see #createRoleScheduleListField
054: */
055: public XMLDataHelper(Logger logger) {
056: final Calendar calendar = Calendar.getInstance();
057: calendar.set(2100, 1, 1);
058: endOfWorld = calendar.getTime();
059: this .logger = logger;
060: }
061:
062: /** set the document that is being created */
063: public void setDoc(Document doc) {
064: this .doc = doc;
065: }
066:
067: /**
068: * Translates Asset role schedule into equivalent Vishnu XML. <p>
069: *
070: * TimeSpans(PlanElements) in the role schedule get translated into Vishnu intervals.<p>
071: *
072: * Calls getScheduleFields to do real work.
073: * @param object - the DOM Element to add the <code>name</code> list field to
074: * @param name - the name of the list field
075: * @param asset - the asset with the role schedule
076: * @see #createScheduleFields
077: * @see org.cougaar.planning.ldm.plan.RoleSchedule
078: */
079: public void createRoleScheduleListField(Object object, String name,
080: Asset asset) {
081: RoleSchedule unavail = asset.getRoleSchedule();
082:
083: Element list = (Element) startList(object, name);
084:
085: createScheduleFields(unavail.getEncapsulatedRoleSchedule(0,
086: endOfWorld.getTime()), list, true);
087: }
088:
089: /**
090: * Translates Asset available schedule into equivalent Vishnu XML. <p>
091: *
092: * TimeSpans(PlanElements) in the available schedule get translated into Vishnu intervals.<p>
093: * Calls getScheduleFields to do real work.
094: * @param object - the DOM Element to add the <code>name</code> list field to
095: * @param name - the name of the list field
096: * @param asset - the asset with the role schedule
097: * @see #createScheduleFields
098: */
099: public void createAvailableScheduleListField(Object object,
100: String name, Asset asset) {
101: Element list = (Element) startList(object, name);
102:
103: Schedule availSchedule = asset.getRoleSchedule()
104: .getAvailableSchedule();
105: if (availSchedule == null) {
106: if (logger.isDebugEnabled())
107: logger.debug("No available schedule on asset " + asset);
108: } else {
109: Collection coll = availSchedule
110: .getEncapsulatedScheduleElements(0, endOfWorld
111: .getTime());
112: if (coll.isEmpty())
113: logger.warn("XMLDataHelper -- availSchedule for "
114: + asset + " is empty");
115:
116: createScheduleFields(availSchedule
117: .getEncapsulatedScheduleElements(0, endOfWorld
118: .getTime()), list, false);
119: }
120: }
121:
122: public Object startList(Object object, String name) {
123: Element field = doc.createElement("FIELD");
124: field.setAttribute("name", name);
125: ((Element) object).appendChild(field);
126: Element list = doc.createElement("LIST");
127: field.appendChild(list);
128: return list;
129: }
130:
131: /**
132: * Walk the schedule and append interval elements to the list element for
133: * each TimeSpan in the schedule.
134: *
135: * @param schedule TimeSpan Collection from the role schedule or available schedule
136: * @param list elem to add interval elements to
137: * @param isRole if true, sets the interval label1 to the Verb of the Task of the
138: * TimeSpan/PlanElement
139: * @see org.cougaar.util.TimeSpan
140: * @see org.cougaar.planning.ldm.plan.PlanElement
141: */
142: protected void createScheduleFields(Collection schedule,
143: Element list, boolean isRole) {
144: for (Iterator iter = schedule.iterator(); iter.hasNext();) {
145: TimeSpan span = (TimeSpan) iter.next();
146:
147: // Element value = doc.createElement("VALUE");
148: // list.appendChild (value);
149:
150: Element interval = doc.createElement("OBJECT");
151: interval.setAttribute("type", "interval");
152: // value.appendChild (interval);
153: addListValue(list, "", "", interval);
154:
155: createField(interval, "interval", "start", format
156: .format(new Date(span.getStartTime())));
157: createField(interval, "interval", "end", format
158: .format(new Date(span.getEndTime())));
159: if (isRole)
160: createField(interval, "interval", "label1", ""
161: + ((PlanElement) span).getTask().getVerb());
162: else
163: createField(interval, "interval", "label1",
164: "available_interval");
165:
166: createField(interval, "interval", "label2", " ");
167:
168: }
169: }
170:
171: public void addListValue(Object parent, String fieldName,
172: String type, Object toAppend) {
173: Element value = doc.createElement("VALUE");
174: ((Element) parent).appendChild(value);
175:
176: value.appendChild((Element) toAppend);
177: }
178:
179: /**
180: * Translate a Cougaar GeolocLocation into the equivalent DOM structure. <p>
181: *
182: * Adds a latlong object to the parent object, and adds the geoloc
183: * to the parent.
184: *
185: * @param parent - the Element to add the <code>parentFieldName</code> geoloc field to
186: * @param parentFieldName - the base name of the geoloc field
187: * @param loc - the Cougaar GeolocLocation to translate into a DOM structure
188: */
189: public void createGeoloc(Object parent, String parentFieldName,
190: NamedPosition loc) {
191: Object geolocObject = createObject(parent, "geoloc");
192: createField(geolocObject, "geoloc", "geolocCode", loc.getUid());
193: Object field = createField(geolocObject, "geoloc", "latlong");
194: Object latlongObject = createObject(field, "latlong");
195:
196: createField(latlongObject, "latlong", "latitude", ""
197: + loc.getLatitude().getDegrees());
198: createField(latlongObject, "latlong", "longitude", ""
199: + loc.getLongitude().getDegrees());
200: }
201:
202: public void createLatLon(Object parent, String parentFieldName,
203: LatLonPoint loc) {
204: Object field = createField(parent, "latlong", parentFieldName);
205: Object latlongObject = createObject(field, "latlong");
206:
207: createField(latlongObject, "latlong", "latitude", ""
208: + loc.getLatitude().getDegrees());
209: createField(latlongObject, "latlong", "longitude", ""
210: + loc.getLongitude().getDegrees());
211: }
212:
213: /**
214: * create a DOM Element of type <code>type</code>
215: *
216: * @param parent - Element to attach this new Element to
217: * @param type - type of the new OBJECT Element
218: */
219: public Object createObject(Object parent, String type) {
220: Element elem = doc.createElement("OBJECT");
221: elem.setAttribute("type", type);
222: ((Element) parent).appendChild(elem);
223: return elem;
224: }
225:
226: /**
227: * add a field on <code>parent</code>
228: *
229: * @param parentType - ignored, important in DirectDataHelper
230: */
231: public Object createField(Object parent, String parentType,
232: String name) {
233: Element elem = doc.createElement("FIELD");
234: elem.setAttribute("name", name);
235: ((Element) parent).appendChild(elem);
236: return elem;
237: }
238:
239: /** create field as name-value pair */
240: protected Object createFieldPair(String name, String value) {
241: Element elem = doc.createElement("FIELD");
242: elem.setAttribute("name", name);
243: elem.setAttribute("value", value);
244: return elem;
245: }
246:
247: /**
248: * Generic field creation <p>
249: *
250: * adds a field as name-value pair to <code>parent</code>
251: *
252: * @see #createFieldPair
253: * @param parent SchObject object to add the field to
254: * @param parentType - ignored, important in DirectDataHelper
255: * @param name field name
256: * @param value field's value
257: */
258: public void createField(Object parent, String parentType,
259: String name, String value) {
260: ((Element) parent).appendChild((Element) createFieldPair(name,
261: value));
262: }
263:
264: /**
265: * shortcut to create a date field on <code>parent</code> <p>
266: *
267: * Translates date into string.
268: */
269: public void createDateField(Object parent, String name, Date date) {
270: try {
271: createField(parent, "", name, format.format(date));
272: } catch (Exception e) {
273: }
274: }
275:
276: /** shortcut to create a boolean field on <code>parent</code> */
277: public void createBooleanField(Object parent, String name,
278: boolean value) {
279: createField(parent, "", name, value ? "true" : "false");
280: }
281:
282: /** shortcut to create a float field on <code>parent</code> */
283: public void createFloatField(Object parent, String name, float value) {
284: createField(parent, "", name, "" + value);
285: }
286:
287: protected Document doc;
288: protected Date endOfWorld;
289: private final SimpleDateFormat format = new SimpleDateFormat(
290: "yyyy-MM-dd HH:mm:ss");
291: Logger logger;
292: }
|