Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/org/opensrp/web/bean/SyncParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class SyncParam {
private String serverVersion;
private String team;
private String teamId;
private String eventType;
private Integer limit;
@JsonProperty("return_count")
private boolean returnCount;
Expand Down Expand Up @@ -67,6 +68,14 @@ public void setTeamId(String teamId) {
this.teamId = teamId;
}

public String getEventType() {
return eventType;
}

public void setEventType(String eventType) {
this.eventType = eventType;
}

public Integer getLimit() {
return limit;
}
Expand Down
91 changes: 51 additions & 40 deletions src/main/java/org/opensrp/web/rest/EventResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,18 @@ protected ResponseEntity<String> sync(HttpServletRequest request) throws JsonPro

String providerId = getStringFilter(PROVIDER_ID, request);
String locationId = getStringFilter(LOCATION_ID, request);
String baseEntityId = getStringFilter(BASE_ENTITY_ID, request);
String serverVersion = getStringFilter(BaseEntity.SERVER_VERSIOIN, request);
String team = getStringFilter(TEAM, request);
String teamId = getStringFilter(TEAM_ID, request);
Integer limit = getIntegerFilter("limit", request);
boolean returnCount = Boolean.getBoolean(getStringFilter(RETURN_COUNT, request));

if (team != null || providerId != null || locationId != null || baseEntityId != null || teamId != null) {

EventSyncBean eventSyncBean = sync(providerId, locationId, baseEntityId, serverVersion, team, teamId, limit, returnCount, false);
String baseEntityId = getStringFilter(BASE_ENTITY_ID, request);
String serverVersion = getStringFilter(BaseEntity.SERVER_VERSIOIN, request);
String team = getStringFilter(TEAM, request);
String teamId = getStringFilter(TEAM_ID, request);
String eventType = getStringFilter(EVENT_TYPE, request);
Integer limit = getIntegerFilter("limit", request);
boolean returnCount = Boolean.getBoolean(getStringFilter(RETURN_COUNT, request));

if (team != null || providerId != null || locationId != null || baseEntityId != null || teamId != null) {

EventSyncBean eventSyncBean = sync(providerId, locationId, baseEntityId, serverVersion, team, teamId,
eventType, limit, returnCount, false);

HttpHeaders headers = getJSONUTF8Headers();
if (returnCount) {
Expand Down Expand Up @@ -170,11 +172,11 @@ protected ResponseEntity<String> syncByPost(@RequestBody SyncParam syncParam) th
if (syncParam.getTeam() != null || syncParam.getProviderId() != null || syncParam.getLocationId() != null
|| syncParam.getBaseEntityId() != null || syncParam.getTeamId() != null) {

EventSyncBean eventSyncBean = sync(syncParam.getProviderId(), syncParam.getLocationId(),
syncParam.getBaseEntityId(), syncParam.getServerVersion(), syncParam.getTeam(),
syncParam.getTeamId(),
syncParam.getLimit(), syncParam.isReturnCount(),
false);
EventSyncBean eventSyncBean = sync(syncParam.getProviderId(), syncParam.getLocationId(),
syncParam.getBaseEntityId(), syncParam.getServerVersion(), syncParam.getTeam(),
syncParam.getTeamId(), syncParam.getEventType(),
syncParam.getLimit(), syncParam.isReturnCount(),
false);

HttpHeaders headers = getJSONUTF8Headers();
if (syncParam.isReturnCount()) {
Expand Down Expand Up @@ -208,15 +210,17 @@ protected ResponseEntity<String> syncOutOfCatchment(HttpServletRequest request)

String providerId = getStringFilter(PROVIDER_ID, request);
String locationId = getStringFilter(LOCATION_ID, request);
String baseEntityId = getStringFilter(BASE_ENTITY_ID, request);
String serverVersion = getStringFilter(BaseEntity.SERVER_VERSIOIN, request);
String team = getStringFilter(TEAM, request);
String teamId = getStringFilter(TEAM_ID, request);
Integer limit = getIntegerFilter("limit", request);
boolean returnCount = Boolean.getBoolean(getStringFilter(RETURN_COUNT, request));

if (team != null || providerId != null || locationId != null || baseEntityId != null || teamId != null) {
EventSyncBean eventSyncBean = sync(providerId, locationId, baseEntityId, serverVersion, team, teamId, limit, returnCount, true);
String baseEntityId = getStringFilter(BASE_ENTITY_ID, request);
String serverVersion = getStringFilter(BaseEntity.SERVER_VERSIOIN, request);
String team = getStringFilter(TEAM, request);
String teamId = getStringFilter(TEAM_ID, request);
String eventType = getStringFilter(EVENT_TYPE, request);
Integer limit = getIntegerFilter("limit", request);
boolean returnCount = Boolean.getBoolean(getStringFilter(RETURN_COUNT, request));

if (team != null || providerId != null || locationId != null || baseEntityId != null || teamId != null) {
EventSyncBean eventSyncBean = sync(providerId, locationId, baseEntityId, serverVersion, team, teamId,
eventType, limit, returnCount, true);

HttpHeaders headers = getJSONUTF8Headers();
if (returnCount) {
Expand All @@ -238,10 +242,10 @@ protected ResponseEntity<String> syncOutOfCatchmentByPost(@RequestBody SyncParam
if (syncParam.getTeam() != null || syncParam.getProviderId() != null || syncParam.getLocationId() != null
|| syncParam.getBaseEntityId() != null || syncParam.getTeamId() != null) {

EventSyncBean eventSyncBean = sync(syncParam.getProviderId(), syncParam.getLocationId(),
syncParam.getBaseEntityId(), syncParam.getServerVersion(), syncParam.getTeam(),
syncParam.getTeamId(), syncParam.getLimit(), syncParam.isReturnCount(),
true);
EventSyncBean eventSyncBean = sync(syncParam.getProviderId(), syncParam.getLocationId(),
syncParam.getBaseEntityId(), syncParam.getServerVersion(), syncParam.getTeam(),
syncParam.getTeamId(), syncParam.getEventType(), syncParam.getLimit(), syncParam.isReturnCount(),
true);

HttpHeaders headers = getJSONUTF8Headers();
if (syncParam.isReturnCount()) {
Expand Down Expand Up @@ -325,23 +329,30 @@ public ResponseEntity<String> syncClientsAndEventsByBaseEntityIds(@RequestBody S
}
}

public EventSyncBean sync(String providerId, String locationId, String baseEntityId, String serverVersion, String team,
String teamId, Integer limit, boolean returnCount, boolean isOutOfCatchment) {
Long lastSyncedServerVersion = null;
if (serverVersion != null) {
lastSyncedServerVersion = Long.parseLong(serverVersion) + 1;
public EventSyncBean sync(String providerId, String locationId, String baseEntityId, String serverVersion, String team,
String teamId, Integer limit, boolean returnCount, boolean isOutOfCatchment) {
return sync(providerId, locationId, baseEntityId, serverVersion, team, teamId, null, limit, returnCount,
isOutOfCatchment);
}

public EventSyncBean sync(String providerId, String locationId, String baseEntityId, String serverVersion, String team,
String teamId, String eventType, Integer limit, boolean returnCount, boolean isOutOfCatchment) {
Long lastSyncedServerVersion = null;
if (serverVersion != null) {
lastSyncedServerVersion = Long.parseLong(serverVersion) + 1;
}

EventSearchBean eventSearchBean = new EventSearchBean();
eventSearchBean.setTeam(team);
eventSearchBean.setTeamId(teamId);
eventSearchBean.setProviderId(providerId);
eventSearchBean.setLocationId(locationId);
eventSearchBean.setBaseEntityId(baseEntityId);
eventSearchBean.setServerVersion(lastSyncedServerVersion);

return getEventsAndClients(eventSearchBean, limit == null || limit == 0 ? 25 : limit, returnCount, isOutOfCatchment);
}
eventSearchBean.setProviderId(providerId);
eventSearchBean.setLocationId(locationId);
eventSearchBean.setBaseEntityId(baseEntityId);
eventSearchBean.setServerVersion(lastSyncedServerVersion);
eventSearchBean.setEventType(eventType);

return getEventsAndClients(eventSearchBean, limit == null || limit == 0 ? 25 : limit, returnCount, isOutOfCatchment);
}

private EventSyncBean getEventsAndClients(EventSearchBean eventSearchBean, Integer limit, boolean returnCount, boolean isOutOfCatchment) {
List<Event> events = new ArrayList<Event>();
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/org/opensrp/web/rest/EventResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public class EventResourceTest extends BaseSecureResourceTest<Event> {
+ "\t\"serverVersion\": 15421904649873,\n"
+ "\t\"team\": \"test\",\n"
+ "\t\"teamId\": \"test\",\n"
+ "\t\"eventType\": \"teamId:Close Referral,LTFU Feedback\",\n"
+ "\t\"limit\": 5\n"
+ "}";

Expand Down Expand Up @@ -454,14 +455,16 @@ public void testGetSync() throws Exception {
doReturn(createClient()).when(clientService).getByBaseEntityId(anyString());

String parameter = PROVIDER_ID + "=providerId&" + LOCATION_ID + "=locationId&" + BASE_ENTITY_ID + "=base-entity-id&"
+ SERVER_VERSIOIN + "=15421904649873&" + TEAM + "=team&" + TEAM_ID + "=team_id";
+ SERVER_VERSIOIN + "=15421904649873&" + TEAM + "=team&" + TEAM_ID + "=team_id&"
+ EVENT_TYPE + "=teamId:Close Referral,LTFU Feedback";
String response = getResponseAsString(BASE_URL + "/sync", parameter, status().isOk());
JsonNode actualObj = mapper.readTree(response);
verify(eventService).findEvents(eventSearchBeanArgumentCaptor.capture(), stringArgumentCaptor.capture(),
stringArgumentCaptor.capture(), integerArgumentCaptor.capture());
assertEquals(integerArgumentCaptor.getValue(), new Integer(25));
assertEquals(stringArgumentCaptor.getAllValues().get(0), SERVER_VERSIOIN);
assertEquals(stringArgumentCaptor.getAllValues().get(1), "asc");
assertEquals("teamId:Close Referral,LTFU Feedback", eventSearchBeanArgumentCaptor.getValue().getEventType());
assertEquals(actualObj.size(), 4);
assertEquals(actualObj.get("clients").size(), 1);
assertEquals(actualObj.get("events").size(), 1);
Expand All @@ -485,6 +488,7 @@ public void testPostSync() throws Exception {
assertEquals(integerArgumentCaptor.getValue(), new Integer(5));
assertEquals(stringArgumentCaptor.getAllValues().get(0), SERVER_VERSIOIN);
assertEquals(stringArgumentCaptor.getAllValues().get(1), "asc");
assertEquals("teamId:Close Referral,LTFU Feedback", eventSearchBeanArgumentCaptor.getValue().getEventType());
}

@Test
Expand Down
Loading