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
Conflicts:
	RapidFTR-Android/src/test/java/com/rapidftr/service/ChildServiceTest.java
  • Loading branch information
Subhas Dandapani committed Nov 26, 2012
2 parents a7c1c87 + c38aadd commit b884388
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.rapidftr.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import com.rapidftr.R;
import com.rapidftr.adapter.ChildViewAdapter;
import com.rapidftr.model.Child;
import com.rapidftr.service.ChildService;
import com.rapidftr.repository.ChildRepository;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -38,18 +40,22 @@ private View.OnClickListener searchListener() {
public void onClick(View v) {
TextView searchTextView = (TextView) findViewById(R.id.search_text);
String subString = searchTextView.getText().toString();
listView(search(subString));
try {
listView(search(subString));
} catch (Exception e) {
Log.e("ChildSearch", "Error while Searching Children");
}
}
};
}

private List<Child> search(String subString) {
ChildService childService = inject(ChildService.class);
private List<Child> search(String subString) throws JSONException {
ChildRepository childRepository = inject(ChildRepository.class);
subString = subString.trim();
if ("".equals(subString)) {
return new ArrayList<Child>();
}
return childService.searchChildrenInDB(subString);
return childRepository.getMatchingChildren(subString);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ private List<Child> getChildren()
childRepository = inject(ChildRepository.class);
try {
children = childRepository.getChildrenByOwner();
Collections.sort(children);
} catch (JSONException e) {
Log.e("ViewAllChildrenActivity","Error while displaying children list");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public boolean toBoolean() {
@RequiredArgsConstructor(suppressConstructorProperties = true)
public enum ChildTableColumn {
id("id"),
name("name"),
content("child_json"),
owner("child_owner"),
synced("synced"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public enum Migrations {
v001_createChildTable(1, MigrationSQL.createChildTable),
v002_addCreatedAtColumn(2, MigrationSQL.addCreatedAtColumn),
v002_addUpdatedAtColumn(2, MigrationSQL.addUpdatedAtColumn),
v003_addNameColumn(3, MigrationSQL.addNameColumn),
;


private int databaseVersion;
private String sql;

Expand Down Expand Up @@ -68,5 +68,11 @@ class MigrationSQL {
+ Database.ChildTableColumn.updated_at.getColumnName()
+ " text";

public static final String addNameColumn = "ALTER TABLE "
+ Database.child.getTableName()
+ " ADD COLUMN "
+ Database.ChildTableColumn.name.getColumnName()
+ " text";

}

35 changes: 23 additions & 12 deletions RapidFTR-Android/src/main/java/com/rapidftr/model/Child.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@
import static com.rapidftr.utils.JSONArrays.asJSONArray;
import static com.rapidftr.utils.JSONArrays.asList;

public class Child extends JSONObject implements Parcelable, Comparable {
public class Child extends JSONObject implements Parcelable {

public static final ObjectMapper JSON_MAPPER = new ObjectMapper();
public static final String EMPTY_STRING = "";

protected @Getter @Setter boolean synced;
protected
@Getter
@Setter
boolean synced;

public Child() {
public Child() {
try {
setSynced(false);
setCreatedAt(RapidFtrDateTime.now().defaultFormat());
Expand Down Expand Up @@ -128,6 +132,18 @@ public void setOwner(String owner) throws JSONException {
put(created_by.getColumnName(), owner);
}

public String getName() {
try {
return getString(name.getColumnName());
} catch (JSONException e) {
return EMPTY_STRING;
}
}

public void setName(String childName) throws JSONException {
put(name.getColumnName(), childName);
}

public String getCreatedAt() throws JSONException {
return getString(created_at.getColumnName());
}
Expand Down Expand Up @@ -175,7 +191,7 @@ public boolean isValid() {
int numberOfNonInternalFields = names().length();

for (ChildTableColumn field : ChildTableColumn.internalFields()) {
if(has(field.getColumnName())){
if (has(field.getColumnName())) {
numberOfNonInternalFields--;
}
}
Expand Down Expand Up @@ -206,10 +222,10 @@ public Object apply(ChildTableColumn childTableColumn) {
public List<History> changeLogs(Child child) throws JSONException {
JSONArray names = this.names();
List<History> histories = new ArrayList<History>();
for(int i=0; i < names.length(); i++){
for (int i = 0; i < names.length(); i++) {
String newValue = this.optString(names.getString(i), "");
String oldValue = child.optString(names.getString(i), "");
if(!oldValue.equals(newValue)){
if (!oldValue.equals(newValue)) {
History history = new History();
JSONObject changes = new JSONObject();
JSONObject fromTo = new JSONObject();
Expand All @@ -229,12 +245,7 @@ public boolean isNew() {
return !has(internal_id.getColumnName());
}

@Override
public int compareTo(Object that) {
return this.optString("name").compareTo(((Child) that).optString("name"));
}

public class History extends JSONObject implements Parcelable{
public class History extends JSONObject implements Parcelable {
public static final String HISTORIES = "histories";
public static final String USER_NAME = "user_name";
public static final String DATETIME = "datetime";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static com.rapidftr.database.Database.BooleanColumn;
import static com.rapidftr.database.Database.BooleanColumn.falseValue;
import static com.rapidftr.database.Database.ChildTableColumn.id;
import static com.rapidftr.database.Database.child;
import static com.rapidftr.model.Child.History.HISTORIES;
import static java.lang.String.format;

Expand All @@ -45,7 +44,7 @@ public Child get(String id) throws JSONException {
}

public boolean exists(String childId) {
@Cleanup Cursor cursor = session.rawQuery("SELECT child_json FROM children WHERE id = ?", new String[] { childId == null ? "" : childId });
@Cleanup Cursor cursor = session.rawQuery("SELECT child_json FROM children WHERE id = ?", new String[]{childId == null ? "" : childId});
return cursor.moveToNext() && cursor.getCount() > 0;
}

Expand All @@ -55,12 +54,13 @@ public int size() {
}

public List<Child> getChildrenByOwner() throws JSONException {
@Cleanup Cursor cursor = session.rawQuery("SELECT child_json, synced FROM children WHERE child_owner = ? ORDER BY id", new String[]{userName});
@Cleanup Cursor cursor = session.rawQuery("SELECT child_json, synced FROM children WHERE child_owner = ? ORDER BY name", new String[]{userName});
return toChildren(cursor);
}

public List<Child> getAllChildren() throws JSONException {
@Cleanup Cursor cursor = session.rawQuery("SELECT child_json, synced FROM children",new String[]{});
public List<Child> getMatchingChildren(String subString) throws JSONException {
String searchString = String.format("%%%s%%", subString);
@Cleanup Cursor cursor = session.rawQuery("SELECT child_json, synced FROM children WHERE name LIKE ? or id LIKE ?", new String[]{searchString,searchString});
return toChildren(cursor);
}

Expand All @@ -71,6 +71,7 @@ public void createOrUpdate(Child child) throws JSONException {
addHistory(child);
values.put(Database.ChildTableColumn.owner.getColumnName(), child.getOwner());
values.put(id.getColumnName(), child.getUniqueId());
values.put(Database.ChildTableColumn.name.getColumnName(), child.getName());
values.put(Database.ChildTableColumn.content.getColumnName(), child.toString());
values.put(Database.ChildTableColumn.synced.getColumnName(), child.isSynced());
values.put(Database.ChildTableColumn.created_at.getColumnName(), child.getCreatedAt());
Expand All @@ -84,18 +85,18 @@ private void addHistory(Child child) throws JSONException {
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))
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 {
StringBuffer json = new StringBuffer("[");
for(int i = 0; existingHistories != null && (i < existingHistories.length()); i++){
json.append(existingHistories.get(i)+",");
for (int i = 0; existingHistories != null && (i < existingHistories.length()); i++) {
json.append(existingHistories.get(i) + ",");
}
for (Child.History history : histories) {
json.append(history.toString()+",");
json.append(history.toString() + ",");
}
json.setLength(json.length() - 1);
return json.append("]").toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,6 @@ public List<Child> getAllChildren() throws IOException {
return convertToChildRecords(childrenJson);
}

public List<Child> searchChildrenInDB(String subString) {
List<Child> filteredList = new ArrayList<Child>();
try {
for (Child child : repository.getAllChildren()) {
try {
if (containsIgnoreCase(child.getUniqueId(), subString) ||
containsIgnoreCase((String) child.get("name"), subString)) {
filteredList.add(child);
}
} catch (JSONException e) {
Log.e("ChildService", "Error while Searching Children");
}
}
} catch (JSONException e) {
Log.e("ChildService", "Error while Searching Children");
}
return filteredList;
}

private boolean containsIgnoreCase(String completeString, String subString) {
return completeString.toLowerCase().contains(subString.toLowerCase());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.rapidftr.CustomTestRunner;
import com.rapidftr.R;
import com.rapidftr.model.Child;
import com.rapidftr.service.ChildService;
import com.rapidftr.repository.ChildRepository;
import org.json.JSONException;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -26,23 +26,23 @@ public class SearchActivityTest {
protected SearchActivity activity;

@Mock
private ChildService childService;
private ChildRepository childRepository;

@Before
public void setUp() {
initMocks(this);
activity = spy(new SearchActivity());
Injector mockInjector = mock(Injector.class);
doReturn(mockInjector).when(activity).getInjector();
doReturn(childService).when(mockInjector).getInstance(ChildService.class);
doReturn(childRepository).when(mockInjector).getInstance(ChildRepository.class);
}

@Test
public void shouldListChildrenForSearchedString() throws JSONException {
List<Child> searchResults = new ArrayList<Child>();
searchResults.add(new Child("id1", "user1", "{ \"name\" : \"child1\", \"test2\" : 0, \"test3\" : [ \"1\", 2, \"3\" ] }"));
String searchString = "Hild";
when(childService.searchChildrenInDB(searchString)).thenReturn(searchResults);
when(childRepository.getMatchingChildren(searchString)).thenReturn(searchResults);

activity.onCreate(null);
TextView textView = (TextView) activity.findViewById(R.id.search_text);
Expand All @@ -57,7 +57,7 @@ public void shouldListChildrenForSearchedString() throws JSONException {
public void shouldShowEmptyViewForNoSearchResults() throws JSONException {
List<Child> searchResults = new ArrayList<Child>();
String searchString = "Hild";
when(childService.searchChildrenInDB(searchString)).thenReturn(searchResults);
when(childRepository.getMatchingChildren(searchString)).thenReturn(searchResults);

activity.onCreate(null);
TextView textView = (TextView) activity.findViewById(R.id.search_text);
Expand All @@ -75,7 +75,7 @@ public void shouldReturnEmptyListForNoSearchString() throws JSONException {
textView.setText(searchString);
activity.findViewById(R.id.search_btn).performClick();
ListView listView = (ListView) activity.findViewById(R.id.child_list);
verify(childService,never()).searchChildrenInDB(searchString);
verify(childRepository,never()).getMatchingChildren(searchString);
assertNotNull(listView.getEmptyView());
}

Expand Down
10 changes: 0 additions & 10 deletions RapidFTR-Android/src/test/java/com/rapidftr/model/ChildTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

Expand Down Expand Up @@ -173,15 +172,6 @@ public void shouldTrimFieldValues() throws JSONException {
assertThat(child.getString("name"), equalTo("line1 \r\n line2"));
}

@Test
public void shouldSortChildrenByNameAlphabetically() throws Exception {
Child child1 = new Child("id1", "user1", "{ \"name\" : \"abc\", \"test2\" : 0, \"test3\" : [ \"1\", 2, \"3\" ] }");
Child child2 = new Child("id2", "user2", "{ \"name\" : \"def\", \"test2\" : 0, \"test3\" : [ \"1\", 2, \"3\" ] }");
List<Child> children = Arrays.asList(child1,child2);
Collections.sort(children);
assertEquals(child1, children.get(0));
}

@Test
public void valuesShouldReturnAllExceptSystemFields() throws JSONException, IOException {
Child child = new Child();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import static com.rapidftr.model.Child.History.*;
import static com.rapidftr.utils.JSONMatcher.equalJSONIgnoreOrder;
import static com.rapidftr.utils.JSONMatcher.hasJSONObjects;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.IsNot.not;
Expand Down Expand Up @@ -89,15 +88,17 @@ public void getShouldThrowExceptionIfRecordDoesNotExist() throws JSONException {
public void shouldReturnMatchedChildRecords() throws JSONException, IOException {
Child child1 = new Child("id1", "user1", "{ 'name' : 'child1', 'test2' : 0, 'test3' : [ '1', 2, '3' ] }");
Child child2 = new Child("id2", "user2", "{ 'name' : 'child2', 'test2' : 0, 'test3' : [ '1', 2, '3' ] }");
Child child3 = new Child("id3", "user3", "{ 'name' : 'child3', 'test2' : 'child01', 'test3' : [ '1', 2, '3' ] }");
Child child3 = new Child("id3", "user3", "{ 'name' : 'child3', 'test2' : 'child1', 'test3' : [ '1', 2, '3' ] }");
Child child4 = new Child("child1", "user4", "{ 'name' : 'child4', 'test2' : 'test2', 'test3' : [ '1', 2, '3' ] }");
repository.createOrUpdate(child1);
repository.createOrUpdate(child2);
repository.createOrUpdate(child3);
repository.createOrUpdate(child4);

List<Child> children = repository.getAllChildren();
assertEquals(4, children.size());
List<Child> children = repository.getMatchingChildren("hiLd1");
assertEquals(2, children.size());
assertThat(child1,equalTo(children.get(0)));
assertThat(child4,equalTo(children.get(1)));
}


Expand Down Expand Up @@ -126,14 +127,15 @@ public void shouldOnlyReturnsOwnRecords() throws JSONException {

@Test
public void shouldReturnsAllRecords() throws JSONException, IOException {
Child child1 = new Child("id1", "user1", null);
Child child2 = new Child("id2", "user1", null);
Child child1 = new Child("id1", "user1", "{'name':'last_user'}");
Child child2 = new Child("id2", "user1", "{'name':'first_user'}");
repository.createOrUpdate(child1);
repository.createOrUpdate(child2);

List<Child> children = repository.getChildrenByOwner();
assertThat(children.size(), equalTo(2));
assertThat(children, hasJSONObjects(child1, child2));
assertThat(child2,equalTo(children.get(0)));
assertThat(child1,equalTo(children.get(1)));
}

@Test
Expand Down
Loading

0 comments on commit b884388

Please sign in to comment.