From 04e1eec2a53fbbe1659b588ebc87de30659c8a7e Mon Sep 17 00:00:00 2001 From: littlefieldnick Date: Sat, 3 Nov 2018 21:39:28 -0400 Subject: [PATCH] SocialGroup bulk updates and inserts. (#15) --- .../domain/util/JsonShallowCopier.java | 1 - .../LocationHierarchyLevelResource.java | 26 ++--- .../resources/api2/LocationResourceApi2.java | 10 +- .../api2/SocialGroupResourceApi2.java | 95 ++++++++++++++++--- .../webservice/util/SynchronizationError.java | 2 - 5 files changed, 102 insertions(+), 32 deletions(-) diff --git a/domain/src/main/java/org/openhds/domain/util/JsonShallowCopier.java b/domain/src/main/java/org/openhds/domain/util/JsonShallowCopier.java index 5324180a..faa1728e 100644 --- a/domain/src/main/java/org/openhds/domain/util/JsonShallowCopier.java +++ b/domain/src/main/java/org/openhds/domain/util/JsonShallowCopier.java @@ -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); diff --git a/webservice/src/main/java/org/openhds/webservice/resources/LocationHierarchyLevelResource.java b/webservice/src/main/java/org/openhds/webservice/resources/LocationHierarchyLevelResource.java index f16d9559..c4f16787 100644 --- a/webservice/src/main/java/org/openhds/webservice/resources/LocationHierarchyLevelResource.java +++ b/webservice/src/main/java/org/openhds/webservice/resources/LocationHierarchyLevelResource.java @@ -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; - } - } diff --git a/webservice/src/main/java/org/openhds/webservice/resources/api2/LocationResourceApi2.java b/webservice/src/main/java/org/openhds/webservice/resources/api2/LocationResourceApi2.java index 8406a021..a0223486 100644 --- a/webservice/src/main/java/org/openhds/webservice/resources/api2/LocationResourceApi2.java +++ b/webservice/src/main/java/org/openhds/webservice/resources/api2/LocationResourceApi2.java @@ -172,10 +172,11 @@ public ResponseEntity 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()); @@ -193,8 +194,11 @@ public ResponseEntity 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(); diff --git a/webservice/src/main/java/org/openhds/webservice/resources/api2/SocialGroupResourceApi2.java b/webservice/src/main/java/org/openhds/webservice/resources/api2/SocialGroupResourceApi2.java index da61fb95..286fe843 100644 --- a/webservice/src/main/java/org/openhds/webservice/resources/api2/SocialGroupResourceApi2.java +++ b/webservice/src/main/java/org/openhds/webservice/resources/api2/SocialGroupResourceApi2.java @@ -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; @@ -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; @@ -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 pushUpdate(@RequestBody SocialGroups socialGroups) throws Exception { long lastClientUpdate = socialGroups.getTimestamp(); long time = new Date().getTime(); - ConstraintViolations cv = new ConstraintViolations(); - + List errors = new ArrayList(); 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 violations = new ArrayList(); group.setCollectedBy(fieldBuilder.referenceField(group.getCollectedBy(), cv)); group.getGroupHead().setCollectedBy(fieldBuilder.referenceField(group.getGroupHead().getCollectedBy(), cv)); @@ -90,15 +97,26 @@ public ResponseEntity 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(sync, HttpStatus.ACCEPTED); } public void update(SocialGroup socialGroup, long currentTimestamp, long lastClientUpdate) throws Exception { @@ -137,25 +155,76 @@ else if(socialGroup.isDeleted()) { } @RequestMapping(method = RequestMethod.POST, produces = "application/json") - public ResponseEntity insert(@RequestBody SocialGroup socialGroup) { + public ResponseEntity insert(@RequestBody SocialGroup socialGroup) { + List errors = new ArrayList(); + ConstraintViolations cv = new ConstraintViolations(); + SynchronizationError err = new SynchronizationError(); + err.setEntityType("socialGroup"); + err.setEntityId(socialGroup.getExtId()); + err.setFieldworkerExtId(socialGroup.getCollectedBy().getExtId()); + List violations = new ArrayList(); + 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(new WebServiceCallException(cv), HttpStatus.BAD_REQUEST); + if(cv.hasViolations()) { + violations.addAll(cv.getViolations()); } try { socialGroupService.createSocialGroup(socialGroup, null); } catch (ConstraintViolations e) { - return new ResponseEntity(new WebServiceCallException(e), HttpStatus.BAD_REQUEST); + violations.addAll(e.getViolations()); } return new ResponseEntity(ShallowCopier.copySocialGroup(socialGroup), HttpStatus.CREATED); } + + @RequestMapping(value = "/bulkInsert", method = RequestMethod.POST, produces = "application/json") + public ResponseEntity bulkInsert(@RequestBody SocialGroups socialGroups) { + List errors = new ArrayList(); + + 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 violations = new ArrayList(); + + 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(sync, HttpStatus.ACCEPTED); + + } + @RequestMapping(method = RequestMethod.GET, value = "/pull/{timestamp}", produces = "application/json") public ResponseEntity getUpdatedSocialGroups(@PathVariable long timestamp) { diff --git a/webservice/src/main/java/org/openhds/webservice/util/SynchronizationError.java b/webservice/src/main/java/org/openhds/webservice/util/SynchronizationError.java index 4323fbe5..6b619ab8 100644 --- a/webservice/src/main/java/org/openhds/webservice/util/SynchronizationError.java +++ b/webservice/src/main/java/org/openhds/webservice/util/SynchronizationError.java @@ -2,8 +2,6 @@ import java.util.List; -import org.openhds.controller.exception.*; - public class SynchronizationError { private String entityType; private String fieldworkerExtId;