Skip to content

Commit

Permalink
Update code so it uses string builder
Browse files Browse the repository at this point in the history
  • Loading branch information
amontenegro committed Jul 1, 2024
1 parent 96977df commit 88aa19c
Showing 1 changed file with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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:
Expand All @@ -534,33 +535,35 @@ public void addElement(XMLGregorianCalendar createdDate, Item item) {
case MEMBERSHIP:
case QUALIFICATION:
case SERVICE:
value = "<i>" + item.getAdditionalInfo().get("org_name") + "</i> " + (item.getItemName() != null ? item.getItemName() : "") + " ("
+ renderCreationDate(createdDate) + ')';
value.append("<i>").append(item.getAdditionalInfo().get("org_name")).append("</i> ").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<String> 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;
}
Expand All @@ -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<Map> extIdsList = (List<Map>) extIds.get("externalIdentifier");
if(extIdsList != null) {
extIdsHtmlList = "<ul>";
extIdsHtmlList.append("<ul>");
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 += "<li style=\"padding-left: 0;margin-top: 2px;\">" + extIdType + ": ";
extIdsHtmlList.append("<li style=\"padding-left: 0;margin-top: 2px;\">").append(extIdType).append(": ");
// Check if there is an URL
if(extractValue(extIdMap, "url") != null) {
String url = extractValue(extIdMap, "url");
extIdsHtmlList += "<a style=\"text-decoration: underline;color: #085c77;\" target=\"_blank\" href=\"" + url + "\">" + url + "</a>";
extIdsHtmlList.append("<a style=\"text-decoration: underline;color: #085c77;\" target=\"_blank\" href=\"").append(url).append("\">").append(url).append("</a>");
} 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 += "</li>";
extIdsHtmlList.append("</li>");
}
extIdsHtmlList += "</ul>";
extIdsHtmlList.append("</ul>");
}
}
}
}
return extIdsHtmlList;
return extIdsHtmlList.toString();
}
}

Expand Down

0 comments on commit 88aa19c

Please sign in to comment.