Skip to content

Commit

Permalink
Merge branch 'master' of github.com:rapidftr/RapidFTR---Android
Browse files Browse the repository at this point in the history
  • Loading branch information
Subhas Dandapani committed Nov 27, 2012
2 parents 150ce17 + adb9dab commit 9c14894
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ public enum ChildTableColumn {
internal_id("_id", true, false),
unique_identifier("unique_identifier", true, false),
created_by("created_by", true, false),
last_updated_at("last_updated_at", true, false),

revision("_rev", true, true),
thumbnail("_thumbnail", true, true),
created_at("created_at", true, true),
updated_at("updated_at", true, true);
created_at("created_at", true, true);


private @Getter final String columnName;
private final boolean isInternal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class MigrationSQL {
public static final String addUpdatedAtColumn = "ALTER TABLE "
+ Database.child.getTableName()
+ " ADD COLUMN "
+ Database.ChildTableColumn.updated_at.getColumnName()
+ Database.ChildTableColumn.last_updated_at.getColumnName()
+ " text";

public static final String addNameColumn = "ALTER TABLE "
Expand Down
10 changes: 9 additions & 1 deletion RapidFTR-Android/src/main/java/com/rapidftr/model/Child.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ protected void setCreatedAt(String createdAt) throws JSONException {
put(created_at.getColumnName(), createdAt);
}

public String getLastUpdatedAt() throws JSONException {
return optString(last_updated_at.getColumnName(), null);
}

public void setLastUpdatedAt(String lastUpdatedAt) throws JSONException {
put(last_updated_at.getColumnName(), lastUpdatedAt);
}

public void addToJSONArray(String key, Object element) throws JSONException {
JSONArray array = has(key) ? getJSONArray(key) : new JSONArray();
List<Object> list = asList(array);
Expand Down Expand Up @@ -233,7 +241,7 @@ public List<History> changeLogs(Child child) throws JSONException {
fromTo.put(TO, newValue);
changes.put(names.getString(i), fromTo);
history.put(USER_NAME, child.getOwner());
history.put(DATETIME, child.getCreatedAt());
history.put(DATETIME, RapidFtrDateTime.now().defaultFormat());
history.put(CHANGES, changes);
histories.add(history);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.rapidftr.database.Database;
import com.rapidftr.database.DatabaseSession;
import com.rapidftr.model.Child;
import com.rapidftr.utils.RapidFtrDateTime;
import lombok.Cleanup;
import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -66,7 +67,10 @@ public List<Child> getMatchingChildren(String subString) throws JSONException {

public void createOrUpdate(Child child) throws JSONException {
ContentValues values = new ContentValues();
addHistory(child);
if (exists(child.getUniqueId())) {
addHistory(child);
child.setLastUpdatedAt(getTimeStamp());
}
values.put(Database.ChildTableColumn.owner.getColumnName(), child.getOwner());
values.put(id.getColumnName(), child.getUniqueId());
values.put(Database.ChildTableColumn.name.getColumnName(), child.getName());
Expand All @@ -79,13 +83,11 @@ public void createOrUpdate(Child child) throws JSONException {
}

private void addHistory(Child child) throws JSONException {
if (exists(child.getUniqueId())) {
Child existingChild = get(child.getUniqueId());
JSONArray existingHistories = (JSONArray) existingChild.opt(HISTORIES);
List<Child.History> histories = child.changeLogs(existingChild);
if (histories.size() > 0 || (existingHistories != null && existingHistories.length() > 1))
child.put(HISTORIES, convertToString(existingHistories, histories));
}
Child existingChild = get(child.getUniqueId());
JSONArray existingHistories = (JSONArray) existingChild.opt(HISTORIES);
List<Child.History> histories = child.changeLogs(existingChild);
if(histories.size() > 0 || (existingHistories != null && existingHistories.length() > 1))
child.put(HISTORIES, convertToString(existingHistories, histories));
}

private String convertToString(JSONArray existingHistories, List<Child.History> histories) throws JSONException {
Expand Down Expand Up @@ -133,4 +135,8 @@ private List<Child> toChildren(Cursor cursor) throws JSONException {
private Child childFrom(Cursor cursor) throws JSONException {
return new Child(cursor.getString(0), BooleanColumn.from(cursor.getString(1)).toBoolean());
}

protected String getTimeStamp(){
return RapidFtrDateTime.now().defaultFormat();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.matchers.JUnitMatchers.hasItem;
import static org.junit.matchers.JUnitMatchers.hasItems;
Expand All @@ -42,21 +43,26 @@ public void setupSession() {
}

@Test
public void shouldCreateChildRecord() throws JSONException {
public void shouldCreateChildRecordAndNotSetLastUpdatedAt() throws JSONException {
repository.createOrUpdate(new Child("id1", "user1", null));
assertThat(repository.size(), equalTo(1));
}

@Test
public void shouldUpdateChildRecordIfIdAlreadyExists() throws Exception {
ChildRepository repository = new ChildRepository("user1", session);
public void shouldUpdateChildRecordIfIdAlreadyExistsAndSetLastUpdateAt() throws Exception {
ChildRepository repository = spy(new ChildRepository("user1", session));
repository.createOrUpdate(new Child("id1", "user1", "{ 'test1' : 'value1', 'test2' : 0, 'test3' : [ '1', 2, '3' ] }"));

String updateString = "{ 'test1' : 'value1' }";
String expectedString = "{'created_by':'user1','test1':'value1','unique_identifier':'id1'}";
String expectedString = "{'created_by':'user1','test1':'value1','unique_identifier':'id1','last_updated_at':'LAST_UPDATED_AT'}";
doReturn("LAST_UPDATED_AT").when(repository).getTimeStamp();

repository.createOrUpdate(new Child("id1", "user1", updateString));
Child child = repository.get("id1");

assertThat(child.getUniqueId(), equalTo("id1"));
assertThat(child.values(), equalJSONIgnoreOrder(expectedString));
assertThat(child.getLastUpdatedAt(), is("LAST_UPDATED_AT"));
}

@Test
Expand All @@ -66,6 +72,7 @@ public void shouldGetCorrectlyDeserializesData() throws JSONException, IOExcepti

Child child2 = repository.get("id1");
assertThat(child1.values(), equalJSONIgnoreOrder(child2.values()));
assertNull(child2.getLastUpdatedAt());
}

@Test
Expand Down

0 comments on commit 9c14894

Please sign in to comment.