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.resources.metadata.properties;
020:
021: import java.sql.*;
022: import java.util.*;
023: import java.util.logging.*;
024:
025: import org.openharmonise.commons.cache.*;
026: import org.openharmonise.commons.dsi.*;
027: import org.openharmonise.commons.dsi.dml.SelectStatement;
028: import org.openharmonise.rm.DataAccessException;
029: import org.openharmonise.rm.factory.*;
030: import org.openharmonise.rm.resources.AbstractObject;
031: import org.openharmonise.rm.resources.lifecycle.*;
032:
033: /**
034: * <code>PropertyFactory</code> provides a method of obtaining <code>Property</code>
035: * objects from a name rather than an id.
036: *
037: * @author Michael Bell
038: * @version $Revision: 1.2 $
039: *
040: */
041: public class PropertyFactory implements EditEventListener {
042:
043: /**
044: * <code>Map</code> of names to <code>CachePointer</code>s to
045: * <code>Property</code> used to cache the data and reduce database queries.
046: */
047: private static Map NAME_2_PROP_MAP = new Hashtable();
048:
049: /**
050: * Logger for this class
051: */
052: private static final Logger m_logger = Logger
053: .getLogger(PropertyFactory.class.getName());
054:
055: /**
056: * Constructs a new <code>PropertyFactory</code>.
057: *
058: * Note: This constructor is <code>private</code> and can therefore not
059: * be executed
060: */
061: private PropertyFactory() {
062: super ();
063: }
064:
065: /**
066: * Returns the <code>Property</code> with the specified name.
067: *
068: * @param dsi the data store inteface
069: * @param sPropName the name of the <code>Property</code> to return
070: * @return the <code>Property</code> with the specified name
071: * @throws HarmoniseFactoryException if an error occurs
072: */
073: public static Property getPropertyFromName(
074: AbstractDataStoreInterface dsi, String sPropName)
075: throws HarmoniseFactoryException {
076: Property prop = null;
077:
078: try {
079:
080: CachePointer propPtr = (CachePointer) NAME_2_PROP_MAP
081: .get(sPropName.intern());
082:
083: if (propPtr == null) {
084: //double checked synchronized block to avoid concurrency problems
085: synchronized (NAME_2_PROP_MAP) {
086: propPtr = (CachePointer) NAME_2_PROP_MAP
087: .get(sPropName.intern());
088:
089: if (propPtr == null) {
090: SelectStatement select = new SelectStatement();
091:
092: select.addSelectColumn(AbstractObject
093: .getColumnRef(Property.class.getName(),
094: AbstractObject.ATTRIB_ID));
095: select.addSelectColumn(AbstractObject
096: .getColumnRef(Property.class.getName(),
097: AbstractObject.ATTRIB_TYPE));
098: select.addWhereCondition(AbstractObject
099: .getColumnRef(Property.class.getName(),
100: AbstractObject.TAG_NAME), "=",
101: sPropName);
102:
103: ResultSet rs = dsi.execute(select);
104:
105: if (rs.next()) {
106: int nId = rs.getInt(1);
107: String sClass = rs.getString(2);
108:
109: prop = (Property) HarmoniseObjectFactory
110: .instantiateHarmoniseObject(dsi,
111: sClass, nId);
112: try {
113: propPtr = CacheHandler.getInstance(dsi)
114: .getCachePointer(prop);
115: } catch (CacheException e) {
116: throw new HarmoniseFactoryException(e);
117: }
118:
119: NAME_2_PROP_MAP.put(sPropName.intern(),
120: propPtr);
121: }
122:
123: rs.close();
124: }
125: }
126:
127: } else {
128: try {
129: prop = (Property) propPtr.getObject();
130:
131: if (prop.exists() == false) {
132: prop = null;
133: NAME_2_PROP_MAP.remove(sPropName);
134: }
135: } catch (CacheException e) {
136: throw new HarmoniseFactoryException(e);
137: }
138: }
139:
140: } catch (DataStoreException e) {
141: throw new HarmoniseFactoryException("ds error", e);
142: } catch (SQLException e) {
143: throw new HarmoniseFactoryException("sql error", e);
144: }
145:
146: return prop;
147: }
148:
149: /* (non-Javadoc)
150: * @see org.openharmonise.rm.resources.lifecycle.EditEventListener#workflowObjectSaved(org.openharmonise.rm.resources.lifecycle.EditEvent)
151: */
152: public void workflowObjectSaved(EditEvent event) {
153: // nothing to do
154:
155: }
156:
157: /* (non-Javadoc)
158: * @see org.openharmonise.rm.resources.lifecycle.EditEventListener#workflowObjectStatusChanged(org.openharmonise.rm.resources.lifecycle.EditEvent)
159: */
160: public void workflowObjectStatusChanged(EditEvent event) {
161: // nothing to do
162:
163: }
164:
165: /* (non-Javadoc)
166: * @see org.openharmonise.rm.resources.lifecycle.EditEventListener#workflowObjectArchived(org.openharmonise.rm.resources.lifecycle.EditEvent)
167: */
168: public void workflowObjectArchived(EditEvent event) {
169: Property prop = (Property) event.getSource();
170: String sName;
171: try {
172: sName = prop.getName();
173:
174: } catch (DataAccessException e) {
175: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
176: }
177:
178: }
179:
180: /* (non-Javadoc)
181: * @see org.openharmonise.rm.resources.lifecycle.EditEventListener#workflowObjectReactivated(org.openharmonise.rm.resources.lifecycle.EditEvent)
182: */
183: public void workflowObjectReactivated(EditEvent event) {
184: // nothing to do
185:
186: }
187:
188: /* (non-Javadoc)
189: * @see org.openharmonise.rm.resources.lifecycle.EditEventListener#workflowObjectLocked(org.openharmonise.rm.resources.lifecycle.EditEvent)
190: */
191: public void workflowObjectLocked(EditEvent event) {
192: // nothing to do
193:
194: }
195:
196: /* (non-Javadoc)
197: * @see org.openharmonise.rm.resources.lifecycle.EditEventListener#workflowObjectUnlocked(org.openharmonise.rm.resources.lifecycle.EditEvent)
198: */
199: public void workflowObjectUnlocked(EditEvent event) {
200: // nothing to do
201:
202: }
203:
204: }
|