Search in sources :

Example 46 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project dhis2-core by dhis2.

the class AttributeOptionComboLoader method loadCategoryOptionCombo.

/**
 * Fetches a {@see CategoryOptionCombo} by "id" (based on the provided
 * IdScheme)
 *
 * The {@see CategoryOptionCombo} contains tha associated
 * {@see CategoryCombo} and all the associated {@see CategoryOption}
 *
 * @param idScheme a {@see IdScheme}
 * @param id the {@see CategoryOptionCombo} id to use
 * @return a {@see CategoryOptionCombo} or null
 */
private CategoryOptionCombo loadCategoryOptionCombo(IdScheme idScheme, String id) {
    String key = "categoryoptioncomboid";
    StringSubstitutor sub = new StringSubstitutor(ImmutableMap.<String, String>builder().put("key", key).put("resolvedScheme", Objects.requireNonNull(resolveId(idScheme, key, id))).build());
    try {
        return jdbcTemplate.queryForObject(sub.replace(SQL_GET_CATEGORYOPTIONCOMBO), (rs, i) -> bind(key, rs));
    } catch (EmptyResultDataAccessException e) {
        return null;
    }
}
Also used : StringSubstitutor(org.apache.commons.text.StringSubstitutor) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Example 47 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project dhis2-core by dhis2.

the class RelationshipTypeJoinGenerator method addRelationshipWhereClause.

private static String addRelationshipWhereClause(Long relationshipTypeId, RelationshipEntity relationshipEntity) {
    String sql = new StringSubstitutor(ImmutableMap.<String, Long>builder().put("relationshipid", relationshipTypeId).build()).replace(RELATIONSHIP_JOIN);
    sql += " AND ";
    switch(relationshipEntity) {
        case TRACKED_ENTITY_INSTANCE:
            return sql + "tei.uid = ax.tei ";
        case PROGRAM_STAGE_INSTANCE:
            return sql + "psi.uid = ax.psi ";
        case PROGRAM_INSTANCE:
            return sql + "pi.uid = ax.pi ";
        default:
            throw new IllegalQueryException(new ErrorMessage(ErrorCode.E7227, relationshipEntity.name()));
    }
}
Also used : StringSubstitutor(org.apache.commons.text.StringSubstitutor) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) ErrorMessage(org.hisp.dhis.feedback.ErrorMessage)

Example 48 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project workbench by all-of-us.

the class MailServiceImpl method buildHtml.

private String buildHtml(final String resource, final Map<EmailSubstitutionField, String> replacementMap) throws MessagingException {
    final String emailContent;
    try {
        emailContent = String.join("\n", Resources.readLines(Resources.getResource(resource), StandardCharsets.UTF_8));
    } catch (IOException e) {
        throw new MessagingException("Error reading in email");
    }
    final Map<String, String> stringMap = replacementMap.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().toString(), Map.Entry::getValue));
    return new StringSubstitutor(stringMap).replace(emailContent);
}
Also used : Provider(javax.inject.Provider) MessagingException(javax.mail.MessagingException) Autowired(org.springframework.beans.factory.annotation.Autowired) HtmlEscapers(com.google.common.html.HtmlEscapers) MandrillApiKeyAndMessage(org.pmiops.workbench.mandrill.model.MandrillApiKeyAndMessage) NumberFormat(java.text.NumberFormat) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) StringSubstitutor(org.apache.commons.text.StringSubstitutor) InternetAddress(javax.mail.internet.InternetAddress) MandrillApi(org.pmiops.workbench.mandrill.api.MandrillApi) Strings(com.google.common.base.Strings) DbWorkspace(org.pmiops.workbench.db.model.DbWorkspace) MandrillMessageStatuses(org.pmiops.workbench.mandrill.model.MandrillMessageStatuses) ImmutableList(com.google.common.collect.ImmutableList) Pair(org.apache.commons.lang3.tuple.Pair) Locale(java.util.Locale) Service(org.springframework.stereotype.Service) Map(java.util.Map) RecipientAddress(org.pmiops.workbench.mandrill.model.RecipientAddress) AddressException(javax.mail.internet.AddressException) ImmutableMap(com.google.common.collect.ImmutableMap) Resources(com.google.common.io.Resources) CloudStorageClient(org.pmiops.workbench.google.CloudStorageClient) RecipientType(org.pmiops.workbench.mandrill.model.RecipientType) IOException(java.io.IOException) SendBillingSetupEmailRequest(org.pmiops.workbench.model.SendBillingSetupEmailRequest) Instant(java.time.Instant) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) ZoneId(java.time.ZoneId) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) List(java.util.List) ServerErrorException(org.pmiops.workbench.exceptions.ServerErrorException) MandrillMessageStatus(org.pmiops.workbench.mandrill.model.MandrillMessageStatus) WorkbenchConfig(org.pmiops.workbench.config.WorkbenchConfig) EgressRemediationAction(org.pmiops.workbench.exfiltration.EgressRemediationAction) DateTimeFormatter(java.time.format.DateTimeFormatter) DbUser(org.pmiops.workbench.db.model.DbUser) Collections(java.util.Collections) MandrillMessage(org.pmiops.workbench.mandrill.model.MandrillMessage) MessagingException(javax.mail.MessagingException) StringSubstitutor(org.apache.commons.text.StringSubstitutor) IOException(java.io.IOException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 49 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project commons by craftercms.

the class DefaultEntitlementValidatorImpl method getDescription.

@Override
public String getDescription() {
    String decoded = new String(Base64.getDecoder().decode(DESCRIPTION));
    StringSubstitutor stringSubstitutor = new StringSubstitutor(Collections.singletonMap("version", getPackageVersion()));
    return stringSubstitutor.replace(decoded);
}
Also used : StringSubstitutor(org.apache.commons.text.StringSubstitutor)

Example 50 with StringSubstitutor

use of org.apache.commons.text.StringSubstitutor in project connectors-workspace-one by vmware.

the class TimeOffTaskService method getTaskActionRequestBody.

private String getTaskActionRequestBody(final String inboxTaskId, final String actionUrl, final String comments) {
    try (InputStream stream = timeOffTaskActionTemplate.getInputStream()) {
        final Map<String, String> paramMap = Maps.newHashMap();
        paramMap.put(TIMEOFF_TASK_ID, inboxTaskId);
        paramMap.put(TIMEOFF_TASK_DESCRIPTOR, TIMEOFF_TASK_DESCRIPTOR_VALUE);
        paramMap.put(TIMEOFF_TASK_URL, actionUrl);
        paramMap.put(TIMEOFF_TASK_ACTION_COMMENTS, HtmlUtils.htmlEscape(defaultIfBlank(comments, EMPTY)));
        final StringSubstitutor stringSubstitutor = new StringSubstitutor(paramMap);
        return stringSubstitutor.replace(IOUtils.toString(stream, UTF_8));
    } catch (IOException e) {
        LOGGER.error("Error occurred in building inbox task action body from template", e);
        throw new TimeOffTaskActionRequestCreationFailedException(e);
    }
}
Also used : TimeOffTaskActionRequestCreationFailedException(com.vmware.ws1connectors.workday.exceptions.TimeOffTaskActionRequestCreationFailedException) StringSubstitutor(org.apache.commons.text.StringSubstitutor) InputStream(java.io.InputStream) IOException(java.io.IOException)

Aggregations

StringSubstitutor (org.apache.commons.text.StringSubstitutor)71 HashMap (java.util.HashMap)24 Test (org.junit.jupiter.api.Test)19 IOException (java.io.IOException)11 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)9 File (java.io.File)8 List (java.util.List)8 InputStream (java.io.InputStream)6 Collectors (java.util.stream.Collectors)6 StringLookup (org.apache.commons.text.lookup.StringLookup)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 YAMLFactory (com.fasterxml.jackson.dataformat.yaml.YAMLFactory)5 SubstitutingSourceProvider (io.dropwizard.configuration.SubstitutingSourceProvider)5 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 MetricRegistry (com.codahale.metrics.MetricRegistry)4 JsonObject (com.google.gson.JsonObject)4 URL (java.net.URL)4 StandardCharsets (java.nio.charset.StandardCharsets)4 Scanner (java.util.Scanner)4