Skip to content

Commit

Permalink
Census Individual Synchronization
Browse files Browse the repository at this point in the history
Updates to synchronization of census individuals. Http Status errors
modified to return Bad Request when data is not sent correctly to the
server. Fieldworkers associated with collecting the information was
added. (#9)
  • Loading branch information
littlefieldnick committed Aug 17, 2018
1 parent 06d17ad commit ac0bbe2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.openhds.domain.model;

public class CensusIndividual {
String uuid;

String socialGroupExtId;

String socialGroupHeadExtId;

FieldWorker collectedBy;
String collectedBy;

String locationExtId;

Expand Down Expand Up @@ -39,11 +41,11 @@ public void setIndividual(Individual individual) {
this.individual = individual ;
}

public FieldWorker getCollectedBy() {
public String getCollectedBy() {
return collectedBy;
}

public void setCollectedBy(FieldWorker collectedBy) {
public void setCollectedBy(String collectedBy) {
this.collectedBy = collectedBy;
}

Expand All @@ -70,4 +72,12 @@ public Individual getSpouse() {
public void setSpouse(Individual spouse) {
this.spouse = spouse;
}

public String getUuid() {
return uuid;
}

public void setUuid(String uuid) {
this.uuid = uuid;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.openhds.controller.exception.ConstraintViolations;
import org.openhds.controller.service.BaselineService;
import org.openhds.controller.service.FieldWorkerService;
import org.openhds.controller.service.IndividualService;
import org.openhds.controller.service.LocationHierarchyService;
import org.openhds.controller.service.SocialGroupService;
Expand Down Expand Up @@ -36,16 +37,19 @@ public class CensusIndividualResource2 {
private SocialGroupService socialGroupService;
private IndividualService individualService;
private LocationHierarchyService locationService;
private FieldWorkerService fwService;
private final FieldBuilder fieldBuilder;

@Autowired
public CensusIndividualResource2(BaselineService baseline, FileResolver fileResolver,
SocialGroupService sg, IndividualService individual, LocationHierarchyService locService, FieldBuilder fieldBuilder) {
SocialGroupService sg, IndividualService individual, LocationHierarchyService locService,
FieldWorkerService fwService, FieldBuilder fieldBuilder) {

this.baselineService = baseline;
this.socialGroupService = sg;
this.individualService = individual;
this.locationService = locService;
this.fwService = fwService;
this.fieldBuilder = fieldBuilder;
}

Expand Down Expand Up @@ -74,16 +78,21 @@ public ResponseEntity<? extends Serializable> insert(@RequestBody Individual ind
@RequestMapping(method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
public ResponseEntity<? extends Serializable> insert(@RequestBody CensusIndividual ind) {
ConstraintViolations cv = new ConstraintViolations();

FieldWorker fw = null;
try {
fw = this.fwService.findFieldWorkerById(ind.getCollectedBy());
} catch (ConstraintViolations e1) {
return new ResponseEntity<FieldWorker>(fw, HttpStatus.BAD_GATEWAY);
}
SocialGroup social = null;
Individual individual = null;
if(ind.getSocialGroupExtId().isEmpty() || ind.getSocialGroupHeadExtId().isEmpty() || ind.getLocationExtId().isEmpty())
return new ResponseEntity<Individual>(JsonShallowCopier.shallowCopyIndividual(individual), HttpStatus.BAD_REQUEST);
return new ResponseEntity<Individual>(JsonShallowCopier.shallowCopyIndividual(individual), HttpStatus.BAD_GATEWAY);

individual = ind.getIndividual();

if(individual == null) {
return new ResponseEntity<Individual>(JsonShallowCopier.shallowCopyIndividual(individual), HttpStatus.BAD_REQUEST);
return new ResponseEntity<Individual>(JsonShallowCopier.shallowCopyIndividual(individual), HttpStatus.BAD_GATEWAY);
}

individual.setCollectedBy(fieldBuilder.referenceField(individual.getCollectedBy(), cv));
Expand All @@ -92,41 +101,41 @@ public ResponseEntity<? extends Serializable> insert(@RequestBody CensusIndividu


if(cv.hasViolations()) {
return new ResponseEntity<WebServiceCallException>(new WebServiceCallException(cv), HttpStatus.BAD_REQUEST);
return new ResponseEntity<WebServiceCallException>(new WebServiceCallException(cv), HttpStatus.BAD_GATEWAY);
}
try {
social = socialGroupService.findSocialGroupById(ind.getSocialGroupExtId(), "Social Group does not exist");
} catch (Exception e) {
cv.addViolations(e.getMessage());
cv.addViolations("Social group cannot be found.");
return new ResponseEntity<WebServiceCallException>(new WebServiceCallException(cv), HttpStatus.BAD_REQUEST);
return new ResponseEntity<WebServiceCallException>(new WebServiceCallException(cv), HttpStatus.BAD_GATEWAY);
}

Location location = locationService.findLocationById(ind.getLocationExtId());
Membership mem = createMembershipObject(individual, ind.getbIsToA(), individual.getCollectedBy(), social);
Membership mem = createMembershipObject(individual, ind.getbIsToA(), fw, social);
Relationship rel = null;

try {
if(ind.getSpouse() != null) {
rel = createRelationshipObject(individual, ind.getSpouse(), ind.getbIsToA(), individual.getCollectedBy());
rel = createRelationshipObject(individual, ind.getSpouse(), ind.getbIsToA(), fw);
Calendar cal = Calendar.getInstance();
cal.set(2018, 5, 5);
baselineService.createResidencyMembershipAndRelationshipForIndividual(individual, mem, rel, location, ind.getCollectedBy(), cal);
baselineService.createResidencyMembershipAndRelationshipForIndividual(individual, mem, rel, location, fw, cal);
} else
baselineService.createResidencyAndMembershipForIndividual(individual, mem, location, individual.getCollectedBy(), Calendar.getInstance());
baselineService.createResidencyAndMembershipForIndividual(individual, mem, location, fw, Calendar.getInstance());
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
cv.addViolations(e.getMessage());
return new ResponseEntity<WebServiceCallException>(new WebServiceCallException(cv), HttpStatus.BAD_REQUEST);
return new ResponseEntity<WebServiceCallException>(new WebServiceCallException(cv), HttpStatus.BAD_GATEWAY);
} catch (SQLException e) {
// TODO Auto-generated catch block
cv.addViolations(e.getMessage());
return new ResponseEntity<WebServiceCallException>(new WebServiceCallException(cv), HttpStatus.BAD_REQUEST);
return new ResponseEntity<WebServiceCallException>(new WebServiceCallException(cv), HttpStatus.BAD_GATEWAY);

} catch (ConstraintViolations e) {
// TODO Auto-generated catch block
cv.addViolations(e.getMessage());
return new ResponseEntity<WebServiceCallException>(new WebServiceCallException(cv), HttpStatus.BAD_REQUEST);
return new ResponseEntity<WebServiceCallException>(new WebServiceCallException(cv), HttpStatus.BAD_GATEWAY);
}
return new ResponseEntity<Individual>(JsonShallowCopier.shallowCopyIndividual(individual), HttpStatus.CREATED);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.openhds.controller.service.IndividualService;
import org.openhds.domain.model.Individual;
import org.openhds.domain.model.wrappers.Individuals;
import org.openhds.domain.util.JsonShallowCopier;
import org.openhds.domain.util.ShallowCopier;
import org.openhds.task.support.FileResolver;
import org.openhds.webservice.CacheResponseWriter;
Expand Down Expand Up @@ -44,7 +45,7 @@ public ResponseEntity<? extends Serializable> getIndividualById(@PathVariable St
return new ResponseEntity<String>("", HttpStatus.NOT_FOUND);
}

return new ResponseEntity<Individual>(ShallowCopier.shallowCopyIndividual(individual), HttpStatus.OK);
return new ResponseEntity<Individual>(JsonShallowCopier.shallowCopyIndividual(individual), HttpStatus.OK);
}

@RequestMapping(method = RequestMethod.GET, produces="application/json")
Expand All @@ -53,7 +54,7 @@ public Individuals getAllIndividuals() {
List<Individual> allIndividual = individualService.getAllIndividuals();
List<Individual> copies = new ArrayList<Individual>(allIndividual.size());
for (Individual individual : allIndividual) {
Individual copy = ShallowCopier.shallowCopyIndividual(individual);
Individual copy = JsonShallowCopier.shallowCopyIndividual(individual);
copies.add(copy);
}

Expand Down

0 comments on commit ac0bbe2

Please sign in to comment.