Skip to content

Commit

Permalink
Merge pull request #34 from rwth-acis/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
AlexanderNeumann authored May 5, 2023
2 parents 41d875b + 9982899 commit 77c2d67
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 8 deletions.
8 changes: 8 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
5 changes: 5 additions & 0 deletions etc/i5.las2peer.services.tmitocar.TmitocarService.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ mongoPassword =
mongoDB =
mongoHost =
mongoAuth =
pgsqlHost =
pgsqlPort =
pgsqlUser =
pgsqlPassword =
pgsqlDB =
address = http://127.0.0.1:8080
4 changes: 4 additions & 0 deletions las2peer-tmitocar-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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() {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<br>\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<br>\n" + text + "\n<br>";
}
} 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")
Expand All @@ -386,8 +553,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)) {
Expand Down Expand Up @@ -447,8 +614,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)) {
Expand Down Expand Up @@ -487,7 +654,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();
Expand Down Expand Up @@ -552,8 +719,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)) {
Expand Down Expand Up @@ -597,7 +764,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)) {
Expand Down

0 comments on commit 77c2d67

Please sign in to comment.