/* * ClobConverter.java * * Created on September 8, 2004, 11:45 AM */ package clobUtilities; import java.sql.Clob; import java.sql.Connection; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.convert.Converter; import javax.faces.convert.ConverterException; import java.sql.SQLException; import oracle.sql.CLOB; /** * Converts Oracle CLOB to String and back to CLOB. * Requires the Oracle JDBC driver to be included in the project. Add as a library using the Library manager. * @author John Baker */ public class ClobConverter implements Converter { private Connection conn; public ClobConverter(Connection conn) { this.conn = conn; } //To define how the data is converted from the presentation view to the model view public Object getAsObject(FacesContext context, UIComponent component, String newValue) throws ConverterException { if (newValue == null) { return null; } CLOB newClob= null; try { newClob = oracle.sql.CLOB.createTemporary(conn, true, oracle.sql.CLOB.DURATION_SESSION); ((oracle.sql.CLOB) newClob).setString(1, newValue); } catch (Exception e) { e.printStackTrace(); } // end try catch return newClob; // hands clob to database } //To define how the data is converted from the model view to the presentation view public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException { if (value == null) { return null; } String convertedValue = null; Clob newClob = (Clob) value; try { convertedValue = newClob.getSubString(1, (int) newClob.length()); } catch (SQLException sx) { convertedValue = "Error in Clobconverter " + sx; } return convertedValue; } }