KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > server > store > UtilsPersistentStore


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.server.store;
20
21 import java.util.ArrayList JavaDoc;
22 import java.sql.*;
23 import javax.sql.DataSource JavaDoc;
24
25 import sync4j.framework.tools.DBTools;
26 import sync4j.framework.server.store.*;
27
28 /**
29  * This class is base class for SQL storage. Each extension of this
30  * class MUST respect the usage of the first 2 queries are for the
31  * counter.
32  *
33  * @version $Id: UtilsPersistentStore.java,v 1.2 2005/05/11 15:39:38 stefano_fornari Exp $
34  *
35  */

36 public class UtilsPersistentStore {
37
38     // --------------------------------------------------------------- Constants
39

40     public static final int SQL_GET_COUNTER = 0;
41     public static final int SQL_UPDATE_COUNTER = 1;
42
43     // -------------------------------------------------------------- Properties
44

45     protected String JavaDoc idSpace = null;
46
47     public void setIdSpace(String JavaDoc idSpace) {
48         this.idSpace = idSpace;
49     }
50
51     public String JavaDoc getIdSpace() {
52         return this.idSpace;
53     }
54
55     protected String JavaDoc[] sql = null;
56
57     public void setSql(String JavaDoc[] sql) {
58         this.sql = sql;
59     }
60
61     public String JavaDoc[] getSql() {
62         return this.sql;
63     }
64
65     // ------------------------------------------------------------ Private data
66
// ------------------------------------------------------------ Constructors
67
// ---------------------------------------------------------- Public methods
68

69     /**
70      * Read the counter of the specific id in order to use it like primary key
71      * in case of inserting a new record into data store.
72      *
73      * @throws PersistentException in case of error reading the data store
74      */

75     protected int readCounter(Connection conn) throws PersistentStoreException {
76         PreparedStatement stmt = null;
77         ResultSet rs = null;
78         int counter = 0;
79
80         try {
81             stmt = conn.prepareStatement(sql[SQL_GET_COUNTER]);
82             rs = stmt.executeQuery();
83
84             if (rs.next() == false) {
85                 throw new NotFoundException("Counter not found for "
86                 + idSpace);
87             }
88             counter = rs.getInt(1) + 1;
89
90             rs.close(); rs = null;
91             stmt.close(); stmt = null;
92             
93             stmt = conn.prepareStatement(sql[SQL_UPDATE_COUNTER]);
94             stmt.setInt(1, counter);
95             stmt.executeUpdate();
96             
97         } catch (SQLException e) {
98             e.printStackTrace();
99             throw new PersistentStoreException("Error reading the counter " + counter, e);
100         } finally {
101             DBTools.close(null, stmt, rs);
102         }
103         return counter;
104     }
105 }
106
107
Popular Tags