protected void setColumn()

in blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/acting/AbstractDatabaseAction.java [391:672]


    protected void setColumn(PreparedStatement statement, int position, Request request, Configuration entry, String param, Object value, int rowIndex) throws Exception {
        getLogger().debug("Setting column "+position+" named "+param+" with value "+value);
        if (value instanceof String) {
            value = ((String) value).trim();
        }
        String typeName = entry.getAttribute("type");
        Integer typeObject = (Integer) AbstractDatabaseAction.typeConstants.get(typeName);
        if (typeObject == null) {
            throw new SQLException("Can't set column because the type "+typeName+" is unrecognized");
        }
        if (value == null) {
            /** If the value is null, set the column value null and return **/
            if (typeName.equals("image-width") || typeName.equals("image-height") || typeName.equals("image-size") || typeName.equals("row-index") || typeName.equals("image-mime-type")) {
              /** these column types are automatically generated so it's ok **/
            } else {
              statement.setNull(position, typeObject.intValue());
              return;
            }
        }
        if ("".equals(value)) {
            switch (typeObject.intValue()) {
                case Types.CHAR:
                case Types.CLOB:
                case Types.VARCHAR:
                    /** If the value is an empty string and the column is
                        a string type, we can continue **/
                    break;
                case Types.INTEGER:
                  if (typeName.equals("image-width") || typeName.equals("image-height") || typeName.equals("image-size") || typeName.equals("row-index")) {
                    /** again, these types are okay to be absent **/
                    break;
                  }
                default:
                    /** If the value is an empty string and the column
                        is something else, we treat it as a null value **/
                    statement.setNull(position, typeObject.intValue());
                    return;
            }
        }

        /** Store the column value in the request attribute
            keyed by the request parameter name. we do this so possible future
            actions can access this data. not sure about the key tho... **/
        setRequestAttribute(request,param,value);
        File file;

        switch (typeObject.intValue()) {
            case Types.CLOB:
                int length = -1;
                InputStream asciiStream = null;

                if (value instanceof File) {
                    File asciiFile = (File) value;
                    asciiStream = new BufferedInputStream(new FileInputStream(asciiFile));
                    length = (int) asciiFile.length();
                } else {
                    String asciiText = (String) value;
                    asciiStream = new BufferedInputStream(new ByteArrayInputStream(asciiText.getBytes()));
                    length = asciiText.length();
                }

                statement.setAsciiStream(position, asciiStream, length);
                break;
            case Types.BIGINT:
                BigDecimal bd = null;

                if (value instanceof BigDecimal) {
                    bd = (BigDecimal) value;
                } else {
                    bd = new BigDecimal((String) value);
                }

                statement.setBigDecimal(position, bd);
                break;
            case Types.TINYINT:
                Byte b = null;

                if (value instanceof Byte) {
                    b = (Byte) value;
                } else {
                    b = new Byte((String) value);
                }

                statement.setByte(position, b.byteValue());
                break;
            case Types.DATE:
                Date d = null;

                if (value instanceof Date) {
                    d = (Date) value;
                } else if (value instanceof java.util.Date) {
                    d = new Date(((java.util.Date) value).getTime());
                } else {
                    d = new Date(this.dateValue((String) value, entry.getAttribute("format", "M/d/yyyy")));
                }

                statement.setDate(position, d);
                break;
            case Types.DOUBLE:
                Double db = null;

                if (value instanceof Double) {
                    db = (Double) value;
                } else {
                    db = new Double((String) value);
                }

                statement.setDouble(position, db.doubleValue());
                break;
            case Types.FLOAT:
                Float f = null;

                if (value instanceof Float) {
                    f = (Float) value;
                } else {
                    f = new Float((String) value);
                }

                statement.setFloat(position, f.floatValue());
                break;
            case Types.NUMERIC:
                Long l = null;

                if (value instanceof Long) {
                    l = (Long) value;
                } else {
                    l = new Long((String) value);
                }

                statement.setLong(position, l.longValue());
                break;
            case Types.SMALLINT:
                Short s = null;

                if (value instanceof Short) {
                    s = (Short) value;
                } else {
                    s = new Short((String) value);
                }

                statement.setShort(position, s.shortValue());
                break;
            case Types.TIME:
                Time t = null;

                if (value instanceof Time) {
                    t = (Time) value;
                } else {
                    t = new Time(this.dateValue((String) value, entry.getAttribute("format", "h:m:s a")));
                }

                statement.setTime(position, t);
                break;
            case Types.TIMESTAMP:
                Timestamp ts = null;

                if (value instanceof Time) {
                    ts = (Timestamp) value;
                } else {
                    ts = new Timestamp(this.dateValue((String) value, entry.getAttribute("format", "M/d/yyyy h:m:s a")));
                }

                statement.setTimestamp(position, ts);
                break;
            case Types.ARRAY:
                statement.setArray(position, (Array) value); // no way to convert string to array
                break;
            case Types.STRUCT:
            case Types.OTHER:
                statement.setObject(position, value);
                break;
            case Types.LONGVARBINARY:
                statement.setTimestamp(position, new Timestamp((new java.util.Date()).getTime()));
                break;
            case Types.VARCHAR:
                if ("string".equals(typeName)) {
                   statement.setString(position, (String) value);
                   break;
                } else if ("image-mime-type".equals(typeName)) {
                    String imageAttr = param.substring(0, (param.length() - "-mime-type".length()));
                    file = (File) request.get(imageAttr);
                    synchronized (this.files) {
                        Parameters parameters = (Parameters) this.files.get(file);
                        String imageMimeType = parameters.getParameter("image-mime-type",
                                                                       (String) settings.get("image-mime-type",""));
                        statement.setString(position, imageMimeType);
                        /** Store the image mime type in the request attributes.
                            Why do we do this? **/
                        setRequestAttribute(request, param, imageMimeType);
                    }
                    break;
                }
            case Types.BLOB:
                if (value instanceof File) {
                    file = (File)value;
                } else if (value instanceof String) {
                    file = new File((String)value);
                } else {
                    throw new SQLException("Invalid type for blob: "+value.getClass().getName());
                }
                //InputStream input = new BufferedInputStream(new FileInputStream(file));
                FileInputStream input = new FileInputStream(file);
                statement.setBinaryStream(position, input, (int)file.length());
                if ("image".equals(typeName)) {
                    /** If this column type is an image, store the
                        size, width, and height in a static table **/
                    Parameters parameters = new Parameters();
                    parameters.setParameter("image-size", Long.toString(file.length()));
                    ImageProperties prop = ImageUtils.getImageProperties(file);
                    parameters.setParameter("image-width", Integer.toString(prop.width));
                    parameters.setParameter("image-height", Integer.toString(prop.height));
                    // TC: if it's really mime-type shouldn't we prepend "image/"?
                    parameters.setParameter("image-mime-type",prop.type);
                    synchronized (this.files) {
                        this.files.put(file, parameters);
                    }
                }
                break;
            case Types.INTEGER:
                if ("int".equals(typeName)) {
                    Integer i = null;
                    if (value instanceof Integer) {
                        i = (Integer) value;
                    } else {
                        i = new Integer((String) value);
                    }
                    statement.setInt(position, i.intValue());
                    break;
                } else if ("image-width".equals(typeName)) {
                    /** Get the image width from the cached image data **/
                    /** Is this why we store the values in the request
                        attributes? **/
                    String imageAttr = param.substring(0, (param.length() - "-width".length()));
                    file = (File) request.get(imageAttr);
                    synchronized (this.files) {
                        Parameters parameters = (Parameters) this.files.get(file);
                        statement.setInt(position, parameters.getParameterAsInteger("image-width",
                         Integer.parseInt((String)settings.get("image-width","-1"))));
                        /** Store the image width in the request attributes.
                            Why do we do this? **/
                        setRequestAttribute(request,
                        param,
                        parameters.getParameter("image-width",
                                    (String) settings.get("image-width","")));
                    }
                    break;
                } else if ("image-height".equals(typeName)) {
                    /** Get the image height from the cached image data **/
                    String imageAttr = param.substring(0, (param.length() - "-height".length()));
                    file = (File) request.get(imageAttr);
                    synchronized (this.files) {
                        Parameters parameters = (Parameters) this.files.get(file);
                        statement.setInt(position, parameters.getParameterAsInteger("image-height",
                         Integer.parseInt((String)settings.get("image-height","-1"))));
                        setRequestAttribute(request,
                        param,
                        parameters.getParameter("image-height",
                                    (String) settings.get("image-height","")));
                    }
                    break;
                } else if ("image-size".equals(typeName)) {
                    /** Get the image file size from the cached image data **/
                    String imageAttr = param.substring(0, (param.length() - "-size".length()));
                    file = (File) request.get(imageAttr);
                    synchronized (this.files) {
                        Parameters parameters = (Parameters) this.files.get(file);
                        statement.setInt(position, parameters.getParameterAsInteger("image-size",
                         Integer.parseInt((String)settings.get("image-height","-1"))));
                        setRequestAttribute(request,
                        param,
                        parameters.getParameter("image-size",
                                    (String) settings.get("image-size","")));
                    }
                    break;
                } else if ("row-index".equals(typeName)) {
                    statement.setInt(position,rowIndex);
                    break;
                }
            default:
                throw new SQLException("Impossible exception - invalid type "+typeName);
        }
    }