001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Kernel/Sources/es/udc/mypersonalizer/kernel/model/repository/sql/storers/ServiceButtonsStateStorer.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.model.annotators.sql.SQLPersistenceTypeAnnotationHelper;
048: import es.udc.mypersonalizer.kernel.model.metainfo.MetaService;
049: import es.udc.mypersonalizer.kernel.model.metainfo.MetaServiceRegistrySingleton;
050: import es.udc.mypersonalizer.kernel.model.properties.Property;
051: import es.udc.mypersonalizer.kernel.model.repository.sql.config.DatabaseConventionsConfigManager;
052: import es.udc.mypersonalizer.kernel.conventions.ServiceConventions;
053: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServiceButtonsState;
054: import es.udc.mypersonalizer.kernel.util.exceptions.DuplicateInstanceException;
055: import es.udc.mypersonalizer.kernel.util.exceptions.InstanceNotFoundException;
056: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
057:
058: /**
059: * This "storer" class stores and removes <code>ServiceButtonsState</code>s
060: * on a database. This class does not assume any policy on how to store and
061: * retrieve the data. This knowledge is encapsulated in classes that implement
062: * the interface <code>PropertyStorageStrategy</code>. This "storer" class
063: * will delegate on the strategy class in order to store or retrieve a
064: * service buttons state on the database. Example: a strategy class that
065: * stores service buttons states by using serialization. This storate strategy
066: * is obtained from a registry on initializing time.
067: *
068: * @author Abel Muinho
069: * @author Fernando Bellas
070: * @since 1.0
071: */
072: public class ServiceButtonsStateStorer {
073:
074: /** Constant for the table name. */
075: private static String TABLE_NAME = null;
076:
077: /** Constant for the key field name. */
078: private static String propertyIdentifierColumnName;
079:
080: /** The class that knows how to store and retrieve properties. */
081: private static PropertyStorageStrategy strategy = null;
082:
083: /**
084: * Creates an instance of this class.
085: *
086: * @throws InternalErrorException if an essential internal element is
087: * missing.
088: */
089: public ServiceButtonsStateStorer() throws InternalErrorException {
090: MetaService metaService = MetaServiceRegistrySingleton
091: .getInstance()
092: .getMetaService(
093: ServiceConventions.SERVICE_BUTTONS_STATE_SERVICE_IDENTIFIER);
094:
095: if (metaService == null) {
096: throw new InternalErrorException(
097: "FATAL ERROR: MetaService not found for service identifier: "
098: + ServiceConventions.SERVICE_BUTTONS_STATE_SERVICE_IDENTIFIER);
099: }
100:
101: TABLE_NAME = SQLPersistenceTypeAnnotationHelper
102: .getTableNameAnnotation(metaService);
103: propertyIdentifierColumnName = DatabaseConventionsConfigManager
104: .getConfig().getPropertyIdentifierColumn();
105:
106: strategy = PropertyStorageStrategyRegistrySingleton
107: .getInstance()
108: .getPropertyStorageStrategy(
109: ServiceConventions.SERVICE_BUTTONS_STATE_SERVICE_IDENTIFIER);
110:
111: if (strategy == null) {
112: throw new InternalErrorException(
113: "FATAL ERROR: PropertyStorageStrategy "
114: + "not found for service identifier: "
115: + ServiceConventions.SERVICE_BUTTONS_STATE_SERVICE_IDENTIFIER);
116: }
117: }
118:
119: /**
120: * Finds a service buttons state.
121: *
122: * @param connection the connection to the database
123: * @param propertyIdentifier the service buttons state property identifier
124: * @return the service buttons state
125: * @throws InternalErrorException if a failure is detected.
126: * @throws InstanceNotFoundException if it was unable the find the service buttons
127: * state
128: */
129: public ServiceButtonsState findServiceButtonsState(
130: Connection connection, Long propertyIdentifier)
131: throws InternalErrorException, InstanceNotFoundException {
132:
133: Property property = strategy.findProperty(connection,
134: getStrategyKey(propertyIdentifier));
135:
136: return new ServiceButtonsState(propertyIdentifier, property);
137: }
138:
139: /**
140: * Removes a service buttons state.
141: *
142: * @param connection the connection to the database
143: * @param propertyIdentifier the service buttons state property identifier
144: * @throws InternalErrorException if a failure is detected.
145: * @throws InstanceNotFoundException if it was unable to find the service buttons
146: * state to be removed
147: */
148: public void removeServiceButtonsState(Connection connection,
149: Long propertyIdentifier) throws InternalErrorException,
150: InstanceNotFoundException {
151:
152: strategy.removeProperty(connection,
153: getStrategyKey(propertyIdentifier));
154: }
155:
156: /**
157: * Updates a service buttons state.
158: *
159: * @param connection the connection to the database
160: * @param serviceButtonsState the new service buttons state
161: * @throws InternalErrorException if a failure is detected.
162: * @throws InstanceNotFoundException if it was unable to find the service buttons
163: * state to be updated
164: */
165: public void updateServiceButtonsState(Connection connection,
166: ServiceButtonsState serviceButtonsState)
167: throws InternalErrorException, InstanceNotFoundException {
168:
169: Map strategyKey = getStrategyKey(serviceButtonsState
170: .getPropertyIdentifier());
171: strategy.updateProperty(connection, strategyKey,
172: serviceButtonsState.getProperty());
173: }
174:
175: /**
176: * Adds a new service buttons state.
177: *
178: * @param connection the connection to the database
179: * @param property the property that represents the new service buttons
180: * state
181: * @return the property identifier for the service buttons state added
182: * @throws InternalErrorException if a failure is detected.
183: */
184: public Long addServiceButtonsState(Connection connection,
185: Property property) throws InternalErrorException {
186:
187: Long nextIdentifier = EntityIdentifierGeneratorSingleton
188: .getInstance().nextIdentifier(connection, TABLE_NAME);
189: Map strategyKey = getStrategyKey(nextIdentifier);
190:
191: try {
192: strategy.addProperty(connection, strategyKey, property);
193:
194: } catch (DuplicateInstanceException exception) {
195: throw new InternalErrorException(
196: "FATAL ERROR: generated identifier is duplicated: "
197: + nextIdentifier
198: + " for service: "
199: + ServiceConventions.SERVICE_BUTTONS_STATE_SERVICE_IDENTIFIER);
200: }
201:
202: return nextIdentifier;
203: }
204:
205: /**
206: * Constructs a <code>Map</code> with a single row that contains a pair
207: * of (<code>String, Long</code>), used as the key for inserting or
208: * updating a property delegating on the strategy class passed.
209: *
210: * @param propertyIdentifier the property identifier
211: * @return the <code>Map</code> containing the key field and the
212: * corresponding value
213: */
214: private Map getStrategyKey(Long propertyIdentifier) {
215: Map map = new HashMap();
216: map.put(propertyIdentifierColumnName, propertyIdentifier);
217: return map;
218: }
219:
220: }
|