From 88aa19c12a237f286e48c9e6aa29191ea6aa89b0 Mon Sep 17 00:00:00 2001 From: amontenegro Date: Mon, 1 Jul 2024 15:05:03 -0600 Subject: [PATCH] Update code so it uses string builder --- .../cli/manager/EmailMessageSenderImpl.java | 53 ++++++++++--------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/orcid-scheduler-web/src/main/java/org/orcid/scheduler/email/cli/manager/EmailMessageSenderImpl.java b/orcid-scheduler-web/src/main/java/org/orcid/scheduler/email/cli/manager/EmailMessageSenderImpl.java index 810bef3977e..a974f4c57f4 100644 --- a/orcid-scheduler-web/src/main/java/org/orcid/scheduler/email/cli/manager/EmailMessageSenderImpl.java +++ b/orcid-scheduler-web/src/main/java/org/orcid/scheduler/email/cli/manager/EmailMessageSenderImpl.java @@ -320,6 +320,7 @@ public void sendEmailMessages() { EmailMessage digestMessage = createDigest(orcid, notifications); digestMessage.setFrom(EmailConstants.DO_NOT_REPLY_NOTIFY_ORCID_ORG); digestMessage.setTo(primaryEmail.getEmail()); + boolean successfullySent = mailGunManager.sendEmail(digestMessage.getFrom(), digestMessage.getTo(), digestMessage.getSubject(), digestMessage.getBodyText(), digestMessage.getBodyHtml()); if (successfullySent) { @@ -516,16 +517,16 @@ public Integer getCounter() { } private String renderCreationDate(XMLGregorianCalendar createdDate) { - String result = new String(); - result += createdDate.getYear(); - result += "-" + (createdDate.getMonth() < 10 ? "0" + createdDate.getMonth() : createdDate.getMonth()); - result += "-" + (createdDate.getDay() < 10 ? "0" + createdDate.getDay() : createdDate.getDay()); - return result; + StringBuilder result = new StringBuilder(); + result.append(createdDate.getYear()); + result.append("-").append(createdDate.getMonth() < 10 ? "0" + createdDate.getMonth() : createdDate.getMonth()); + result.append("-").append(createdDate.getDay() < 10 ? "0" + createdDate.getDay() : createdDate.getDay()); + return result.toString(); } public void addElement(XMLGregorianCalendar createdDate, Item item) { init(item.getItemType().name(), item.getActionType() == null ? null : item.getActionType().name()); - String value = null; + StringBuilder value = new StringBuilder(); switch (item.getItemType()) { case DISTINCTION: case EDUCATION: @@ -534,33 +535,35 @@ public void addElement(XMLGregorianCalendar createdDate, Item item) { case MEMBERSHIP: case QUALIFICATION: case SERVICE: - value = "" + item.getAdditionalInfo().get("org_name") + " " + (item.getItemName() != null ? item.getItemName() : "") + " (" - + renderCreationDate(createdDate) + ')'; + value.append("").append(item.getAdditionalInfo().get("org_name")).append(" ").append((item.getItemName() != null ? item.getItemName() : "")).append(" (") + .append(renderCreationDate(createdDate)).append(')'); break; default: - value = item.getItemName() != null ? item.getItemName() : ""; + value.append(item.getItemName() != null ? item.getItemName() : ""); if (item.getExternalIdentifier() != null) { - value += " " + item.getExternalIdentifier().getType() + ": " + item.getExternalIdentifier().getValue(); + value.append(" ").append(item.getExternalIdentifier().getType()).append(": ").append(item.getExternalIdentifier().getValue()); } - value += " (" + renderCreationDate(createdDate) + ')'; + value.append(" (").append(renderCreationDate(createdDate)).append(')'); break; } // Set the external identifiers list String externalIdentifiersList = generateExternalIdentifiersList(item); - if(externalIdentifiersList != null) { - value += externalIdentifiersList; + if(StringUtils.isNotBlank(externalIdentifiersList)) { + value.append(externalIdentifiersList); } + String stringValue = value.toString(); + Set elements; if (item.getActionType() != null) { elements = updates.get(item.getItemType().name()).get(item.getActionType().name()); } else { elements = updates.get(item.getItemType().name()).get(ActionType.UNKNOWN.name()); } - if (!elements.contains(value)) { + if (!elements.contains(stringValue)) { if (counter < maxNotificationsToShowPerClient) { - elements.add(value); + elements.add(stringValue); } counter += 1; } @@ -579,48 +582,48 @@ private void init(String itemType, String actionType) { } private String generateExternalIdentifiersList(Item item) { - String extIdsHtmlList = null; + StringBuilder extIdsHtmlList = new StringBuilder(); if (item.getAdditionalInfo() != null) { if(item.getAdditionalInfo().containsKey("external_identifiers")) { Map extIds = (Map) item.getAdditionalInfo().get("external_identifiers"); if(extIds != null && extIds.containsKey("externalIdentifier")) { List extIdsList = (List) extIds.get("externalIdentifier"); if(extIdsList != null) { - extIdsHtmlList = "
    "; + extIdsHtmlList.append("
      "); for(Map extIdMap : extIdsList) { String extIdType = extIdMap.containsKey("type") ? (String) extIdMap.get("type") : null; // External id type must not be null, so, in case it is lets log a warning if(extIdType == null) { LOGGER.warn("External ID type is null for '" + item.getPutCode() + "', '" + item.getItemName() + "'"); } - extIdsHtmlList += "
    • " + extIdType + ": "; + extIdsHtmlList.append("
    • ").append(extIdType).append(": "); // Check if there is an URL if(extractValue(extIdMap, "url") != null) { String url = extractValue(extIdMap, "url"); - extIdsHtmlList += "" + url + ""; + extIdsHtmlList.append("").append(url).append(""); } else if (extractValue(extIdMap, "normalized") != null) { //If there is no URL, check for the normalized value String value = extractValue(extIdMap, "normalized"); - extIdsHtmlList += value; + extIdsHtmlList.append(value); } else if(extIdMap.containsKey("value")) { try { String value = (String) extIdMap.get("value"); - extIdsHtmlList += value; + extIdsHtmlList.append(value); } catch (NullPointerException e) { LOGGER.warn("External ID value is null for '" + item.getPutCode() + "', '" + item.getItemName() + "'"); } } else { - extIdsHtmlList += "Unavailable - please contact support"; + extIdsHtmlList.append("Unavailable - please contact support"); LOGGER.warn("Unable to find a printable value for External ID '" + item.getPutCode() + "', '" + item.getItemName() + "'"); } - extIdsHtmlList += "
    • "; + extIdsHtmlList.append(""); } - extIdsHtmlList += "
    "; + extIdsHtmlList.append("
"); } } } } - return extIdsHtmlList; + return extIdsHtmlList.toString(); } }