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.ldm.asset;
028:
029: import org.cougaar.core.domain.FactoryException;
030:
031: /**
032: * This is the class that is responsible for generating a LDM object
033: * based upon a Data Dictionary string
034: */
035: public class EssentialAssetFactory {
036:
037: /**
038: * Constructor. Create a new instance of a the Factory.
039: **/
040: public EssentialAssetFactory() {
041: }
042:
043: /**
044: * Copy method for any ASSET object.
045: * @return ASSET object The object created by the factory returned as a unique instance
046: * @exception ASSETFactoryException If the method fails to create a new ASSET object .
047: **/
048: public Asset copy(Asset original, String aUniqueID)
049: throws FactoryException {
050: // We need to clone it before treating it like a unique instance
051: Asset anInstance = null;
052: try {
053: anInstance = (Asset) (original.clone());
054:
055: // Associate an ItemIdentifierProperty with the new Asset.
056: // The clone already has it's own IIP, no need to cons a new one.
057: //NewItemIdentificationPG iip = PropertyGroupFactory.newItemIdentificationPG();
058: NewItemIdentificationPG iip = (NewItemIdentificationPG) anInstance
059: .getItemIdentificationPG();
060: iip.setItemIdentification(aUniqueID);
061: iip.setNomenclature(aUniqueID);
062: //iip.setAlternateItemIdentification( aUniqueID );
063: //anInstance.setItemIdentificationPG( iip );
064: } catch (CloneNotSupportedException ce) {
065: ce.printStackTrace();
066: throw new FactoryException("Clone not supported for "
067: + aUniqueID + " " + original.getClass());
068: }
069:
070: return anInstance;
071: }
072:
073: /**
074: * Method to create a new object of type Asset.
075: * This should only be called from the LDMFactory.
076: * @return Asset object The object created by the factory returned as an abstract object type
077: * @exception FactoryException If the method fails to create a new Asset .
078: **/
079: public Asset create(Class myClass, String aTypeID)
080: throws FactoryException {
081: // Assign existing prototypes from the cache
082: // otherwise create a new prototype and add it to the cache
083: // The dictionary name is required to find the correct instance of a
084: // prototype object.
085: Asset theAsset = null;
086: try {
087: theAsset = (Asset) myClass.newInstance();
088: if (theAsset == null)
089: throw new FactoryException(
090: "Could not create the Data Model Object for class "
091: + myClass);
092:
093: // Attach the Type Identification property here because
094: // 1. We have the value, and
095: // 2. We depend on the Type Identification value to form Domain Names for other Properties
096: // TypeIdentificationPGImpl tip = (TypeIdentificationPropertyImpl)theAsset.createTypeIdentificationProperty();
097:
098: String convertedID = aTypeID;
099: if (aTypeID == null)
100: convertedID = "RuntimePrototype";
101:
102: NewTypeIdentificationPG tip = (NewTypeIdentificationPG) theAsset
103: .getTypeIdentificationPG();
104: tip.setTypeIdentification(convertedID);
105: tip.setNomenclature(convertedID);
106: //theAsset.setTypeIdentificationPG( tip ); // All TypeIds are mutable!
107:
108: /*
109: theAsset.setTypeIdentificationPG((TypeIdentificationPG)
110: tip.lock(getKey()));
111: */
112: theAsset.setTypeIdentificationPG(tip);
113: } catch (Exception be) {
114: be.printStackTrace();
115: throw new FactoryException(
116: "Exception caught while building " + aTypeID
117: + " based on asset class: " + myClass
118: + ".\n" + be.toString());
119: }
120:
121: return theAsset;
122: }
123:
124: /** return the assetFactory's property key **/
125: private Object getKey() {
126: return this ;
127: }
128:
129: public int hashCode() {
130: return this .getClass().hashCode();
131: }
132:
133: public boolean equals(Object o) {
134: return o != null
135: && (this == o || this.getClass() == o.getClass());
136: }
137: }
|