KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > rm > resources > metadata > properties > PropertyFactory


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.resources.metadata.properties;
20
21 import java.sql.*;
22 import java.util.*;
23 import java.util.logging.*;
24
25 import org.openharmonise.commons.cache.*;
26 import org.openharmonise.commons.dsi.*;
27 import org.openharmonise.commons.dsi.dml.SelectStatement;
28 import org.openharmonise.rm.DataAccessException;
29 import org.openharmonise.rm.factory.*;
30 import org.openharmonise.rm.resources.AbstractObject;
31 import org.openharmonise.rm.resources.lifecycle.*;
32
33
34 /**
35  * <code>PropertyFactory</code> provides a method of obtaining <code>Property</code>
36  * objects from a name rather than an id.
37  *
38  * @author Michael Bell
39  * @version $Revision: 1.2 $
40  *
41  */

42 public class PropertyFactory implements EditEventListener {
43
44     /**
45      * <code>Map</code> of names to <code>CachePointer</code>s to
46      * <code>Property</code> used to cache the data and reduce database queries.
47      */

48     private static Map NAME_2_PROP_MAP = new Hashtable();
49     
50     /**
51      * Logger for this class
52      */

53     private static final Logger m_logger = Logger.getLogger(PropertyFactory.class.getName());
54
55     /**
56      * Constructs a new <code>PropertyFactory</code>.
57      *
58      * Note: This constructor is <code>private</code> and can therefore not
59      * be executed
60      */

61     private PropertyFactory() {
62         super();
63     }
64     
65     /**
66      * Returns the <code>Property</code> with the specified name.
67      *
68      * @param dsi the data store inteface
69      * @param sPropName the name of the <code>Property</code> to return
70      * @return the <code>Property</code> with the specified name
71      * @throws HarmoniseFactoryException if an error occurs
72      */

73     public static Property getPropertyFromName(AbstractDataStoreInterface dsi, String JavaDoc sPropName) throws HarmoniseFactoryException {
74         Property prop = null;
75         
76         try {
77             
78             CachePointer propPtr = (CachePointer) NAME_2_PROP_MAP.get(sPropName.intern());
79             
80             if(propPtr == null) {
81                 //double checked synchronized block to avoid concurrency problems
82
synchronized (NAME_2_PROP_MAP) {
83                     propPtr = (CachePointer) NAME_2_PROP_MAP.get(sPropName.intern());
84                     
85                     if(propPtr == null) {
86                         SelectStatement select = new SelectStatement();
87                         
88                         select.addSelectColumn(AbstractObject.getColumnRef(Property.class.getName(),AbstractObject.ATTRIB_ID));
89                         select.addSelectColumn(AbstractObject.getColumnRef(Property.class.getName(),AbstractObject.ATTRIB_TYPE));
90                         select.addWhereCondition(AbstractObject.getColumnRef(Property.class.getName(),AbstractObject.TAG_NAME),"=",sPropName);
91                         
92                         ResultSet rs = dsi.execute(select);
93                         
94                         
95                         if(rs.next()) {
96                             int nId = rs.getInt(1);
97                             String JavaDoc sClass = rs.getString(2);
98                             
99                             prop = (Property) HarmoniseObjectFactory.instantiateHarmoniseObject(dsi,sClass,nId);
100                             try {
101                                 propPtr = CacheHandler.getInstance(dsi).getCachePointer(prop);
102                             } catch (CacheException e) {
103                                 throw new HarmoniseFactoryException(e);
104                             }
105                             
106                             NAME_2_PROP_MAP.put(sPropName.intern(),propPtr);
107                         }
108                     
109                         rs.close();
110                     }
111                 }
112                 
113             } else {
114                 try {
115                     prop = (Property) propPtr.getObject();
116                      
117                     if(prop.exists() == false) {
118                         prop = null;
119                         NAME_2_PROP_MAP.remove(sPropName);
120                     }
121                 } catch (CacheException e) {
122                     throw new HarmoniseFactoryException(e);
123                 }
124             }
125             
126         } catch (DataStoreException e) {
127             throw new HarmoniseFactoryException("ds error",e);
128         } catch (SQLException e) {
129             throw new HarmoniseFactoryException("sql error",e);
130         }
131
132                 
133         return prop;
134     }
135
136     /* (non-Javadoc)
137      * @see org.openharmonise.rm.resources.lifecycle.EditEventListener#workflowObjectSaved(org.openharmonise.rm.resources.lifecycle.EditEvent)
138      */

139     public void workflowObjectSaved(EditEvent event) {
140         // nothing to do
141

142     }
143
144     /* (non-Javadoc)
145      * @see org.openharmonise.rm.resources.lifecycle.EditEventListener#workflowObjectStatusChanged(org.openharmonise.rm.resources.lifecycle.EditEvent)
146      */

147     public void workflowObjectStatusChanged(EditEvent event) {
148         // nothing to do
149

150     }
151
152     /* (non-Javadoc)
153      * @see org.openharmonise.rm.resources.lifecycle.EditEventListener#workflowObjectArchived(org.openharmonise.rm.resources.lifecycle.EditEvent)
154      */

155     public void workflowObjectArchived(EditEvent event) {
156         Property prop = (Property) event.getSource();
157         String JavaDoc sName;
158         try {
159             sName = prop.getName();
160             
161         } catch (DataAccessException e) {
162             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
163         }
164         
165     }
166
167     /* (non-Javadoc)
168      * @see org.openharmonise.rm.resources.lifecycle.EditEventListener#workflowObjectReactivated(org.openharmonise.rm.resources.lifecycle.EditEvent)
169      */

170     public void workflowObjectReactivated(EditEvent event) {
171         // nothing to do
172

173     }
174
175     /* (non-Javadoc)
176      * @see org.openharmonise.rm.resources.lifecycle.EditEventListener#workflowObjectLocked(org.openharmonise.rm.resources.lifecycle.EditEvent)
177      */

178     public void workflowObjectLocked(EditEvent event) {
179         // nothing to do
180

181     }
182
183     /* (non-Javadoc)
184      * @see org.openharmonise.rm.resources.lifecycle.EditEventListener#workflowObjectUnlocked(org.openharmonise.rm.resources.lifecycle.EditEvent)
185      */

186     public void workflowObjectUnlocked(EditEvent event) {
187         // nothing to do
188

189     }
190
191 }
192
Popular Tags