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.planning.servlet.data.hierarchy;
027:
028: import java.io.IOException;
029: import java.io.Serializable;
030: import java.util.ArrayList;
031: import java.util.List;
032:
033: import org.cougaar.planning.servlet.data.xml.DeXMLable;
034: import org.cougaar.planning.servlet.data.xml.UnexpectedXMLException;
035: import org.cougaar.planning.servlet.data.xml.XMLWriter;
036: import org.cougaar.planning.servlet.data.xml.XMLable;
037: import org.xml.sax.Attributes;
038:
039: /**
040: * Represents the organization data within the hierarchy PSP
041: *
042: * @since 1/24/01
043: **/
044: public class Organization implements XMLable, DeXMLable, Serializable,
045: Comparable {
046:
047: //Variables:
048: ////////////
049:
050: public final static String NAME_TAG = "Org";
051: protected final static String UID_TAG = "OrgID";
052: protected final static String PRETTY_NAME_TAG = "Name";
053: protected final static String RELATION_TAG = "Rel";
054:
055: public static final int ADMIN_SUBORDINATE = 0;
056: public static final int SUBORDINATE = 1;
057:
058: protected String UID;
059: protected String prettyName;
060: protected List relations;
061:
062: //Constructors:
063: ///////////////
064:
065: public Organization() {
066: relations = new ArrayList();
067: }
068:
069: //Members:
070: //////////
071:
072: public void setUID(String UID) {
073: this .UID = UID;
074: }
075:
076: public void setPrettyName(String name) {
077: prettyName = name;
078: }
079:
080: public void addRelation(String UID, int relation) {
081: relations.add(new OrgRelation(UID, relation));
082: }
083:
084: public void addRelation(String UID, String relation) {
085: relations.add(new OrgRelation(UID, relation));
086: }
087:
088: public String getUID() {
089: return UID;
090: }
091:
092: public String getPrettyName() {
093: return prettyName;
094: }
095:
096: public int getNumRelations() {
097: return relations.size();
098: }
099:
100: public String getRelationUIDAt(int i) {
101: OrgRelation or = (OrgRelation) relations.get(i);
102: return or.org;
103: }
104:
105: public int getRelationAt(int i) {
106: OrgRelation or = (OrgRelation) relations.get(i);
107: return or.relation;
108: }
109:
110: public OrgRelation getOrgRelationAt(int i) {
111: return (OrgRelation) relations.get(i);
112: }
113:
114: public String getRelatedOrgAt(int i) {
115: return ((OrgRelation) relations.get(i)).getRelatedOrg();
116: }
117:
118: public String getNamedRelationAt(int i) {
119: return ((OrgRelation) relations.get(i)).getName();
120: }
121:
122: public List getRelations() {
123: return relations;
124: }
125:
126: public int compareTo(Object other) {
127: if (!(other instanceof Organization))
128: return 0;
129: int value = UID.compareTo(((Organization) other).UID);
130: return value;
131: }
132:
133: public boolean equals(Object other) {
134: if (!(other instanceof Organization)) {
135: System.out
136: .println("ERROR ERROR ERROR comparing with non-org "
137: + other);
138: return false;
139: }
140: boolean value = UID.equals(((Organization) other).UID);
141: return value;
142: }
143:
144: public String toString() {
145: return UID;
146: }
147:
148: //XMLable members:
149: //----------------
150:
151: /**
152: * Write this class out to the Writer in XML format
153: * @param w output Writer
154: **/
155: public void toXML(XMLWriter w) throws IOException {
156: w.optagln(NAME_TAG);
157:
158: w.tagln(UID_TAG, getUID());
159: w.tagln(PRETTY_NAME_TAG, getPrettyName());
160:
161: for (int i = 0; i < getNumRelations(); i++) {
162: OrgRelation relation = getOrgRelationAt(i);
163: w.sitagln(RELATION_TAG, UID_TAG, getRelationUIDAt(i),
164: RELATION_TAG, (relation.hasName() ? relation
165: .getName() : Integer
166: .toString(getRelationAt(i))));
167: }
168:
169: w.cltagln(NAME_TAG);
170: }
171:
172: //DeXMLable members:
173: //------------------
174:
175: /**
176: * Report a startElement that pertains to THIS object, not any
177: * sub objects. Call also provides the elements Attributes and data.
178: * Note, that unlike in a SAX parser, data is guaranteed to contain
179: * ALL of this tag's data, not just a 'chunk' of it.
180: * @param name startElement tag
181: * @param attr attributes for this tag
182: * @param data data for this tag
183: **/
184: public void openTag(String name, Attributes attr, String data)
185: throws UnexpectedXMLException {
186: if (name.equals(NAME_TAG)) {
187: } else if (name.equals(PRETTY_NAME_TAG)) {
188: setPrettyName(data);
189: } else if (name.equals(UID_TAG)) {
190: setUID(data);
191: } else if (name.equals(RELATION_TAG)) {
192: try {
193: addRelation(attr.getValue(UID_TAG), Integer
194: .parseInt(attr.getValue(RELATION_TAG)));
195: } catch (NumberFormatException e) {
196: addRelation(attr.getValue(UID_TAG), attr
197: .getValue(RELATION_TAG));
198: }
199: } else {
200: throw new UnexpectedXMLException("Unexpected tag: " + name);
201: }
202: }
203:
204: /**
205: * Report an endElement.
206: * @param name endElement tag
207: * @return true iff the object is DONE being DeXMLized
208: **/
209: public boolean closeTag(String name) throws UnexpectedXMLException {
210: return name.equals(NAME_TAG);
211: }
212:
213: /**
214: * This function will be called whenever a subobject has
215: * completed de-XMLizing and needs to be encorporated into
216: * this object.
217: * @param name the startElement tag that caused this subobject
218: * to be created
219: * @param obj the object itself
220: **/
221: public void completeSubObject(String name, DeXMLable obj)
222: throws UnexpectedXMLException {
223: }
224:
225: public static class OrgRelation implements Serializable, Comparable {
226: public String org;
227: public int relation;
228: public String relationName;
229: public boolean hasName = false;
230:
231: public OrgRelation(String o, int r) {
232: org = o;
233: relation = r;
234: }
235:
236: public OrgRelation(String org, String relation) {
237: this .org = org;
238: relationName = relation;
239: hasName = true;
240: }
241:
242: public int compareTo(Object other) {
243: if (!(other instanceof OrgRelation))
244: return 0;
245: int comp = org.compareTo(((OrgRelation) other).org);
246: if (comp == 0)
247: return relationName
248: .compareTo(((OrgRelation) other).relationName);
249: else
250: return comp;
251: }
252:
253: public boolean hasName() {
254: return hasName;
255: }
256:
257: public String getName() {
258: return relationName;
259: }
260:
261: public String getRelatedOrg() {
262: return org;
263: }
264:
265: /**
266: * Set the serialVersionUID to keep the object serializer from seeing
267: * xerces (org.xml.sax.Attributes).
268: */
269: private static final long serialVersionUID = 109382094832059403L;
270: }
271:
272: /**
273: * Set the serialVersionUID to keep the object serializer from seeing
274: * xerces (org.xml.sax.Attributes).
275: */
276: private static final long serialVersionUID = 893487223987438473L;
277: }
|