001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Kernel/Sources/es/udc/mypersonalizer/kernel/model/repository/sql/storers/UserRegistrationInformationStorer.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.List;
045: import java.util.Iterator;
046: import java.util.Collection;
047: import java.util.Collections;
048: import java.util.HashMap;
049: import java.util.ArrayList;
050: import java.sql.Connection;
051:
052: import es.udc.mypersonalizer.kernel.log.Log;
053: import es.udc.mypersonalizer.kernel.log.LogManager;
054: import es.udc.mypersonalizer.kernel.log.LogNamingConventions;
055: import es.udc.mypersonalizer.kernel.model.properties.Property;
056: import es.udc.mypersonalizer.kernel.model.annotators.sql.SQLPersistenceTypeAnnotationHelper;
057: import es.udc.mypersonalizer.kernel.model.metainfo.MetaService;
058: import es.udc.mypersonalizer.kernel.model.metainfo.MetaServiceRegistrySingleton;
059: import es.udc.mypersonalizer.kernel.model.repository.sql.config.DatabaseConventionsConfig;
060: import es.udc.mypersonalizer.kernel.model.repository.sql.config.DatabaseConventionsConfigManager;
061: import es.udc.mypersonalizer.kernel.conventions.ServiceConventions;
062: import es.udc.mypersonalizer.kernel.model.repository.interfaces.UserRegistrationInformation;
063: import es.udc.mypersonalizer.kernel.util.exceptions.DuplicateInstanceException;
064: import es.udc.mypersonalizer.kernel.util.exceptions.InstanceNotFoundException;
065: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
066:
067: /**
068: * This "storer" class stores and removes
069: * <code>UserRegistrationInformation</code>s on a database. This class
070: * does not assume any policy on how to store and retrieve the data. This
071: * knowledge is encapsulated in classes that implement the interface
072: * <code>PropertyStorageStrategy</code>. This "storer" class will delegate
073: * on the strategy class in order to store or retrieve user registration
074: * information as a property on the database. Example: a strategy class
075: * that stores properties by using serialization.
076: *
077: * @author Fernando Bellas
078: * @author Abel Muinho
079: * @since 1.0
080: */
081: public class UserRegistrationInformationStorer {
082:
083: /** Constant for the table name. */
084: private static String tableName = null;
085:
086: /** Constant for the service identifier field */
087: private static final String LOGIN_COLUMN_NAME;
088:
089: static {
090: String loginColumnName = null;
091: try {
092: DatabaseConventionsConfig config = DatabaseConventionsConfigManager
093: .getConfig();
094: loginColumnName = config.getLoginColumn();
095: } catch (Exception e) {
096: Log mypersonalizerLog = LogManager
097: .getLog(LogNamingConventions.MYPERSONALIZER);
098: mypersonalizerLog.write(
099: "Could not initialize configuration for "
100: + "UserRegistrationInformationStorer", e,
101: UserRegistrationInformationStorer.class);
102: }
103: LOGIN_COLUMN_NAME = loginColumnName;
104: }
105:
106: /** The class that knows how to store and retrieve properties. */
107: private static PropertyStorageStrategy strategy = null;
108:
109: /**
110: * Creates an instance of this class.
111: *
112: * @throws InternalErrorException if it was unable to create an instance of
113: * this class
114: */
115: public UserRegistrationInformationStorer()
116: throws InternalErrorException {
117: MetaService metaService = MetaServiceRegistrySingleton
118: .getInstance()
119: .getMetaService(
120: ServiceConventions.USER_REGISTRATION_INFORMATION_SERVICE_IDENTIFIER);
121:
122: if (metaService == null) {
123: throw new InternalErrorException(
124: "FATAL ERROR: MetaService not found "
125: + "for service identifier: "
126: + ServiceConventions.USER_REGISTRATION_INFORMATION_SERVICE_IDENTIFIER);
127: }
128:
129: tableName = SQLPersistenceTypeAnnotationHelper
130: .getTableNameAnnotation(metaService);
131:
132: strategy = PropertyStorageStrategyRegistrySingleton
133: .getInstance()
134: .getPropertyStorageStrategy(
135: ServiceConventions.USER_REGISTRATION_INFORMATION_SERVICE_IDENTIFIER);
136:
137: if (strategy == null) {
138: throw new InternalErrorException(
139: "FATAL ERROR: PropertyStorageStrategy "
140: + "not found for service identifier: "
141: + ServiceConventions.USER_REGISTRATION_INFORMATION_SERVICE_IDENTIFIER);
142: }
143: }
144:
145: /**
146: * Finds a user registration information.
147: *
148: * @param connection the connection to the database
149: * @param loginName the login name of the user registration information
150: * @return the user registration information
151: * @throws InternalErrorException if a failure is detected.
152: * @throws InstanceNotFoundException if it was unable to find the user registration
153: * information
154: */
155: public UserRegistrationInformation findUserRegistrationInformation(
156: Connection connection, String loginName)
157: throws InternalErrorException, InstanceNotFoundException {
158:
159: Property property = strategy.findProperty(connection,
160: getStrategyKey(loginName));
161:
162: return new UserRegistrationInformation(loginName, property);
163: }
164:
165: /**
166: * Finds user registration informations by range.
167: *
168: * @param connection the connection to the database
169: * @param offset the number of initial user registration informations to
170: * be skipped
171: * @param size the number of user registration informations to be fetched
172: * @throws InternalErrorException if a failure is detected.
173: * @return the user registration informations ordered by login name. If
174: * the collection size is less than the parameter "size" that
175: * means that there are no more user registration informations to
176: * read
177: */
178: public Collection findUserRegistrationInformationsByRange(
179: Connection connection, int offset, int size)
180: throws InternalErrorException {
181:
182: List keyFields = new ArrayList();
183: keyFields.add(LOGIN_COLUMN_NAME);
184:
185: Map properties = strategy.findPropertiesByRange(connection,
186: keyFields, offset, size);
187:
188: List userRegistrationInformations = new ArrayList();
189: Iterator keysIterator = properties.keySet().iterator();
190: while (keysIterator.hasNext()) {
191: Map keys = (Map) keysIterator.next();
192: String loginName = (String) keys.get(LOGIN_COLUMN_NAME);
193: Property property = (Property) properties.get(keys);
194:
195: userRegistrationInformations
196: .add(new UserRegistrationInformation(loginName,
197: property));
198: }
199:
200: Collections.sort(userRegistrationInformations);
201:
202: return userRegistrationInformations;
203: }
204:
205: /**
206: * Removes a user registration information.
207: *
208: * @param connection the connection to the database
209: * @param loginName the login of the user registration information
210: * @throws InternalErrorException if a failure is detected.
211: * @throws InstanceNotFoundException if it was unable to find the user registration
212: * information to be removed
213: */
214: public void removeUserRegistrationInformation(
215: Connection connection, String loginName)
216: throws InternalErrorException, InstanceNotFoundException {
217:
218: strategy.removeProperty(connection, getStrategyKey(loginName));
219: }
220:
221: /**
222: * Updates a user registration information.
223: *
224: * @param connection the connection to the database
225: * @param userRegistrationInformation the new user registration
226: * information
227: * @throws InternalErrorException if a failure is detected.
228: * @throws InstanceNotFoundException if it was unable to find the user registration
229: * information to be updated
230: */
231: public void updateUserRegistrationInformation(
232: Connection connection,
233: UserRegistrationInformation userRegistrationInformation)
234: throws InternalErrorException, InstanceNotFoundException {
235:
236: Map strategyKey = getStrategyKey(userRegistrationInformation
237: .getLoginName());
238:
239: strategy.updateProperty(connection, strategyKey,
240: userRegistrationInformation.getProperty());
241: }
242:
243: /**
244: * Adds a user registration information.
245: *
246: * @param connection the connection to the database
247: * @param userRegistrationInformation the new user registration
248: * information to be added
249: * @throws InternalErrorException if a failure is detected.
250: * @throws DuplicateInstanceException if the user registration information
251: * already exists
252: */
253: public void addUserRegistrationInformation(Connection connection,
254: UserRegistrationInformation userRegistrationInformation)
255: throws InternalErrorException, DuplicateInstanceException {
256:
257: String loginName = userRegistrationInformation.getLoginName();
258: Map strategyKey = getStrategyKey(loginName);
259:
260: strategy.addProperty(connection, strategyKey,
261: userRegistrationInformation.getProperty());
262:
263: }
264:
265: /**
266: * Constructs a <code>Map</code> with a single row that contains a pair
267: * like (<code>String, String</code>), used as the key for inserting or
268: * updating a service configuration delegating on the passed strategy..
269: *
270: * @param loginName the login name
271: * @return the <code>Map</code> containing the key field and the
272: * corresponding value
273: */
274: private Map getStrategyKey(String loginName) {
275: Map map = new HashMap();
276: map.put(LOGIN_COLUMN_NAME, loginName);
277: return map;
278: }
279:
280: }
|