package de.anxietyprime.swajodel; import java.util.Objects; import java.util.Optional; import java.util.Vector; import java.sql.*; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; public class JodelPost { // id of the post public Long id; // id of the author in db private final 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 Timestamp date; // location if the post public Location location; // list of all comments for the post public Vector comments = new Vector<>(); // the own reaction (null = none, true = positive, false = negative) public Optional reaction; // all other reactions public Reactions reactions; // Parent id public Optional parent = Optional.empty(); // anonymize function to recursively anonymize the posts public void anonymize(Vector idCache) { // get the anonymized id as index in cached authorIDs int i = idCache.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.size(); // push the current authorID to to cache idCache.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) { if (post.parent.isEmpty()) return true; // check if the post is a direct comment if (Objects.equals(post.parent.get(), 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)) 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.getTimestamp("postdate"); this.location = new Location(rs.getFloat("longitude"), rs.getFloat("latitude")); this.reaction = Optional.of(rs.getBoolean("reaction")); this.reactions = new Reactions(rs.getLong("positive"), rs.getLong("negative")); this.parent = Optional.of(rs.getLong("parent")); } // constructor from authorID for tests public JodelPost(int authorID) { this.authorID = (long) authorID; } @JsonCreator public JodelPost(@JsonProperty("title") String title, @JsonProperty("content") String content, @JsonProperty("date") Timestamp date, @JsonProperty("location") Location location, @JsonProperty("parent") Optional parent) { this.authorID = User.getUserID(); // TODO: getter from Keycloak this.title = title; this.content = content; this.date = date; this.location = location; this.parent = parent; } @JsonIgnore public long getAuthorID() { return authorID; } @JsonIgnore public boolean isComment() { return parent.filter(aLong -> !Objects.equals(id, aLong)).isPresent(); } }