001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Kernel/Sources/es/udc/mypersonalizer/kernel/model/repository/sql/plain/PlainUserRegistrationInformationAccessor.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.plain;
042:
043: import java.util.Collection;
044: import java.sql.Connection;
045:
046: import es.udc.mypersonalizer.kernel.model.repository.interfaces.UserRegistrationInformationAccessor;
047: import es.udc.mypersonalizer.kernel.model.repository.interfaces.UserRegistrationInformation;
048: import es.udc.mypersonalizer.kernel.model.repository.sql.storers.UserRegistrationInformationStorer;
049: import es.udc.mypersonalizer.kernel.util.exceptions.DuplicateInstanceException;
050: import es.udc.mypersonalizer.kernel.util.exceptions.InstanceNotFoundException;
051: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
052:
053: /**
054: * This class provides a Plain-based implementation of
055: * {@link UserRegistrationInformationAccessor}. The implementation
056: * uses the {@link TransactionManager} which returns pooled connections,
057: * and needs to rely on the storer to perform the requested operation.
058: *
059: * @author Fernando Bellas
060: * @since 1.0
061: */
062: public class PlainUserRegistrationInformationAccessor implements
063: UserRegistrationInformationAccessor {
064:
065: private UserRegistrationInformationStorer storer;
066:
067: /**
068: * Creates an instance of this accessor.
069: * @throws InternalErrorException if the instance can't be created.
070: */
071: public PlainUserRegistrationInformationAccessor()
072: throws InternalErrorException {
073:
074: storer = new UserRegistrationInformationStorer();
075: }
076:
077: public UserRegistrationInformation findUserRegistrationInformation(
078: String loginName) throws InternalErrorException,
079: InstanceNotFoundException {
080:
081: Transaction transaction = null;
082:
083: try {
084:
085: /* Get the transaction and the associated connection. */
086: transaction = TransactionManager.getInstance()
087: .getTransaction(Transaction.TRANSACTION_SUPPORTS);
088: Connection connection = transaction.getConnection();
089:
090: /* Delegate on the storer. */
091: UserRegistrationInformation userRegistrationInformation = storer
092: .findUserRegistrationInformation(connection,
093: loginName);
094:
095: /* Release transaction and return result. */
096: transaction.release();
097:
098: return userRegistrationInformation;
099:
100: } catch (InternalErrorException e) {
101: rollbackTransaction(transaction);
102: throw e;
103: } catch (InstanceNotFoundException e) {
104: transaction.release();
105: throw e;
106: } catch (RuntimeException e) {
107: rollbackTransaction(transaction);
108: throw new InternalErrorException(e);
109: }
110: }
111:
112: public Collection findUserRegistrationInformationsByRange(
113: int offset, int size) throws InternalErrorException {
114:
115: Transaction transaction = null;
116:
117: try {
118:
119: /* Get the transaction and the associated connection. */
120: transaction = TransactionManager.getInstance()
121: .getTransaction(Transaction.TRANSACTION_SUPPORTS);
122: Connection connection = transaction.getConnection();
123:
124: /* Delegate on the storer. */
125: Collection userRegistrationInformations = storer
126: .findUserRegistrationInformationsByRange(
127: connection, offset, size);
128:
129: /* Release transaction and return result. */
130: transaction.release();
131:
132: return userRegistrationInformations;
133:
134: } catch (InternalErrorException e) {
135: rollbackTransaction(transaction);
136: throw e;
137: } catch (RuntimeException e) {
138: rollbackTransaction(transaction);
139: throw new InternalErrorException(e);
140: }
141:
142: }
143:
144: public void removeUserRegistrationInformation(String loginName)
145: throws InternalErrorException, InstanceNotFoundException {
146:
147: Transaction transaction = null;
148:
149: try {
150:
151: /* Get the transaction and the associated connection. */
152: transaction = TransactionManager.getInstance()
153: .getTransaction(Transaction.TRANSACTION_REQUIRED);
154: Connection connection = transaction.getConnection();
155:
156: /* Delegate on the storer. */
157: storer.removeUserRegistrationInformation(connection,
158: loginName);
159:
160: /* Release transaction. */
161: transaction.release();
162:
163: } catch (InternalErrorException e) {
164: rollbackTransaction(transaction);
165: throw e;
166: } catch (InstanceNotFoundException e) {
167: transaction.release();
168: throw e;
169: } catch (RuntimeException e) {
170: rollbackTransaction(transaction);
171: throw new InternalErrorException(e);
172: }
173: }
174:
175: public void updateUserRegistrationInformation(
176: UserRegistrationInformation userRegistrationInformation)
177: throws InternalErrorException, InstanceNotFoundException {
178:
179: Transaction transaction = null;
180:
181: try {
182: /* Get the transaction and the associated connection. */
183: transaction = TransactionManager.getInstance()
184: .getTransaction(Transaction.TRANSACTION_REQUIRED);
185: Connection connection = transaction.getConnection();
186:
187: /* Delegate on the storer. */
188: storer.updateUserRegistrationInformation(connection,
189: userRegistrationInformation);
190:
191: /* Release transaction. */
192: transaction.release();
193:
194: } catch (InternalErrorException e) {
195: rollbackTransaction(transaction);
196: throw e;
197: } catch (InstanceNotFoundException e) {
198: transaction.release();
199: throw e;
200: } catch (RuntimeException e) {
201: rollbackTransaction(transaction);
202: throw new InternalErrorException(e);
203: }
204: }
205:
206: public void addUserRegistrationInformation(
207: UserRegistrationInformation userRegistrationInformation)
208: throws InternalErrorException, DuplicateInstanceException {
209:
210: Transaction transaction = null;
211:
212: try {
213:
214: /* Get the transaction and the associated connection. */
215: transaction = TransactionManager.getInstance()
216: .getTransaction(Transaction.TRANSACTION_REQUIRED);
217: Connection connection = transaction.getConnection();
218:
219: /* Delegate on the storer. */
220: storer.addUserRegistrationInformation(connection,
221: userRegistrationInformation);
222:
223: /* Release transaction. */
224: transaction.release();
225:
226: } catch (InternalErrorException e) {
227: rollbackTransaction(transaction);
228: throw e;
229: } catch (DuplicateInstanceException e) {
230: transaction.release();
231: throw e;
232: } catch (RuntimeException e) {
233: rollbackTransaction(transaction);
234: throw new InternalErrorException(e);
235: }
236: }
237:
238: private void rollbackTransaction(Transaction transaction)
239: throws InternalErrorException {
240:
241: if (transaction != null) {
242: transaction.rollback();
243: }
244: }
245:
246: }
|