-
Notifications
You must be signed in to change notification settings - Fork 1
/
SupplierProvider.java
376 lines (335 loc) · 11.2 KB
/
SupplierProvider.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/**
*
*/
package com.uc_mobileapps.seifesample02.provider;
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import com.uc_mobileapps.seifesample02.db.SupplierDB;
import com.uc_mobileapps.seifesample02.delivery.Delivery;
import com.uc_mobileapps.seifesample02.delivery.Product;
import com.uc_mobileapps.seifesample02.delivery.schema.DeliverySchema;
import com.uc_mobileapps.seifesample02.delivery.schema.ProductSchema;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Content provider implementation for com.uc_mobileapps.seifesample02.delivery.Delivery, com.uc_mobileapps.seifesample02.delivery.Product * @author ${properties.author}
*/
public class SupplierProvider extends ContentProvider {
/**
* Pass a query limit to the provider
*/
public static final String QUERY_PARAMETER_LIMIT = "_limit";
/**
* Pass a having clause to the provider
*/
public static final String QUERY_PARAMETER_HAVING = "_having";
/**
* Pass a group by to the provider
*/
public static final String QUERY_PARAMETER_GROUP_BY = "_groupby";
/* (non-Javadoc)
* @see android.content.ContentProvider#onCreate()
*/
@Override
public boolean onCreate() {
if (!seifeCreate()) {
return false;
}
return true;
}
//[begin seife autogenerated@
/**
* Content-Provider Authority for Uris (is in the demo package for historical reasons)
*/
public static final String AUTHORITY = "com.uc_mobileapps.seifesample02.provider";
/**
* Better don't use this directly, use {@link #getContentUriDelivery()} instead
*/
public static Uri CONTENT_URI_DELIVERY = Uri.parse("content://" + AUTHORITY + "/" + DeliverySchema.TBL_DELIVERY);
/**
* Better don't use this directly, use {@link #getContentUriProduct()} instead
*/
public static Uri CONTENT_URI_PRODUCT = Uri.parse("content://" + AUTHORITY + "/" + ProductSchema.TBL_PRODUCT);
/**
* Uri for com.uc_mobileapps.seifesample02.delivery.Delivery
*/
public static Uri getContentUriDelivery() {
return CONTENT_URI_DELIVERY;
}
/**
* Explicit URI configuration
*/
public static void setContentUriDelivery(Uri uri) {
CONTENT_URI_DELIVERY = uri;
}
/**
* Uri for com.uc_mobileapps.seifesample02.delivery.Product
*/
public static Uri getContentUriProduct() {
return CONTENT_URI_PRODUCT;
}
/**
* Explicit URI configuration
*/
public static void setContentUriProduct(Uri uri) {
CONTENT_URI_PRODUCT = uri;
}
/**
* Code for urimatcher of the content provider
*/
protected static final int URI_CODE_DELIVERY = 0;
/**
* Delivery by id uri
*/
protected static final int URI_CODE_DELIVERY_ID = 1;
/**
* Delivery for batch updates
*/
protected static final int URI_CODE_DELIVERY_NO_NOTIFY_ID = 2;
/**
* Code for urimatcher of the content provider
*/
protected static final int URI_CODE_PRODUCT = 3;
/**
* Product by id uri
*/
protected static final int URI_CODE_PRODUCT_ID = 4;
/**
* Product for batch updates
*/
protected static final int URI_CODE_PRODUCT_NO_NOTIFY_ID = 5;
/**
* The database helper class where the table definitions reside
*/
private com.uc_mobileapps.seifesample02.db.SupplierDB dbHelper;
/**
* Autogenerated provider onCreate
* @see SupplierProvider#onCreate()
*/
public boolean seifeCreate() {
dbHelper = new com.uc_mobileapps.seifesample02.db.SupplierDB(getContext());
return true;
}
/**
* Uri Matcher of the content provider
*/
private UriMatcher uriMatcher;
/**
* @return
*/
protected UriMatcher getUriMatcher() {
if (uriMatcher == null) {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY, DeliverySchema.TBL_DELIVERY, URI_CODE_DELIVERY);
uriMatcher.addURI(AUTHORITY, DeliverySchema.TBL_DELIVERY + "/#", URI_CODE_DELIVERY_ID);
uriMatcher.addURI(AUTHORITY, DeliverySchema.TBL_DELIVERY + "/batch/#", URI_CODE_DELIVERY_NO_NOTIFY_ID);
uriMatcher.addURI(AUTHORITY, ProductSchema.TBL_PRODUCT, URI_CODE_PRODUCT);
uriMatcher.addURI(AUTHORITY, ProductSchema.TBL_PRODUCT + "/#", URI_CODE_PRODUCT_ID);
uriMatcher.addURI(AUTHORITY, ProductSchema.TBL_PRODUCT + "/batch/#", URI_CODE_PRODUCT_NO_NOTIFY_ID);
}
return uriMatcher;
}
/**
* (non-Javadoc)
*/
@Override
public String getType(Uri uri) {
switch (getUriMatcher().match(uri)) {
case URI_CODE_DELIVERY:
return ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd." + Delivery.class.getPackage() + "." + DeliverySchema.TBL_DELIVERY;
case URI_CODE_DELIVERY_ID:
return ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd." + Delivery.class.getPackage() + "." + DeliverySchema.TBL_DELIVERY;
case URI_CODE_DELIVERY_NO_NOTIFY_ID:
return ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd." + Delivery.class.getPackage() + "." + DeliverySchema.TBL_DELIVERY;
case URI_CODE_PRODUCT:
return ContentResolver.CURSOR_DIR_BASE_TYPE + "/vnd." + Product.class.getPackage() + "." + ProductSchema.TBL_PRODUCT;
case URI_CODE_PRODUCT_ID:
return ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd." + Product.class.getPackage() + "." + ProductSchema.TBL_PRODUCT;
case URI_CODE_PRODUCT_NO_NOTIFY_ID:
return ContentResolver.CURSOR_ITEM_BASE_TYPE + "/vnd." + Product.class.getPackage() + "." + ProductSchema.TBL_PRODUCT;
}
return null;
}
/**
* Inserts a new object to the database
* @return the uri of the created object, null if the mandatory check failed
* @see android.content.ContentProvider\#insert(android.net.Uri, android.content.ContentValues)
*/
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
long rowId;
switch (getUriMatcher().match(uri))
{
case URI_CODE_DELIVERY:
if (!DeliverySchema.instance().checkConstraints(values)) {
return null;
}
rowId = db.insert(DeliverySchema.TBL_DELIVERY, null, values);
if (rowId > 0) {
Uri resUri = ContentUris.withAppendedId(uri, rowId);
getContext().getContentResolver().notifyChange(resUri, null);
return resUri;
}
break;
case URI_CODE_DELIVERY_ID: //unimplemented since id is generated
break;
case URI_CODE_PRODUCT:
if (!ProductSchema.instance().checkConstraints(values)) {
return null;
}
rowId = db.insert(ProductSchema.TBL_PRODUCT, null, values);
if (rowId > 0) {
Uri resUri = ContentUris.withAppendedId(uri, rowId);
getContext().getContentResolver().notifyChange(resUri, null);
return resUri;
}
break;
case URI_CODE_PRODUCT_ID: //unimplemented since id is generated
break;
}
return null;
}
/* (non-Javadoc)
* @see android.content.ContentProvider\#update(android.net.Uri, android.content.ContentValues, java.lang.String, java.lang.String[])
*/
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs)
{
int rowsAffected = 0;
SQLiteDatabase db = dbHelper.getWritableDatabase();
int matchId = getUriMatcher().match(uri);
String[] primaryKey;
switch (matchId)
{
case URI_CODE_DELIVERY_ID:
case URI_CODE_DELIVERY_NO_NOTIFY_ID:
primaryKey = new String[] { uri.getQueryParameter(DeliverySchema.COL_ID) };
rowsAffected = db.update(DeliverySchema.TBL_DELIVERY, values, DeliverySchema.COL_ID + " = ?" , primaryKey);
break;
case URI_CODE_PRODUCT_ID:
case URI_CODE_PRODUCT_NO_NOTIFY_ID:
primaryKey = new String[] { uri.getQueryParameter(ProductSchema.COL_ID) };
rowsAffected = db.update(ProductSchema.TBL_PRODUCT, values, ProductSchema.COL_ID + " = ?" , primaryKey);
break;
default:
break;
}
if (rowsAffected > 0) {
switch (matchId) {
case URI_CODE_DELIVERY_ID:
case URI_CODE_PRODUCT_ID:
getContext().getContentResolver().notifyChange(uri, null);
break;
default:
break;
}
}
return rowsAffected;
}
/* (non-Javadoc)
* @see android.content.ContentProvider\#delete(android.net.Uri, java.lang.String, java.lang.String[])
*/
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int rowsAffected = 0;
SQLiteDatabase db = dbHelper.getWritableDatabase();
String[] primaryKey;
int matchId = getUriMatcher().match(uri);
switch (matchId)
{
case URI_CODE_DELIVERY_ID:
case URI_CODE_DELIVERY_NO_NOTIFY_ID:
primaryKey = new String[] { uri.getQueryParameter(DeliverySchema.COL_ID) };
rowsAffected = db.delete(DeliverySchema.TBL_DELIVERY,DeliverySchema.COL_ID + " = ?" , primaryKey);
if (rowsAffected > 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
break;
case URI_CODE_PRODUCT_ID:
case URI_CODE_PRODUCT_NO_NOTIFY_ID:
primaryKey = new String[] { uri.getQueryParameter(ProductSchema.COL_ID) };
rowsAffected = db.delete(ProductSchema.TBL_PRODUCT,ProductSchema.COL_ID + " = ?" , primaryKey);
if (rowsAffected > 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
break;
default:
break;
}
if (rowsAffected > 0) {
switch (matchId) {
case URI_CODE_DELIVERY_ID:
case URI_CODE_PRODUCT_ID:
getContext().getContentResolver().notifyChange(uri, null);
break;
default:
break;
}
}
return rowsAffected;
}
/* (non-Javadoc)
* @see android.content.ContentProvider\#query(android.net.Uri, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String)
*/
@Override
public Cursor query(Uri uri, String[] projection, String selectionParam,
String[] selectionParamArgs, String sortOrder) {
StringBuilder selection = new StringBuilder();;
List<String> selectionArgs = new ArrayList<String>();
String groupBy = uri.getQueryParameter(QUERY_PARAMETER_GROUP_BY);
String having = uri.getQueryParameter(QUERY_PARAMETER_HAVING);
String limit = uri.getQueryParameter(QUERY_PARAMETER_LIMIT);
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
int matchId = getUriMatcher().match(uri);
switch (matchId) {
case URI_CODE_DELIVERY:
qb.setTables(DeliverySchema.TBL_DELIVERY);
if (selectionParam != null) {
selection.append(selectionParam);
}
if (selectionParamArgs != null) {
selectionArgs = Arrays.asList(selectionParamArgs);
}
break;
case URI_CODE_DELIVERY_ID:
case URI_CODE_DELIVERY_NO_NOTIFY_ID:
qb.setTables(DeliverySchema.TBL_DELIVERY);
selection
.append(DeliverySchema.COL_ID + " = ?");
selectionArgs.add(uri.getQueryParameter(DeliverySchema.COL_ID));
break;
case URI_CODE_PRODUCT:
qb.setTables(ProductSchema.TBL_PRODUCT);
if (selectionParam != null) {
selection.append(selectionParam);
}
if (selectionParamArgs != null) {
selectionArgs = Arrays.asList(selectionParamArgs);
}
break;
case URI_CODE_PRODUCT_ID:
case URI_CODE_PRODUCT_NO_NOTIFY_ID:
qb.setTables(ProductSchema.TBL_PRODUCT);
selection
.append(ProductSchema.COL_ID + " = ?");
selectionArgs.add(uri.getQueryParameter(ProductSchema.COL_ID));
break;
default:
return null;
}
Cursor c = qb.query(dbHelper.getReadableDatabase(), projection, selection.toString(), selectionArgs.toArray(new String[selectionArgs.size()]), groupBy, having, sortOrder, limit);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
//@end seife autogenerated]
}