ContentServiceImpl.java
/*
* Copyright 2019 Global Crop Diversity Trust
*
* Licensed 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.genesys.server.service.impl;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Locale;
import java.util.Map;
import org.genesys.blocks.auditlog.service.ClassPKService;
import org.genesys.blocks.model.ClassPK;
import org.genesys.server.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Service
@Validated
public class ContentServiceImpl implements ContentService {
public static final Logger LOG = LoggerFactory.getLogger(ContentServiceImpl.class);
@Autowired
private ClassPKService classPkService;
@Autowired
private VelocityEngine velocityEngine;
@Override
public Locale getDefaultLocale() {
return Locale.ENGLISH;
}
@Override
public ClassPK getClassPk(Class<?> clazz) {
return classPkService.getClassPk(clazz);
}
@Override
public ClassPK getClassPk(String shortName) {
return classPkService.findByShortName(shortName);
}
@Override
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public synchronized ClassPK ensureClassPK(Class<?> clazz) {
return classPkService.getClassPk(clazz);
}
@Override
public String processTemplate(String templateStr, Map<String, Object> root) {
final StringWriter swOut = new StringWriter();
processTemplate(templateStr, root, swOut);
if (LOG.isTraceEnabled()) {
LOG.trace(swOut.toString());
}
return swOut.toString();
}
@Override
public void processTemplate(String templateStr, Map<String, Object> root, Writer writer) {
final VelocityContext context = new VelocityContext();
for (final var entry : root.entrySet()) {
context.put(entry.getKey(), entry.getValue());
}
context.put("esc", new org.apache.velocity.tools.generic.EscapeTool());
/**
* Merge data and template
*/
velocityEngine.evaluate(context, writer, "log tag name", templateStr);
}
}