Skip to content

Commit

Permalink
SocialGroup bulk updates and inserts. (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
littlefieldnick committed Nov 4, 2018
1 parent ebb4c72 commit 04e1eec
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public static Location copyLocation(Location loc) {
copy.setInsertDate(loc.getInsertDate());
copy.setServerInsertTime(loc.getServerInsertTime());
copy.setServerUpdateTime(loc.getServerUpdateTime());
copy.setResidencies(loc.getResidencies());
FieldWorker fw = new FieldWorker();
fw.setExtId(loc.getCollectedBy().getExtId());
copy.setCollectedBy(fw);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@
@Controller
@RequestMapping("/locationhierarchylevels")
public class LocationHierarchyLevelResource {
private LocationHierarchyLevelService locationHierarchyLevelService;
private LocationHierarchyLevelService locationHierarchyLevelService;

@Autowired
public LocationHierarchyLevelResource(LocationHierarchyLevelService locationHierarchyLevelService) {
this.locationHierarchyLevelService = locationHierarchyLevelService;
}
@Autowired
public LocationHierarchyLevelResource(LocationHierarchyLevelService locationHierarchyLevelService) {
this.locationHierarchyLevelService = locationHierarchyLevelService;
}

@RequestMapping(method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public LocationHierarchyLevels getAllLevels() {
LocationHierarchyLevels locationHierarchyLevels = new LocationHierarchyLevels();
locationHierarchyLevels.setLocationHierarchies(locationHierarchyLevelService.getAllLevels());
return locationHierarchyLevels;
}

@RequestMapping(method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public LocationHierarchyLevels getAllLevels() {
LocationHierarchyLevels locationHierarchyLevels = new LocationHierarchyLevels();
locationHierarchyLevels.setLocationHierarchies(locationHierarchyLevelService.getAllLevels());
return locationHierarchyLevels;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,11 @@ public ResponseEntity<? extends Serializable> bulkInsert(@RequestBody Locations

for(Location loc: locations.getLocations()) {
ConstraintViolations cv = new ConstraintViolations();

SynchronizationError err = new SynchronizationError();
loc.setCollectedBy(fieldBuilder.referenceField(loc.getCollectedBy(), cv));
loc.setLocationLevel(fieldBuilder.referenceField(loc.getLocationLevel(), cv));

SynchronizationError err = new SynchronizationError();
err.setEntityType("location");
err.setEntityId(loc.getExtId());
err.setFieldworkerExtId(loc.getCollectedBy().getExtId());
Expand All @@ -193,8 +194,11 @@ public ResponseEntity<? extends Serializable> bulkInsert(@RequestBody Locations
violations.addAll(cv.getViolations());
}

err.setViolations(violations);
errors.add(err);
// Violations have occurred, add list of entities where sync failed
if(violations.size() > 0) {
err.setViolations(violations);
errors.add(err);
}
}

Synchronization sync = new Synchronization();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.openhds.controller.exception.ConstraintViolations;
import org.openhds.controller.service.IndividualService;
import org.openhds.controller.service.SocialGroupService;
import org.openhds.domain.model.Individual;
import org.openhds.domain.model.Location;
import org.openhds.domain.model.SocialGroup;
import org.openhds.domain.model.wrappers.Locations;
Expand All @@ -23,6 +22,8 @@
import org.openhds.webservice.CacheResponseWriter;
import org.openhds.webservice.FieldBuilder;
import org.openhds.webservice.WebServiceCallException;
import org.openhds.webservice.util.Synchronization;
import org.openhds.webservice.util.SynchronizationError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -72,14 +73,20 @@ public SocialGroups getAllSocialGroups() {
return sgs;
}

@RequestMapping(value="/pushUpdates", method = RequestMethod.PUT, consumes= {"application/json"}, produces = { "application/json" })
@RequestMapping(value="/bulkUpdate", method = RequestMethod.PUT, consumes= {"application/json"}, produces = { "application/json" })
public ResponseEntity<? extends Serializable> pushUpdate(@RequestBody SocialGroups socialGroups) throws Exception {
long lastClientUpdate = socialGroups.getTimestamp();
long time = new Date().getTime();

ConstraintViolations cv = new ConstraintViolations();

List<SynchronizationError> errors = new ArrayList<SynchronizationError>();
for(SocialGroup group: socialGroups.getSocialGroups()) {
ConstraintViolations cv = new ConstraintViolations();

SynchronizationError err = new SynchronizationError();
err.setEntityType("socialGroup");
err.setEntityId(group.getExtId());
err.setFieldworkerExtId(group.getCollectedBy().getExtId());
List<String> violations = new ArrayList<String>();

group.setCollectedBy(fieldBuilder.referenceField(group.getCollectedBy(), cv));
group.getGroupHead().setCollectedBy(fieldBuilder.referenceField(group.getGroupHead().getCollectedBy(), cv));
Expand All @@ -90,15 +97,26 @@ public ResponseEntity<? extends Serializable> pushUpdate(@RequestBody SocialGrou

this.individualService.createIndividual(group.getGroupHead());
}

group.setGroupHead(fieldBuilder.referenceField(group.getGroupHead(), cv, "Individual does not exist"));

try {
group.setServerUpdateTime(new Date().getTime());
this.update(group, lastClientUpdate, time);
} catch(ConstraintViolations e){
violations.addAll(e.getViolations());
}

group.setServerUpdateTime(new Date().getTime());
this.update(group, lastClientUpdate, time);
if(cv.hasViolations()) {
violations.addAll(cv.getViolations());
}

errors.add(err);
}


return new ResponseEntity<>(time, HttpStatus.ACCEPTED);
Synchronization sync = new Synchronization();
sync.setErrors(errors);
sync.setSyncTime(new Date().getTime());
return new ResponseEntity<Synchronization>(sync, HttpStatus.ACCEPTED);
}

public void update(SocialGroup socialGroup, long currentTimestamp, long lastClientUpdate) throws Exception {
Expand Down Expand Up @@ -137,25 +155,76 @@ else if(socialGroup.isDeleted()) {
}

@RequestMapping(method = RequestMethod.POST, produces = "application/json")
public ResponseEntity<? extends Serializable> insert(@RequestBody SocialGroup socialGroup) {
public ResponseEntity<? extends Serializable> insert(@RequestBody SocialGroup socialGroup) {
List<SynchronizationError> errors = new ArrayList<SynchronizationError>();

ConstraintViolations cv = new ConstraintViolations();

SynchronizationError err = new SynchronizationError();
err.setEntityType("socialGroup");
err.setEntityId(socialGroup.getExtId());
err.setFieldworkerExtId(socialGroup.getCollectedBy().getExtId());
List<String> violations = new ArrayList<String>();

socialGroup.setCollectedBy(fieldBuilder.referenceField(socialGroup.getCollectedBy(), cv));
socialGroup.setGroupHead(fieldBuilder.referenceField(socialGroup.getGroupHead(), cv,
"Invalid Ext Id for Group Head"));

if (cv.hasViolations()) {
return new ResponseEntity<WebServiceCallException>(new WebServiceCallException(cv), HttpStatus.BAD_REQUEST);
if(cv.hasViolations()) {
violations.addAll(cv.getViolations());
}

try {
socialGroupService.createSocialGroup(socialGroup, null);
} catch (ConstraintViolations e) {
return new ResponseEntity<WebServiceCallException>(new WebServiceCallException(e), HttpStatus.BAD_REQUEST);
violations.addAll(e.getViolations());
}

return new ResponseEntity<SocialGroup>(ShallowCopier.copySocialGroup(socialGroup), HttpStatus.CREATED);
}

@RequestMapping(value = "/bulkInsert", method = RequestMethod.POST, produces = "application/json")
public ResponseEntity<? extends Serializable> bulkInsert(@RequestBody SocialGroups socialGroups) {
List<SynchronizationError> errors = new ArrayList<SynchronizationError>();

for(SocialGroup social: socialGroups.getSocialGroups()) {
ConstraintViolations cv = new ConstraintViolations();

SynchronizationError err = new SynchronizationError();
err.setEntityType("socialGroup");
err.setEntityId(social.getExtId());
err.setFieldworkerExtId(social.getCollectedBy().getExtId());
List<String> violations = new ArrayList<String>();

social.setCollectedBy(fieldBuilder.referenceField(social.getCollectedBy(), cv));
social.setGroupHead(fieldBuilder.referenceField(social.getGroupHead(), cv,
"Invalid Ext Id for Group Head"));

try {
socialGroupService.createSocialGroup(social, null);
} catch (ConstraintViolations e) {

violations.addAll(e.getViolations());
}

// Check violations for fieldworker
if(cv.hasViolations()) {
violations.addAll(cv.getViolations());
}

if(violations.size() > 0) {
err.setViolations(violations);
errors.add(err);
}
}

Synchronization sync = new Synchronization();
sync.setErrors(errors);
sync.setSyncTime(new Date().getTime());
return new ResponseEntity<Synchronization>(sync, HttpStatus.ACCEPTED);

}


@RequestMapping(method = RequestMethod.GET, value = "/pull/{timestamp}", produces = "application/json")
public ResponseEntity<SocialGroups> getUpdatedSocialGroups(@PathVariable long timestamp) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.util.List;

import org.openhds.controller.exception.*;

public class SynchronizationError {
private String entityType;
private String fieldworkerExtId;
Expand Down

0 comments on commit 04e1eec

Please sign in to comment.