Skip to content

Commit 7c80991

Browse files
committed
Extracted and tested the database interactions from Contribution.
1 parent d45ff21 commit 7c80991

File tree

11 files changed

+687
-256
lines changed

11 files changed

+687
-256
lines changed

app/src/main/java/fr/free/nrw/commons/CommonsApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import dagger.android.AndroidInjector;
2222
import dagger.android.DaggerApplication;
2323
import fr.free.nrw.commons.auth.SessionManager;
24-
import fr.free.nrw.commons.contributions.Contribution;
24+
import fr.free.nrw.commons.contributions.ContributionDao;
2525
import fr.free.nrw.commons.data.Category;
2626
import fr.free.nrw.commons.data.DBOpenHelper;
2727
import fr.free.nrw.commons.di.CommonsApplicationComponent;
@@ -142,7 +142,7 @@ private void updateAllDatabases() {
142142

143143
ModifierSequence.Table.onDelete(db);
144144
Category.Table.onDelete(db);
145-
Contribution.Table.onDelete(db);
145+
ContributionDao.Table.onDelete(db);
146146
}
147147

148148
public interface LogoutListener {

app/src/main/java/fr/free/nrw/commons/contributions/Contribution.java

Lines changed: 41 additions & 222 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
package fr.free.nrw.commons.contributions;
22

3-
import android.content.ContentProviderClient;
4-
import android.content.ContentValues;
5-
import android.database.Cursor;
6-
import android.database.sqlite.SQLiteDatabase;
73
import android.net.Uri;
84
import android.os.Parcel;
9-
import android.os.RemoteException;
105
import android.support.annotation.NonNull;
11-
import android.text.TextUtils;
126

137
import java.text.SimpleDateFormat;
148
import java.util.Date;
@@ -43,32 +37,49 @@ public Contribution[] newArray(int i) {
4337
public static final String SOURCE_GALLERY = "gallery";
4438
public static final String SOURCE_EXTERNAL = "external";
4539

46-
private ContentProviderClient client;
4740
private Uri contentUri;
4841
private String source;
4942
private String editSummary;
5043
private Date timestamp;
5144
private int state;
5245
private long transferred;
5346
private String decimalCoords;
54-
5547
private boolean isMultiple;
5648

57-
public boolean getMultiple() {
58-
return isMultiple;
59-
}
60-
61-
public void setMultiple(boolean multiple) {
62-
isMultiple = multiple;
49+
public Contribution(Uri contentUri, String filename, Uri localUri, String imageUrl, Date timestamp,
50+
int state, long dataLength, Date dateUploaded, long transferred,
51+
String source, String description, String creator, boolean isMultiple,
52+
int width, int height, String license) {
53+
super(localUri, imageUrl, filename, description, dataLength, timestamp, dateUploaded, creator);
54+
this.contentUri = contentUri;
55+
this.state = state;
56+
this.timestamp = timestamp;
57+
this.transferred = transferred;
58+
this.source = source;
59+
this.isMultiple = isMultiple;
60+
this.width = width;
61+
this.height = height;
62+
this.license = license;
6363
}
6464

65-
public Contribution(Uri localUri, String remoteUri, String filename, String description, long dataLength, Date dateCreated, Date dateUploaded, String creator, String editSummary, String decimalCoords) {
66-
super(localUri, remoteUri, filename, description, dataLength, dateCreated, dateUploaded, creator);
65+
public Contribution(Uri localUri, String imageUrl, String filename, String description, long dataLength,
66+
Date dateCreated, Date dateUploaded, String creator, String editSummary, String decimalCoords) {
67+
super(localUri, imageUrl, filename, description, dataLength, dateCreated, dateUploaded, creator);
6768
this.decimalCoords = decimalCoords;
6869
this.editSummary = editSummary;
6970
timestamp = new Date(System.currentTimeMillis());
7071
}
7172

73+
public Contribution(Parcel in) {
74+
super(in);
75+
contentUri = in.readParcelable(Uri.class.getClassLoader());
76+
source = in.readString();
77+
timestamp = (Date) in.readSerializable();
78+
state = in.readInt();
79+
transferred = in.readLong();
80+
isMultiple = in.readInt() == 1;
81+
}
82+
7283
@Override
7384
public void writeToParcel(Parcel parcel, int flags) {
7485
super.writeToParcel(parcel, flags);
@@ -80,14 +91,12 @@ public void writeToParcel(Parcel parcel, int flags) {
8091
parcel.writeInt(isMultiple ? 1 : 0);
8192
}
8293

83-
public Contribution(Parcel in) {
84-
super(in);
85-
contentUri = in.readParcelable(Uri.class.getClassLoader());
86-
source = in.readString();
87-
timestamp = (Date) in.readSerializable();
88-
state = in.readInt();
89-
transferred = in.readLong();
90-
isMultiple = in.readInt() == 1;
94+
public boolean getMultiple() {
95+
return isMultiple;
96+
}
97+
98+
public void setMultiple(boolean multiple) {
99+
isMultiple = multiple;
91100
}
92101

93102
public long getTransferred() {
@@ -106,10 +115,18 @@ public Uri getContentUri() {
106115
return contentUri;
107116
}
108117

118+
public void setContentUri(Uri contentUri) {
119+
this.contentUri = contentUri;
120+
}
121+
109122
public Date getTimestamp() {
110123
return timestamp;
111124
}
112125

126+
public void setTimestamp(Date timestamp) {
127+
this.timestamp = timestamp;
128+
}
129+
113130
public int getState() {
114131
return state;
115132
}
@@ -155,62 +172,6 @@ public String getPageContents() {
155172
return buffer.toString();
156173
}
157174

158-
public void setContentProviderClient(ContentProviderClient client) {
159-
this.client = client;
160-
}
161-
162-
public void save() {
163-
try {
164-
if (contentUri == null) {
165-
contentUri = client.insert(ContributionsContentProvider.BASE_URI, this.toContentValues());
166-
} else {
167-
client.update(contentUri, toContentValues(), null, null);
168-
}
169-
} catch (RemoteException e) {
170-
throw new RuntimeException(e);
171-
}
172-
}
173-
174-
public void delete() {
175-
try {
176-
if (contentUri == null) {
177-
// noooo
178-
throw new RuntimeException("tried to delete item with no content URI");
179-
} else {
180-
client.delete(contentUri, null, null);
181-
}
182-
} catch (RemoteException e) {
183-
throw new RuntimeException(e);
184-
}
185-
}
186-
187-
188-
public ContentValues toContentValues() {
189-
ContentValues cv = new ContentValues();
190-
cv.put(Table.COLUMN_FILENAME, getFilename());
191-
if (getLocalUri() != null) {
192-
cv.put(Table.COLUMN_LOCAL_URI, getLocalUri().toString());
193-
}
194-
if (getImageUrl() != null) {
195-
cv.put(Table.COLUMN_IMAGE_URL, getImageUrl());
196-
}
197-
if (getDateUploaded() != null) {
198-
cv.put(Table.COLUMN_UPLOADED, getDateUploaded().getTime());
199-
}
200-
cv.put(Table.COLUMN_LENGTH, getDataLength());
201-
cv.put(Table.COLUMN_TIMESTAMP, getTimestamp().getTime());
202-
cv.put(Table.COLUMN_STATE, getState());
203-
cv.put(Table.COLUMN_TRANSFERRED, transferred);
204-
cv.put(Table.COLUMN_SOURCE, source);
205-
cv.put(Table.COLUMN_DESCRIPTION, description);
206-
cv.put(Table.COLUMN_CREATOR, creator);
207-
cv.put(Table.COLUMN_MULTIPLE, isMultiple ? 1 : 0);
208-
cv.put(Table.COLUMN_WIDTH, width);
209-
cv.put(Table.COLUMN_HEIGHT, height);
210-
cv.put(Table.COLUMN_LICENSE, license);
211-
return cv;
212-
}
213-
214175
@Override
215176
public void setFilename(String filename) {
216177
this.filename = filename;
@@ -224,33 +185,6 @@ public Contribution() {
224185
timestamp = new Date(System.currentTimeMillis());
225186
}
226187

227-
public static Contribution fromCursor(Cursor cursor) {
228-
// Hardcoding column positions!
229-
Contribution c = new Contribution();
230-
231-
//Check that cursor has a value to avoid CursorIndexOutOfBoundsException
232-
if (cursor.getCount() > 0) {
233-
c.contentUri = ContributionsContentProvider.uriForId(cursor.getInt(0));
234-
c.filename = cursor.getString(1);
235-
c.localUri = TextUtils.isEmpty(cursor.getString(2)) ? null : Uri.parse(cursor.getString(2));
236-
c.imageUrl = cursor.getString(3);
237-
c.timestamp = cursor.getLong(4) == 0 ? null : new Date(cursor.getLong(4));
238-
c.state = cursor.getInt(5);
239-
c.dataLength = cursor.getLong(6);
240-
c.dateUploaded = cursor.getLong(7) == 0 ? null : new Date(cursor.getLong(7));
241-
c.transferred = cursor.getLong(8);
242-
c.source = cursor.getString(9);
243-
c.description = cursor.getString(10);
244-
c.creator = cursor.getString(11);
245-
c.isMultiple = cursor.getInt(12) == 1;
246-
c.width = cursor.getInt(13);
247-
c.height = cursor.getInt(14);
248-
c.license = cursor.getString(15);
249-
}
250-
251-
return c;
252-
}
253-
254188
public String getSource() {
255189
return source;
256190
}
@@ -263,121 +197,6 @@ public void setLocalUri(Uri localUri) {
263197
this.localUri = localUri;
264198
}
265199

266-
public static class Table {
267-
public static final String TABLE_NAME = "contributions";
268-
269-
public static final String COLUMN_ID = "_id";
270-
public static final String COLUMN_FILENAME = "filename";
271-
public static final String COLUMN_LOCAL_URI = "local_uri";
272-
public static final String COLUMN_IMAGE_URL = "image_url";
273-
public static final String COLUMN_TIMESTAMP = "timestamp";
274-
public static final String COLUMN_STATE = "state";
275-
public static final String COLUMN_LENGTH = "length";
276-
public static final String COLUMN_UPLOADED = "uploaded";
277-
public static final String COLUMN_TRANSFERRED = "transferred"; // Currently transferred number of bytes
278-
public static final String COLUMN_SOURCE = "source";
279-
public static final String COLUMN_DESCRIPTION = "description";
280-
public static final String COLUMN_CREATOR = "creator"; // Initial uploader
281-
public static final String COLUMN_MULTIPLE = "multiple";
282-
public static final String COLUMN_WIDTH = "width";
283-
public static final String COLUMN_HEIGHT = "height";
284-
public static final String COLUMN_LICENSE = "license";
285-
286-
// NOTE! KEEP IN SAME ORDER AS THEY ARE DEFINED UP THERE. HELPS HARD CODE COLUMN INDICES.
287-
public static final String[] ALL_FIELDS = {
288-
COLUMN_ID,
289-
COLUMN_FILENAME,
290-
COLUMN_LOCAL_URI,
291-
COLUMN_IMAGE_URL,
292-
COLUMN_TIMESTAMP,
293-
COLUMN_STATE,
294-
COLUMN_LENGTH,
295-
COLUMN_UPLOADED,
296-
COLUMN_TRANSFERRED,
297-
COLUMN_SOURCE,
298-
COLUMN_DESCRIPTION,
299-
COLUMN_CREATOR,
300-
COLUMN_MULTIPLE,
301-
COLUMN_WIDTH,
302-
COLUMN_HEIGHT,
303-
COLUMN_LICENSE
304-
};
305-
306-
307-
private static final String CREATE_TABLE_STATEMENT = "CREATE TABLE " + TABLE_NAME + " ("
308-
+ "_id INTEGER PRIMARY KEY,"
309-
+ "filename STRING,"
310-
+ "local_uri STRING,"
311-
+ "image_url STRING,"
312-
+ "uploaded INTEGER,"
313-
+ "timestamp INTEGER,"
314-
+ "state INTEGER,"
315-
+ "length INTEGER,"
316-
+ "transferred INTEGER,"
317-
+ "source STRING,"
318-
+ "description STRING,"
319-
+ "creator STRING,"
320-
+ "multiple INTEGER,"
321-
+ "width INTEGER,"
322-
+ "height INTEGER,"
323-
+ "LICENSE STRING"
324-
+ ");";
325-
326-
327-
public static void onCreate(SQLiteDatabase db) {
328-
db.execSQL(CREATE_TABLE_STATEMENT);
329-
}
330-
331-
public static void onDelete(SQLiteDatabase db) {
332-
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
333-
onCreate(db);
334-
}
335-
336-
public static void onUpdate(SQLiteDatabase db, int from, int to) {
337-
if (from == to) {
338-
return;
339-
}
340-
if (from == 1) {
341-
db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN description STRING;");
342-
db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN creator STRING;");
343-
from++;
344-
onUpdate(db, from, to);
345-
return;
346-
}
347-
if (from == 2) {
348-
db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN multiple INTEGER;");
349-
db.execSQL("UPDATE " + TABLE_NAME + " SET multiple = 0");
350-
from++;
351-
onUpdate(db, from, to);
352-
return;
353-
}
354-
if (from == 3) {
355-
// Do nothing
356-
from++;
357-
onUpdate(db, from, to);
358-
return;
359-
}
360-
if (from == 4) {
361-
// Do nothing -- added Category
362-
from++;
363-
onUpdate(db, from, to);
364-
return;
365-
}
366-
if (from == 5) {
367-
// Added width and height fields
368-
db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN width INTEGER;");
369-
db.execSQL("UPDATE " + TABLE_NAME + " SET width = 0");
370-
db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN height INTEGER;");
371-
db.execSQL("UPDATE " + TABLE_NAME + " SET height = 0");
372-
db.execSQL("ALTER TABLE " + TABLE_NAME + " ADD COLUMN license STRING;");
373-
db.execSQL("UPDATE " + TABLE_NAME + " SET license='" + Prefs.Licenses.CC_BY_SA_3 + "';");
374-
from++;
375-
onUpdate(db, from, to);
376-
return;
377-
}
378-
}
379-
}
380-
381200
@NonNull
382201
private String licenseTemplateFor(String license) {
383202
switch (license) {

0 commit comments

Comments
 (0)