added support for member picture upload
This commit is contained in:
parent
8317dff035
commit
bcfd272b2b
3 changed files with 96 additions and 5 deletions
|
@ -13,6 +13,7 @@ import org.xml.sax.SAXException;
|
|||
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
import static java.lang.Boolean.FALSE;
|
||||
import static java.lang.Boolean.TRUE;
|
||||
|
@ -42,11 +43,19 @@ import java.util.stream.Collectors;
|
|||
public class Main {
|
||||
|
||||
public static final String RESOURCES_DIR="src/main/resources/";
|
||||
public static boolean UPLOAD_MEMBER_PHOTOS;
|
||||
private static final FileObjectFactory xmlFactory = FileObjectFactory.getFactory();
|
||||
private static final MongoObjectFactory mongoFactory = MongoObjectFactory.getFactory();
|
||||
private static final SpeechParser speechParser = new SpeechParser();
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
UPLOAD_MEMBER_PHOTOS = Arrays.asList(args).contains("uploadMemberPhotos");
|
||||
|
||||
System.out.println("Starting Speech Indexer...");
|
||||
System.out.println("--------------------------");
|
||||
System.out.println("Arguments:");
|
||||
System.out.println("- Upload the Member Photos to the DB: " + UPLOAD_MEMBER_PHOTOS);
|
||||
System.out.println("--------------------------");
|
||||
|
||||
//TEST
|
||||
MongoDBHandler mongoDBHandler = new MongoDBHandler();
|
||||
|
@ -100,6 +109,11 @@ public class Main {
|
|||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
if (UPLOAD_MEMBER_PHOTOS) {
|
||||
Logger.pink("Uploading Member Photos to DB...");
|
||||
mongoDBHandler.uploadMemberPhotos();
|
||||
}
|
||||
mongoDBHandler.close();
|
||||
NlpUtils.runRemoteDriver();
|
||||
RESTHandler restHandler = new RESTHandler();
|
||||
|
|
|
@ -18,10 +18,14 @@ import org.texttechnologylab.project.gruppe_05_1.database.domainimp.speeches.Age
|
|||
import org.texttechnologylab.project.gruppe_05_1.database.domainimp.speeches.Session_MongoDB_Impl;
|
||||
import org.texttechnologylab.project.gruppe_05_1.database.domainimp.speeches.Speech_MongoDB_Impl;
|
||||
import org.texttechnologylab.project.gruppe_05_1.util.Logger;
|
||||
import org.texttechnologylab.project.gruppe_05_1.util.PPRUtils;
|
||||
import org.texttechnologylab.project.gruppe_05_1.util.PropertiesUtils;
|
||||
import org.texttechnologylab.project.gruppe_05_1.xml.speeches.Impls.*;
|
||||
import org.texttechnologylab.project.gruppe_05_1.xml.speeches.Interfaces.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
@ -31,6 +35,7 @@ public class MongoDBHandler {
|
|||
|
||||
public static final String propertiesFileName = "mongoDB.properties";
|
||||
public static final String DEFAULT_ID_FIELD_NAME = "_id";
|
||||
public static final String MEMBER_IMAGES_DIR = "src/main/resources/membersOfParliamentImages/";
|
||||
private final MongoClient mongoClient;
|
||||
private final MongoDatabase database;
|
||||
private static MongoDatabase mongoDatabase = null;
|
||||
|
@ -45,11 +50,12 @@ public class MongoDBHandler {
|
|||
private static String collection;
|
||||
private static String databaseName;
|
||||
|
||||
private MongoCollection<Document> speakerCollection;
|
||||
private MongoCollection<Document> speechesCollection;
|
||||
private MongoCollection<Document> sessionsCollection;
|
||||
private MongoCollection<Document> agendaItemsCollection;
|
||||
private MongoCollection<Document> historyCollection;
|
||||
private MongoCollection<Document> speakerCollection;
|
||||
private MongoCollection<Document> speechesCollection;
|
||||
private MongoCollection<Document> sessionsCollection;
|
||||
private MongoCollection<Document> agendaItemsCollection;
|
||||
private MongoCollection<Document> memberPhotoCollection;
|
||||
private MongoCollection<Document> historyCollection;
|
||||
|
||||
|
||||
public MongoDBHandler() {
|
||||
|
@ -97,6 +103,7 @@ public class MongoDBHandler {
|
|||
speechesCollection = database.getCollection(MongoPprUtils.SPEECH_COLLECTION_NAME);
|
||||
sessionsCollection = database.getCollection(MongoPprUtils.SESSION_COLLECTION_NAME);
|
||||
agendaItemsCollection = database.getCollection(MongoPprUtils.AGENDA_ITEMS_COLLECTION_NAME);
|
||||
memberPhotoCollection = database.getCollection(MongoPprUtils.PICTURES_COLLECTION_NAME);
|
||||
historyCollection = database.getCollection(MongoPprUtils.HISTORY_COLLECTION_NAME);
|
||||
|
||||
createIndicesForSpeakerCollection();
|
||||
|
@ -703,6 +710,62 @@ public class MongoDBHandler {
|
|||
return speechesCollection.countDocuments(Filters.exists("analysisResults"));
|
||||
}
|
||||
|
||||
public String loadMemberImageFromFileById(String memberId) {
|
||||
// lookup the first and lastname of the memberId in the speaker collection in the DB
|
||||
Document speaker = speakerCollection.find(eq("speakerId", memberId)).first();
|
||||
if (speaker == null) {
|
||||
return null;
|
||||
}
|
||||
String name = speaker.getString("name");
|
||||
String firstName = speaker.getString("firstName");
|
||||
|
||||
return loadMemberImageFromFileByName(firstName, name);
|
||||
}
|
||||
|
||||
public String loadMemberImageFromFileByName(String firstName, String name) {
|
||||
// get the member photo from the resources/membersOfParliamentImages folder
|
||||
File photo = new File(MEMBER_IMAGES_DIR + name + "_" + firstName + ".jpg");
|
||||
|
||||
// get the base64 encoded string from the photo
|
||||
String image_data;
|
||||
try {
|
||||
// get base64 string of the image photo.path()
|
||||
image_data = Base64.getEncoder().encodeToString(Files.readAllBytes(photo.toPath()));
|
||||
} catch (IOException e) {
|
||||
Logger.warn("Couldn't read file: " + e.getMessage());
|
||||
image_data = "";
|
||||
}
|
||||
|
||||
return image_data;
|
||||
}
|
||||
|
||||
public void uploadMemberPhoto(String memberId, String base64String) {
|
||||
if (memberPhotoCollection.find(eq("memberId", memberId)).first() != null) {
|
||||
Logger.warn("Member photo for " + memberId + " already exists in the database. Overwriting...");
|
||||
memberPhotoCollection.deleteOne(eq("memberId", memberId));
|
||||
}
|
||||
Document photoDocument = new Document("memberId", memberId)
|
||||
.append("base64", base64String);
|
||||
memberPhotoCollection.insertOne(photoDocument);
|
||||
}
|
||||
|
||||
public void uploadMemberPhotos() {
|
||||
Logger.info("Found " + PPRUtils.listFilesInDirectory(MEMBER_IMAGES_DIR).size() + " member photos to upload.");
|
||||
// loop over file names in the directory
|
||||
// for each file name, extract the name of the member
|
||||
for (String filename : PPRUtils.listFilesInDirectory(MEMBER_IMAGES_DIR)) {
|
||||
String firstname = filename.split("_")[1].split(".jpg")[0];
|
||||
String name = filename.split("_")[0];
|
||||
|
||||
Document speaker = speakerCollection.find(Filters.and(eq("firstName", firstname), eq("name", name))).first();
|
||||
if (speaker == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
uploadMemberPhoto(speaker.getString("_id"), loadMemberImageFromFileByName(firstname, name));
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
mongoClient.close();
|
||||
}
|
||||
|
|
|
@ -409,4 +409,18 @@ public abstract class PPRUtils {
|
|||
return doc;
|
||||
}
|
||||
|
||||
public static ArrayList<String> listFilesInDirectory(String directory) {
|
||||
File folder = new File(directory);
|
||||
File[] files = folder.listFiles();
|
||||
ArrayList<String> fileNames = new ArrayList<>();
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
if (file.isFile()) {
|
||||
fileNames.add(file.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
return fileNames;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue