added filter functions for member list
This commit is contained in:
parent
332fe49a02
commit
61fb607b3e
1 changed files with 100 additions and 0 deletions
|
@ -3,7 +3,12 @@ package org.texttechnologylab.project.gruppe_05_1.database;
|
|||
import com.mongodb.client.FindIterable;
|
||||
import com.mongodb.client.MongoCollection;
|
||||
import com.mongodb.client.MongoCursor;
|
||||
import com.mongodb.client.model.Filters;
|
||||
import com.mongodb.client.model.Projections;
|
||||
import io.javalin.http.Context;
|
||||
import org.bson.Document;
|
||||
import org.bson.conversions.Bson;
|
||||
import org.texttechnologylab.project.gruppe_05_1.database.domainimp.speeches.Speaker_MongoDB_Impl;
|
||||
import org.texttechnologylab.project.gruppe_05_1.database.domainimp.speeches.Speech_MongoDB_Impl;
|
||||
import org.texttechnologylab.project.gruppe_05_1.domain.html.HtmlSpeech;
|
||||
import org.texttechnologylab.project.gruppe_05_1.domain.html.Parlamentarier;
|
||||
|
@ -15,11 +20,14 @@ import org.texttechnologylab.project.gruppe_05_1.util.GeneralUtils;
|
|||
import org.texttechnologylab.project.gruppe_05_1.util.Logger;
|
||||
import org.texttechnologylab.project.gruppe_05_1.xml.speeches.Interfaces.Speech;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.*;
|
||||
|
||||
import static com.mongodb.client.model.Filters.eq;
|
||||
|
||||
/**
|
||||
* Diese Klasse beinhaltet Mongo-Utilities, welche spezifisch für die PPR-Datenstrukturen sind.
|
||||
* Mongo-Utilities genereller Natur stehen in der Klasse MongoDBHandler.
|
||||
|
@ -145,6 +153,89 @@ public class MongoPprUtils {
|
|||
return plist;
|
||||
}
|
||||
|
||||
public static List<Parlamentarier> getFilteredMembers(Context ctx) {
|
||||
// Get optional filter arguments
|
||||
String memberIdParam = ctx.queryParam("memberId");
|
||||
Integer memberId = (memberIdParam != null) ? Integer.parseInt(memberIdParam) : null;
|
||||
|
||||
String name = ctx.queryParam("name");
|
||||
|
||||
String lastName = ctx.queryParam("lastName");
|
||||
|
||||
String firstName = ctx.queryParam("firstName");
|
||||
|
||||
String title = ctx.queryParam("title");
|
||||
|
||||
String dateOfBirth = ctx.queryParam("dateOfBirth");
|
||||
|
||||
String dateOfDeath = ctx.queryParam("dateOfDeath");
|
||||
|
||||
String placeOfBirth = ctx.queryParam("placeOfBirth");
|
||||
|
||||
String gender = ctx.queryParam("gender");
|
||||
|
||||
String religion = ctx.queryParam("religion");
|
||||
|
||||
String party = (!Objects.equals(ctx.queryParam("party"), "")) ? ctx.queryParam("party") : null;
|
||||
|
||||
String firstLegislativePeriod = ctx.queryParam("firstLegislativePeriod");
|
||||
Integer firstLegislativePeriodInt = (firstLegislativePeriod != null) ? Integer.parseInt(firstLegislativePeriod) : null;
|
||||
|
||||
String lastLegislativePeriod = ctx.queryParam("lastLegislativePeriod");
|
||||
Integer lastLegislativePeriodInt = (lastLegislativePeriod != null) ? Integer.parseInt(lastLegislativePeriod) : null;
|
||||
|
||||
List<Bson> filters = new ArrayList<>();
|
||||
if (memberId != null) filters.add(eq("id", memberId));
|
||||
if (name != null) filters.add(Filters.regex("name", ".*" + name + ".*", "i"));
|
||||
if (lastName != null) filters.add(Filters.regex("lastName", ".*" + lastName + ".*", "i"));
|
||||
if (firstName != null) filters.add(Filters.regex("firstName", ".*" + firstName + ".*", "i"));
|
||||
if (title != null) filters.add(Filters.regex("title", ".*" + title + ".*", "i"));
|
||||
if (dateOfBirth != null) filters.add(eq("dateOfBirth", dateOfBirth));
|
||||
if (dateOfDeath != null) filters.add(eq("dateOfDeath", dateOfDeath));
|
||||
if (placeOfBirth != null) filters.add(Filters.regex("placeOfBirth", ".*" + placeOfBirth + ".*", "i"));
|
||||
if (gender != null) filters.add(eq("gender", gender));
|
||||
if (religion != null) filters.add(eq("religion", religion));
|
||||
if (party != null) filters.add(eq("party", party));
|
||||
if (firstLegislativePeriodInt != null)
|
||||
filters.add(eq("firstLegislativePeriod", firstLegislativePeriodInt));
|
||||
if (lastLegislativePeriodInt != null)
|
||||
filters.add(eq("lastLegislativePeriod", lastLegislativePeriodInt));
|
||||
|
||||
// Check if the filters list is empty
|
||||
Bson filter;
|
||||
if (filters.isEmpty()) {
|
||||
filter = Filters.empty(); // No filters, match all documents
|
||||
} else {
|
||||
filter = Filters.and(filters); // Combine all filters with AND
|
||||
}
|
||||
Bson projection = Projections.fields(Projections.exclude("image_data"));
|
||||
|
||||
try {
|
||||
List<Parlamentarier> members = retrieveAllMembersOfParliament(filter, projection);
|
||||
ctx.json(members);
|
||||
return members;
|
||||
} catch (IOException e) {
|
||||
ctx.status(500);
|
||||
ctx.result("Server error occurred");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static List<Parlamentarier> retrieveAllMembersOfParliament(Bson filter, Bson projection) throws IOException {
|
||||
List<Document> speeches = getSpeakerCollection().find(filter).projection(projection).into(new ArrayList<>());
|
||||
List<Parlamentarier> result = new ArrayList<>();
|
||||
for (Document speech : speeches) {
|
||||
Parlamentarier parlamentarier = new Parlamentarier();
|
||||
parlamentarier.setId(speech.getString("_id"));
|
||||
parlamentarier.setVorname(speech.getString("firstName"));
|
||||
parlamentarier.setNachname(speech.getString("name"));
|
||||
parlamentarier.setPartei(speech.getString("party"));
|
||||
result.add(parlamentarier);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Liest einen Parlamentarier von der MongoDB
|
||||
|
@ -436,4 +527,13 @@ public class MongoPprUtils {
|
|||
|
||||
return new HtmlSpeech(speechDoc);
|
||||
}
|
||||
|
||||
// getMemberPhoto
|
||||
|
||||
public static String getMemberPhoto(String id) {
|
||||
Document doc = MongoDBHandler.findFirstDocumentInCollection(getPicturesCollection(), "memberId", id);
|
||||
if (doc == null) {
|
||||
return null;
|
||||
} else return doc.getString("base64");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue