001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.rm.metadata;
020:
021: import org.openharmonise.commons.dsi.AbstractDataStoreInterface;
022: import org.openharmonise.commons.xml.XMLUtils;
023: import org.openharmonise.rm.*;
024: import org.openharmonise.rm.factory.*;
025: import org.openharmonise.rm.resources.AbstractObject;
026: import org.openharmonise.rm.resources.metadata.properties.Property;
027: import org.openharmonise.rm.resources.metadata.properties.ranges.Range;
028: import org.w3c.dom.Element;
029:
030: /**
031: * A factory class providing <code>static</code> methods for returning
032: * property instances.
033: *
034: * @author Michael Bell
035: * @version $Revision: 1.6 $
036: *
037: */
038: public class PropertyInstanceFactory {
039:
040: /**
041: * Constructs a <code>PropertyInstanceFactory</code>
042: *
043: * Note: this a <code>private</code> method so will never be called
044: */
045: private PropertyInstanceFactory() {
046: super ();
047: }
048:
049: /**
050: * Returns the appropriate property instance for the given property insatnce
051: * XML element.
052: *
053: * @param dsi the data store interface
054: * @param el the property instance XML element
055: * @param prof the <code>Profile</code> that the returned property
056: * instance will be associated with
057: * @return the appropriate property instance for the given
058: * XML element
059: * @throws DataAccessException if there is an error getting the
060: * <code>Range</code> details from the associated <code>Property</code>
061: * @throws HarmoniseFactoryException if there is an error getting the
062: * assocaited <code>Property</code> from the cache
063: * @throws ProfileException if there is an error adding the property
064: * instance to the profile
065: */
066: public static AbstractPropertyInstance getPropertyInstance(
067: AbstractDataStoreInterface dsi, Element el, Profile prof)
068: throws DataAccessException, HarmoniseFactoryException,
069: ProfileException {
070: AbstractPropertyInstance propInst = getPropertyInstance(dsi, el);
071: if (prof != null) {
072: AbstractPropertyInstance tmpPropInst = prof
073: .getPropertyInstance(propInst.getProperty());
074:
075: if (tmpPropInst != null) {
076: try {
077: tmpPropInst.populate(el, null);
078: } catch (PopulateException e) {
079: throw new DataAccessException(e);
080: }
081: propInst = tmpPropInst;
082: } else {
083: prof.addPropertyInstance(propInst);
084: }
085:
086: }
087:
088: return propInst;
089: }
090:
091: /**
092: * Returns the appropriate property instance for the given property instance
093: * XML element.
094: *
095: * @param dsi the data store interface
096: * @param el the property instance XML element
097: * @return the appropriate property instance for the given XML element
098: * @throws DataAccessException if there is an error getting the <code>Range</code> details
099: * from the associated <code>Property</code>
100: * @throws HarmoniseFactoryException if there is an error getting the assocaited
101: * <code>Property</code> from the cache
102: */
103: public static AbstractPropertyInstance getPropertyInstance(
104: AbstractDataStoreInterface dsi, Element el)
105: throws DataAccessException, HarmoniseFactoryException {
106: Element propertyElm = XMLUtils.getFirstNamedChild(el,
107: Property.TAG_PROPERTY);
108: // get the property tag
109: String sPropertyId = el.getAttribute(AbstractObject.ATTRIB_ID);
110:
111: Property prop = null;
112:
113: prop = (Property) HarmoniseObjectFactory
114: .instantiatePublishableObject(dsi, propertyElm, null);
115:
116: if (prop == null) {
117: throw new HarmoniseFactoryException(
118: "Could not find property requested");
119: }
120:
121: return getPropertyInstance(dsi, prop);
122: }
123:
124: /**
125: * Returns an appropriate concrete implementation of
126: * <code>AbstractPropertyInstance</code> for the specified
127: * <code>Property</code>.
128: *
129: * @param dsi the data store interface
130: * @param prop the <code>Property</code> for which a property
131: * instance is needed
132: * @return an appropriate concrete implementation of
133: * <code>AbstractPropertyInstance</code> for the specified
134: * <code>Property</code>
135: * @throws DataAccessException if there is an error getting
136: * the <code>Range</code> details from the associated <code>Property</code>
137: */
138: public static AbstractPropertyInstance getPropertyInstance(
139: AbstractDataStoreInterface dsi, Property prop)
140: throws DataAccessException {
141: AbstractPropertyInstance propInst = null;
142:
143: Range range = prop.getRange();
144:
145: try {
146: if (range != null) {
147: propInst = (AbstractPropertyInstance) range
148: .getPropertyInstanceClass().newInstance();
149: propInst.setDataStoreInterface(dsi);
150: propInst.setProperty(prop);
151: } else {
152: throw new DataAccessException(
153: "Unable to get Property instance with no range");
154: }
155: } catch (PopulateException e) {
156: throw new DataAccessException(e.getLocalizedMessage(), e);
157: } catch (InstantiationException e) {
158: throw new DataAccessException(e.getLocalizedMessage(), e);
159: } catch (IllegalAccessException e) {
160: throw new DataAccessException(e.getLocalizedMessage(), e);
161: } catch (ClassNotFoundException e) {
162: throw new DataAccessException(e.getLocalizedMessage(), e);
163: }
164:
165: return propInst;
166: }
167: }
|