001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Kernel/Sources/es/udc/mypersonalizer/kernel/model/repository/sql/storers/ServicePropertyStorer.java,v 1.1.1.1 2004/03/25 12:08:36 fbellas Exp $
003: * $Revision: 1.1.1.1 $
004: * $Date: 2004/03/25 12:08:36 $
005: *
006: * =============================================================================
007: *
008: * Copyright (c) 2003, The MyPersonalizer Development Group
009: * (http://www.tic.udc.es/~fbellas/mypersonalizer/index.html) at
010: * University Of A Coruna
011: * All rights reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions are met:
015: *
016: * - Redistributions of source code must retain the above copyright notice,
017: * this list of conditions and the following disclaimer.
018: *
019: * - Redistributions in binary form must reproduce the above copyright notice,
020: * this list of conditions and the following disclaimer in the documentation
021: * and/or other materials provided with the distribution.
022: *
023: * - Neither the name of the University Of A Coruna nor the names of its
024: * contributors may be used to endorse or promote products derived from
025: * this software without specific prior written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
028: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
029: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
030: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
031: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
032: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
033: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
034: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
035: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
036: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
037: * POSSIBILITY OF SUCH DAMAGE.
038: *
039: */
040:
041: package es.udc.mypersonalizer.kernel.model.repository.sql.storers;
042:
043: import java.util.Map;
044: import java.util.HashMap;
045: import java.sql.Connection;
046:
047: import es.udc.mypersonalizer.kernel.log.Log;
048: import es.udc.mypersonalizer.kernel.log.LogManager;
049: import es.udc.mypersonalizer.kernel.log.LogNamingConventions;
050: import es.udc.mypersonalizer.kernel.model.properties.Property;
051: import es.udc.mypersonalizer.kernel.model.repository.sql.config.DatabaseConventionsConfig;
052: import es.udc.mypersonalizer.kernel.model.repository.sql.config.DatabaseConventionsConfigManager;
053: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServiceProperty;
054: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServicePropertyKey;
055: import es.udc.mypersonalizer.kernel.util.exceptions.DuplicateInstanceException;
056: import es.udc.mypersonalizer.kernel.util.exceptions.InstanceNotFoundException;
057: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
058: import es.udc.mypersonalizer.kernel.model.annotators.sql.SQLPersistenceTypeAnnotationHelper;
059: import es.udc.mypersonalizer.kernel.model.metainfo.MetaService;
060: import es.udc.mypersonalizer.kernel.model.metainfo.MetaServiceRegistrySingleton;
061:
062: /**
063: * This "storer" class allows to access and modify
064: * <code>ServiceProperty</code>s on the database. This class does not assume
065: * any policy on how to store and retrieve the data. This knowledge is
066: * encapsulated in classes that implement the interface
067: * <code>PropertyStorageStrategy</code>. This "storer" class will delegate on
068: * a strategy in order to store or retrieve service properties on the
069: * database. Example: a strategy class that stores service properties by using
070: * serialization.
071: *
072: * @author Fernando Bellas
073: * @author Abel Muinho
074: * @since 1.0
075: */
076: public class ServicePropertyStorer {
077:
078: /** The name of the column for the single key field. */
079: private static final String PROPERTY_IDENTIFIER_COLUMN_NAME;
080:
081: static {
082: String propertyIdentifierColumnName = null;
083: try {
084: DatabaseConventionsConfig config = DatabaseConventionsConfigManager
085: .getConfig();
086: propertyIdentifierColumnName = config
087: .getPropertyIdentifierColumn();
088: } catch (Exception e) {
089: Log mypersonalizerLog = LogManager
090: .getLog(LogNamingConventions.MYPERSONALIZER);
091: mypersonalizerLog.write(
092: "Could not initialize configuration for "
093: + "ServicePropertyStorer", e,
094: ServicePropertyStorer.class);
095: }
096: PROPERTY_IDENTIFIER_COLUMN_NAME = propertyIdentifierColumnName;
097: }
098:
099: /**
100: * Constructs an instance of this class.
101: */
102: public ServicePropertyStorer() {
103: }
104:
105: /**
106: * Finds a service property.
107: *
108: * @param connection the connection to the database
109: * @param key the service property key
110: * @return the service property
111: * @throws InternalErrorException if a failure is detected.
112: * @throws InstanceNotFoundException if it was unable to find the service property
113: */
114: public ServiceProperty findServiceProperty(Connection connection,
115: ServicePropertyKey key) throws InternalErrorException,
116: InstanceNotFoundException {
117:
118: PropertyStorageStrategy strategy = getPropertyStorageStrategy(key
119: .getServiceIdentifier());
120:
121: Property property = strategy.findProperty(connection,
122: getStrategyKey(key.getPropertyIdentifier()));
123:
124: return new ServiceProperty(key, property);
125: }
126:
127: /**
128: * Removes a service property.
129: *
130: * @param connection the connection to the database
131: * @param key the service property key
132: * @throws InternalErrorException if a failure is detected.
133: * @throws InstanceNotFoundException if it was unable to find the service property
134: * to be removed
135: */
136: public void removeServiceProperty(Connection connection,
137: ServicePropertyKey key) throws InternalErrorException,
138: InstanceNotFoundException {
139:
140: PropertyStorageStrategy strategy = getPropertyStorageStrategy(key
141: .getServiceIdentifier());
142:
143: strategy.removeProperty(connection, getStrategyKey(key
144: .getPropertyIdentifier()));
145: }
146:
147: /**
148: * Updates a service property.
149: *
150: * @param connection the connection to the database
151: * @param serviceProperty the new service property
152: * @throws InternalErrorException if a failure is detected.
153: * @throws InstanceNotFoundException if it was unable to find the service property
154: * to be updated
155: */
156: public void updateServiceProperty(Connection connection,
157: ServiceProperty serviceProperty)
158: throws InternalErrorException, InstanceNotFoundException {
159:
160: ServicePropertyKey key = serviceProperty.getKey();
161: PropertyStorageStrategy strategy = getPropertyStorageStrategy(key
162: .getServiceIdentifier());
163:
164: strategy.updateProperty(connection, getStrategyKey(key
165: .getPropertyIdentifier()), serviceProperty
166: .getProperty());
167: }
168:
169: /**
170: * Adds a new service property.
171: *
172: * @param connection the connection to the database
173: * @param serviceIdentifier the service identifier
174: * @param property the property that represents the service properties.
175: * @return the property identifier of the service property added
176: * @throws InternalErrorException if a failure is detected.
177: */
178: public Long addServiceProperty(Connection connection,
179: String serviceIdentifier, Property property)
180: throws InternalErrorException {
181:
182: /* Generate an identifier. */
183: String tableName = getTableName(serviceIdentifier);
184: Long nextIdentifier = EntityIdentifierGeneratorSingleton
185: .getInstance().nextIdentifier(connection, tableName);
186:
187: Map strategyKey = getStrategyKey(nextIdentifier);
188:
189: /* Add the service property to the database. */
190: try {
191: PropertyStorageStrategy strategy = getPropertyStorageStrategy(serviceIdentifier);
192: strategy.addProperty(connection, strategyKey, property);
193: } catch (DuplicateInstanceException e) {
194: throw new InternalErrorException(e);
195: }
196:
197: /* Return identifier. */
198: return nextIdentifier;
199:
200: }
201:
202: /**
203: * Constructs a <code>Map</code> with the single entry
204: * (<code>String, Long</code>), where the <code>String</code> is
205: * the property identifier column name and the <code>Long</code> is the
206: * property identifier passed as a parameter.
207: *
208: * @param propertyIdentifier the property identifier
209: * @return the <code>Map</code> containing the key field and the
210: * corresponding value
211: */
212: private Map getStrategyKey(Long propertyIdentifier) {
213: Map map = new HashMap();
214: map.put(PROPERTY_IDENTIFIER_COLUMN_NAME, propertyIdentifier);
215: return map;
216: }
217:
218: /**
219: * Returns the appropriate <code>PropertyStorageStrategy</code> for
220: * a given service identifier.
221: *
222: * @param serviceIdentifier the service identifier
223: * @return the appropriate property storage strategy for the service
224: * identifier
225: * @throws InternalErrorException if it was unable to find the property
226: * storage strategy
227: */
228: private PropertyStorageStrategy getPropertyStorageStrategy(
229: String serviceIdentifier) throws InternalErrorException {
230:
231: PropertyStorageStrategy strategy = PropertyStorageStrategyRegistrySingleton
232: .getInstance().getPropertyStorageStrategy(
233: serviceIdentifier);
234:
235: if (strategy == null) {
236: throw new InternalErrorException(
237: "FALTAL ERROR: PropertyStorageStrategy not found "
238: + "for service identifier: "
239: + serviceIdentifier);
240: }
241:
242: return strategy;
243: }
244:
245: /**
246: * Returns the table name corresponding to a given service identifier.
247: *
248: * @param serviceIdentifier a service identifier
249: * @return the table name corresponding to the service identifier
250: * @throws InternalErrorException if it was unable to retrieve the meta
251: * service of such service.
252: */
253: private String getTableName(String serviceIdentifier)
254: throws InternalErrorException {
255:
256: MetaService metaService = MetaServiceRegistrySingleton
257: .getInstance().getMetaService(serviceIdentifier);
258:
259: if (metaService == null) {
260: throw new InternalErrorException(
261: "FALTAL ERROR: MetaService "
262: + "not found for service identifier: "
263: + serviceIdentifier);
264: }
265: return SQLPersistenceTypeAnnotationHelper
266: .getTableNameAnnotation(metaService);
267: }
268:
269: }
|