Java tutorial
package com.webbfontaine.valuewebb.utils; import com.webbfontaine.valuewebb.model.Company; import com.webbfontaine.valuewebb.model.TtGen; import com.webbfontaine.valuewebb.model.util.Utils; import com.webbfontaine.valuewebb.props.ApplicationProperties; import org.apache.commons.lang3.StringUtils; import org.jboss.seam.log.Log; import org.jboss.seam.log.Logging; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.regex.Pattern; /** * Copyrights 2002-2013 Webb Fontaine * This software is the proprietary information of Webb Fontaine. * Its use is subject to License terms. * Developer: nigiyan * Date: 03/05/2013 */ public class TTMailUtils { private static final Log LOGGER = Logging.getLog(TTMailUtils.class); private static final Pattern RECIPIENT_PATTERN = Pattern.compile(","); private static final int SMS_BODY_LENGTH = 128; private static final String RESPONSE_OK_MAIL_SUBJECT = "NOTICE OF FCVR RELEASE"; private static final String RESPONSE_OK_SMS_BODY = "WEBB FONTAINE: FCVR No. $FCVR_Number$ for IDF No. $IDF_Number$ is ready. Kindly check your email. Please do not reply."; private HashMap textVariables; private TtGen ttGen; public TTMailUtils(TtGen ttGen) { this.ttGen = ttGen; initTextVariables(); } public void initTextVariables() { textVariables = new HashMap<String, String>(); textVariables.put("$IDF_Number$", ttGen.getIdfNum()); textVariables.put("$FCVR_Number$", ttGen.getFcvrNum()); textVariables.put("$Importer_TIN$", ttGen.getImpTin()); textVariables.put("$Importer_Name$", StringUtils.defaultString(ttGen.getImpNam())); textVariables.put("$Invoice_Number$", ttGen.getTtInvs().get(0).getdInvNum()); textVariables.put("$contact_email$", StringUtils.defaultString(ApplicationProperties.getContactEmail())); textVariables.put("$service_tel$", ApplicationProperties.getCustomerServiceTel()); textVariables.put("$idf_string$", Utils.getIdfStringDependsOnCountryVersion()); } public String getResponseOkSMSBody() { return replaceTextVariables(Utils.translate(RESPONSE_OK_SMS_BODY), this.textVariables); } public String getResponseOkMailSubject() { return Utils.translate(RESPONSE_OK_MAIL_SUBJECT); } public String getResponseOkMailBody() { StringBuilder body = new StringBuilder("\n").append(Utils.translate( "We are pleased to advise that you may pick up the following FCVR at our office premises:")); body.append("\n\n").append(Utils.translate("FCVR No. $FCVR_Number$")); body.append('\n').append(Utils.translate("IDF No. $IDF_Number$")); body.append('\n').append(Utils.translate("Importer. $Importer_TIN$ $Importer_Name$")); body.append('\n').append(Utils.translate("Invoice No. $Invoice_Number$")); body.append("\n\n").append(Utils.translate("We look forward to seeing you again.")); body.append("\n\n").append(Utils.translate("WEBB FONTAINE CUSTOMER SERVICE")); body.append("\n\n").append(Utils.translate( "1. Please disregard this notice should you have already retrieved the above-mentioned FCVR.")); body.append('\n').append(Utils.translate( "2. This is a system generated notification. Please do not reply to the email address above.")); body.append("\n\n").append(Utils.translate( "The information contained in this email and its attachment(s) if any, are confidential and intended solely for the use its addressee(s).")); body.append('\n').append(Utils.translate( "If you are not an intended recipient of this e-mail kindly send it back to $contact_email$ and delete it at your end. Unauthorized publication, use, dissemination or disclosure of the content and/or attachment of this e-mail, either in whole or in part is strictly prohibited.")); return replaceTextVariables(body.toString(), textVariables); } public List<String> getMailRecepientsForResponseOk() { List<String> emails = new ArrayList<>(); if (ApplicationProperties.isImporterMailingEnabled()) { Company company = loadCompany(); if (company != null) { String email = company.getEmail(); if (!StringUtils.isEmpty(email)) { emails.add(email); } } } if (ApplicationProperties.isApplicantMailingEnabled()) { if (!StringUtils.isEmpty(ttGen.getAppEmail())) { emails.add(ttGen.getAppEmail()); } } if (StringUtils.trimToNull(ApplicationProperties.getAdditionalMailingList()) != null) { String[] additionalEmails = RECIPIENT_PATTERN.split(ApplicationProperties.getAdditionalMailingList()); emails.addAll(Arrays.asList(additionalEmails)); } return emails; } public String getDuplicationWarningMailSubject(Long ttId) { return "DA2019 POSSIBLE DUPLICATION TT " + ttId; } public List<String> getDuplicationWarningMailingList() { List<String> emails = new ArrayList<>(); if (StringUtils.trimToNull(ApplicationProperties.getDuplicationWarningMailingList()) != null) { String[] duplicationWarningEmails = RECIPIENT_PATTERN .split(ApplicationProperties.getDuplicationWarningMailingList()); emails.addAll(Arrays.asList(duplicationWarningEmails)); } return emails; } public String getDuplicationWarningMailBody() { StringBuilder body = new StringBuilder("\n<html><body>"); body.append( "<p>This TT has been targeted due to risk of duplication by the importer and/or declarant.</p>"); body.append("At least two of the following fields have been duplicated on a previous TT"); body.append("<ul><li>Bill of Lading Number</li>"); body.append("<li>Container Number</li>"); body.append("<li>Invoice Date</li>"); body.append("<li>Seal Number</li>"); body.append("<li>IDF Number</li></ul>"); body.append("<p>Believe that this may be an attempt by importer/declarant to reapply with same "); body.append("commercial documentation in order to try and get a lower value FCVR.</p>"); body.append("</body></html>"); return body.toString(); } public List<String> getSMSRecepientsForResponseOk() { StringBuilder smsRecepients = new StringBuilder(SMS_BODY_LENGTH); if (ApplicationProperties.isApplicantSMSEnabled()) { if (!StringUtils.isEmpty(ttGen.getAppTel())) { smsRecepients.append(ttGen.getAppTel()).append(','); } } if (ApplicationProperties.isImporterSMSEnabled()) { Company company = loadCompany(); if (company != null) { String tel = company.getTel(); if (!StringUtils.isEmpty(tel)) { smsRecepients.append(tel).append(','); } } } if (StringUtils.trimToNull(ApplicationProperties.getAdditionalSmsList()) != null) { smsRecepients.append(Arrays.asList(ApplicationProperties.getAdditionalSmsList())); } return extractRecepients(smsRecepients.toString()); } static List<String> extractRecepients(CharSequence str) { String[] recepients = RECIPIENT_PATTERN.split(str); List<String> retVal = new ArrayList<>(recepients.length); for (String recepient : recepients) { if ((recepient = StringUtils.trimToNull(recepient)) != null) { retVal.add(recepient); } } return retVal; } public String replaceTextVariables(String text, HashMap<String, String> textVariables) { for (String key : textVariables.keySet()) { String replace = ""; if (textVariables.get(key) != null) { replace = textVariables.get(key); } text = text.replaceAll(Pattern.quote(key), replace); } return text; } private Company loadCompany() { Company company = null; try { List companies = Utils.createDirectQuery("select c from Company c where c.tin = :tin") .setParameter("tin", ttGen.getImpTin()).getResultList(); if (companies.isEmpty()) { LOGGER.warn("Cound not find Company for tin {0}, TT ID: {1}.", ttGen.getImpTin(), ttGen.getId()); } else { if (companies.size() == 1) { company = (Company) companies.get(0); } else { LOGGER.warn("More than one company found for tin {0}, TT ID: {1}.", ttGen.getImpTin(), ttGen.getId()); } } } catch (Exception e) { LOGGER.warn("Could not load Company profile for TT {0}", e, ttGen.getId()); } return company; } }