/
DatabaseWrapper.java
242 lines (210 loc) · 6.77 KB
/
DatabaseWrapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package com.iraycd.testcouchdblite.models;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import com.couchbase.lite.*;
import com.couchbase.lite.android.AndroidContext;
import com.couchbase.lite.replicator.Replication;
import com.iraycd.testcouchdblite.utils.ErrorChecker;
import android.content.Context;
import com.iraycd.testcouchdblite.R;
/**
* Created by iraycd on 13/12/14.
*/
public class DatabaseWrapper {
private Database database;
private android.content.Context ctx;
private Manager manager;
// keep a reference to a running replication to avoid GC
private Replication replica;
/**
* Ctor
* @param dbname
* @param context
*/
public DatabaseWrapper(String dbname, Context context){
this.ctx = context;
/* Manages access to databases */
try {
manager = new Manager( new AndroidContext(ctx), Manager.DEFAULT_OPTIONS );
}
catch (IOException e) {
ErrorChecker.ShowException(ctx, R.string.err_create_manager, e );
return;
}
// create a name for the database and make sure the name is legal
// Only the following characters are valid:
// abcdefghijklmnopqrstuvwxyz0123456789_$()+-/
if ( ! Manager.isValidDatabaseName(dbname)) {
ErrorChecker.showError(ctx, R.string.err_db_name);
return;
}
// get existing db with that name
// or create a new one if it doesn't exist
try {
database = manager.getDatabase(dbname);
}
catch (CouchbaseLiteException e) {
ErrorChecker.ShowException(ctx, R.string.err_no_db, e );
return;
}
}
/** Release all resources and close all Databases. */
public void close(){
if(manager != null){
manager.close();
}
}
/**
* A replica can be active/stopped/off-line/idle
* @param rep
* @return
*/
public boolean isReplicaActive( Replication rep){
return rep != null && (rep.getStatus() ==
Replication.ReplicationStatus.REPLICATION_ACTIVE);
}
/* Attachments *********************************/
/**
* Write an Attachment for a given Document
* @param docId
* @param attachName
* @param mimeType e.g. "image/jpeg"
* @param in
*/
public void writeAttachment(String docId, String attachName,
String mimeType, InputStream in){
try {
Document doc = database.getDocument(docId);
UnsavedRevision newRev = doc.getCurrentRevision().createRevision();
newRev.setAttachment(attachName, mimeType, in);
newRev.save();
}
catch (CouchbaseLiteException e) {
ErrorChecker.ShowException(ctx, R.string.err_write_attach, e);
}
}
/**
* Get a given Document's attachment if any
* @param docId
* @param attachName
* @return Attachment
*/
public Attachment getAttachment(String docId, String attachName){
Document doc = database.getDocument(docId);
Revision rev = doc.getCurrentRevision();
return rev.getAttachment(attachName);
}
/**
* Remove an Attachment from a Document
* @param docId
* @param attachName
*/
public void deleteAttachment(String docId, String attachName){
try {
Document doc = database.getDocument(docId);
UnsavedRevision newRev = doc.getCurrentRevision().createRevision();
newRev.removeAttachment(attachName);
// (You could also update newRev.properties while you're here)
newRev.save();
}
catch (CouchbaseLiteException e) {
ErrorChecker.ShowException(ctx, R.string.err_delete_attach, e );
}
}
/* CRUD Operations *********************************/
/**
* C-rud
* @param docContent
* @return docId
*/
public String create( Map<String, Object> docContent ){
if( ! ErrorChecker.checkDb(ctx, database)){
return "";
}
// create an empty document
Document doc = database.createDocument();
// add content to document and write the document to the database
try {
doc.putProperties(docContent);
}
catch (CouchbaseLiteException e) {
ErrorChecker.ShowException(ctx, R.string.err_db_write, e ) ;
return "";
}
return doc.getId();
}
/**
* c-R-ud
* @param docId
* @return Doc content
*/
public Map<String, Object> retrieve(String docId){
if( ! ErrorChecker.checkDb(ctx, database)){
return new HashMap<String, Object>();//empty
}
// retrieve the document from the database
Document doc = database.getDocument(docId);
// display the retrieved document
return doc.getProperties();
}
/**
* cr-U-d
* @param key
* @param value
* @param docId
* @return success or failure
*/
public boolean update( final String key, final Object value, String docId ){
if( ! ErrorChecker.checkDb(ctx, database)){
return false;
}
// update the document
try {
Document doc = database.getDocument(docId);
// this alternative way is better for handling write conflicts
doc.update(new Document.DocumentUpdater() {
@Override
public boolean update(UnsavedRevision newRevision) {
Map<String, Object> properties = newRevision.getUserProperties();
properties.put(key, value);
newRevision.setUserProperties(properties);
return true;
}
});
/* Map<String, Object> docContent = doc.getProperties();
//Working on a copy
Map<String, Object> updatedContent = new HashMap<String, Object>();
updatedContent.putAll(docContent);
updatedContent.put(key, value);
doc.putProperties(updatedContent);*/
}
catch (CouchbaseLiteException e) {
ErrorChecker.ShowException(ctx, R.string.err_db_update, e ) ;
return false;
}
return true;
}
/**
* cru-D
* @param docId
* @return
*/
public boolean delete(String docId){
if( ! ErrorChecker.checkDb(ctx, database)){
return false;
}
Document doc = null;
// delete the document
try {
doc = database.getDocument(docId);
doc.delete();
}
catch (CouchbaseLiteException e) {
ErrorChecker.ShowException(ctx, R.string.err_db_delete, e ) ;
}
return doc.isDeleted();
}
}