Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/Config/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,9 @@
import com.mongodb.client.MongoDatabase;
import io.javalin.Javalin;
import io.javalin.http.HttpResponseException;
import org.bson.types.ObjectId;

import java.util.HashMap;
import java.util.Optional;
import org.bson.types.ObjectId;

public class AppConfig {
public static Long ASYNC_TIME_OUT = 10L;
Expand Down Expand Up @@ -103,6 +102,8 @@ public static Javalin appFactory(DeploymentLevel deploymentLevel) {
app.post("/get-documents", pdfController.pdfGetFilesInformation);
app.post("/get-application-questions", pdfController.getApplicationQuestions);
app.post("/fill-application", pdfController.fillPDFForm);
app.post("/mail-file", fileController.fileMail);
app.post("/get-mail-info", fileController.getMailInformation);

/* -------------- FILE MANAGEMENT v2 --------------------- */
app.post("/upload-file", fileController.fileUpload);
Expand Down
2 changes: 2 additions & 0 deletions src/main/File/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bson.types.ObjectId;

import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Objects;

Expand All @@ -21,6 +22,7 @@ public class File {
@Getter @Setter private Date uploadedAt;
@Getter @Setter private String username;
@Getter @Setter private String organizationName;
@Getter @Setter private LocalDateTime lastMailedAt; // not used yet while mail form uses GridFS

@Getter
@Setter
Expand Down
48 changes: 48 additions & 0 deletions src/main/File/FileController.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.bson.types.ObjectId;
import org.json.JSONException;
import org.json.JSONObject;

Expand All @@ -32,9 +33,13 @@ public class FileController {
private FileDao fileDao;
private EncryptionController encryptionController;

// need to include while viewDocuments used PDF controller (for mailing)
private MongoDatabase db;

public FileController(MongoDatabase db, UserDao userDao, FileDao fileDao) {
this.userDao = userDao;
this.fileDao = fileDao;
this.db = db;
try {
this.encryptionController = new EncryptionController(db);
} catch (Exception e) {
Expand Down Expand Up @@ -466,6 +471,49 @@ public FileController(MongoDatabase db, UserDao userDao, FileDao fileDao) {
}
};

public Handler fileMail =
ctx -> {
String username = ctx.sessionAttribute("username");
UserType userType = ctx.sessionAttribute("privilegeLevel");
JSONObject req = new JSONObject(ctx.body());
String fileIDStr = req.getString("fileId");
String price = req.getString("price");
String mailAddress = req.getString("mailAddress");
String returnAddress = req.getString("returnAddress");
ObjectId fileId = new ObjectId(fileIDStr);
String description = req.getString("description");

MailFileService mailFileService =
new MailFileService(
db,
fileId,
username,
userType,
description,
price,
mailAddress,
returnAddress,
encryptionController);

Message response = mailFileService.executeAndGetResponse();
ctx.result(response.toResponseString());
};
public Handler getMailInformation =
ctx -> {
log.error("hello");
String username = ctx.sessionAttribute("username");
String organization = ctx.sessionAttribute("organization");
String applicationType = ctx.sessionAttribute("applicationType");

GetMailInformationService getMailInformationService =
new GetMailInformationService(username, organization, applicationType);

Message response = getMailInformationService.executeAndGetResponse();
if (response == FileMessage.SUCCESS) {
ctx.result(getMailInformationService.getMailInformation().toString());
}
};

public static String getPDFTitle(String fileName, PDDocument pdfDocument) {
String title = fileName;
pdfDocument.setAllSecurityToBeRemoved(true);
Expand Down
3 changes: 2 additions & 1 deletion src/main/File/FileMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public enum FileMessage implements Message {
SUCCESS("SUCCESS:Success."),
NO_SUCH_FILE("NO_SUCH_FILE:PDF does not exist"),
ENCRYPTION_ERROR("ENCRYPTION_ERROR:Error encrypting/decrypting"),
FILE_EXISTS("ERROR: File already exists!");
FILE_EXISTS("ERROR: File already exists!"),
ALREADY_MAILED_RECENTLY("ALREADY_MAILED_RECENTLY:The form has been mailed recently and cannot be mailed again so soon.");

private String errorMessage;
private String fileId; // optional
Expand Down
46 changes: 46 additions & 0 deletions src/main/File/Services/GetMailInformationService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package File.Services;

import static io.javalin.Javalin.log;

import Config.Message;
import Config.Service;
import File.FileMessage;
import kong.unirest.json.JSONObject;

public class GetMailInformationService implements Service {
private String username;
private String organization;
private String applicationType;
private String price;
private String mailAddress;
private String returnAddress;

public GetMailInformationService(String applicationType, String username, String organization) {
this.applicationType = applicationType;
this.username = username;
this.organization = organization;
}

@Override
public Message executeAndGetResponse() {
try {
this.mailAddress = "example street";
this.returnAddress = "return street";
this.price = "0.10";
// Assuming Message is a type that can encapsulate a JSON response.
return FileMessage.SUCCESS;
} catch (Exception e) {
log.error("Error in executeAndGetResponse: " + e.getMessage(), e);
return FileMessage.SERVER_ERROR;
}
}

public JSONObject getMailInformation() {
JSONObject jsonResponse = new JSONObject();
jsonResponse.put("price", "0.10");
jsonResponse.put("mailAddress", "123 example street");
jsonResponse.put("returnAddress", "456 return street");
jsonResponse.put("status", "SUCCESS");
return jsonResponse;
}
}
168 changes: 168 additions & 0 deletions src/main/File/Services/MailFileService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package File.Services;

import static Issue.IssueController.issueReportActualURL;
import static io.javalin.Javalin.log;

import Config.Message;
import Config.Service;
import File.FileMessage;
import Issue.IssueReportMessage;
import Security.EncryptionController;
import User.UserType;
import com.mongodb.MongoException;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.model.Updates;
import java.time.LocalDateTime;
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import org.json.JSONArray;
import org.json.JSONObject;

public class MailFileService implements Service {

private ObjectId fileId;
private String username;
private UserType privilegeLevel;
private MongoDatabase db;
private EncryptionController encryptionController;
private String description;
private String mailAddress;
private String returnAddress;
private String price;
private String applicationTitle;

public MailFileService(
MongoDatabase db,
ObjectId fileId,
String username,
UserType privilegeLevel,
String description,
String price,
String mailAddress,
String returnAddress,
EncryptionController encryptionController) {
this.db = db;
this.fileId = fileId;
this.username = username;
this.privilegeLevel = privilegeLevel;
this.description = description;
this.applicationTitle = this.findApplicationTitle(fileId);
this.mailAddress = mailAddress;
this.returnAddress = returnAddress;
this.price = price;
this.encryptionController = encryptionController;
}

@Override
public Message executeAndGetResponse() {
try {
MongoCollection<Document> mailCollection = db.getCollection("mail");

// if (shouldMail(mailCollection, fileId)) {
if (true) {
Message slackResponse = sendSlackNotification("File Mailing Request", this.description);
if (slackResponse != IssueReportMessage.SUCCESS) {
return slackResponse;
}

updateLastMailedTime(mailCollection, fileId);
return FileMessage.SUCCESS;
} else {
return FileMessage.ALREADY_MAILED_RECENTLY;
}
} catch (Exception e) {
log.error("Error in executeAndGetResponse: " + e.getMessage(), e);
return FileMessage.SERVER_ERROR;
}
}

private String findApplicationTitle(ObjectId fileId) {
try {
MongoCollection<Document> completedApplicationsCollection =
db.getCollection("COMPLETED_APPLICATION.files");

// Find the document in the collection with the matching fileId
Document applicationDocument =
completedApplicationsCollection.find(Filters.eq("_id", fileId)).first();
log.error(String.valueOf(applicationDocument));

if (applicationDocument != null) {
// Extract the applicationTitle field from the document
String applicationTitle = applicationDocument.getString("filename");
if (applicationTitle != null) {
return applicationTitle;
}
}
} catch (MongoException e) {
log.error("MongoDB error in findApplicationTitle: " + e.getMessage(), e);
} catch (Exception e) {
log.error("Error in findApplicationTitle: " + e.getMessage(), e);
}

// Return a default value or handle the case when the title is not found
return "No Title found";
}

private void updateLastMailedTime(MongoCollection<Document> mailCollection, ObjectId fileId) {
Bson filter = Filters.eq("fileId", fileId);
Bson updateOperation =
Updates.combine(
Updates.set("lastMailedAt", LocalDateTime.now().toString()),
Updates.set("applicationTitle", applicationTitle),
Updates.set("price", price),
Updates.set("mailAddress", mailAddress),
Updates.set("returnAddress", returnAddress));
mailCollection.updateOne(filter, updateOperation, new UpdateOptions().upsert(true));
}

// want to remove this for the future
private Message sendSlackNotification(String title, String description) {
JSONArray blocks = new JSONArray();
JSONObject titleJson = new JSONObject();
titleJson.put("type", "section");
titleJson.put(
"text", new JSONObject().put("type", "mrkdwn").put("text", "*Issue Title: * " + title));
blocks.put(titleJson);

JSONObject desJson = new JSONObject();
desJson.put("type", "section");
desJson.put(
"text",
new JSONObject().put("type", "mrkdwn").put("text", "*Issue Description: * " + description));
blocks.put(desJson);

JSONObject input = new JSONObject();
input.put("blocks", blocks);

HttpResponse<?> posted =
Unirest.post(issueReportActualURL)
.header("accept", "application/json")
.body(input.toString())
.asEmpty();

if (!posted.isSuccess()) {
return IssueReportMessage.SLACK_FAILED;
}
return IssueReportMessage.SUCCESS;
}

private boolean shouldMail(MongoCollection<Document> mailCollection, ObjectId fileId) {
Document mailData = mailCollection.find(Filters.eq("fileId", fileId)).first();

if (mailData == null) {
// If no record exists, it means it hasn't been mailed before
return true;
}

String lastMailedAtStr = mailData.getString("lastMailedAt");
LocalDateTime lastMailedAt =
lastMailedAtStr != null ? LocalDateTime.parse(lastMailedAtStr) : null;
return lastMailedAt == null || LocalDateTime.now().isAfter(lastMailedAt.plusDays(1));
}
}
1 change: 0 additions & 1 deletion src/main/Form/Form.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ public String getUploaderUsername() {
public boolean isTemplate() {
return isTemplate;
}

public FormMetadata getMetadata() {
return metadata;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/Form/FormController.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public FormController(MongoDatabase db, FormDao formDao) {
ctx.result(response.toResponseString());
};

public User userCheck(String req) {
public User userCheck(String req) {
log.info("userCheck Helper started");
String username;
User user = null;
Expand Down