Skip to content
Snippets Groups Projects
Commit 6514af70 authored by Sven Kästle's avatar Sven Kästle
Browse files

feat: Implement annotation database management

The AnnotationController handles the database queries
parent 20c5bd18
No related branches found
No related tags found
No related merge requests found
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);
}
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);
}
}
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;
}
......
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 +
'}';
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment