KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > sps > pocketpc > RapiDB


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.syncclient.sps.pocketpc;
20
21 import java.util.Hashtable JavaDoc;
22
23 import sync4j.syncclient.sps.pocketpc.RapiConstants;
24 import sync4j.syncclient.sps.common.DataAccessException;
25 import sync4j.syncclient.sps.common.RecordMetadata;
26
27 /**
28  * This class collects the native methods used to interact with WindowsMobile/
29  * PocketPC devices throught the RAPI library.
30  *
31  *
32  * @author Stefano Fornari
33  */

34 public class RapiDB implements RapiConstants{
35
36
37     // ------------------------------------------------------------ Private data
38

39     private String JavaDoc dbName;
40     private String JavaDoc volume;
41     private long dbHandle;
42     private long dbOid;
43     private byte[] volOid;
44     private int keyField;
45     private int statusField;
46     private int sortField;
47
48
49     // ------------------------------------------------------------ Constructors
50

51     /**
52      * Creates a new instance of RapiDB
53      *
54      * @param volume the volume where the database is stored - NOT NULL
55      * @param dbName the database name - NOT NULL
56      */

57     public RapiDB(String JavaDoc volume, String JavaDoc dbName) {
58         try {
59             System.load(new java.io.File JavaDoc("jrapi.dll").getAbsolutePath());
60         } catch (RuntimeException JavaDoc e) {
61             e.printStackTrace();
62             throw e;
63         }
64
65         if (volume == null) {
66             throw new NullPointerException JavaDoc("volume is null!");
67         }
68         if (dbName == null) {
69             throw new NullPointerException JavaDoc("dbName is null!");
70         }
71
72         this.volume = volume;
73         this.dbName = dbName;
74     }
75
76     /**
77      * Create a database with the name given in the constructor and with the
78      * structure defined by the given RecordMetaDAta object.
79      *
80      * @param keyField position (1 based) of the key field
81      * @param statusField position (1 based) of the status field
82      *
83      * @return true id the database has been successfully open, false otherwise
84      *
85      * @throws DataAccessException in case of error
86      */

87     public native boolean createDB(int keyField, int statusField)
88     throws DataAccessException;
89
90     /**
91      * Opens the database. Remember to close it with closeDB()
92      *
93      * @param sortField position (1 based) of the field used as sort order
94      *
95      * @return true id the database has been successfully open, false otherwise
96      *
97      * @throws DataAccessException in case of a low-level failure
98      */

99     public native boolean openDB(int keyField, int statusField, int sortField)
100     throws DataAccessException;
101
102     /**
103      * Closes a database previously opened.
104      *
105      * @throws DataAccessException in case of a low-level failure
106      */

107     public native void closeDB()
108     throws DataAccessException;
109
110     /**
111      * Deletes the database with name specified by dbName
112      *
113      * throws DataAccessException in case of low-level failure
114      */

115     public native void deleteDB()
116     throws DataAccessException;
117
118     /**
119      * Returns a record by its key
120      *
121      * @param key the record key
122      *
123      * @return the record fields as an Object[] or null if the record is not
124      * found
125      *
126      * @throws DataAccessException in case of database access or low-level failure
127      */

128     public native Object JavaDoc[] readRecord(String JavaDoc key)
129     throws DataAccessException;
130
131     /**
132      * Writes the given record fields. If the record already exists, an update
133      * is performed, otherwise an insert is performed
134      *
135      * @param key the record key
136      * @param fields record fields
137      *
138      * @throws DataAccessException in case of database access or low-level failure
139      */

140     public native Object JavaDoc[] storeRecord(Object JavaDoc key, Object JavaDoc[] fields)
141     throws DataAccessException;
142
143     /**
144      * Sets the value of a specified field of a specified record.
145      *
146      * @param key record key
147      * @param pos field 1-based field position
148      * @param value the field value
149      *
150      * @throws DataAccessException in case of database access or low-level failure
151      */

152     public native void setRecordField(Object JavaDoc key, int pos, Object JavaDoc value)
153     throws DataAccessException;
154
155     /**
156      * Deletes the given record
157      *
158      * @param key the key of record to be removed
159      *
160      * @throws DataAccessException in case of error
161      */

162     public native void deleteRecord(Object JavaDoc key)
163     throws DataAccessException;
164
165     /**
166      * Returns all records of the opened database. The
167      *
168      * @return a Object[][] containing all records of the database, where each
169      * record is represented by an array of Objects. Each record field
170      * value is encapsulated in an appropriate Java type. A zero-length
171      * array is returned if the database contains no records.
172      *
173      * @throws DataAccessException in case of database access or low-level failure
174      */

175     public native Object JavaDoc[][] getAllRecords()
176     throws DataAccessException;
177
178     /**
179      * Returns the next record in the database with the given state.
180      * The database must be opened with the modification status field as sort
181      * order.
182      *
183      * @param state the status to be used as searching key
184      *
185      * @throws DataAccessException in case of low-level error
186      */

187     public native Object JavaDoc[] getNextRecord(char state)
188     throws DataAccessException;
189 }
Popular Tags