Skip to content

Commit

Permalink
Social Group Synchronization
Browse files Browse the repository at this point in the history
Synchronization of social groups to the server. Data is sent from the
client. Existing social groups are updated accordingly, and new ones are
created and stored. (#9)
  • Loading branch information
littlefieldnick committed Aug 17, 2018
1 parent 54ba871 commit 06d17ad
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,8 @@ public interface SocialGroupService {
List<SocialGroup> getAllSocialGroupsInRange(int i, int pageSize);

@Authorized({PrivilegeConstants.VIEW_ENTITY})
long getTotalSocialGroupCount();
long getTotalSocialGroupCount();

@Authorized({PrivilegeConstants.EDIT_ENTITY})
void updateSocialGroup(SocialGroup socialGroup) throws ConstraintViolations;
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ public void createSocialGroup(SocialGroup socialGroup, Location location) throws
throw new ConstraintViolations("There was a problem saving the social group to the database");
}
}

@Override
public void updateSocialGroup(SocialGroup socialGroup) throws ConstraintViolations {
try {
this.service.save(socialGroup);
} catch (IllegalArgumentException e) {
}
catch (SQLException e) {
throw new ConstraintViolations("There was a problem saving the social group to the database");
}

}

private void assignId(SocialGroup socialGroup, Location location) throws ConstraintViolations {
String id = socialGroup.getExtId() == null ? "" : socialGroup.getExtId();
Expand All @@ -205,4 +217,6 @@ public long getTotalSocialGroupCount() {
return genericDao.getTotalCount(SocialGroup.class);
}



}
19 changes: 19 additions & 0 deletions domain/src/main/java/org/openhds/domain/model/SocialGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public class SocialGroup
}, mappedBy = "socialGroup")
@Description(description = "The set of all memberships of the social group.")
private Set<Membership> memberships;

private long serverUpdateTime;
private long serverInsertTime;


@PostRemove
Expand Down Expand Up @@ -103,5 +106,21 @@ public void setMemberships(Set<Membership> list) {
memberships = list;
}

public long getServerUpdateTime() {
return serverUpdateTime;
}

public void setServerUpdateTime(long serverUpdateTime) {
this.serverUpdateTime = serverUpdateTime;
}

public long getServerInsertTime() {
return serverInsertTime;
}

public void setServerInsertTime(long serverInsertTime) {
this.serverInsertTime = serverInsertTime;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
public class SocialGroups {

private List<SocialGroup> socialGroups;
private long updateTimestamp;

@XmlElement(name = "socialgroup")
public List<SocialGroup> getSocialGroups() {
Expand All @@ -21,4 +22,12 @@ public void setSocialGroups(List<SocialGroup> socialGroups) {
this.socialGroups = socialGroups;
}

public long getTimestamp() {
return updateTimestamp;
}

public void setTimestamp(long timestamp) {
this.updateTimestamp = timestamp;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

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;
import org.openhds.domain.model.wrappers.SocialGroups;
import org.openhds.domain.util.JsonShallowCopier;
import org.openhds.domain.util.ShallowCopier;
Expand All @@ -23,6 +29,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
Expand All @@ -31,74 +38,172 @@
@Controller
@RequestMapping("/socialgroups2")
public class SocialGroupResourceApi2 {
private static final Logger logger = LoggerFactory.getLogger(SocialGroupResourceApi2.class);

private SocialGroupService socialGroupService;
private FieldBuilder fieldBuilder;
private FileResolver fileResolver;

@Autowired
public SocialGroupResourceApi2(SocialGroupService socialGroupService, FieldBuilder fieldBuilder,
FileResolver fileResolver) {
this.socialGroupService = socialGroupService;
this.fieldBuilder = fieldBuilder;
this.fileResolver = fileResolver;
}

@RequestMapping(method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public SocialGroups getAllSocialGroups() {
List<SocialGroup> allSocialGroups = socialGroupService.getAllSocialGroups();
List<SocialGroup> copies = new ArrayList<SocialGroup>();

for (SocialGroup sg : allSocialGroups) {
SocialGroup copy = JsonShallowCopier.copySocialGroup(sg);
copies.add(copy);
}

SocialGroups sgs = new SocialGroups();
sgs.setSocialGroups(copies);

return sgs;
}

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

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);
}

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

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

@RequestMapping(value = "/cached", method = RequestMethod.GET)
public void getCachedSocialGroups(HttpServletResponse response) {
try {
CacheResponseWriter.writeResponse(fileResolver.resolvesocialGroupXmlFile(), response);
} catch (IOException e) {
logger.error("Problem writing social group xml file: " + e.getMessage());
}
}

@RequestMapping(value = "/zipped", method = RequestMethod.GET)
public void getZippedSocialGroups(HttpServletResponse response) {
try {
CacheResponseWriter.writeResponse(fileResolver.resolvesocialGroupZipFile(), response);
} catch (IOException e) {
logger.error("Problem writing social group zip file: " + e.getMessage());
}
}
private static final Logger logger = LoggerFactory.getLogger(SocialGroupResourceApi2.class);

private SocialGroupService socialGroupService;
private IndividualService individualService;
private FieldBuilder fieldBuilder;
private FileResolver fileResolver;

@Autowired
public SocialGroupResourceApi2(SocialGroupService socialGroupService, FieldBuilder fieldBuilder,
FileResolver fileResolver) {
this.socialGroupService = socialGroupService;
this.fieldBuilder = fieldBuilder;
this.fileResolver = fileResolver;
}

@RequestMapping(method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public SocialGroups getAllSocialGroups() {
List<SocialGroup> allSocialGroups = socialGroupService.getAllSocialGroups();
List<SocialGroup> copies = new ArrayList<SocialGroup>();

for (SocialGroup sg : allSocialGroups) {
SocialGroup copy = JsonShallowCopier.copySocialGroup(sg);
copies.add(copy);
}

SocialGroups sgs = new SocialGroups();
sgs.setSocialGroups(copies);
sgs.setTimestamp(new Date().getTime());

return sgs;
}

@RequestMapping(value="/pushUpdates", 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();

for(SocialGroup group: socialGroups.getSocialGroups()) {
try {
group.setCollectedBy(fieldBuilder.referenceField(group.getCollectedBy(), cv));
boolean indivExists = false;
Individual ind = null;

ind =fieldBuilder.referenceField(group.getGroupHead(), cv, "Individual doesn't exist");
if(ind != null) {
indivExists = true;
}


if(!indivExists) {
this.individualService.createIndividual(group.getGroupHead());
}

group.setGroupHead(fieldBuilder.referenceField(group.getGroupHead(), cv, "Group head doesn't exist."));
group.setServerUpdateTime(new Date().getTime());
this.update(group, lastClientUpdate, time);
} catch(ConstraintViolations e){
return new ResponseEntity<WebServiceCallException>(new WebServiceCallException(e), HttpStatus.EXPECTATION_FAILED);
}
}


return new ResponseEntity<>(time, HttpStatus.ACCEPTED);
}

public void update(SocialGroup socialGroup, long currentTimestamp, long lastClientUpdate) throws Exception {

boolean doesExist = false;
SocialGroup sg = null;
try {
sg = socialGroupService.findSocialGroupById(socialGroup.getExtId(), "");
if(sg != null) {
doesExist = true;
}
} catch(Exception e) {

}

if(!doesExist) {
this.insert(socialGroup);
}
else if(doesExist){
//Set UUID to prevent assignment of a new one.
socialGroup.setUuid(socialGroupService.findSocialGroupById(socialGroup.getExtId(),"").getUuid());
if(lastClientUpdate > currentTimestamp) {
//Convert timestamp to calendar instance
Calendar c = Calendar.getInstance();
c.setTimeInMillis(currentTimestamp);
socialGroup.setInsertDate(c);
}
this.socialGroupService.updateSocialGroup(socialGroup);
}
else if(socialGroup.isDeleted()) {
if(socialGroup.getUuid() == null)
throw new ConstraintViolations("The social group uuid is null.");

this.socialGroupService.updateSocialGroup(socialGroup);
}
}

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

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);
}

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

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

@RequestMapping(method = RequestMethod.GET, value = "/pull/{timestamp}", produces = "application/json")
public ResponseEntity<SocialGroups> getUpdatedLocations(@PathVariable long timestamp) {
long time = new Date().getTime();

List<SocialGroup> socialGroup = socialGroupService.getAllSocialGroups();
List<SocialGroup> copies = new ArrayList<SocialGroup>(socialGroup.size());

for (SocialGroup group : socialGroup) {
long compTime;
if(group.getServerUpdateTime() == 0)
compTime = group.getServerInsertTime();
else
compTime = group.getServerUpdateTime();

if(timestamp <= compTime && timestamp < time) {
copies.add(JsonShallowCopier.copySocialGroup(group));
}
}
SocialGroups all = new SocialGroups();
ArrayList<SocialGroup> allSocialGroups = new ArrayList<SocialGroup>();
allSocialGroups.addAll(copies);
all.setSocialGroups(allSocialGroups);
all.setTimestamp(time);
return new ResponseEntity<SocialGroups>(all, HttpStatus.ACCEPTED);
}

@RequestMapping(value = "/cached", method = RequestMethod.GET)
public void getCachedSocialGroups(HttpServletResponse response) {
try {
CacheResponseWriter.writeResponse(fileResolver.resolvesocialGroupXmlFile(), response);
} catch (IOException e) {
logger.error("Problem writing social group xml file: " + e.getMessage());
}
}

@RequestMapping(value = "/zipped", method = RequestMethod.GET)
public void getZippedSocialGroups(HttpServletResponse response) {
try {
CacheResponseWriter.writeResponse(fileResolver.resolvesocialGroupZipFile(), response);
} catch (IOException e) {
logger.error("Problem writing social group zip file: " + e.getMessage());
}
}

}

0 comments on commit 06d17ad

Please sign in to comment.