diff --git a/src/main/java/org/texttechnologylab/project/gruppe_05_1/database/MongoDBHandler.java b/src/main/java/org/texttechnologylab/project/gruppe_05_1/database/MongoDBHandler.java index 5cb60f1..47a73a0 100644 --- a/src/main/java/org/texttechnologylab/project/gruppe_05_1/database/MongoDBHandler.java +++ b/src/main/java/org/texttechnologylab/project/gruppe_05_1/database/MongoDBHandler.java @@ -7,6 +7,7 @@ import com.mongodb.client.MongoClient; import com.mongodb.client.MongoClients; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; +import com.mongodb.client.model.Indexes; import com.mongodb.client.model.Updates; import org.bson.Document; import org.bson.conversions.Bson; @@ -20,6 +21,7 @@ import static com.mongodb.client.model.Filters.eq; public class MongoDBHandler { public static final String propertiesFileName = "mongoDB.properties"; + public static final String DEFAULT_ID_FIELD_NAME = "_id"; private static MongoDatabase mongoDatabase = null; @@ -168,6 +170,34 @@ public class MongoDBHandler { } } + + static public void createIndexForCollection(MongoCollection collection, String indexName, boolean isAscending) { + // MongoDB creates automatically an index on "_id" + if (indexName.equals(DEFAULT_ID_FIELD_NAME)) { + return; + } + + if (isAscending) { + collection.createIndex(Indexes.ascending(indexName)); + } else { + collection.createIndex(Indexes.descending(indexName)); + } + } + + static public void createIndexForCollection(MongoCollection collection, List indexNames, boolean isAscending) { + // MongoDB creates automatically an index on "_id" + if (indexNames.contains(DEFAULT_ID_FIELD_NAME)) { + indexNames.remove(DEFAULT_ID_FIELD_NAME); + if (indexNames.size() == 0) return; + } + + if (isAscending) { + collection.createIndex(Indexes.ascending(indexNames)); + } else { + collection.createIndex(Indexes.descending(indexNames)); + } + } + static public void createOrTrancateCollection(String collectionName) { createOrTrancateCollection(getMongoDatabase(), collectionName); } diff --git a/src/main/java/org/texttechnologylab/project/gruppe_05_1/database/MongoPprUtils.java b/src/main/java/org/texttechnologylab/project/gruppe_05_1/database/MongoPprUtils.java index a49dc55..38ca84e 100644 --- a/src/main/java/org/texttechnologylab/project/gruppe_05_1/database/MongoPprUtils.java +++ b/src/main/java/org/texttechnologylab/project/gruppe_05_1/database/MongoPprUtils.java @@ -2,6 +2,7 @@ package org.texttechnologylab.project.gruppe_05_1.database; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCursor; +import com.mongodb.client.model.Indexes; import org.bson.Document; import org.texttechnologylab.project.gruppe_05_1.domain.html.Parlamentarier; import org.texttechnologylab.project.gruppe_05_1.domain.html.ParlamentarierDetails; @@ -13,6 +14,7 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; import java.util.List; @@ -57,6 +59,53 @@ public class MongoPprUtils { return commentCollecion; } + /** + * Create the Speaker Collection and useful indices for it + */ + public static void createSpeakerCollection() { + MongoDBHandler.createCollection(MongoPprUtils.SPEAKER_COLLECTION_NAME); + + MongoDBHandler.createIndexForCollection(getSpeakerCollection(), Arrays.asList("name", "firstName", "party"), true); + } + + /** + * Create the Speech Collection and useful indices for it + */ + public static void createSpeechCollection() { + MongoDBHandler.createCollection(MongoPprUtils.SPEECH_COLLECTION_NAME); + MongoDBHandler.createIndexForCollection(getSpeechCollection(), "speaker", true); + } + + + /** + * Create the Comment Collection and useful indices for it + */ + public static void createCommentCollection() { + MongoDBHandler.createCollection(MongoPprUtils.COMMENT_COLLECTION_NAME); + MongoDBHandler.createIndexForCollection(getCommentCollection(), Arrays.asList("speaker", "speech"), true); + } + + + /** + * Create the Picture Collection and useful indices for it + */ + public static void createPictureCollection() { + MongoDBHandler.createCollection(MongoPprUtils.PICTURES_COLLECTION_NAME); + + // TODO: für welche Felder sollen Indizes gebaut werden? + // MongoDBHandler.createIndexForCollection(getPicturesCollection(), Arrays.asList("field_1", "field_2"), true); + } + + /** + * Truncate the Speaker Collection. + * Note that it is quicker (and saves space) to drop and re-create rather than removing all documents using "remove({})" + */ + public static void truncateSpeakerCollection() { + + getSpeakerCollection().drop(); + createSpeechCollection(); + } + /* * Parlamentarier * ======================================================================= diff --git a/src/main/java/org/texttechnologylab/project/gruppe_05_1/domain/speech/Comment.java b/src/main/java/org/texttechnologylab/project/gruppe_05_1/domain/speech/Comment.java new file mode 100644 index 0000000..3439a31 --- /dev/null +++ b/src/main/java/org/texttechnologylab/project/gruppe_05_1/domain/speech/Comment.java @@ -0,0 +1,68 @@ +package org.texttechnologylab.project.gruppe_05_1.domain.speech; + +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.StringJoiner; + +public abstract class Comment { + + String id; + String text; + String speaker; + String speech; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public String getSpeaker() { + return speaker; + } + + public void setSpeaker(String speaker) { + this.speaker = speaker; + } + + public String getSpeech() { + return speech; + } + + public void setSpeech(String speech) { + this.speech = speech; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Comment comment)) return false; + return Objects.equals(id, comment.id) && Objects.equals(text, comment.text) && Objects.equals(speaker, comment.speaker) && Objects.equals(speech, comment.speech); + } + + @Override + public int hashCode() { + return Objects.hash(id, text, speaker, speech); + } + + @Override + public String toString() { + return new StringJoiner(", ", Comment.class.getSimpleName() + "[", "]") + .add("id='" + id + "'") + .add("text='" + text + "'") + .add("speaker='" + speaker + "'") + .add("speech='" + speech + "'") + .toString(); + } +} diff --git a/src/main/java/org/texttechnologylab/project/gruppe_05_1/util/PPRUtils.java b/src/main/java/org/texttechnologylab/project/gruppe_05_1/util/PPRUtils.java index 902d2e1..9f980d8 100644 --- a/src/main/java/org/texttechnologylab/project/gruppe_05_1/util/PPRUtils.java +++ b/src/main/java/org/texttechnologylab/project/gruppe_05_1/util/PPRUtils.java @@ -37,42 +37,20 @@ public abstract class PPRUtils { Set existingCollectionNames = MongoDBHandler.getCollectionNames(); - if (existingCollectionNames.contains(MongoPprUtils.SPEAKER_COLLECTION_NAME)) { - MongoDBHandler.createCollection(MongoPprUtils.SPEAKER_COLLECTION_NAME); - - // Add Indexes - MongoCollection speakerCollection = MongoPprUtils.getSpeakerCollection(); - // MongoDB creates automatically an index on "_id" - speakerCollection.createIndex(Indexes.ascending("name")); - speakerCollection.createIndex(Indexes.ascending("firstName")); - speakerCollection.createIndex(Indexes.ascending("party")); + if ( ! existingCollectionNames.contains(MongoPprUtils.SPEAKER_COLLECTION_NAME)) { + MongoPprUtils.createSpeakerCollection(); } - if (existingCollectionNames.contains(MongoPprUtils.SPEECH_COLLECTION_NAME)) { - MongoDBHandler.createCollection(MongoPprUtils.SPEECH_COLLECTION_NAME); - - // Add Indexes - MongoCollection speechCollection = MongoPprUtils.getSpeechCollection(); - // MongoDB creates automatically an index on "_id" - speechCollection.createIndex(Indexes.ascending("speaker")); + if ( ! existingCollectionNames.contains(MongoPprUtils.SPEECH_COLLECTION_NAME)) { + MongoPprUtils.createSpeechCollection(); } - if (existingCollectionNames.contains(MongoPprUtils.COMMENT_COLLECTION_NAME)) { - MongoDBHandler.createCollection(MongoPprUtils.COMMENT_COLLECTION_NAME); - - // TODO: Add Indexes - MongoCollection commentCollection = MongoPprUtils.getCommentCollection(); - // MongoDB creates automatically an index on "_id" - // commentCollection.createIndex(Indexes.ascending("???")); + if ( ! existingCollectionNames.contains(MongoPprUtils.COMMENT_COLLECTION_NAME)) { + MongoPprUtils.createCommentCollection(); } - if (existingCollectionNames.contains(MongoPprUtils.PICTURES_COLLECTION_NAME)) { - MongoDBHandler.createCollection(MongoPprUtils.PICTURES_COLLECTION_NAME); - - // TODO: Add Indexes - MongoCollection pictureCollection = MongoPprUtils.getPicturesCollection(); - // MongoDB creates automatically an index on "_id" - // pictureCollection.createIndex(Indexes.ascending("???")); + if ( ! existingCollectionNames.contains(MongoPprUtils.PICTURES_COLLECTION_NAME)) { + MongoPprUtils.createPictureCollection(); } } diff --git a/src/main/java/org/texttechnologylab/project/gruppe_05_1/website/templates/filterForm.ftl b/src/main/java/org/texttechnologylab/project/gruppe_05_1/website/templates/filterForm.ftl new file mode 100644 index 0000000..5bd2261 --- /dev/null +++ b/src/main/java/org/texttechnologylab/project/gruppe_05_1/website/templates/filterForm.ftl @@ -0,0 +1,4 @@ +
+ Name : + +
\ No newline at end of file diff --git a/src/main/java/org/texttechnologylab/project/gruppe_05_1/website/templates/parlamentarier.ftl b/src/main/java/org/texttechnologylab/project/gruppe_05_1/website/templates/parlamentarier.ftl index c539b15..8d74102 100644 --- a/src/main/java/org/texttechnologylab/project/gruppe_05_1/website/templates/parlamentarier.ftl +++ b/src/main/java/org/texttechnologylab/project/gruppe_05_1/website/templates/parlamentarier.ftl @@ -19,10 +19,7 @@
-
- Name : - -
+ <#include "filterForm.ftl">
diff --git a/src/main/resources/mongoDB_Uebung2.properties b/src/main/resources/mongoDB_Uebung2.properties deleted file mode 100644 index 098c8c7..0000000 --- a/src/main/resources/mongoDB_Uebung2.properties +++ /dev/null @@ -1,7 +0,0 @@ -localserver=mongodb://localhost:27017 -remote_host=ppr.lehre.texttechnologylab.org -remote_database=PPR_WiSe24_264 -remote_user=PPR_WiSe24_264_rw -remote_password=XPs5GfDf -remote_port=27020 -remote_collection=speeches \ No newline at end of file