vidakovic commented on code in PR #5851: URL: https://github.com/apache/fineract/pull/5851#discussion_r3247891160
########## fineract-provider/src/main/java/org/apache/fineract/template/service/TemplateMergeServiceImpl.java: ########## @@ -0,0 +1,215 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.template.service; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.github.mustachejava.DefaultMustacheFactory; +import com.github.mustachejava.Mustache; +import com.github.mustachejava.MustacheFactory; +import java.io.IOException; +import java.io.InputStream; +import java.io.StringReader; +import java.io.StringWriter; +import java.net.Authenticator; +import java.net.HttpURLConnection; +import java.net.PasswordAuthentication; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.security.KeyManagementException; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.apache.fineract.infrastructure.core.config.FineractProperties; +import org.apache.fineract.infrastructure.core.service.ThreadLocalContextUtil; +import org.apache.fineract.template.data.TemplateData; +import org.apache.fineract.template.data.TemplateMapperData; +import org.apache.fineract.template.domain.TemplateFunctions; +import org.apache.fineract.template.exception.TemplateForbiddenException; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Service; + +@Slf4j +@RequiredArgsConstructor +@Service +@ConditionalOnMissingBean(value = TemplateMergeService.class, ignored = TemplateMergeServiceImpl.class) +public class TemplateMergeServiceImpl implements TemplateMergeService { + + private final FineractProperties fineractProperties; + + @Override + public String compile(final TemplateData template, final Map<String, Object> scopes) { + scopes.put("static", TemplateFunctions.INSTANCE); + + var mf = new DefaultMustacheFactory(); + var mustache = mf.compile(new StringReader(template.getText()), template.getName()); + + compiledMapFromMappers(asMap(template.getMappers()), scopes); + + expandMapArrays(scopes); + + var stringWriter = new StringWriter(); + mustache.execute(stringWriter, scopes); + + return stringWriter.toString(); + } + + private void compiledMapFromMappers(final Map<String, String> data, final Map<String, Object> scopes) { + final MustacheFactory mf = new DefaultMustacheFactory(); + + if (data != null) { + for (final Map.Entry<String, String> entry : data.entrySet()) { + final Mustache mappersMustache = mf.compile(new StringReader(entry.getValue()), ""); + final StringWriter stringWriter = new StringWriter(); + + mappersMustache.execute(stringWriter, scopes); + String url = stringWriter.toString(); + if (!url.startsWith("http")) { + url = scopes.get("BASE_URI") + url; + } + try { + scopes.put(entry.getKey(), getMapFromUrl(url)); + } catch (final IOException e) { + log.error("getCompiledMapFromMappers() failed", e); + } + } + } + } + + private LinkedHashMap<String, String> asMap(List<TemplateMapperData> mappers) { + final LinkedHashMap<String, String> map = new LinkedHashMap<>(); + + for (var mapper : mappers) { + map.put(mapper.getMapperkey(), mapper.getMappervalue()); + } + + return map; + } + + @SuppressWarnings("unchecked") Review Comment: See comment above. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
