/
AbstractEntityManager.java
205 lines (173 loc) · 4.83 KB
/
AbstractEntityManager.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
/**
* Copyright 2012 Alex Yanchenko
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.droidparts.manager.sql;
import java.util.Collection;
import org.droidparts.contract.DB;
import org.droidparts.contract.SQL;
import org.droidparts.manager.sql.stmt.DeleteBuilder;
import org.droidparts.manager.sql.stmt.SelectBuilder;
import org.droidparts.manager.sql.stmt.StatementBuilder;
import org.droidparts.manager.sql.stmt.UpdateBuilder;
import org.droidparts.model.Entity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public abstract class AbstractEntityManager<EntityType extends Entity>
implements SQL {
// CRUD methods
public Cursor list(String... columns) {
return list(null, columns);
}
protected Cursor list(String orderBy, String... columns) {
if (columns != null && columns.length == 0) {
columns = null;
}
Cursor cursor = getDB().query(getTableName(), columns, null, null,
null, null, orderBy);
return cursor;
}
public boolean create(EntityType item) {
createOrUpdateForeignKeys(item);
ContentValues cv = toContentValues(item);
cv.remove(DB.Column.ID);
long id = getDB().insert(getTableName(), null, cv);
if (id > 0) {
item.id = id;
return true;
} else {
return false;
}
}
public EntityType read(long id) {
EntityType item = null;
Cursor cursor = getDB().query(getTableName(), null,
DB.Column.ID + EQUAL, toArgs(id), null, null, null);
if (cursor.moveToFirst()) {
item = readFromCursor(cursor);
}
cursor.close();
return item;
}
public boolean update(EntityType item) {
createOrUpdateForeignKeys(item);
ContentValues cv = toContentValues(item);
cv.remove(DB.Column.ID);
int rowCount = getDB().update(getTableName(), cv, DB.Column.ID + EQUAL,
toArgs(item.id));
return rowCount > 0;
}
public boolean delete(long id) {
int rowCount = getDB().delete(getTableName(), DB.Column.ID + EQUAL,
toArgs(id));
return rowCount > 0;
}
public boolean createOrUpdate(EntityType item) {
boolean success;
if (item.id > 0) {
success = update(item);
} else {
success = create(item);
}
return success;
}
//
public boolean create(Collection<EntityType> items) {
int count = 0;
getDB().beginTransaction();
try {
for (EntityType item : items) {
boolean success = create(item);
if (success) {
count++;
}
}
boolean success = (count == items.size());
if (success) {
getDB().setTransactionSuccessful();
}
return success;
} finally {
getDB().endTransaction();
}
}
public boolean update(Collection<EntityType> items) {
int count = 0;
getDB().beginTransaction();
try {
for (EntityType item : items) {
boolean success = update(item);
if (success) {
count++;
}
}
boolean success = (count == items.size());
if (success) {
getDB().setTransactionSuccessful();
}
return success;
} finally {
getDB().endTransaction();
}
}
public boolean delete(Collection<EntityType> items) {
int count = 0;
getDB().beginTransaction();
try {
for (EntityType item : items) {
boolean success = delete(item.id);
if (success) {
count++;
}
}
boolean success = (count == items.size());
if (success) {
getDB().setTransactionSuccessful();
}
return success;
} finally {
getDB().endTransaction();
}
}
// statement builders
public SelectBuilder select() {
return new SelectBuilder(getDB(), getTableName());
};
public UpdateBuilder update() {
return new UpdateBuilder(getDB(), getTableName());
};
public DeleteBuilder delete() {
return new DeleteBuilder(getDB(), getTableName());
};
//
public abstract EntityType readFromCursor(Cursor cursor);
public abstract void fillForeignKeys(EntityType item, String... fieldNames);
// utility methods
public static final String[] toArgs(Object... args) {
String[] arr = new String[args.length];
for (int i = 0; i < args.length; i++) {
arr[i] = StatementBuilder.toArg(args[i]);
}
return arr;
}
public static String sqlEscapeString(String val) {
return StatementBuilder.sqlEscapeString(val);
}
protected abstract SQLiteDatabase getDB();
protected abstract String getTableName();
// boring stuff
protected abstract ContentValues toContentValues(EntityType item);
protected abstract void createOrUpdateForeignKeys(EntityType item);
}