Files
SWA-backend/src/main/java/de/anxietyprime/swajodel/JodelPost.java

91 lines
3.1 KiB
Java

package de.anxietyprime.swajodel;
import java.util.Date;
import java.util.Optional;
import java.util.Vector;
import java.sql.*;
public class JodelPost {
// id of the post
public Long id;
// id of the author in db
private Long authorID;
// anonymized authorID
public Long anonymousID;
// title of the post
public String title;
// content of the post
public String content;
// date of the post
public Date date;
// location if the post
public Location location;
// list of all comments for the post
public Vector<JodelPost> comments = new Vector<>();
// the own reaction (null = none, true = positive, false = negative)
public Optional<Boolean> reaction;
// all other reactions
public Reactions reactions;
// anonymize function to recursively anonymize the posts
public void anonymize(Optional<Vector<Long>> idCache) {
// check if this is the first post in this process
if (idCache.isEmpty()) {
// create a new Vector as cache
idCache = Optional.of(new Vector());
}
// get the anonymized id as index in cached authorIDs
int i = idCache.get().indexOf(this.authorID);
// if the index is -1 the authorID has not been cached jet
if (i == -1) {
// set the current anonymousID as length of the cache (== next index)
this.anonymousID = (long) idCache.get().size();
// push the current authorID to to cache
idCache.get().add(this.authorID);
}
// the authorID has been anonymized once before, so we can get it from cache
else this.anonymousID = (long) i;
// recursively apply this algorithm for all comments, but include the idCache
for (JodelPost comment : this.comments) {
comment.anonymize(idCache);
}
}
// add a comment if it is really a comment
public boolean addComment(JodelPost post, long parent) {
// check if the post is a direct comment
if (parent == this.id) {
// add the comment
this.comments.add(post);
// return success
return true;
}
// recursively repeat this for all comments
for (JodelPost comment : this.comments) {
// return success if the post is a comment on a child
if (comment.addComment(post, parent)) return true;
}
// return no success
return false;
}
// constructor from
public JodelPost(ResultSet rs) throws SQLException {
// add all other information to the post
this.authorID = rs.getLong("author");
this.id = rs.getLong("id");
this.title = rs.getString("title");
this.content = rs.getString("content");
this.date = rs.getDate("postdate");
this.location = new Location(rs.getLong("longitude"), rs.getLong("latitude"));
this.reactions = new Reactions(rs.getLong("positive"), rs.getLong("negative"));
}
// constructor from authorID for tests
public JodelPost(int authorID) {
this.authorID = (long) authorID;
}
}