Search in sources :

Example 1 with KeyValuesFinder

use of org.kuali.kfs.krad.keyvalues.KeyValuesFinder in project cu-kfs by CU-CommunityApps.

the class PayeeACHAccountExtractServiceImpl method getResolvedEmailBody.

/**
 * Helper method for replacing "[propertyName]"-style placeholders in the email body
 * with actual property values from the given Payee ACH Account, in addition to
 * replacing literal "\n" with newline characters accordingly. Potentially sensitive
 * placeholders will be replaced with empty text except for the bank name.
 * Placeholders referencing properties with value finders will print the matching key/value
 * label instead. Any placeholders that could not be resolved successfully will be replaced
 * with empty text.
 */
@SuppressWarnings("deprecation")
protected String getResolvedEmailBody(PayeeACHAccount achAccount, String emailBody) {
    Pattern placeholderPattern = Pattern.compile("\\[([^\\]]+)\\]");
    Matcher emailMatcher = placeholderPattern.matcher(emailBody.replace("\\n", "\n"));
    // Use a StringBuffer here, due to the Matcher class not supporting StringBuilder for appending operations.
    StringBuffer resolvedEmailBody = new StringBuffer(emailBody.length());
    // Replace all placeholders one by one. The pattern has a single group in it to help with retrieving just the property name and not the brackets.
    while (emailMatcher.find()) {
        String propertyName = emailMatcher.group(1);
        AttributeDefinition attDefinition = dataDictionaryService.getAttributeDefinition(PayeeACHAccount.class.getName(), propertyName);
        String replacement;
        // Make sure property exists in data dictionary and is either not potentially sensitive or is the safe-to-use bank name property.
        if (attDefinition != null) {
            AttributeSecurity attSecurity = attDefinition.getAttributeSecurity();
            if (attSecurity != null && (attSecurity.isHide() || attSecurity.isMask() || attSecurity.isPartialMask()) && !CUPdpConstants.PAYEE_ACH_ACCOUNT_EXTRACT_BANK_NAME_PROPERTY.equals(propertyName)) {
                // Replace potentially-sensitive placeholders with an empty string.
                replacement = KFSConstants.EMPTY_STRING;
            } else {
                // Replace the placeholder with the property value, or with an empty string if null or invalid.
                try {
                    Object propertyValue = ObjectPropertyUtils.getPropertyValue(achAccount, propertyName);
                    replacement = ObjectUtils.isNotNull(propertyValue) ? propertyValue.toString() : KFSConstants.EMPTY_STRING;
                    // If a values finder is defined, use the label from the matching key/value pair instead.
                    if (attDefinition.getControl() != null && StringUtils.isNotBlank(attDefinition.getControl().getValuesFinderClass())) {
                        KeyValuesFinder valuesFinder = (KeyValuesFinder) Class.forName(attDefinition.getControl().getValuesFinderClass()).newInstance();
                        String key = replacement;
                        replacement = valuesFinder.getKeyLabel(key);
                        // If the key is in the label, then remove it from the label.
                        if (attDefinition.getControl().getIncludeKeyInLabel() != null && attDefinition.getControl().getIncludeKeyInLabel().booleanValue()) {
                            // Check for key-and-dash or key-in-parentheses, and remove them if found.
                            // (Former can come from BO values finders, latter is only for custom values finders that append the keys as such.)
                            String keyAndDashPrefix = key + " - ";
                            String keyInParenSuffix = " (" + key + ")";
                            replacement = replacement.startsWith(keyAndDashPrefix) ? StringUtils.substringAfter(replacement, keyAndDashPrefix) : (replacement.endsWith(keyInParenSuffix) ? StringUtils.substringBeforeLast(replacement, keyInParenSuffix) : replacement);
                        }
                    }
                    // Because of the way that Matcher.appendReplacement() works, escape the special replacement characters accordingly.
                    if (replacement.indexOf('\\') != -1) {
                        replacement = replacement.replace("\\", "\\\\");
                    }
                    if (replacement.indexOf('$') != -1) {
                        replacement = replacement.replace("$", "\\$");
                    }
                } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | RuntimeException e) {
                    replacement = KFSConstants.EMPTY_STRING;
                }
            }
        } else {
            // Replace non-data-dictionary-defined property placeholders with an empty string.
            replacement = KFSConstants.EMPTY_STRING;
        }
        emailMatcher.appendReplacement(resolvedEmailBody, replacement);
    }
    emailMatcher.appendTail(resolvedEmailBody);
    return resolvedEmailBody.toString();
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) PayeeACHAccount(org.kuali.kfs.pdp.businessobject.PayeeACHAccount) KeyValuesFinder(org.kuali.kfs.krad.keyvalues.KeyValuesFinder) AttributeDefinition(org.kuali.kfs.krad.datadictionary.AttributeDefinition) AttributeSecurity(org.kuali.kfs.krad.datadictionary.AttributeSecurity)

Aggregations

Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 AttributeDefinition (org.kuali.kfs.krad.datadictionary.AttributeDefinition)1 AttributeSecurity (org.kuali.kfs.krad.datadictionary.AttributeSecurity)1 KeyValuesFinder (org.kuali.kfs.krad.keyvalues.KeyValuesFinder)1 PayeeACHAccount (org.kuali.kfs.pdp.businessobject.PayeeACHAccount)1