Added comment entity, filter now standalone template, helperfunctions for mongo, creating all databases and indices on startup if not existing

This commit is contained in:
vysitor 2025-03-02 17:08:33 +01:00
parent f90280f4a8
commit 1e6c3954ce
7 changed files with 160 additions and 41 deletions

View file

@ -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<Document> 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<Document> collection, List<String> 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);
}

View file

@ -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
* =======================================================================

View file

@ -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();
}
}

View file

@ -37,42 +37,20 @@ public abstract class PPRUtils {
Set<String> existingCollectionNames = MongoDBHandler.getCollectionNames();
if (existingCollectionNames.contains(MongoPprUtils.SPEAKER_COLLECTION_NAME)) {
MongoDBHandler.createCollection(MongoPprUtils.SPEAKER_COLLECTION_NAME);
// Add Indexes
MongoCollection<org.bson.Document> 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<org.bson.Document> 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<org.bson.Document> 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<org.bson.Document> pictureCollection = MongoPprUtils.getPicturesCollection();
// MongoDB creates automatically an index on "_id"
// pictureCollection.createIndex(Indexes.ascending("???"));
if ( ! existingCollectionNames.contains(MongoPprUtils.PICTURES_COLLECTION_NAME)) {
MongoPprUtils.createPictureCollection();
}
}

View file

@ -0,0 +1,4 @@
<form name="searchForm" action="/" method="get">
Name : <input type="text" name="filter" value="${filter!' '}" />
<input type="submit" value="Suche" />
</form>

View file

@ -19,10 +19,7 @@
</header>
<main>
<form name="searchForm" action="/" method="get">
Name : <input type="text" name="filter" value="${filter!' '}" />
<input type="submit" value="Suche" />
</form>
<#include "filterForm.ftl">
<br>

View file

@ -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