Refactored Event to be its own object. Tests passing.
This commit is contained in:
parent
5271544f73
commit
aee69453c4
|
@ -1,15 +1,12 @@
|
|||
package no.nilsnh.uibevents;
|
||||
|
||||
import android.app.Application;
|
||||
import android.content.ContentValues;
|
||||
import android.test.AndroidTestCase;
|
||||
import android.test.ApplicationTestCase;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import no.nilsnh.uibevents.data.Event;
|
||||
import no.nilsnh.uibevents.data.EventContract;
|
||||
import no.nilsnh.uibevents.data.EventDbHelper;
|
||||
|
||||
|
@ -30,7 +27,7 @@ public class ApplicationTest extends AndroidTestCase {
|
|||
}
|
||||
|
||||
public void testParseJsonEvent() {
|
||||
ArrayList<ContentValues> events = null;
|
||||
ArrayList<Event> events = null;
|
||||
try {
|
||||
events = EventDbHelper.parseJsonEvents(TestUtils.getExampleJson());
|
||||
} catch (JSONException e) {
|
||||
|
@ -40,14 +37,14 @@ public class ApplicationTest extends AndroidTestCase {
|
|||
}
|
||||
|
||||
public void testInsertIntoTextFile() {
|
||||
ArrayList<ContentValues> events = null;
|
||||
ArrayList<Event> events = null;
|
||||
try {
|
||||
events = EventDbHelper.parseJsonEvents(TestUtils.getExampleJson());
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
getContext().getContentResolver()
|
||||
.insert(EventContract.EventEntry.CONTENT_URI, events.get(0));
|
||||
.insert(EventContract.EventEntry.CONTENT_URI, events.get(0).getContentValues());
|
||||
assertTrue(eventDbHelper.getStoredData().contains(events.get(0)));
|
||||
}
|
||||
}
|
|
@ -1,21 +1,146 @@
|
|||
package no.nilsnh.uibevents.data;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.net.Uri;
|
||||
|
||||
import java.util.Date;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class Event {
|
||||
|
||||
private String id;
|
||||
private String type;
|
||||
private String title;
|
||||
private Date dateFrom;
|
||||
private Date dateTo;
|
||||
private String dateFrom;
|
||||
private String dateTo;
|
||||
private String location;
|
||||
private String details;
|
||||
private Uri url;
|
||||
private String url;
|
||||
|
||||
public Event(ContentValues values) {
|
||||
id = values.getAsString("id");
|
||||
type = values.getAsString("type");
|
||||
title = values.getAsString("title");
|
||||
dateFrom = values.getAsString("date_from");
|
||||
dateTo = values.getAsString("date_to");
|
||||
location = values.getAsString("location");
|
||||
details = values.getAsString("details");
|
||||
url = values.getAsString("url");
|
||||
}
|
||||
|
||||
public Event(String event) {
|
||||
String[] eventDetailsString = event.split(";");
|
||||
id = eventDetailsString[0];
|
||||
type = eventDetailsString[1];
|
||||
title = eventDetailsString[2];
|
||||
dateFrom = eventDetailsString[3];
|
||||
dateTo = eventDetailsString[4];
|
||||
location = eventDetailsString[5];
|
||||
details = eventDetailsString[6];
|
||||
url = eventDetailsString[7];
|
||||
}
|
||||
|
||||
public Event(JSONObject event) {
|
||||
try {
|
||||
id = event.getString("id");
|
||||
type = event.getString("type");
|
||||
title = event.getString("title");
|
||||
dateFrom = event.getString("date_from");
|
||||
dateTo = event.getString("date_to");
|
||||
location = event.getString("location");
|
||||
details = event.getString("lead");
|
||||
url = event.getString("path");
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public ContentValues getContentValues() {
|
||||
ContentValues cValues = new ContentValues();
|
||||
cValues.put(EventContract.EventEntry.COLUMN_EVENT_ID, id);
|
||||
cValues.put(EventContract.EventEntry.COLUMN_EVENT_TYPE, type);
|
||||
cValues.put(EventContract.EventEntry.COLUMN_EVENT_TITLE, title);
|
||||
cValues.put(EventContract.EventEntry.COLUMN_EVENT_DATE_FROM, dateFrom);
|
||||
cValues.put(EventContract.EventEntry.COLUMN_EVENT_DATE_TO, dateTo);
|
||||
cValues.put(EventContract.EventEntry.COLUMN_EVENT_LOCATION, location);
|
||||
cValues.put(EventContract.EventEntry.COLUMN_EVENT_DETAILS, details);
|
||||
cValues.put(EventContract.EventEntry.COLUMN_EVENT_URL, url);
|
||||
return cValues;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public String getDateFrom() {
|
||||
return dateFrom;
|
||||
}
|
||||
|
||||
public String getDateTo() {
|
||||
return dateTo;
|
||||
}
|
||||
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public String getDetails() {
|
||||
return details;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return id + ";" +
|
||||
type + ";" +
|
||||
title + ";" +
|
||||
dateFrom + ";" +
|
||||
dateTo + ";" +
|
||||
location + ";" +
|
||||
details + ";" +
|
||||
url;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Event event = (Event) o;
|
||||
|
||||
if (id != null ? !id.equals(event.id) : event.id != null) return false;
|
||||
if (type != null ? !type.equals(event.type) : event.type != null) return false;
|
||||
if (title != null ? !title.equals(event.title) : event.title != null) return false;
|
||||
if (dateFrom != null ? !dateFrom.equals(event.dateFrom) : event.dateFrom != null)
|
||||
return false;
|
||||
if (dateTo != null ? !dateTo.equals(event.dateTo) : event.dateTo != null) return false;
|
||||
if (location != null ? !location.equals(event.location) : event.location != null)
|
||||
return false;
|
||||
if (details != null ? !details.equals(event.details) : event.details != null) return false;
|
||||
return !(url != null ? !url.equals(event.url) : event.url != null);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = id != null ? id.hashCode() : 0;
|
||||
result = 31 * result + (type != null ? type.hashCode() : 0);
|
||||
result = 31 * result + (title != null ? title.hashCode() : 0);
|
||||
result = 31 * result + (dateFrom != null ? dateFrom.hashCode() : 0);
|
||||
result = 31 * result + (dateTo != null ? dateTo.hashCode() : 0);
|
||||
result = 31 * result + (location != null ? location.hashCode() : 0);
|
||||
result = 31 * result + (details != null ? details.hashCode() : 0);
|
||||
result = 31 * result + (url != null ? url.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -95,31 +95,12 @@ public class EventDbHelper {
|
|||
return eventJsonStr;
|
||||
}
|
||||
|
||||
public static ArrayList<ContentValues> parseJsonEvents(String data) throws JSONException {
|
||||
ArrayList<ContentValues> parsedEvents = new ArrayList<>();
|
||||
public static ArrayList<Event> parseJsonEvents(String data) throws JSONException {
|
||||
ArrayList<Event> parsedEvents = new ArrayList<>();
|
||||
JSONArray events = new JSONObject(data).getJSONArray("events");
|
||||
for (int i = 0; i < events.length(); i++) {
|
||||
JSONObject event = events.getJSONObject(i);
|
||||
|
||||
Integer id = event.getInt("id");
|
||||
String type = event.getString("type");
|
||||
String title = event.getString("title");
|
||||
String date_from = event.getString("date_from");
|
||||
String date_to = event.getString("date_to");
|
||||
String location = event.getString("location");
|
||||
String details = event.getString("lead");
|
||||
String url = event.getString("path");
|
||||
|
||||
ContentValues cValues = new ContentValues();
|
||||
cValues.put(EventContract.EventEntry.COLUMN_EVENT_ID, id);
|
||||
cValues.put(EventContract.EventEntry.COLUMN_EVENT_TYPE, type);
|
||||
cValues.put(EventContract.EventEntry.COLUMN_EVENT_TITLE, title);
|
||||
cValues.put(EventContract.EventEntry.COLUMN_EVENT_DATE_FROM, date_from);
|
||||
cValues.put(EventContract.EventEntry.COLUMN_EVENT_DATE_TO, date_to);
|
||||
cValues.put(EventContract.EventEntry.COLUMN_EVENT_LOCATION, location);
|
||||
cValues.put(EventContract.EventEntry.COLUMN_EVENT_DETAILS, details);
|
||||
cValues.put(EventContract.EventEntry.COLUMN_EVENT_URL, url);
|
||||
parsedEvents.add(cValues);
|
||||
Event event = new Event(events.getJSONObject(i));
|
||||
parsedEvents.add(event);
|
||||
}
|
||||
return parsedEvents;
|
||||
}
|
||||
|
@ -137,7 +118,7 @@ public class EventDbHelper {
|
|||
}
|
||||
}
|
||||
|
||||
public ArrayList<ContentValues> getStoredData() {
|
||||
public ArrayList<Event> getStoredData() {
|
||||
File file = new File(ctx.getFilesDir() + "/" + filename);
|
||||
|
||||
if(file.exists()){
|
||||
|
@ -153,65 +134,48 @@ public class EventDbHelper {
|
|||
e.printStackTrace();
|
||||
}
|
||||
|
||||
String[] eventDetailsString = null;
|
||||
ContentValues eventDetailsCValue = new ContentValues();
|
||||
ArrayList<ContentValues> events = new ArrayList<>();
|
||||
for (String event: eventStrings) {
|
||||
eventDetailsString = event.split(";");
|
||||
eventDetailsCValue.put("id", eventDetailsString[0]);
|
||||
eventDetailsCValue.put("type", eventDetailsString[1]);
|
||||
eventDetailsCValue.put("title", eventDetailsString[2]);
|
||||
eventDetailsCValue.put("date_from", eventDetailsString[3]);
|
||||
eventDetailsCValue.put("date_to", eventDetailsString[4]);
|
||||
eventDetailsCValue.put("location", eventDetailsString[5]);
|
||||
eventDetailsCValue.put("details", eventDetailsString[6]);
|
||||
eventDetailsCValue.put("url", eventDetailsString[7]);
|
||||
events.add(eventDetailsCValue);
|
||||
ArrayList<Event> events = new ArrayList<>();
|
||||
for (String eventString: eventStrings) {
|
||||
events.add(new Event(eventString));
|
||||
}
|
||||
return events;
|
||||
}
|
||||
else {
|
||||
Log.d(LOG_TAG, "Was not able to find file");
|
||||
return new ArrayList<ContentValues>();
|
||||
return new ArrayList<Event>();
|
||||
}
|
||||
}
|
||||
|
||||
public Long insert(ContentValues values) {
|
||||
File file = new File(ctx.getFilesDir(),filename);
|
||||
Event newEvent = new Event(values);
|
||||
|
||||
//First retrieve all data,then check if the value is already stored.
|
||||
ArrayList<ContentValues> storedValues = getStoredData();
|
||||
ArrayList<Event> storedValues = getStoredData();
|
||||
|
||||
//If no data exist already create new arrayList
|
||||
if (storedValues == null) storedValues = new ArrayList<>();
|
||||
if (storedValues == null) storedValues = new ArrayList<Event>();
|
||||
|
||||
//If the new data is already there stop execution.
|
||||
if (storedValues.contains(values)) return null;
|
||||
if (storedValues.contains(newEvent)) {
|
||||
return Long.valueOf(storedValues.indexOf(newEvent) + 1);
|
||||
}
|
||||
|
||||
storedValues.add(values);
|
||||
Integer storedDataPosition = storedValues.indexOf(values) + 1;
|
||||
storedValues.add(newEvent);
|
||||
Long storedDataPosition = Long.valueOf(storedValues.indexOf(newEvent) + 1);
|
||||
|
||||
//Write new data to textFile
|
||||
try {
|
||||
|
||||
if (!file.exists()) file.createNewFile();
|
||||
|
||||
BufferedWriter buf = new BufferedWriter(new FileWriter(ctx.getFilesDir() + "/" + filename, true));
|
||||
for (ContentValues event : storedValues) {
|
||||
buf.write(event.getAsString("id") + ";");
|
||||
buf.write(event.getAsString("type") + ";");
|
||||
buf.write(event.getAsString("title") + ";");
|
||||
buf.write(event.getAsString("date_from") + ";");
|
||||
buf.write(event.getAsString("date_to") + ";");
|
||||
buf.write(event.getAsString("location") + ";");
|
||||
buf.write(event.getAsString("details") + ";");
|
||||
buf.write(event.getAsString("url"));
|
||||
for (Event event : storedValues) {
|
||||
buf.write(event.toString());
|
||||
buf.newLine();
|
||||
}
|
||||
buf.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return Long.valueOf(storedDataPosition);
|
||||
return storedDataPosition;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue