added missing tx annoations

This commit is contained in:
2019-03-28 12:30:22 +01:00
parent cc7e1b5e73
commit f8fe805ba6

View File

@ -1,98 +1,106 @@
/* /*
* Copyright 2018 joern.muehlencord. * Copyright 2018 joern.muehlencord.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package de.muehlencord.shared.account.business.mail.boundary; package de.muehlencord.shared.account.business.mail.boundary;
import de.muehlencord.shared.account.business.mail.entity.MailDatamodel; import de.muehlencord.shared.account.business.mail.entity.MailDatamodel;
import de.muehlencord.shared.account.business.mail.entity.MailTemplateEntity; import de.muehlencord.shared.account.business.mail.entity.MailTemplateEntity;
import de.muehlencord.shared.account.business.mail.entity.MailTemplateException; import de.muehlencord.shared.account.business.mail.entity.MailTemplateException;
import de.muehlencord.shared.account.util.AccountPU; import de.muehlencord.shared.account.util.AccountPU;
import freemarker.cache.StringTemplateLoader; import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration; import freemarker.template.Configuration;
import freemarker.template.Template; import freemarker.template.Template;
import freemarker.template.TemplateException; import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler; import freemarker.template.TemplateExceptionHandler;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.io.StringWriter; import java.io.StringWriter;
import java.io.Writer; import java.io.Writer;
import javax.ejb.Stateless; import javax.ejb.Lock;
import javax.inject.Inject; import javax.ejb.LockType;
import javax.persistence.EntityManager; import javax.ejb.Stateless;
import javax.persistence.Query; import javax.ejb.TransactionAttribute;
import org.slf4j.Logger; import javax.ejb.TransactionAttributeType;
import org.slf4j.LoggerFactory; import javax.inject.Inject;
import javax.persistence.EntityManager;
/** import javax.persistence.Query;
* import org.slf4j.Logger;
* @author jomu import org.slf4j.LoggerFactory;
*/
@Stateless /**
public class MailTemplateService implements Serializable { *
* @author jomu
private static final long serialVersionUID = -136113381443058697L; */
@Stateless
private static final Logger LOGGER = LoggerFactory.getLogger(MailTemplateService.class.getName()); public class MailTemplateService implements Serializable {
@Inject private static final long serialVersionUID = -136113381443058697L;
@AccountPU
EntityManager em; private static final Logger LOGGER = LoggerFactory.getLogger(MailTemplateService.class.getName());
public String getStringFromTemplate(String templateName, MailDatamodel dataModel) throws MailTemplateException { @Inject
try { @AccountPU
Query query = em.createNamedQuery("MailTemplateEntity.findByTemplateName"); EntityManager em;
query.setParameter("templateName", templateName);
MailTemplateEntity templateEntity = (MailTemplateEntity) query.getSingleResult(); @Lock(LockType.READ)
if (templateEntity == null) { @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
LOGGER.error("Tempate with name " + templateName + " not found"); public String getStringFromTemplate(String templateName, MailDatamodel dataModel) throws MailTemplateException {
return null; try {
} Query query = em.createNamedQuery("MailTemplateEntity.findByTemplateName");
return fillTemplate(templateName, templateEntity.getTemplateValue(), dataModel); query.setParameter("templateName", templateName);
} catch (MailTemplateException ex) { MailTemplateEntity templateEntity = (MailTemplateEntity) query.getSingleResult();
String hint = "Error while processing template with name " + templateName + "."; if (templateEntity == null) {
LOGGER.error(hint + " " + ex.toString()); LOGGER.error("Tempate with name " + templateName + " not found");
if (LOGGER.isDebugEnabled()) { return null;
LOGGER.debug(hint, ex); }
} return fillTemplate(templateName, templateEntity.getTemplateValue(), dataModel);
throw new MailTemplateException(hint, ex); } catch (MailTemplateException ex) {
String hint = "Error while processing template with name " + templateName + ".";
} LOGGER.error(hint + " " + ex.toString());
} if (LOGGER.isDebugEnabled()) {
LOGGER.debug(hint, ex);
public String fillTemplate(String templateName, String templateString, MailDatamodel dataModel) throws MailTemplateException { }
try { throw new MailTemplateException(hint, ex);
Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
configuration.setDefaultEncoding("UTF-8"); // FIXME - make encoding configurable }
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); }
StringTemplateLoader stringLoader = new StringTemplateLoader(); @Lock(LockType.READ)
stringLoader.putTemplate(templateName, templateString); @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
configuration.setTemplateLoader(stringLoader); public String fillTemplate(String templateName, String templateString, MailDatamodel dataModel) throws MailTemplateException {
Template template = configuration.getTemplate(templateName); try {
Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
Writer out = new StringWriter(); configuration.setDefaultEncoding("UTF-8"); // FIXME - make encoding configurable
template.process(dataModel, out); configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
String resultString = out.toString();
return resultString; StringTemplateLoader stringLoader = new StringTemplateLoader();
} catch (TemplateException | IOException ex) { stringLoader.putTemplate(templateName, templateString);
String hint = "Error while processing template with name " + templateName + "."; configuration.setTemplateLoader(stringLoader);
LOGGER.error(hint + " " + ex.toString()); Template template = configuration.getTemplate(templateName);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(hint, ex); Writer out = new StringWriter();
} template.process(dataModel, out);
throw new MailTemplateException(hint, ex); String resultString = out.toString();
} return resultString;
} } catch (TemplateException | IOException ex) {
} String hint = "Error while processing template with name " + templateName + ".";
LOGGER.error(hint + " " + ex.toString());
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(hint, ex);
}
throw new MailTemplateException(hint, ex);
}
}
}