Skip to content

Commit 95c552e

Browse files
authored
Fixes issue with emails not being sent (#178)
* rfr * removes try
1 parent fb42253 commit 95c552e

16 files changed

+208
-190
lines changed

src/main/java/com/google/sps/servlets/IntervieweeFeedbackServlet.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.google.sps.data.ScheduledInterviewDao;
2626
import com.google.sps.data.SendgridEmailSender;
2727
import com.google.sps.utils.EmailUtils;
28-
import com.google.sps.utils.SendgridEmailUtils;
2928
import com.sendgrid.helpers.mail.Mail;
3029
import com.sendgrid.helpers.mail.objects.Content;
3130
import com.sendgrid.helpers.mail.objects.Email;
@@ -50,7 +49,6 @@ public class IntervieweeFeedbackServlet extends HttpServlet {
5049
private ScheduledInterviewDao scheduledInterviewDao;
5150
private PersonDao personDao;
5251
private EmailSender emailSender;
53-
private EmailUtils emailUtils;
5452
static final Email sender = new Email("interviewme.business@gmail.com");
5553
private Path emailsPath =
5654
Paths.get(
@@ -64,22 +62,14 @@ public void init() {
6462
} catch (IOException e) {
6563
throw new RuntimeException(e);
6664
}
67-
init(
68-
new DatastoreScheduledInterviewDao(),
69-
new DatastorePersonDao(),
70-
emailSender,
71-
new SendgridEmailUtils());
65+
init(new DatastoreScheduledInterviewDao(), new DatastorePersonDao(), emailSender);
7266
}
7367

7468
public void init(
75-
ScheduledInterviewDao scheduledInterviewDao,
76-
PersonDao personDao,
77-
EmailSender emailSender,
78-
EmailUtils emailUtils) {
69+
ScheduledInterviewDao scheduledInterviewDao, PersonDao personDao, EmailSender emailSender) {
7970
this.scheduledInterviewDao = scheduledInterviewDao;
8071
this.personDao = personDao;
8172
this.emailSender = emailSender;
82-
this.emailUtils = emailUtils;
8373
}
8474

8575
@Override
@@ -148,8 +138,8 @@ private void sendFeedback(String intervieweeEmail, HashMap<String, String> answe
148138
String subject = "Your Interviewer has submitted feedback for your interview!";
149139
Email recipient = new Email(intervieweeEmail);
150140
String contentString =
151-
emailUtils.fileContentToString(emailsPath + "/feedbackToInterviewee.txt");
152-
Content content = new Content("text/plain", emailUtils.replaceAllPairs(answers, contentString));
141+
EmailUtils.fileContentToString(emailsPath + "/feedbackToInterviewee.txt");
142+
Content content = new Content("text/plain", EmailUtils.replaceAllPairs(answers, contentString));
153143
emailSender.sendEmail(recipient, subject, content);
154144
}
155145
}

src/main/java/com/google/sps/servlets/InterviewerFeedbackServlet.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.google.sps.data.ScheduledInterviewDao;
2626
import com.google.sps.data.SendgridEmailSender;
2727
import com.google.sps.utils.EmailUtils;
28-
import com.google.sps.utils.SendgridEmailUtils;
2928
import com.sendgrid.helpers.mail.Mail;
3029
import com.sendgrid.helpers.mail.objects.Content;
3130
import com.sendgrid.helpers.mail.objects.Email;
@@ -50,7 +49,6 @@ public class InterviewerFeedbackServlet extends HttpServlet {
5049
private ScheduledInterviewDao scheduledInterviewDao;
5150
private PersonDao personDao;
5251
private EmailSender emailSender;
53-
private EmailUtils emailUtils;
5452
static final Email sender = new Email("interviewme.business@gmail.com");
5553
private Path emailsPath =
5654
Paths.get(
@@ -64,22 +62,14 @@ public void init() {
6462
} catch (IOException e) {
6563
throw new RuntimeException(e);
6664
}
67-
init(
68-
new DatastoreScheduledInterviewDao(),
69-
new DatastorePersonDao(),
70-
emailSender,
71-
new SendgridEmailUtils());
65+
init(new DatastoreScheduledInterviewDao(), new DatastorePersonDao(), emailSender);
7266
}
7367

7468
public void init(
75-
ScheduledInterviewDao scheduledInterviewDao,
76-
PersonDao personDao,
77-
EmailSender emailSender,
78-
EmailUtils emailUtils) {
69+
ScheduledInterviewDao scheduledInterviewDao, PersonDao personDao, EmailSender emailSender) {
7970
this.scheduledInterviewDao = scheduledInterviewDao;
8071
this.personDao = personDao;
8172
this.emailSender = emailSender;
82-
this.emailUtils = emailUtils;
8373
}
8474

8575
@Override
@@ -147,8 +137,8 @@ private void sendFeedback(String interviewerEmail, HashMap<String, String> answe
147137
String subject = "Your Interviewee has submitted feedback for your interview!";
148138
Email recipient = new Email(interviewerEmail);
149139
String contentString =
150-
emailUtils.fileContentToString(emailsPath + "/feedbackToInterviewer.txt");
151-
Content content = new Content("text/plain", emailUtils.replaceAllPairs(answers, contentString));
140+
EmailUtils.fileContentToString(emailsPath + "/feedbackToInterviewer.txt");
141+
Content content = new Content("text/plain", EmailUtils.replaceAllPairs(answers, contentString));
152142
emailSender.sendEmail(recipient, subject, content);
153143
}
154144
}

src/main/java/com/google/sps/servlets/ScheduledInterviewServlet.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import com.google.sps.data.SendgridEmailSender;
3939
import com.google.sps.data.TimeRange;
4040
import com.google.sps.utils.EmailUtils;
41-
import com.google.sps.utils.SendgridEmailUtils;
4241
import com.sendgrid.Response;
4342
import com.sendgrid.SendGrid;
4443
import com.sendgrid.helpers.mail.Mail;
@@ -81,12 +80,8 @@ public class ScheduledInterviewServlet extends HttpServlet {
8180
private EmailSender emailSender;
8281
private CalendarAccess calendarAccess;
8382
private Calendar service;
84-
private EmailUtils emailUtils;
8583
private final UserService userService = UserServiceFactory.getUserService();
8684
static final Email sender = new Email("interviewme.business@gmail.com");
87-
private Path emailsPath =
88-
Paths.get(
89-
System.getProperty("user.home") + "/InterviewMe/src/main/resources/templates/email");
9085

9186
@Override
9287
public void init() {
@@ -113,23 +108,20 @@ public void init() {
113108
new DatastoreAvailabilityDao(),
114109
new DatastorePersonDao(),
115110
calendar,
116-
emailSender,
117-
new SendgridEmailUtils());
111+
emailSender);
118112
}
119113

120114
public void init(
121115
ScheduledInterviewDao scheduledInterviewDao,
122116
AvailabilityDao availabilityDao,
123117
PersonDao personDao,
124118
CalendarAccess calendarAccess,
125-
EmailSender emailSender,
126-
EmailUtils emailUtils) {
119+
EmailSender emailSender) {
127120
this.scheduledInterviewDao = scheduledInterviewDao;
128121
this.availabilityDao = availabilityDao;
129122
this.personDao = personDao;
130123
this.calendarAccess = calendarAccess;
131124
this.emailSender = emailSender;
132-
this.emailUtils = emailUtils;
133125
}
134126

135127
// Gets the current user's email and returns the ScheduledInterviews for that person.
@@ -447,22 +439,21 @@ private void sendParticipantEmail(
447439
}
448440

449441
String subject = "You have been requested to conduct a mock interview!";
450-
String contentString =
451-
emailUtils.fileContentToString(emailsPath + "/NewInterview_Interviewer.txt");
442+
String contentString = EmailUtils.fileContentToString("NewInterview_Interviewer.txt");
452443

453444
if (participantId.equals(scheduledInterview.intervieweeId())) {
454445
subject = "You have been registered for a mock interview!";
455-
contentString = emailUtils.fileContentToString(emailsPath + "/NewInterview_Interviewee.txt");
446+
contentString = EmailUtils.fileContentToString("NewInterview_Interviewee.txt");
456447
}
457448

458449
if (participantId.equals(scheduledInterview.shadowId())) {
459450
subject = "You have been registered for a mock interview!";
460-
contentString = emailUtils.fileContentToString(emailsPath + "/NewInterview_Shadow.txt");
451+
contentString = EmailUtils.fileContentToString("NewInterview_Shadow.txt");
461452
}
462453

463454
Email recipient = new Email(recipientEmail);
464455
Content content =
465-
new Content("text/plain", emailUtils.replaceAllPairs(emailedDetails, contentString));
456+
new Content("text/plain", EmailUtils.replaceAllPairs(emailedDetails, contentString));
466457
emailSender.sendEmail(recipient, subject, content);
467458
}
468459
// Formats the position string that is sent in an email. For example SOFTWARE_ENGINEER -> Software
@@ -471,7 +462,7 @@ private static String formatPositionString(String str) {
471462
String splitString[] = str.split("_", 0);
472463
String formattedPositionString = "";
473464
for (String s : splitString) {
474-
formattedPositionString += s.substring(0, 1) + s.substring(1).toLowerCase();
465+
formattedPositionString += s.substring(0, 1) + s.substring(1).toLowerCase() + " ";
475466
}
476467
return formattedPositionString.trim();
477468
}

src/main/java/com/google/sps/utils/EmailUtils.java

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,36 @@
1414

1515
package com.google.sps.utils;
1616

17+
// using SendGrid's Java Library
18+
// https://github.com/sendgrid/sendgrid-java
19+
import com.sendgrid.Method;
20+
import com.sendgrid.Request;
1721
import com.sendgrid.Response;
18-
import com.sendgrid.helpers.mail.objects.Content;
19-
import com.sendgrid.helpers.mail.objects.Email;
22+
import com.sendgrid.SendGrid;
2023
import java.io.IOException;
24+
import java.nio.charset.StandardCharsets;
25+
import java.nio.file.Files;
26+
import java.nio.file.Paths;
27+
import java.util.stream.Stream;
28+
import java.util.Map;
2129
import java.util.HashMap;
30+
import java.lang.ClassLoader;
31+
import java.net.URI;
32+
import java.net.URL;
33+
import java.net.URISyntaxException;
2234

23-
/** EmailUtils includes the basic methods used when formatting emails. */
24-
public interface EmailUtils {
35+
// Used to format contents of an email
36+
public class EmailUtils {
2537

2638
// Returns the contents of the file specified at filePath as a String. Useful for converting
2739
// predefined email templates to text.
28-
public String fileContentToString(String filePath) throws IOException;
40+
public static String fileContentToString(String fileName) throws IOException {
41+
StringBuilder contentBuilder = new StringBuilder();
42+
Stream<String> stream =
43+
Files.lines(Paths.get(getEmailTemplateResource(fileName)), StandardCharsets.UTF_8);
44+
stream.forEach(s -> contentBuilder.append(s).append("\n"));
45+
return contentBuilder.toString();
46+
}
2947

3048
/**
3149
* Modifies and returns @param str. Replaces all occurences in @param str of each key in @param
@@ -34,5 +52,21 @@ public interface EmailUtils {
3452
// Ex. str = "You will be mock interviewing {{interviewee_full_name}} on {{formatted_date}}."
3553
// toReplace = { ("{{interviewee_full_name}}","Tess"), ("{{formatted_date}}", "June 6, 2022") }
3654
// Returned: "You will be mock interviewing Tess on June 6, 2022."
37-
public String replaceAllPairs(HashMap<String, String> toReplace, String str);
55+
public static String replaceAllPairs(HashMap<String, String> toReplace, String str) {
56+
for (Map.Entry<String, String> entry : toReplace.entrySet()) {
57+
str = str.replace(entry.getKey(), entry.getValue());
58+
}
59+
return str;
60+
}
61+
62+
private static URI getEmailTemplateResource(String fileName) {
63+
URL resource = EmailUtils.class.getResource("/templates/email/" + fileName);
64+
URI result;
65+
try {
66+
result = resource.toURI();
67+
} catch (URISyntaxException e) {
68+
throw new RuntimeException(e);
69+
}
70+
return result;
71+
}
3872
}

src/main/java/com/google/sps/utils/FakeEmailUtils.java

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/main/java/com/google/sps/utils/SendgridEmailUtils.java

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)