diff --git a/RapidFTR-Android/src/main/java/com/rapidftr/database/Database.java b/RapidFTR-Android/src/main/java/com/rapidftr/database/Database.java index 84306fd9..2941de93 100644 --- a/RapidFTR-Android/src/main/java/com/rapidftr/database/Database.java +++ b/RapidFTR-Android/src/main/java/com/rapidftr/database/Database.java @@ -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; diff --git a/RapidFTR-Android/src/main/java/com/rapidftr/database/migration/Migrations.java b/RapidFTR-Android/src/main/java/com/rapidftr/database/migration/Migrations.java index f2bbaec8..03d117c5 100644 --- a/RapidFTR-Android/src/main/java/com/rapidftr/database/migration/Migrations.java +++ b/RapidFTR-Android/src/main/java/com/rapidftr/database/migration/Migrations.java @@ -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 " diff --git a/RapidFTR-Android/src/main/java/com/rapidftr/model/Child.java b/RapidFTR-Android/src/main/java/com/rapidftr/model/Child.java index e91e0fd4..0816d40c 100644 --- a/RapidFTR-Android/src/main/java/com/rapidftr/model/Child.java +++ b/RapidFTR-Android/src/main/java/com/rapidftr/model/Child.java @@ -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 list = asList(array); @@ -233,7 +241,7 @@ public List 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); } diff --git a/RapidFTR-Android/src/main/java/com/rapidftr/repository/ChildRepository.java b/RapidFTR-Android/src/main/java/com/rapidftr/repository/ChildRepository.java index 26822bec..15317303 100644 --- a/RapidFTR-Android/src/main/java/com/rapidftr/repository/ChildRepository.java +++ b/RapidFTR-Android/src/main/java/com/rapidftr/repository/ChildRepository.java @@ -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; @@ -66,7 +67,10 @@ public List 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()); @@ -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 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 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 histories) throws JSONException { @@ -133,4 +135,8 @@ private List 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(); + } } diff --git a/RapidFTR-Android/src/test/java/com/rapidftr/repository/ChildRepositoryTest.java b/RapidFTR-Android/src/test/java/com/rapidftr/repository/ChildRepositoryTest.java index 8fdb2fe0..fd18fa88 100644 --- a/RapidFTR-Android/src/test/java/com/rapidftr/repository/ChildRepositoryTest.java +++ b/RapidFTR-Android/src/test/java/com/rapidftr/repository/ChildRepositoryTest.java @@ -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; @@ -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 @@ -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