templates.util.ClobUtilImpl.ftl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of autoplsp Show documentation
Show all versions of autoplsp Show documentation
Maven plugin to generate Java classes from StoredProcedure and Functions in Database
<#if header>
/*
* Copyright (C) 2019 Yadickson Soto
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package ${javaPackage}.${utilFolderName};
<#assign importList = ["java.io.InputStream", "java.sql.Connection", "java.sql.Clob", "org.apache.commons.io.IOUtils", "org.springframework.stereotype.Component"]>
<#if logger>
<#assign importList = importList + ["org.slf4j.Logger", "org.slf4j.LoggerFactory"]>
<#if driverName == 'oracle'>
<#assign importList = importList + ["java.io.Writer", "oracle.jdbc.OracleConnection"]>
<#if driverVersionName == 'ojdbc6' >
<#assign importList = importList + ["oracle.sql.CLOB"]>
<#list importSort(importList) as import>
<#if previousImportMatch?? && !import?starts_with(previousImportMatch)>
import ${import};
<#assign previousImportMatch = import?keep_before_last(".") >
<#if importList?has_content>
<#if documentation>
/**
* Class to process clob element.
*
* @author @GENERATOR.NAME@
* @version @GENERATOR.VERSION@
*/
@Component
<#if driverVersionName == 'ojdbc6' >
@SuppressWarnings({"deprecation"})
final class ${prefixUtilityName}ClobUtilImpl
implements ${prefixUtilityName}ClobUtil {
<#if logger>
<#if documentation>
/**
* Logger.
*/
private static final Logger LOGGER
= LoggerFactory.getLogger(${prefixUtilityName}ClobUtilImpl.class);
<#if documentation>
/**
* {@inheritDoc}
*/
@Override
public String process(final Object object) {
if (object == null) {
return null;
}
Clob clob = (Clob) object;
String result;
try (InputStream stream = clob.getAsciiStream()) {
result = IOUtils.toString(stream, "${encode}");
} catch (Exception ex) {
<#if logger>
LOGGER.error(ex.getMessage(), ex);
result = null;
}
return result;
}
<#if documentation>
/**
* {@inheritDoc}
*/
@Override
public Object process(
final Connection connection,
final String param
) {
<#if driverName != 'oracle' >
return param;
<#else>
<#if driverVersionName == 'ojdbc6' >CLOB<#else>Clob clob;
try {
OracleConnection <#if prefixUtilityName??>${prefixUtilityName?uncap_first}Conn<#else>conn;
<#if prefixUtilityName??>${prefixUtilityName?uncap_first}Conn<#else>conn = connection.unwrap(OracleConnection.class);
<#if driverVersionName != 'ojdbc6' >
clob = <#if prefixUtilityName??>${prefixUtilityName?uncap_first}Conn<#else>conn.createClob();
<#else>
clob = CLOB.createTemporary(
<#if prefixUtilityName??>${prefixUtilityName?uncap_first}Conn<#else>conn,
false,
CLOB.DURATION_SESSION
);
try (Writer <#if prefixUtilityName??>${prefixUtilityName?uncap_first}Writer<#else>writer = clob.<#if driverVersionName != 'ojdbc6' >setCharacterStream(0)<#else>getCharacterOutputStream()) {
<#if prefixUtilityName??>${prefixUtilityName?uncap_first}Writer<#else>writer.write(param.toCharArray());
} catch (Exception ex) {
<#if logger>
LOGGER.error(ex.getMessage(), ex);
clob = null;
}
} catch (Exception ex) {
<#if logger>
LOGGER.error(ex.getMessage(), ex);
clob = null;
}
return clob;
}
}