Skip to content

Commit

Permalink
Changed: Replace RequestBody Class with ImportPathBody, CreateProject…
Browse files Browse the repository at this point in the history
…Body, CsvDatasource, Csv DatasourceTable and changed ViewFactory to JSONFactory
  • Loading branch information
marlenaMyr committed Dec 25, 2024
1 parent f1d8428 commit 7204c22
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 83 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- OPAL CSV (replacement of quotes with escape charachters)
- Replaced Python commands in OpalEngine with WebClient request
- waitUntilTaskIsFinished Method in OpalEngine
- Replace RequestBody Class with ImportPathBody, CreateProjectBody, CsvDatasource, Csv DatasourceTable
- Changed ViewFactory to JSONFactory


### Removed
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/de/samply/opal/CsvToOpalConverter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.samply.opal;

import com.fasterxml.jackson.core.JsonProcessingException;
import de.samply.converter.Format;
import de.samply.converter.TargetConverterImpl;
import de.samply.exporter.ExporterConst;
Expand Down Expand Up @@ -55,6 +56,8 @@ private void sendPathToOpal(Path input, Session session) {
opalEngine.sendPathToOpal(input, session);
} catch (OpalEngineException e) {
logger.error(ExceptionUtils.getStackTrace(e));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

Expand All @@ -70,6 +73,8 @@ private void createProjectIfNotExists(Session session) {
opalEngine.createProjectIfNotExists(session);
} catch (OpalEngineException e) {
throw new RuntimeException(e);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import de.samply.exporter.ExporterConst;
import de.samply.opal.model.Attribute;
import de.samply.opal.model.MagmaView;
import de.samply.opal.model.Variable;
import de.samply.opal.model.View;
import de.samply.opal.model.*;
import de.samply.template.AttributeTemplate;
import de.samply.template.ContainerTemplate;

Expand All @@ -17,14 +14,25 @@
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class ViewFactory {

public class JSONFactory {
private final static ObjectMapper objectMapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT)
.registerModule(new JavaTimeModule());


public static String createViewAndSerializeAsJson(Session session, ContainerTemplate containerTemplate) throws JsonProcessingException {
return objectMapper.writeValueAsString(ViewFactory.createView(session, containerTemplate));
return objectMapper.writeValueAsString(JSONFactory.createView(session, containerTemplate));
}

public static String createBodyAndSerializeAsJson(Session session, ContainerTemplate containerTemplate, String data, String separator) throws JsonProcessingException {
return objectMapper.writeValueAsString(JSONFactory.createCSVDatasourceBody(session, containerTemplate, data, separator));
}

public static String createBodyAndSerializeAsJson(String projectName, String database) throws JsonProcessingException {
return objectMapper.writeValueAsString(JSONFactory.createProjectBody(projectName, database));
}

public static String createBodyAndSerializeAsJson(Session session, ContainerTemplate template, String transientUid) throws JsonProcessingException {
return objectMapper.writeValueAsString(JSONFactory.createPathBody(session, template, transientUid));
}

public static View createView(Session session, ContainerTemplate containerTemplate) {
Expand All @@ -35,6 +43,46 @@ public static View createView(Session session, ContainerTemplate containerTempla
return view;
}

public static CsvDatasource createCSVDatasourceBody(Session session, ContainerTemplate containerTemplate, String data, String separator) {
CsvDatasource body = new CsvDatasource();
CsvDatasource.Params params = new CsvDatasource.Params();
params.setCharacterSet("UTF-8");
params.setFirstRow(1);
params.setQuote("\"");
params.setSeparator(separator);
params.setDefaultValueType("text");
params.setTables(List.of(createCSVDatasourceTable(session, containerTemplate, data)));
body.setParams(params);
return body;
}

public static CreateProjectBody createProjectBody(String projectName, String database) {
CreateProjectBody body = new CreateProjectBody();
body.setName(projectName);
body.setTitle(projectName);
body.setDescription("");
body.setDatabase(database);
body.setVcfStoreService(null);
body.setExportFolder("");
return body;
}

public static ImportPathBody createPathBody(Session session, ContainerTemplate containerTemplate, String transientUid) {
ImportPathBody body = new ImportPathBody();
body.setDestination(session.fetchProject());
body.setTables(new String[]{transientUid + "." + containerTemplate.getOpalTable()});
return body;
}

public static CsvDatasourceTable createCSVDatasourceTable(Session session, ContainerTemplate containerTemplate, String data) {
CsvDatasourceTable table = new CsvDatasourceTable();
table.setName(containerTemplate.getOpalTable());
table.setData(data);
table.setEntityType(containerTemplate.getOpalEntityType());
table.setRefTable(session.fetchProject() + "." + containerTemplate.getOpalTable());
return table;
}

private static List<String> fetchViewFrom(Session session, ContainerTemplate containerTemplate) {
return Arrays.asList(session.fetchProject() + '.' + containerTemplate.getOpalTable());
}
Expand Down
52 changes: 12 additions & 40 deletions src/main/java/de/samply/opal/OpalEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public OpalEngine(OpalServer opalServer) {
this.webClient = opalServer.createWebClient();
}

public void sendPathToOpal(Path path, Session session) throws OpalEngineException {
public void sendPathToOpal(Path path, Session session) throws OpalEngineException, JsonProcessingException {
ContainerTemplate containerTemplate = session.getContainerTemplate(
path.getFileName().toString());
if (containerTemplate != null) {
Expand All @@ -44,7 +44,7 @@ public void sendPathToOpal(Path path, Session session) throws OpalEngineExceptio
}
}

public void createProjectIfNotExists(Session session) throws OpalEngineException {
public void createProjectIfNotExists(Session session) throws OpalEngineException, JsonProcessingException {
if (!existsProject(session)) {
createProject(session);
createProjectPermissions(session);
Expand All @@ -69,20 +69,13 @@ private boolean existsProject(Session session) {
.block());
}

private void createProject(Session session) {
private void createProject(Session session) throws JsonProcessingException {
String projectName = session.fetchProject();
logger.info("Create Project " + projectName);
webClient.post()
.uri(ExporterConst.OPAL_PROJECT_WS + ExporterConst.PROJECTS_OPAL)
.headers(headers -> headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.bodyValue("{\n" +
" \"name\": \"" + projectName + "\",\n" +
" \"title\": \"" + projectName + "\",\n" +
" \"description\": \"\",\n" +
" \"database\": \"" + opalServer.getDatabase() + "\",\n" +
" \"vcfStoreService\": null,\n" +
" \"exportFolder\": \"\"\n" +
"}")
.bodyValue(JSONFactory.createBodyAndSerializeAsJson(projectName, opalServer.getDatabase()))
.retrieve()
.onStatus(HttpStatusCode::is4xxClientError, this::handleError)
.onStatus(HttpStatusCode::is5xxServerError, this::handleError)
Expand Down Expand Up @@ -141,7 +134,7 @@ private void deletePath(Path path, Session session) {
logger.info("File successfully deleted.");
}

private String importPathTransient(Path path, Session session, ContainerTemplate template) throws OpalEngineException {
private String importPathTransient(Path path, Session session, ContainerTemplate template) throws OpalEngineException, JsonProcessingException {
logger.info("Import started for: " + template.getOpalTable() + " in Project " + session.fetchProject());
Map<String, Object> response = webClient.post()
.uri(uriBuilder -> uriBuilder
Expand All @@ -152,19 +145,9 @@ private String importPathTransient(Path path, Session session, ContainerTemplate
headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
headers.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE);
})
.bodyValue(new RequestBody(
"UTF-8",
1,
"\"",
normalizeSepartor(session.getConverterTemplate().getCsvSeparator()),
"text",
List.of(RequestBody.createTable(
template.getOpalTable(),
session.fetchOpalProjectDirectoryPath(opalServer.getFilesDirectory(), path),
template.getOpalEntityType(),
session.fetchProject() + "." + template.getOpalTable()
))
).getRequestMap())
.bodyValue(JSONFactory.createBodyAndSerializeAsJson(session, template,
session.fetchOpalProjectDirectoryPath(opalServer.getFilesDirectory(), path),
normalizeSepartor(session.getConverterTemplate().getCsvSeparator())))
.retrieve()
.onStatus(HttpStatusCode::is4xxClientError, this::handleError)
.onStatus(HttpStatusCode::is5xxServerError, this::handleError)
Expand All @@ -183,14 +166,11 @@ private String importPathTransient(Path path, Session session, ContainerTemplate
return transientUid;
}

private String importPath(Session session, ContainerTemplate template, String transientUid) {
private String importPath(Session session, ContainerTemplate template, String transientUid) throws JsonProcessingException {
return webClient.post()
.uri(ExporterConst.OPAL_PROJECT + session.fetchProject() + ExporterConst.OPAL_PROJECT_IMPORT)
.headers(headers -> headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE))
.bodyValue(Map.of(
"destination", session.fetchProject(),
"tables", new String[]{transientUid + "." + template.getOpalTable()}
))
.bodyValue(JSONFactory.createBodyAndSerializeAsJson(session, template, transientUid))
.retrieve()
.onStatus(HttpStatusCode::is4xxClientError, this::handleError)
.onStatus(HttpStatusCode::is5xxServerError, this::handleError)
Expand Down Expand Up @@ -253,13 +233,6 @@ public void waitUntilTaskIsFinished(String taskId) throws OpalEngineException {
}
}

private String extractErrorMessage(List<Map<String, Object>> messages) {
return messages.stream()
.map(msg -> (String) msg.get("msg"))
.reduce((first, second) -> first + "; " + second)
.orElse("No specific message found");
}

private void createView(Session session, ContainerTemplate containerTemplate)
throws OpalEngineException {
try {
Expand All @@ -270,7 +243,7 @@ private void createView(Session session, ContainerTemplate containerTemplate)
}

private void createViewWithoutExceptionHandling(Session session, ContainerTemplate containerTemplate) throws JsonProcessingException {
String view = ViewFactory.createViewAndSerializeAsJson(session, containerTemplate);
String view = JSONFactory.createViewAndSerializeAsJson(session, containerTemplate);
logger.info("Create View");
logger.info("Request body: " + view);
webClient.post()
Expand All @@ -285,7 +258,6 @@ private void createViewWithoutExceptionHandling(Session session, ContainerTempla
.onStatus(HttpStatusCode::is5xxServerError, this::handleError)
.bodyToMono(String.class)
.block();

}

private String normalizeSepartor(String separator) {
Expand All @@ -300,4 +272,4 @@ private Mono<? extends Throwable> handleError(ClientResponse clientResponse) {
});
}

}
}
36 changes: 0 additions & 36 deletions src/main/java/de/samply/opal/RequestBody.java

This file was deleted.

23 changes: 23 additions & 0 deletions src/main/java/de/samply/opal/model/CreateProjectBody.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package de.samply.opal.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CreateProjectBody {

@JsonProperty(value = "name")
private String name;
@JsonProperty(value = "title")
private String title;
@JsonProperty(value = "description")
private String description;
@JsonProperty(value = "database")
private String database;
@JsonProperty(value = "vcfStoreService")
private String vcfStoreService;
@JsonProperty(value = "exportFolder")
private String exportFolder;
}
37 changes: 37 additions & 0 deletions src/main/java/de/samply/opal/model/CsvDatasource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package de.samply.opal.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.util.List;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CsvDatasource {

@JsonProperty("Magma.CsvDatasourceFactoryDto.params")
private Params params;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public static class Params {
@JsonProperty("characterSet")
private String characterSet;

@JsonProperty("firstRow")
private int firstRow;

@JsonProperty("quote")
private String quote;

@JsonProperty("separator")
private String separator;

@JsonProperty("defaultValueType")
private String defaultValueType;

@JsonProperty("tables")
private List<CsvDatasourceTable> tables;
}
}
22 changes: 22 additions & 0 deletions src/main/java/de/samply/opal/model/CsvDatasourceTable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package de.samply.opal.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CsvDatasourceTable {

@JsonProperty("name")
private String name;

@JsonProperty("data")
private String data;

@JsonProperty("entityType")
private String entityType;

@JsonProperty("refTable")
private String refTable;
}
15 changes: 15 additions & 0 deletions src/main/java/de/samply/opal/model/ImportPathBody.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.samply.opal.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ImportPathBody {

@JsonProperty(value = "destination")
private String destination;
@JsonProperty(value = "tables")
private String[] tables;
}

0 comments on commit 7204c22

Please # to comment.