KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jorm > facility > naming > rdbsequence > RdbSequenceHelper


1 /**
2  * Copyright (C) 2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.jorm.facility.naming.rdbsequence;
19
20 import org.objectweb.jorm.api.PException;
21 import org.objectweb.jorm.api.PExceptionIO;
22 import org.objectweb.jorm.mapper.rdb.adapter.api.RdbAdapter;
23 import org.objectweb.jorm.mapper.rdb.lib.RdbConnectionWrapper;
24 import org.objectweb.util.monolog.api.Logger;
25
26 import java.sql.Connection JavaDoc;
27 import java.sql.Statement JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30
31 /**
32  * This helper permits to create and consult a RDB sequence. It requires a
33  * RdbAdapter.
34  *
35  * @author S.Chassande-Barrioz
36  */

37 public class RdbSequenceHelper {
38
39     /**
40      * The SQL request for asking a new value
41      */

42     private String JavaDoc nextValRequest;
43
44     /**
45      * The Rdb Adapter permitting to choose the SQL syntax corresponding to
46      * the relational database.
47      */

48     private RdbAdapter adapter;
49
50     /**
51      * The sequence name
52      */

53     private String JavaDoc seqName;
54
55     /**
56      * the sequence start. A null value means that no value has been specified.
57      * In this case the default sequence is built with the default sequence
58      * start.
59      */

60     private Integer JavaDoc seqStart = null;
61
62     /**
63      * The sequence increment. A null value means that no value has been
64      * specified. In this case the default sequence is built with the default
65      * sequence increment.
66      */

67     private Integer JavaDoc seqIncrement = null;
68
69     /**
70      * The sequence cache. A null value means that no value has been
71      * specified. In this case the default sequence is built with the default
72      * sequence cache.
73      */

74     private Integer JavaDoc seqCache = null;
75
76     private boolean created;
77
78     private Logger logger;
79
80     public RdbSequenceHelper() {
81     }
82
83     /**
84      * @param adapter is the Rdb Adapter permitting to choose the SQL syntax corresponding to
85      * the relational database. (not null)
86      * @param seqName is the sequence name (not null)
87      */

88     public RdbSequenceHelper(RdbAdapter adapter,
89                              String JavaDoc seqName,
90                              boolean sequenceCreated) {
91         this();
92         setAdapter(adapter);
93         setSequenceName(seqName);
94         setSequenceCreated(sequenceCreated);
95     }
96
97     public void setSequenceCreated(boolean sequencecreated) {
98         this.created = sequencecreated;
99     }
100
101     public boolean isSequenceCreated() {
102         return created;
103     }
104
105     public void setSequenceName(String JavaDoc seqName) {
106         this.seqName = seqName;
107     }
108
109     public String JavaDoc getSequenceName() {
110         return seqName;
111     }
112
113     public RdbAdapter getAdapter() {
114         return adapter;
115     }
116
117     public void setAdapter(RdbAdapter adapter) {
118         this.adapter = adapter;
119     }
120
121     public Integer JavaDoc getSequenceStart() {
122         return seqStart;
123     }
124
125     public void setSequenceStart(Integer JavaDoc seqStart) {
126         this.seqStart = seqStart;
127     }
128
129     public Integer JavaDoc getSequenceIncrement() {
130         return seqIncrement;
131     }
132
133     public void setSequenceIncrement(Integer JavaDoc seqIncrement) {
134         this.seqIncrement = seqIncrement;
135     }
136
137     public Integer JavaDoc getSequenceCache() {
138         return seqCache;
139     }
140
141     public void setSequenceCache(Integer JavaDoc seqCache) {
142         this.seqCache = seqCache;
143     }
144
145     public Logger getLogger() {
146         return logger;
147     }
148
149     public void setLogger(Logger logger) {
150         this.logger = logger;
151     }
152
153     /**
154      * Create the sql sequence on the relational database
155      * @param c is the connection for the acces to the database
156      * @return true if the sequence has been create, false if the sequence
157      * already exists.
158      * @throws PException if an error occurs during the SQL request
159      */

160     public synchronized boolean createSequence(Object JavaDoc c) throws PException {
161         if (created) {
162             return false;
163         }
164         if (seqName == null) {
165             throw new PException("No sequence name specified");
166         }
167         if (adapter == null) {
168             throw new PException("No RdbAdapter specified");
169         }
170         Connection JavaDoc conn = RdbConnectionWrapper.narrow2SQL(c);
171         created = true;
172         Statement JavaDoc s = null;
173         try {
174             if (!adapter.existSequence(conn, seqName)) {
175                 s = conn.createStatement();
176                 s.execute(adapter.getCreateSequence(
177                     seqName, seqStart, seqIncrement, seqCache));
178                 return true;
179             } else {
180                 return false;
181             }
182         } catch (SQLException JavaDoc e) {
183             created = false;
184             throw new PExceptionIO(e, "Impossible to create the sequence '"
185                 + seqName + "'.");
186         } finally {
187             if (s != null) {
188                 try {
189                     s.close();
190                 } catch (SQLException JavaDoc e) {
191                 }
192             }
193         }
194     }
195
196     public long allocateId(Object JavaDoc c) throws PException {
197         if (nextValRequest == null) {
198             if (seqName == null) {
199                 throw new PException("No sequence name specified");
200             }
201             if (adapter == null) {
202                 throw new PException("No RdbAdapter specified");
203             }
204             if (!created) {
205                 throw new PException("Protocol Error: You have to ask the sequence creation before id allocation");
206             }
207             nextValRequest = adapter.getNextValInSequence(seqName);
208         }
209         Connection JavaDoc conn = RdbConnectionWrapper.narrow2SQL(c);
210         Statement JavaDoc s = null;
211         ResultSet JavaDoc rs = null;
212         long res = -1;
213         try {
214             s = conn.createStatement();
215             rs = s.executeQuery(nextValRequest);
216             if (rs.next()) {
217                 res = rs.getLong(1);
218             } else {
219                 throw new PExceptionIO("Strong case: The nextval operation does " +
220                     "not return any value!");
221             }
222         } catch (SQLException JavaDoc e) {
223             throw new PExceptionIO(e, "Impossible to allocate a new identifier"
224                 + " on the sequence: " + seqName);
225         } finally {
226             try {
227                 if (rs != null) {
228                     rs.close();
229                 }
230                 if (s != null) {
231                     s.close();
232                 }
233             } catch (SQLException JavaDoc e) {
234             }
235         }
236         return res;
237     }
238 }
239
Popular Tags