KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > metadata > PropertyInstanceFactory


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.rm.metadata;
20
21 import org.openharmonise.commons.dsi.AbstractDataStoreInterface;
22 import org.openharmonise.commons.xml.XMLUtils;
23 import org.openharmonise.rm.*;
24 import org.openharmonise.rm.factory.*;
25 import org.openharmonise.rm.resources.AbstractObject;
26 import org.openharmonise.rm.resources.metadata.properties.Property;
27 import org.openharmonise.rm.resources.metadata.properties.ranges.Range;
28 import org.w3c.dom.Element JavaDoc;
29
30 /**
31  * A factory class providing <code>static</code> methods for returning
32  * property instances.
33  *
34  * @author Michael Bell
35  * @version $Revision: 1.6 $
36  *
37  */

38 public class PropertyInstanceFactory {
39
40     /**
41      * Constructs a <code>PropertyInstanceFactory</code>
42      *
43      * Note: this a <code>private</code> method so will never be called
44      */

45     private PropertyInstanceFactory() {
46         super();
47     }
48
49     /**
50      * Returns the appropriate property instance for the given property insatnce
51      * XML element.
52      *
53      * @param dsi the data store interface
54      * @param el the property instance XML element
55      * @param prof the <code>Profile</code> that the returned property
56      * instance will be associated with
57      * @return the appropriate property instance for the given
58      * XML element
59      * @throws DataAccessException if there is an error getting the
60      * <code>Range</code> details from the associated <code>Property</code>
61      * @throws HarmoniseFactoryException if there is an error getting the
62      * assocaited <code>Property</code> from the cache
63      * @throws ProfileException if there is an error adding the property
64      * instance to the profile
65      */

66     public static AbstractPropertyInstance getPropertyInstance(
67             AbstractDataStoreInterface dsi, Element JavaDoc el, Profile prof)
68             throws DataAccessException, HarmoniseFactoryException, ProfileException {
69         AbstractPropertyInstance propInst = getPropertyInstance(dsi, el);
70         if (prof != null) {
71             AbstractPropertyInstance tmpPropInst = prof
72                     .getPropertyInstance(propInst.getProperty());
73
74             if (tmpPropInst != null) {
75                 try {
76                     tmpPropInst.populate(el, null);
77                 } catch (PopulateException e) {
78                     throw new DataAccessException(e);
79                 }
80                 propInst = tmpPropInst;
81             } else {
82                 prof.addPropertyInstance(propInst);
83             }
84
85         }
86
87         return propInst;
88     }
89
90     /**
91      * Returns the appropriate property instance for the given property instance
92      * XML element.
93      *
94      * @param dsi the data store interface
95      * @param el the property instance XML element
96      * @return the appropriate property instance for the given XML element
97      * @throws DataAccessException if there is an error getting the <code>Range</code> details
98      * from the associated <code>Property</code>
99      * @throws HarmoniseFactoryException if there is an error getting the assocaited
100      * <code>Property</code> from the cache
101      */

102     public static AbstractPropertyInstance getPropertyInstance(
103             AbstractDataStoreInterface dsi, Element JavaDoc el)
104             throws DataAccessException, HarmoniseFactoryException {
105         Element JavaDoc propertyElm = XMLUtils.getFirstNamedChild(el,
106                 Property.TAG_PROPERTY);
107         // get the property tag
108
String JavaDoc sPropertyId = el.getAttribute(AbstractObject.ATTRIB_ID);
109
110         Property prop = null;
111
112         prop = (Property) HarmoniseObjectFactory.instantiatePublishableObject(dsi,
113                 propertyElm, null);
114
115         if (prop == null) {
116             throw new HarmoniseFactoryException("Could not find property requested");
117         }
118
119         return getPropertyInstance(dsi, prop);
120     }
121
122     /**
123      * Returns an appropriate concrete implementation of
124      * <code>AbstractPropertyInstance</code> for the specified
125      * <code>Property</code>.
126      *
127      * @param dsi the data store interface
128      * @param prop the <code>Property</code> for which a property
129      * instance is needed
130      * @return an appropriate concrete implementation of
131      * <code>AbstractPropertyInstance</code> for the specified
132      * <code>Property</code>
133      * @throws DataAccessException if there is an error getting
134      * the <code>Range</code> details from the associated <code>Property</code>
135      */

136     public static AbstractPropertyInstance getPropertyInstance(
137             AbstractDataStoreInterface dsi, Property prop)
138             throws DataAccessException {
139         AbstractPropertyInstance propInst = null;
140
141         Range range = prop.getRange();
142
143         try {
144             if(range != null) {
145                 propInst = (AbstractPropertyInstance) range
146                     .getPropertyInstanceClass().newInstance();
147                 propInst.setDataStoreInterface(dsi);
148                 propInst.setProperty(prop);
149             } else {
150                 throw new DataAccessException("Unable to get Property instance with no range");
151             }
152         } catch (PopulateException e) {
153             throw new DataAccessException(e.getLocalizedMessage(), e);
154         } catch (InstantiationException JavaDoc e) {
155             throw new DataAccessException(e.getLocalizedMessage(), e);
156         } catch (IllegalAccessException JavaDoc e) {
157             throw new DataAccessException(e.getLocalizedMessage(), e);
158         } catch (ClassNotFoundException JavaDoc e) {
159             throw new DataAccessException(e.getLocalizedMessage(), e);
160         }
161
162         return propInst;
163     }
164 }
Popular Tags