001: /*
002: * $Header: /export/home/cvsroot/MyPersonalizerRepository/MyPersonalizer/Subsystems/Kernel/Sources/es/udc/mypersonalizer/kernel/model/repository/sql/plain/PlainServiceButtonsStateAccessor.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.sql.Connection;
044:
045: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServiceButtonsStateAccessor;
046: import es.udc.mypersonalizer.kernel.model.repository.interfaces.ServiceButtonsState;
047: import es.udc.mypersonalizer.kernel.model.repository.sql.storers.ServiceButtonsStateStorer;
048: import es.udc.mypersonalizer.kernel.util.exceptions.InstanceNotFoundException;
049: import es.udc.mypersonalizer.kernel.util.exceptions.InternalErrorException;
050: import es.udc.mypersonalizer.kernel.model.properties.Property;
051:
052: /**
053: * This class provides a Plain-based implementation of
054: * {@link ServiceButtonsStateAccessor}. The implementation uses the
055: * {@link TransactionManager} which returns pooled connections,
056: * and needs to rely on the storer to perform the requested
057: * operation.
058: *
059: * @author Fernando Bellas
060: * @since 1.0
061: */
062: public class PlainServiceButtonsStateAccessor implements
063: ServiceButtonsStateAccessor {
064:
065: private ServiceButtonsStateStorer storer;
066:
067: /**
068: * Creates an instance of this accessor.
069: *
070: * @throws InternalErrorException if a severe failure occured
071: */
072: public PlainServiceButtonsStateAccessor()
073: throws InternalErrorException {
074: storer = new ServiceButtonsStateStorer();
075: }
076:
077: public ServiceButtonsState findServiceButtonsState(
078: Long propertyIdentifier) 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: ServiceButtonsState serviceButtonsState = storer
092: .findServiceButtonsState(connection,
093: propertyIdentifier);
094:
095: /* Release transaction and return result. */
096: transaction.release();
097:
098: return serviceButtonsState;
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 void removeServiceButtonsState(Long propertyIdentifier)
113: throws InternalErrorException, InstanceNotFoundException {
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_REQUIRED);
122: Connection connection = transaction.getConnection();
123:
124: /* Delegate on the storer. */
125: storer.removeServiceButtonsState(connection,
126: propertyIdentifier);
127:
128: /* Release transaction. */
129: transaction.release();
130:
131: } catch (InternalErrorException e) {
132: rollbackTransaction(transaction);
133: throw e;
134: } catch (InstanceNotFoundException e) {
135: transaction.release();
136: throw e;
137: } catch (RuntimeException e) {
138: rollbackTransaction(transaction);
139: throw new InternalErrorException(e);
140: }
141: }
142:
143: public void updateServiceButtonsState(
144: ServiceButtonsState serviceButtonsState)
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.updateServiceButtonsState(connection,
158: serviceButtonsState);
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 ServiceButtonsState addServiceButtonsState(Property property)
176: throws InternalErrorException {
177:
178: Transaction transaction = null;
179:
180: try {
181:
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: Long propertyIdentifier = storer.addServiceButtonsState(
189: connection, property);
190: ServiceButtonsState serviceButtonsState = new ServiceButtonsState(
191: propertyIdentifier, property);
192:
193: /* Release transaction and return result. */
194: transaction.release();
195:
196: return serviceButtonsState;
197:
198: } catch (InternalErrorException e) {
199: rollbackTransaction(transaction);
200: throw e;
201: } catch (RuntimeException e) {
202: rollbackTransaction(transaction);
203: throw new InternalErrorException(e);
204: }
205: }
206:
207: private void rollbackTransaction(Transaction transaction)
208: throws InternalErrorException {
209:
210: if (transaction != null) {
211: transaction.rollback();
212: }
213: }
214:
215: }
|