From c7737d452e20050f84fc2a93aa9bb972103031df Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Wed, 3 May 2023 15:21:59 +0200 Subject: [PATCH 1/3] Fix annotations --- .../services/tmitocar/TmitocarService.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/las2peer-tmitocar-service/src/main/java/i5/las2peer/services/tmitocar/TmitocarService.java b/las2peer-tmitocar-service/src/main/java/i5/las2peer/services/tmitocar/TmitocarService.java index 5ac51e8..d817482 100644 --- a/las2peer-tmitocar-service/src/main/java/i5/las2peer/services/tmitocar/TmitocarService.java +++ b/las2peer-tmitocar-service/src/main/java/i5/las2peer/services/tmitocar/TmitocarService.java @@ -386,8 +386,8 @@ public static class TMitocarText { @ApiResponses(value = { @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "") }) @ApiOperation(value = "analyzeText", notes = "Analyzes a text and generates a PDF report") public Response analyzeText(@PathParam("label1") String label1, - @FormDataParam("text") InputStream textInputStream, - @FormDataParam("text") FormDataContentDisposition textFileDetail, @FormDataParam("type") String type, + @FormDataParam("file") InputStream textInputStream, + @FormDataParam("file") FormDataContentDisposition textFileDetail, @FormDataParam("type") String type, @FormDataParam("topic") String topic, @FormDataParam("template") String template, @FormDataParam("wordSpec") String wordSpec) throws ParseException, IOException { if (isActive.getOrDefault(label1, false)) { @@ -447,8 +447,8 @@ public static class Feedback { @ApiResponses(value = { @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "") }) @ApiOperation(value = "analyzeText", notes = "Analyzes a text and generates a PDF report") public Response analyzeText(@PathParam("label1") String label1, - @FormDataParam("text") InputStream textInputStream, - @FormDataParam("text") FormDataContentDisposition textFileDetail, @FormDataParam("type") String type, + @FormDataParam("file") InputStream textInputStream, + @FormDataParam("file") FormDataContentDisposition textFileDetail, @FormDataParam("type") String type, @FormDataParam("topic") String topic, @FormDataParam("template") String template, @FormDataParam("wordSpec") String wordSpec) throws ParseException, IOException { if (isActive.getOrDefault(label1, false)) { @@ -487,7 +487,7 @@ public Response analyzeText(@PathParam("label1") String label1, @Path("/{label1}") @Produces(MediaType.APPLICATION_JSON) @ApiResponses(value = { @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "") }) - @ApiOperation(value = "compareText", notes = "Returns analyzed text report (PDF)") + @ApiOperation(value = "getAnalyzedText", notes = "Returns analyzed text report (PDF)") public Response getAnalyzedText(@PathParam("label1") String label1) throws ParseException, IOException { if (!isActive.containsKey(label1)) { JSONObject err = new JSONObject(); @@ -552,8 +552,8 @@ public Response getAnalyzedText(@PathParam("label1") String label1) throws Parse @ApiResponses(value = { @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "") }) @ApiOperation(value = "compareText", notes = "Compares two texts and generates a PDF report") public Response compareText(@PathParam("label1") String label1, @PathParam("label2") String label2, - @FormDataParam("text") InputStream textInputStream, - @FormDataParam("text") FormDataContentDisposition textFileDetail, @FormDataParam("type") String type, + @FormDataParam("file") InputStream textInputStream, + @FormDataParam("file") FormDataContentDisposition textFileDetail, @FormDataParam("type") String type, @FormDataParam("topic") String topic, @FormDataParam("template") String template, @FormDataParam("wordSpec") String wordSpec) throws ParseException, IOException { if (isActive.getOrDefault(label1, false)) { @@ -597,7 +597,7 @@ public Response compareText(@PathParam("label1") String label1, @PathParam("labe @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) @ApiResponses(value = { @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "") }) - @ApiOperation(value = "compareText", notes = "Returns compared text report (PDF)") + @ApiOperation(value = "getComparedText", notes = "Returns compared text report (PDF)") public Response getComparedText(@PathParam("label1") String label1, @PathParam("label2") String label2) throws ParseException, IOException { if (!isActive.containsKey(label1)) { From 0bc36055f695683af5018e5b0c087cfd6bcdf430 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Fri, 5 May 2023 13:16:33 +0200 Subject: [PATCH 2/3] Start with querying writing tasks from db --- ...rvices.tmitocar.TmitocarService.properties | 5 + las2peer-tmitocar-service/build.gradle | 4 + .../services/tmitocar/TmitocarService.java | 167 ++++++++++++++++++ 3 files changed, 176 insertions(+) diff --git a/etc/i5.las2peer.services.tmitocar.TmitocarService.properties b/etc/i5.las2peer.services.tmitocar.TmitocarService.properties index 1928b52..dfb4c43 100644 --- a/etc/i5.las2peer.services.tmitocar.TmitocarService.properties +++ b/etc/i5.las2peer.services.tmitocar.TmitocarService.properties @@ -8,4 +8,9 @@ mongoPassword = mongoDB = mongoHost = mongoAuth = +pgsqlHost = +pgsqlPort = +pgsqlUser = +pgsqlPassword = +pgsqlDB = address = http://127.0.0.1:8080 \ No newline at end of file diff --git a/las2peer-tmitocar-service/build.gradle b/las2peer-tmitocar-service/build.gradle index e5dbe4b..7bb112c 100644 --- a/las2peer-tmitocar-service/build.gradle +++ b/las2peer-tmitocar-service/build.gradle @@ -41,6 +41,10 @@ dependencies { implementation "com.google.code.gson:gson:2.10.1" + // for writing tasks postgresql connection + implementation "org.apache.commons:commons-dbcp2:2.9.0" + implementation 'org.postgresql:postgresql:42.6.0' + // javax.websocket-api;version="1.1", jslack;version="1.8.1", rocketchat-common;version="0.7.1, rocketchat-core;version="0.7.1, rocketchat-livechat;version="0.7.1" } diff --git a/las2peer-tmitocar-service/src/main/java/i5/las2peer/services/tmitocar/TmitocarService.java b/las2peer-tmitocar-service/src/main/java/i5/las2peer/services/tmitocar/TmitocarService.java index d817482..4a4fa9b 100644 --- a/las2peer-tmitocar-service/src/main/java/i5/las2peer/services/tmitocar/TmitocarService.java +++ b/las2peer-tmitocar-service/src/main/java/i5/las2peer/services/tmitocar/TmitocarService.java @@ -15,12 +15,20 @@ import javax.ws.rs.POST; import javax.ws.rs.Path; import java.nio.file.Paths; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; +import org.apache.commons.dbcp2.BasicDataSource; import org.apache.commons.io.FileUtils; import org.java_websocket.util.Base64; @@ -50,6 +58,7 @@ import io.swagger.annotations.Info; import io.swagger.annotations.License; import io.swagger.annotations.SwaggerDefinition; +import net.minidev.json.JSONArray; import net.minidev.json.JSONObject; import net.minidev.json.parser.ParseException; import org.apache.pdfbox.pdfparser.PDFParser; @@ -94,6 +103,14 @@ public class TmitocarService extends RESTService { private String mongoUri; private String mongoAuth = "admin"; + private String pgsqlHost; + private String pgsqlPort; + private String pgsqlUser; + private String pgsqlPassword; + private String pgsqlDB; + + private static BasicDataSource dataSource; + private final static String AUTH_FILE = "tmitocar/auth.json"; // This is the constructor of the TmitocarService class. @@ -119,6 +136,7 @@ protected void initResources() { getResourceConfig().register(this); getResourceConfig().register(Feedback.class); getResourceConfig().register(TMitocarText.class); + getResourceConfig().register(WritingTask.class); } private void initVariables() { @@ -186,8 +204,24 @@ private void initDB() { } finally { mongoClient.close(); } + // postgresql + if (dataSource == null) { + dataSource = new BasicDataSource(); + dataSource.setDriverClassName("org.postgresql.Driver"); + dataSource.setUrl("jdbc:postgresql://"+pgsqlHost+":"+pgsqlPort+"/"+pgsqlDB); + dataSource.setUsername(pgsqlUser); + dataSource.setPassword(pgsqlPassword); + + // Set connection pool properties + dataSource.setInitialSize(5); + dataSource.setMaxTotal(10); + } } + protected Connection getConnection() throws SQLException { + return dataSource.getConnection(); + } + private void uploadToTmitocar(String label1, String fileName, String wordspec) throws InterruptedException, IOException { ProcessBuilder pb; @@ -362,6 +396,139 @@ public void run() { } } + @Api(value = "Writing Task Resource") + @SwaggerDefinition(info = @Info(title = "Writing Task Resource", version = "1.0.0", description = "Todo.", termsOfService = "https://tech4comp.de/", contact = @Contact(name = "Alexander Tobias Neumann", url = "https://tech4comp.dbis.rwth-aachen.de/", email = "neumann@dbis.rwth-aachen.de"), license = @License(name = "ACIS License (BSD3)", url = "https://github.com/rwth-acis/las2peer-tmitocar-Service/blob/master/LICENSE"))) + @Path("/task") + public static class WritingTask { + TmitocarService service = (TmitocarService) Context.get().getService(); + + @GET + @Path("/") + @Produces(MediaType.APPLICATION_JSON) + @ApiResponses(value = { @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "") }) + @ApiOperation(value = "getAllTasks", notes = "Returns all writing tasks") + public Response getWritingTasks(@QueryParam("courseId") int courseId) { + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; + JSONArray jsonArray = new JSONArray(); + String chatMessage = ""; + try { + conn = service.getConnection(); + if (courseId == 0) { + stmt = conn.prepareStatement("SELECT * FROM writingtask"); + } else { + stmt = conn.prepareStatement("SELECT * FROM writingtask WHERE courseid = ?"); + stmt.setInt(1, courseId); + } + rs = stmt.executeQuery(); + + while (rs.next()) { + courseId = rs.getInt("courseid"); + int nr = rs.getInt("nr"); + String text = rs.getString("text"); + String title = rs.getString("title"); + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("courseId", courseId); + jsonObject.put("nr", nr); + jsonObject.put("text", text); + jsonObject.put("title", title); + + jsonArray.add(jsonObject); + chatMessage += nr+": "+title+"\n
\n"; + } + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } finally { + try { + if (rs != null) { + rs.close(); + } + if (stmt != null) { + stmt.close(); + } + if (conn != null) { + conn.close(); + } + } catch (SQLException ex) { + System.out.println(ex.getMessage()); + } + } + JSONObject response = new JSONObject(); + response.put("data", jsonArray); + response.put("chatMessage", chatMessage); + return Response.ok().entity(response.toString()).build(); + } + + + @GET + @Path("/{tasknr}") + @Produces(MediaType.APPLICATION_JSON) + @ApiResponses(value = { @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "") }) + @ApiOperation(value = "getAllTasks", notes = "Returns writing task by id") + public Response getWritingTaskByNr(@PathParam("tasknr") int tasknr, @QueryParam("courseId") int courseId) { + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; + JSONArray jsonArray = new JSONArray(); + String chatMessage = ""; + try { + conn = service.getConnection(); + if (courseId == 0) { + stmt = conn.prepareStatement("SELECT * FROM writingtask WHERE nr = ?"); + stmt.setInt(1, tasknr); + } else { + stmt = conn.prepareStatement("SELECT * FROM writingtask WHERE nr = ? AND courseid = ?"); + stmt.setInt(1, tasknr); + stmt.setInt(2, courseId); + } + rs = stmt.executeQuery(); + + while (rs.next()) { + courseId = rs.getInt("courseid"); + int nr = rs.getInt("nr"); + String text = rs.getString("text"); + String title = rs.getString("title"); + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("courseId", courseId); + jsonObject.put("nr", nr); + jsonObject.put("text", text); + jsonObject.put("title", title); + + jsonArray.add(jsonObject); + chatMessage += nr+": "+title+"\n
\n" + text + "\n
"; + } + } catch (SQLException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } finally { + try { + if (rs != null) { + rs.close(); + } + if (stmt != null) { + stmt.close(); + } + if (conn != null) { + conn.close(); + } + } catch (SQLException ex) { + System.out.println(ex.getMessage()); + } + } + JSONObject response = new JSONObject(); + response.put("data", jsonArray); + response.put("chatMessage", chatMessage); + return Response.ok().entity(response.toString()).build(); + } + + } + @Api(value = "Text Resource") @SwaggerDefinition(info = @Info(title = "Text Resource", version = "1.0.0", description = "This API is responsible for storing a text file on the T-MITOCAR server for later use as a comparison text.", termsOfService = "https://tech4comp.de/", contact = @Contact(name = "Alexander Tobias Neumann", url = "https://tech4comp.dbis.rwth-aachen.de/", email = "neumann@dbis.rwth-aachen.de"), license = @License(name = "ACIS License (BSD3)", url = "https://github.com/rwth-acis/las2peer-tmitocar-Service/blob/master/LICENSE"))) @Path("/text") From 99828994a1f5bce08bcc46424cded8a589b4c771 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Fri, 5 May 2023 13:18:14 +0200 Subject: [PATCH 3/3] Update docker-entrypoint.sh --- docker-entrypoint.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 7e4de84..0a44516 100644 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -48,12 +48,20 @@ set_in_service_config publicKey ${PUBLIC_KEY} set_in_service_config lrsURL ${LRS_URL} set_in_service_config lrsAuthTokenLeipzig ${LRS_AUTH_TOKEN_LEIPZIG} set_in_service_config lrsAuthTokenDresden ${LRS_AUTH_TOKEN_DRESDEN} + set_in_service_config mongoHost ${MONGO_HOST} set_in_service_config mongoDB ${MONGO_DB} set_in_service_config mongoUser ${MONGO_USER} set_in_service_config mongoPassword ${MONGO_PASSWORD} set_in_service_config mongoAuth ${MONGO_AUTH} +set_in_service_config pgsqlHost ${PGSQL_HOST} +set_in_service_config pgsqlPort ${PGSQL_PORT} +set_in_service_config pgsqlUser ${PGSQL_USER} +set_in_service_config pgsqlPassword ${PGSQL_PASSWORD} +set_in_service_config pgsqlDB ${PGSQL_DB} + + # prevent glob expansion in lib/* set -f LAUNCH_COMMAND='java -cp lib/* --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED i5.las2peer.tools.L2pNodeLauncher -s service -p '"${LAS2PEER_PORT} ${SERVICE_EXTRA_ARGS}"