-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Thiru & Kumaran | #985 | [This is a temporary commit and will be reve…
…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
Showing
7 changed files
with
322 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
drishti-interface/src/main/java/org/ei/drishti/dto/register/EntityDetailDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
drishti-mother-child/src/main/java/org/ei/drishti/domain/EntityDetail.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
drishti-mother-child/src/main/java/org/ei/drishti/service/EntitiesService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
drishti-web/src/main/java/org/ei/drishti/web/controller/EntitiesController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |