Skip to content

Commit

Permalink
avniproject/avni-media#171 | Support media search by subject name
Browse files Browse the repository at this point in the history
  • Loading branch information
1t5j0y committed Apr 15, 2024
1 parent 6bfa6ba commit 64db3e1
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/avniproject/etl/dto/ImageData.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public record ImageData(
String uuid,
String subjectFirstName,
String subjectLastName,
String subjectMiddleName,
String url,
String conceptName,
String subjectTypeName,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/avniproject/etl/dto/MediaDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

import java.net.URL;

public record MediaDTO(String uuid, String subjectFirstName, String subjectLastName, String url, String conceptName, URL signedUrl, String thumbnailUrl, URL signedThumbnailUrl, String subjectTypeName, String programEnrolment, String encounterTypeName, String lastModifiedDateTime, String createdDateTime, String syncConcept1Name, String syncConcept2Name, String syncParameterValue1, String syncParameterValue2, Object address, long entityId){
public record MediaDTO(String uuid, String subjectFirstName, String subjectLastName, String subjectMiddleName, String url, String conceptName, URL signedUrl, String thumbnailUrl, URL signedThumbnailUrl, String subjectTypeName, String programEnrolment, String encounterTypeName, String lastModifiedDateTime, String createdDateTime, String syncConcept1Name, String syncConcept2Name, String syncParameterValue1, String syncParameterValue2, Object address, long entityId){
}
32 changes: 25 additions & 7 deletions src/main/java/org/avniproject/etl/dto/MediaSearchRequest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.avniproject.etl.dto;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;

public final class MediaSearchRequest {
Expand All @@ -16,6 +13,8 @@ public final class MediaSearchRequest {
private List<ConceptFilter> conceptFilters = new ArrayList<>();
private Date fromDate;
private Date toDate;
private String subjectName;
private List<String> subjectNameTokens = new ArrayList<>();

public MediaSearchRequest() {
}
Expand Down Expand Up @@ -96,6 +95,23 @@ public void setToDate(Date toDate) {
this.toDate = toDate;
}

public String getSubjectName() {
return subjectName;
}

public void setSubjectName(String subjectName) {
this.subjectName = subjectName;
this.setSubjectNameTokens(subjectName == null ? null : Arrays.stream(subjectName.split(" ")).toList());
}

public List<String> getSubjectNameTokens() {
return subjectNameTokens;
}

private void setSubjectNameTokens(List<String> subjectNameTokens) {
this.subjectNameTokens = subjectNameTokens;
}

@Override
public boolean equals(Object obj) {
if (obj == this) return true;
Expand All @@ -108,12 +124,13 @@ public boolean equals(Object obj) {
Objects.equals(this.syncValues, that.syncValues) &&
Objects.equals(this.fromDate, that.fromDate) &&
Objects.equals(this.toDate, that.toDate) &&
Objects.equals(this.conceptFilters, that.conceptFilters);
Objects.equals(this.conceptFilters, that.conceptFilters) &&
Objects.equals(this.subjectName, that.subjectName);
}

@Override
public int hashCode() {
return Objects.hash(subjectTypeNames, programNames, encounterTypeNames, imageConcepts, syncValues, fromDate, toDate, conceptFilters);
return Objects.hash(subjectTypeNames, programNames, encounterTypeNames, imageConcepts, syncValues, fromDate, toDate, conceptFilters, subjectName);
}

@Override
Expand All @@ -126,7 +143,8 @@ public String toString() {
"syncValues=" + syncValues + ", " +
"conceptFilters=" + conceptFilters + ", " +
"fromDate=" + fromDate + ", " +
"toDate=" + toDate + ']';
"toDate=" + toDate + ", " +
"subjectName=" + subjectName + ']';
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public List<Column> columns() {
.withColumn(new Column("entity_id", Column.Type.integer))
.withColumn(new Column("subject_first_name", Column.Type.text))
.withColumn(new Column("subject_last_name", Column.Type.text))
.withColumn(new Column("subject_middle_name", Column.Type.text))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public MediaDTO setMediaDto(ResultSet rs) {
compositeUUID,
rs.getString("subject_first_name"),
rs.getString("subject_last_name"),
rs.getString("subject_middle_name"),
imageUrl,
rs.getString("concept_name"),
signedImageUrl,
Expand Down Expand Up @@ -72,6 +73,7 @@ public ImageData setImageData(ResultSet rs) {
return new ImageData(compositeImageUUID,
rs.getString("subject_first_name"),
rs.getString("subject_last_name"),
rs.getString("subject_middle_name"),
imageUrl,
rs.getString("concept_name"),
rs.getString("subject_type_name"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import org.springframework.util.CollectionUtils;
import org.stringtemplate.v4.ST;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;

import static org.avniproject.etl.repository.sql.SqlFile.readFile;
Expand Down Expand Up @@ -46,6 +43,8 @@ private void addParameters(MediaSearchRequest request) {
addParameter("imageConcepts", getConceptNames(request.getImageConcepts()));
addParameter("fromDate", request.getFromDate());
addParameter("toDate", request.getToDate());
addParameter("subjectName", request.getSubjectName());
addParameter("subjectNameTokens", request.getSubjectNameTokens());

List<AddressRequest> addressRequests = request.getAddresses();
for (int index = 0; index < addressRequests.size(); index++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ private void syncNewerRows(TableMetadata mediaTableMetadata, TableMetadata table
.add("syncRegistrationConcept2Name", wrapStringValue(syncRegistrationConcepts[1].getName()))
.add("syncRegistrationConcept2ColumnName", wrapInQuotes(syncRegistrationConcepts[1].getColumnName()));
}
if (tableMetadata.getType().equals(TableMetadata.Type.Person) && tableMetadata.hasColumn("middle_name")) {
template = template
.add("hasMiddleName", true);
}

String sql = template.render();

Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/sql/api/searchMedia.sql.st
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ SELECT media.id as id,
media.uuid as uuid,
media.subject_first_name as subject_first_name,
media.subject_last_name as subject_last_name,
media.subject_middle_name as subject_middle_name,
media.is_voided as is_voided,
media.created_by_id as created_by_id,
media.last_modified_by_id as last_modified_by_id,
Expand Down Expand Up @@ -38,6 +39,10 @@ where media.image_url is not null
and media.is_voided is false
<if(request.fromDate)> and media.created_date_time >= :fromDate <endif>
<if(request.toDate)> and media.created_date_time \<= :toDate <endif>
<if(request.subjectName)> and (<request.subjectNameTokens : {subjectNameToken |
(media.subject_first_name ilike '%<subjectNameToken>%' or media.subject_middle_name ilike '%<subjectNameToken>%' or media.subject_last_name ilike '%<subjectNameToken>%')
}; separator="\n OR ">)
<endif>
<if(request.subjectTypeNames)> and media.subject_type_name in (:subjectTypeNames) <endif>
<if(request.programNames)> and media.program_name in (:programNames) <endif>
<if(request.encounterTypeNames)> and media.encounter_type_name in (:encounterTypeNames) <endif>
Expand Down
9 changes: 7 additions & 2 deletions src/main/resources/sql/etl/media.sql.st
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ insert into <schemaName>.<tableName> (entity_id, uuid, is_voided, created_by_id,
address_id, image_url, sync_parameter_key1, sync_parameter_value1,
sync_parameter_key2,
sync_parameter_value2, subject_type_name, encounter_type_name, program_name,
concept_name, subject_first_name, subject_last_name)
concept_name, subject_first_name, subject_last_name, subject_middle_name)
select entity.id,
entity.uuid,
entity.is_voided,
Expand Down Expand Up @@ -31,7 +31,12 @@ select entity.id,
<programName>,
<conceptName>,
subject.first_name,
subject.last_name
subject.last_name,
<if(hasMiddleName)>
subject.middle_name
<else>
null
<endif>
from <schemaName>.<fromTableName> entity
inner join <schemaName>.<subjectTableName> subject on entity.<individualId> = subject.id
where entity.last_modified_date_time > '<startTime>'
Expand Down

0 comments on commit 64db3e1

Please sign in to comment.