Skip to content

Commit

Permalink
avniproject/avni-media#152 | Introduce end-point to create a download…
Browse files Browse the repository at this point in the history
… request on media server

for all images matching a search criterion
  • Loading branch information
Suhas Vishwanath committed Sep 21, 2023
1 parent 934b5d0 commit 3ac31e4
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 8 deletions.
17 changes: 17 additions & 0 deletions src/main/java/org/avniproject/etl/controller/MediaController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import jakarta.servlet.http.HttpServletRequest;
import jakarta.websocket.server.PathParam;
import org.apache.log4j.Logger;
import org.avniproject.etl.dto.DownloadAllMediaRequest;
import org.avniproject.etl.dto.DownloadRequest;
import org.avniproject.etl.dto.MediaSearchRequest;
import org.avniproject.etl.repository.sql.Page;
import org.avniproject.etl.service.MediaService;
Expand All @@ -20,6 +23,8 @@ public class MediaController {
this.mediaService = mediaService;
}

private static final Logger log = Logger.getLogger(MediaController.class);

@PreAuthorize("hasAnyAuthority('analytics_user')")
@GetMapping("/media")
public ResponseEntity list(HttpServletRequest request,
Expand All @@ -43,4 +48,16 @@ public ResponseEntity search(HttpServletRequest request,
return ResponseEntity.badRequest().body(exception.getMessage());
}
}

@PreAuthorize("hasAnyAuthority('analytics_user')")
@PostMapping("/requestDownloadAll")
public ResponseEntity downloadAllImages(@RequestBody(required = true) DownloadAllMediaRequest downloadAllMediaRequest) {
try {
mediaService.createDownloadRequest(downloadAllMediaRequest);
return ResponseEntity.ok().body("");
} catch (Exception exception) {
log.error("Error reading object: ", exception);
return ResponseEntity.internalServerError().body(exception.getMessage());
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/org/avniproject/etl/dto/AddressLevelType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.avniproject.etl.dto;

public record AddressLevelType(String uuid, long id, String name, int level) {
}
49 changes: 49 additions & 0 deletions src/main/java/org/avniproject/etl/dto/DownloadAllMediaRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.avniproject.etl.dto;

import java.util.List;

public class DownloadAllMediaRequest {
private MediaSearchRequest mediaSearchRequest;
private String username;
private String description;
private List<AddressLevelType> addressLevelTypes;

public DownloadAllMediaRequest (MediaSearchRequest mediaSearchRequest, String username, String description, List<AddressLevelType> addressLevelTypes){
this.mediaSearchRequest = mediaSearchRequest;
this.username = username;
this.description = description;
this.addressLevelTypes = addressLevelTypes;
}

public void setMediaSearchRequest(MediaSearchRequest mediaSearchRequest) {
this.mediaSearchRequest = mediaSearchRequest;
}

public void setUsername(String username) {
this.username = username;
}

public void setDescription(String description) {
this.description = description;
}

public void setAddressLevelTypes(List<AddressLevelType> addressLevelTypes) {
this.addressLevelTypes = addressLevelTypes;
}

public MediaSearchRequest getMediaSearchRequest() {
return (mediaSearchRequest == null) ? new MediaSearchRequest(): mediaSearchRequest;
}

public String getUsername() {
return username;
}

public String getDescription() {
return description;
}

public List<AddressLevelType> getAddressLevelTypes() {
return addressLevelTypes;
}
}
9 changes: 9 additions & 0 deletions src/main/java/org/avniproject/etl/dto/DownloadRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.avniproject.etl.dto;

import java.util.List;

public record DownloadRequest(String username,
String description,
List<AddressLevelType> addressLevelTypes,
List<ImageData> data) {
}
17 changes: 17 additions & 0 deletions src/main/java/org/avniproject/etl/dto/ImageData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.avniproject.etl.dto;

public record ImageData(
String uuid,
String subjectFirstName,
String subjectLastName,
String url,
String conceptName,
String subjectTypeName,
String programEnrolment,
String encounterTypeName,
String syncConcept1Name,
String syncConcept2Name,
String syncParameterValue1,
String syncParameterValue2,
String address) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import org.avniproject.etl.domain.metadata.ColumnMetadata;
import org.avniproject.etl.domain.metadata.SchemaMetadata;
import org.avniproject.etl.domain.metadata.TableMetadata;
import org.avniproject.etl.dto.ConceptFilter;
import org.avniproject.etl.dto.ConceptFilterSearch;
import org.avniproject.etl.dto.MediaDTO;
import org.avniproject.etl.dto.MediaSearchRequest;
import org.avniproject.etl.dto.*;
import org.avniproject.etl.repository.service.MediaTableRepositoryService;
import org.avniproject.etl.repository.sql.MediaSearchQueryBuilder;
import org.avniproject.etl.repository.sql.Page;
Expand All @@ -15,6 +12,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

Expand Down Expand Up @@ -78,6 +76,10 @@ private List<ConceptFilterSearch> determineConceptFilterTablesAndColumns(List<Co
}

public List<MediaDTO> search(MediaSearchRequest mediaSearchRequest, Page page) {
return searchInternal(mediaSearchRequest, page, (rs, rowNum) -> mediaTableRepositoryService.setMediaDto(rs));
}

private <T> List<T> searchInternal(MediaSearchRequest mediaSearchRequest, Page page, RowMapper<T> rowMapper) {
List<ConceptFilterSearch> conceptFilterSearches = null;
if (mediaSearchRequest.getConceptFilters() != null) {
conceptFilterSearches = determineConceptFilterTablesAndColumns(mediaSearchRequest.getConceptFilters());
Expand All @@ -89,7 +91,12 @@ public List<MediaDTO> search(MediaSearchRequest mediaSearchRequest, Page page) {
.withSearchConceptFilters(conceptFilterSearches)
.build();
return runInSchemaUserContext(() -> new NamedParameterJdbcTemplate(jdbcTemplate)
.query(query.sql(), query.parameters(),
(rs, rowNum) -> mediaTableRepositoryService.setMediaDto(rs)), jdbcTemplate);
.query(query.sql(), query.parameters(), rowMapper), jdbcTemplate);
}



public List<ImageData> getImageData(MediaSearchRequest mediaSearchRequest, Page page) {
return searchInternal(mediaSearchRequest, page, (rs, rowNum) -> mediaTableRepositoryService.setImageData(rs));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.avniproject.etl.repository.service;

import org.avniproject.etl.config.AmazonClientService;
import org.avniproject.etl.dto.ImageData;
import org.avniproject.etl.dto.MediaDTO;
import org.avniproject.etl.util.Utils;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -49,4 +50,24 @@ public MediaDTO setMediaDto(ResultSet rs) {
throw new Error("Error:" + e.getMessage());
}
}

public ImageData setImageData(ResultSet rs) {
try {
return new ImageData(rs.getString("uuid"),
rs.getString("subject_first_name"),
rs.getString("subject_last_name"),
rs.getString("image_url"),
rs.getString("concept_name"),
rs.getString("subject_type_name"),
rs.getString("program_name"),
rs.getString("encounter_type_name"),
rs.getString("sync_parameter_key1"),
rs.getString("sync_parameter_key2"),
rs.getString("sync_parameter_value1"),
rs.getString("sync_parameter_value2"),
rs.getString("address"));
}catch(SQLException e) {
throw new Error("Error:" + e.getMessage());
}
}
}
29 changes: 27 additions & 2 deletions src/main/java/org/avniproject/etl/service/MediaService.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
package org.avniproject.etl.service;

import org.avniproject.etl.dto.MediaSearchRequest;
import org.avniproject.etl.dto.ResponseDTO;
import org.avniproject.etl.dto.*;
import org.avniproject.etl.repository.AddressRepository;
import org.avniproject.etl.repository.MediaTableRepository;
import org.avniproject.etl.repository.sql.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Service
public class MediaService {

private final MediaTableRepository mediaTableRepository;
private final AddressRepository addressRepository;
private final RestTemplate restTemplate;

@Value("${media.server.downloadRequestURL}")
private String mediaServerDownloadRequestURL;

@Autowired
public MediaService(MediaTableRepository mediaTableRepository, AddressRepository addressRepository) {
this.mediaTableRepository = mediaTableRepository;
this.addressRepository = addressRepository;
this.restTemplate = new RestTemplate();
}

@Transactional (readOnly = true)
Expand All @@ -29,4 +38,20 @@ public ResponseDTO search(MediaSearchRequest mediaSearchRequest, Page page) {

throw new IllegalArgumentException("Address level type names are incorrect");
}

public void createDownloadRequest(DownloadAllMediaRequest downloadAllMediaRequest) {
MediaSearchRequest mediaSearchRequest = downloadAllMediaRequest.getMediaSearchRequest();
if (addressRepository.doAllAddressLevelTypeNamesExist(mediaSearchRequest.getAddressLevelTypes())) {
Page page = new Page(0, 1000);
List<ImageData> imageData = mediaTableRepository.getImageData(mediaSearchRequest, page);
DownloadRequest downloadRequest = new DownloadRequest(downloadAllMediaRequest.getUsername(),
downloadAllMediaRequest.getDescription(),
downloadAllMediaRequest.getAddressLevelTypes(),
imageData);
HttpEntity<DownloadRequest> request = new HttpEntity<>(downloadRequest);
restTemplate.postForLocation(mediaServerDownloadRequestURL, request);
return;
}
throw new IllegalArgumentException("Address level type names are incorrect");
}
}
3 changes: 3 additions & 0 deletions src/main/resources/main-application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@ spring.security.user.name=admin
spring.security.user.password=secret

avni.defaultUserName=${OPENCHS_USER_NAME:admin}

#MediaServer
media.server.downloadRequestURL=${AVNI_MEDIA_SERVER_DOWNLOAD_URL:http://localhost:3010/media-viewer/requestDownload}
2 changes: 2 additions & 0 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ avni.keycloak.realms=%s/realms/%s
avni.keycloak.realm=dummy

avni.idp.type=${AVNI_IDP_TYPE:none}

media.server.downloadRequestURL=dummy

0 comments on commit 3ac31e4

Please sign in to comment.