diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/Annotatable.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/IAnnotation.java similarity index 54% rename from gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/Annotatable.java rename to gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/IAnnotation.java index c674c916e9a54c721d5db795e51e09c3714fe20a..7c0de7dfb481c1de28cf5ca69b58472688bc5c50 100644 --- a/gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/Annotatable.java +++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/interfaces/IAnnotation.java @@ -1,6 +1,7 @@ package unipotsdam.gf.interfaces; -import unipotsdam.gf.modules.annotation.Annotation; +import unipotsdam.gf.modules.annotation.model.Annotation; +import unipotsdam.gf.modules.annotation.model.AnnotationPostRequest; import java.util.ArrayList; @@ -8,15 +9,15 @@ import java.util.ArrayList; * @author Sven Kästle * skaestle@uni-potsdam.de */ -public interface Annotatable { +public interface IAnnotation { /** * Adds an annotation to a target and returns the new id * - * @param newAnnotation The new annotation as an Object - * @return Returns the id of the new annotation + * @param annotationPostRequest The new annotation as an Object + * @return Returns the new annotation */ - int addAnnotation(Annotation newAnnotation); + Annotation addAnnotation(AnnotationPostRequest annotationPostRequest); /** * Alters an annotation @@ -24,23 +25,22 @@ public interface Annotatable { * @param annotationId The id of the original annotation * @param newBody The new body of the annotation */ - void alterAnnotation(int annotationId, String newBody); + void alterAnnotation(String annotationId, String newBody); /** * Deletes an annotation * * @param annotationId The id of the annotation */ - void deleteAnnotation(int annotationId); + void deleteAnnotation(String annotationId); /** * Returns a specific annotation from a target * * @param annotationId The id of the annotation - * @param targetId The id of the target * @return Returns a specific annotation */ - Annotation getAnnotation(int annotationId, int targetId); + Annotation getAnnotation(String annotationId); /** * Returns all annotations from a target @@ -50,4 +50,12 @@ public interface Annotatable { */ ArrayList<Annotation> getAnnotations(ArrayList<Integer> targetIds); + /** + * Checks if an annotation id already exists in the database + * + * @param annotationId The id of the annotation + * @return Returns true if the id exists + */ + boolean existsAnnotationId(String annotationId); + } diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/controller/AnnotationController.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/controller/AnnotationController.java new file mode 100644 index 0000000000000000000000000000000000000000..4df361eeffd6e6ca2263dde4462c715b8dbc6478 --- /dev/null +++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/controller/AnnotationController.java @@ -0,0 +1,190 @@ +package unipotsdam.gf.modules.annotation.controller; + +import unipotsdam.gf.core.database.mysql.MysqlConnect; +import unipotsdam.gf.core.database.mysql.VereinfachtesResultSet; +import unipotsdam.gf.interfaces.IAnnotation; +import unipotsdam.gf.modules.annotation.model.Annotation; +import unipotsdam.gf.modules.annotation.model.AnnotationPostRequest; + +import java.time.ZonedDateTime; +import java.util.ArrayList; +import java.util.UUID; + +public class AnnotationController implements IAnnotation { + @Override + public Annotation addAnnotation(AnnotationPostRequest annotationPostRequest) { + + // create a new id if we found no id. + String uuid = UUID.randomUUID().toString(); + while (existsAnnotationId(uuid)) { + uuid = UUID.randomUUID().toString(); + } + + // build the annotation + Annotation annotation = new Annotation(uuid, + ZonedDateTime.now().toEpochSecond(), + annotationPostRequest.getUserId(), + annotationPostRequest.getTargetId(), + annotationPostRequest.getBody(), + annotationPostRequest.getStartCharacter(), + annotationPostRequest.getEndCharacter()); + + // establish connection + MysqlConnect connection = new MysqlConnect(); + connection.connect(); + + // build and execute request + String request = "INSERT INTO annotations (`id`, `userId`, `targetId`, `body`, `startCharacter`, `endCharacter`) VALUES (?,?,?,?,?,?);"; + connection.issueInsertOrDeleteStatement(request, annotation.getId(), annotation.getUserId(), annotation.getTargetId(), annotation.getBody(), annotation.getStartCharacter(), annotation.getEndCharacter()); + + // close connection + connection.close(); + + return annotation; + + } + + @Override + public void alterAnnotation(String annotationId, String newBody) { + + // establish connection + MysqlConnect connection = new MysqlConnect(); + connection.connect(); + + // build and execute request + String request = "UPDATE `annotations` SET `body` = ? WHERE `id` = ?"; + connection.issueUpdateStatement(request, newBody, annotationId); + + // close connection + connection.close(); + + } + + @Override + public void deleteAnnotation(String annotationId) { + + // establish connection + MysqlConnect connection = new MysqlConnect(); + connection.connect(); + + // build and execute request + String request = "DELETE FROM annotations WHERE id = ?;"; + connection.issueInsertOrDeleteStatement(request, annotationId); + + // close connection + connection.close(); + + } + + @Override + public Annotation getAnnotation(String annotationId) { + + // establish connection + MysqlConnect connection = new MysqlConnect(); + connection.connect(); + + // build and execute request + String request = "SELECT * FROM annotations WHERE id = ?;"; + VereinfachtesResultSet rs = connection.issueSelectStatement(request, annotationId); + + if (rs.next()) { + + // save annotation + Annotation annotation = getAnnotationFromResultSet(rs); + + // close connection + connection.close(); + + return annotation; + } + else { + + // close connection + connection.close(); + + return null; + } + + } + + @Override + public ArrayList<Annotation> getAnnotations(ArrayList<Integer> targetIds) { + + // declare annotation ArrayList + ArrayList<Annotation> annotations = new ArrayList<>(); + + // establish connection + MysqlConnect connection = new MysqlConnect(); + connection.connect(); + + // build request + StringBuilder builder = new StringBuilder(); + builder.append("SELECT * FROM annotations WHERE targetId IN ("); + for (int i = 0; i < targetIds.size(); i++) { + builder.append("?"); + if (i < targetIds.size() - 1) { + builder.append(","); + } + } + builder.append(");"); + String request = builder.toString(); + + // execute request + VereinfachtesResultSet rs = connection.issueSelectStatement(request, targetIds.toArray(new Integer[targetIds.size()])); + + while (rs.next()) { + annotations.add(getAnnotationFromResultSet(rs)); + } + + // close connection + connection.close(); + + return annotations; + } + + @Override + public boolean existsAnnotationId(String annotationId) { + + // establish connection + MysqlConnect connection = new MysqlConnect(); + connection.connect(); + + // build and execute request + String request = "SELECT COUNT(*) > 0 AS `exists` FROM annotations WHERE id = ?;"; + VereinfachtesResultSet rs = connection.issueSelectStatement(request, annotationId); + + if (rs.next()) { + // save the response + int count = rs.getInt("exists"); + + // close connection + connection.close(); + + // return true if we found the id + if (count < 1) { + return false; + } + else { + return true; + } + } + + // something happened + return true; + + } + + private Annotation getAnnotationFromResultSet(VereinfachtesResultSet rs) { + + String id = rs.getString("id"); + long timestamp = rs.getTimestamp(2).getTime(); + int userId = rs.getInt("userId"); + int targetId = rs.getInt("targetId"); + String body = rs.getString("body"); + int startCharacter = rs.getInt("startCharacter"); + int endCharacter = rs.getInt("endCharacter"); + + return new Annotation(id, timestamp, userId, targetId, body, startCharacter, endCharacter); + + } +} diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/Annotation.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/model/Annotation.java similarity index 88% rename from gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/Annotation.java rename to gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/model/Annotation.java index 5535a7b95b14412e5f431d4d1b79c9b6bfdb73bc..725967353f5685e93c53302b7a8a0e799e4ccc12 100644 --- a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/Annotation.java +++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/model/Annotation.java @@ -1,4 +1,4 @@ -package unipotsdam.gf.modules.annotation; +package unipotsdam.gf.modules.annotation.model; /** * @author Sven Kästle @@ -7,7 +7,7 @@ package unipotsdam.gf.modules.annotation; public class Annotation { // variables - private int id; + private String id; private long timestamp; private int userId; private int targetId; @@ -16,7 +16,7 @@ public class Annotation { private int endCharacter; // constructor - public Annotation(int id, long timestamp, int userId, int targetId, String body, int startCharacter, int endCharacter) { + public Annotation(String id, long timestamp, int userId, int targetId, String body, int startCharacter, int endCharacter) { this.id = id; this.timestamp = timestamp; this.userId = userId; @@ -27,11 +27,11 @@ public class Annotation { } // methods - public int getId() { + public String getId() { return id; } - public void setId(int id) { + public void setId(String id) { this.id = id; } diff --git a/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/model/AnnotationPostRequest.java b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/model/AnnotationPostRequest.java new file mode 100644 index 0000000000000000000000000000000000000000..2127789071a05193d6d810992a47416e282667bd --- /dev/null +++ b/gemeinsamforschen/src/main/java/unipotsdam/gf/modules/annotation/model/AnnotationPostRequest.java @@ -0,0 +1,76 @@ +package unipotsdam.gf.modules.annotation.model; + +/** + * @author Sven Kästle + * skaestle@uni-potsdam.de + */ +public class AnnotationPostRequest { + + // variables + private int userId; + private int targetId; + private String body; + private int startCharacter; + private int endCharacter; + + // constructor + public AnnotationPostRequest(int userId, int targetId, String body, int startCharacter, int endCharacter) { + this.userId = userId; + this.targetId = targetId; + this.body = body; + this.startCharacter = startCharacter; + this.endCharacter = endCharacter; + } + + // methods + public int getUserId() { + return userId; + } + + public void setUserId(int userId) { + this.userId = userId; + } + + public int getTargetId() { + return targetId; + } + + public void setTargetId(int targetId) { + this.targetId = targetId; + } + + public String getBody() { + return body; + } + + public void setBody(String body) { + this.body = body; + } + + public int getStartCharacter() { + return startCharacter; + } + + public void setStartCharacter(int startCharacter) { + this.startCharacter = startCharacter; + } + + public int getEndCharacter() { + return endCharacter; + } + + public void setEndCharacter(int endCharacter) { + this.endCharacter = endCharacter; + } + + @Override + public String toString() { + return "AnnotationPostRequest{" + + "userId=" + userId + + ", targetId=" + targetId + + ", body='" + body + '\'' + + ", startCharacter=" + startCharacter + + ", endCharacter=" + endCharacter + + '}'; + } +}