Skip to content

Commit

Permalink
Thiru & Kumaran | #985 | [This is a temporary commit and will be reve…
Browse files Browse the repository at this point in the history
…rted] Created Migration for fetching all entities in the system as JSON
  • Loading branch information
Kumaran Venkataraman committed May 21, 2014
1 parent 50b4c6a commit c55b89d
Show file tree
Hide file tree
Showing 7 changed files with 322 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package org.ei.drishti.dto.register;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.codehaus.jackson.annotate.JsonProperty;
import org.ei.drishti.dto.LocationDTO;

public class EntityDetailDTO {
@JsonProperty
private String entityId;
@JsonProperty
private String entityType;
@JsonProperty
private String thayiCardNumber;
@JsonProperty
private String ecNumber;
@JsonProperty
private String anmIdentifier;

public String anmIdentifier() {
return this.anmIdentifier;
}
public String entityID() {
return this.entityId;
}

public String entityType() {
return this.entityType;
}

public String thayiCardNumber() {
return this.thayiCardNumber;
}

public String ecNumber() {
return this.ecNumber;
}

public EntityDetailDTO withThayiCardNumber(String thayiCardNumber) {
this.thayiCardNumber = thayiCardNumber;
return this;
}

public EntityDetailDTO withECNumber(String ecNumber) {
this.ecNumber = ecNumber;
return this;
}

public EntityDetailDTO withEntityID(String entityId) {
this.entityId = entityId;
return this;
}

public EntityDetailDTO withANMIdentifier(String anmIdentifier) {
this.anmIdentifier = anmIdentifier;
return this;
}

public EntityDetailDTO withEntityType(String type) {
this.entityType = type;
return this;
}
@Override
public boolean equals(Object o) {
return EqualsBuilder.reflectionEquals(this, o);
}

@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}

@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.ei.drishti.domain;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

public class EntityDetail {

private String entityId;
private String entityType;
private String thayiCardNumber;
private String ecNumber;
private String anmIdentifier;

public String anmIdentifier() {
return this.anmIdentifier;
}
public String entityID() {
return this.entityId;
}

public String entityType() {
return this.entityType;
}

public String thayiCardNumber() {
return this.thayiCardNumber;
}

public String ecNumber() {
return this.ecNumber;
}

public EntityDetail withThayiCardNumber(String thayiCardNumber) {
this.thayiCardNumber = thayiCardNumber;
return this;
}

public EntityDetail withECNumber(String ecNumber) {
this.ecNumber = ecNumber;
return this;
}

public EntityDetail withEntityID(String entityId) {
this.entityId = entityId;
return this;
}

public EntityDetail withANMIdentifier(String anmIdentifier) {
this.anmIdentifier = anmIdentifier;
return this;
}

public EntityDetail withEntityType(String type) {
this.entityType = type;
return this;
}

@Override
public boolean equals(Object o) {
return EqualsBuilder.reflectionEquals(this, o, "id", "revision");
}

@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this, "id", "revision");
}

@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,10 @@ public List<Child> findAllOpenChildrenForANM(String anmIdentifier) {
.key(anmIdentifier)
.includeDocs(true), Child.class);
}

@View(name = "all_children",
map = "function(doc) { if (doc.type === 'Child' && doc.isClosed === 'false') { emit(doc.caseId); }}")
public List<Child> all() {
return db.queryView(createQuery("all_children").includeDocs(true), Child.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,11 @@ public Map<String, Integer> fpCountForANM(List<String> anmIdentifiers) {
}
return fpCount;
}

@View(name = "all_ecs",
map = "function(doc) { if (doc.type === 'EligibleCouple' && doc.isClosed === 'false') { emit(doc.caseId); } }")
public List<EligibleCouple> all() {
return db.queryView(createQuery("all_ecs").includeDocs(true), EligibleCouple.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,12 @@ public List<Mother> findAllOpenPNCsForANM(String anmIdentifier) {
.includeDocs(true), Mother.class);

}

@View(name = "all_open_mothers",
map = "function(doc) { if (doc.type === 'Mother' && doc.isClosed === 'false') { emit(doc.caseId); } }")
public List<Mother> all() {
return db.queryView(createQuery("all_open_mothers")
.includeDocs(true), Mother.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.ei.drishti.service;

import org.ei.drishti.common.AllConstants;
import org.ei.drishti.domain.Child;
import org.ei.drishti.domain.EligibleCouple;
import org.ei.drishti.domain.EntityDetail;
import org.ei.drishti.domain.Mother;
import org.ei.drishti.repository.AllChildren;
import org.ei.drishti.repository.AllEligibleCouples;
import org.ei.drishti.repository.AllMothers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EntitiesService {
private static Logger logger = LoggerFactory.getLogger(EntitiesService.class.toString());
private final AllChildren allChildren;
private final AllMothers allMothers;
private final AllEligibleCouples allEligibleCouples;

@Autowired
public EntitiesService(AllEligibleCouples allEligibleCouples, AllMothers allMothers, AllChildren allChildren) {
this.allChildren = allChildren;
this.allMothers = allMothers;
this.allEligibleCouples = allEligibleCouples;
}

public List<EntityDetail> entities() {
List<EntityDetail> entityDetails = new ArrayList<>();
updateWithECDetails(entityDetails);
updateWithMotherDetails(entityDetails);
updateWithChildDetails(entityDetails);
return entityDetails;
}

private void updateWithChildDetails(List<EntityDetail> entityDetails) {
List<Child> children = allChildren.all();
for (Child child : children) {
EntityDetail entity = new EntityDetail()
.withEntityID(child.caseId())
.withECNumber(ecNumber(child))
.withANMIdentifier(child.anmIdentifier())
.withEntityType(AllConstants.FormEntityTypes.CHILD_TYPE)
.withThayiCardNumber(child.thayiCardNumber());
entityDetails.add(entity);
}
}

private String ecNumber(Child child) {
return ecNumber(allMothers.findByCaseId(child.motherCaseId()));
}

private String ecNumber(Mother mother) {
return allEligibleCouples.findByCaseId(mother.ecCaseId()).ecNumber();
}

private void updateWithMotherDetails(List<EntityDetail> entityDetails) {
List<Mother> mothers = allMothers.all();
for (Mother mother : mothers) {
EntityDetail entity = new EntityDetail()
.withEntityID(mother.caseId())
.withECNumber(ecNumber(mother))
.withANMIdentifier(mother.anmIdentifier())
.withEntityType(AllConstants.FormEntityTypes.MOTHER_TYPE)
.withThayiCardNumber(mother.thayiCardNumber());
entityDetails.add(entity);
}
}

private void updateWithECDetails(List<EntityDetail> entityDetails) {
List<EligibleCouple> allECs = allEligibleCouples.all();
for (EligibleCouple ec : allECs) {
EntityDetail entity = new EntityDetail()
.withEntityID(ec.caseId())
.withECNumber(ec.ecNumber())
.withANMIdentifier(ec.anmIdentifier())
.withEntityType(AllConstants.FormEntityTypes.ELIGIBLE_COUPLE_TYPE)
.withThayiCardNumber("");
entityDetails.add(entity);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.ei.drishti.web.controller;

import ch.lambdaj.function.convert.Converter;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.ei.drishti.domain.EntityDetail;
import org.ei.drishti.dto.register.EntityDetailDTO;
import org.ei.drishti.service.EntitiesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.MessageFormat;
import java.util.List;

import static ch.lambdaj.collection.LambdaCollections.with;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

@Controller
public class EntitiesController {
private static Logger logger = LoggerFactory.getLogger(EntitiesController.class.toString());
private EntitiesService service;

@Autowired
public EntitiesController(EntitiesService service) {
this.service = service;
}

@RequestMapping(method = GET, value = "/entities-as-json")
@ResponseBody
public ResponseEntity<List<EntityDetailDTO>> allEntities() {
try {
List<EntityDetail> entityDetails = service.entities();
logger.info("JSON map of all entities");
return new ResponseEntity<>(mapToDTO(entityDetails), HttpStatus.OK);
} catch (Exception exception) {
logger.error(MessageFormat.format("{0} occurred while fetching ANM Details. StackTrace: \n {1}", exception.getMessage(), ExceptionUtils.getFullStackTrace(exception)));
}
return new ResponseEntity<>(INTERNAL_SERVER_ERROR);
}

private List<EntityDetailDTO> mapToDTO(List<EntityDetail> entityDetails) {
List<EntityDetailDTO> entityDetailsDTO =
with(entityDetails)
.convert(new Converter<EntityDetail, EntityDetailDTO>() {
@Override
public EntityDetailDTO convert(EntityDetail entry) {
return new EntityDetailDTO()
.withECNumber(entry.ecNumber())
.withThayiCardNumber(entry.thayiCardNumber())
.withEntityID(entry.entityID())
.withANMIdentifier(entry.anmIdentifier())
.withEntityType(entry.entityType());
}
});
return entityDetailsDTO;
}
}

0 comments on commit c55b89d

Please sign in to comment.