All Downloads are FREE. Search and download functionalities are using the official Maven repository.

src-main.org.awakefw.sql.jdbc.util.FileNameFromBlobBuilder Maven / Gradle / Ivy

/*
 * Awake File: Easy file upload & download through HTTP with Java
 * Awake SQL: Remote JDBC access through HTTP.                                    
 * Copyright (C) 2012, Kawan Softwares S.A.S.
 * (http://www.awakeframework.org). All rights reserved.                                
 *                                                                               
 * Awake File/SQL is free software; you can redistribute it and/or                 
 * modify it under the terms of the GNU Lesser General Public                    
 * License as published by the Free Software Foundation; either                  
 * version 2.1 of the License, or (at your option) any later version.            
 *                                                                               
 * Awake File/SQL 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             
 * Lesser General Public License for more details.                               
 *                                                                               
 * You should have received a copy of the GNU Lesser General Public              
 * License along with this library; if not, write to the Free Software           
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
 * 02110-1301  USA
 *
 * Any modifications to this file must keep this entire header
 * intact.
 */

//FileNameFromBlobBuilder.java
//Copyright (c) Kawan Softwares S.A.S, 2012
//
//Last Updates: 
// 12 f?vr. 2010 18:33:31 Nicolas de Pomereu
// 18/02/10 17:30 NDP - Add setBinaryStream()
// 01/04/10 11:10 NDP - getUniqueId() moved to FileUtil
// 12/11/11 18:05 NDP : FileNameFromBlobBuilder: uses now "-" as separator char
// 18/11/11 22:45 NDP : FileNameFromBlobBuilder: if table or statement type == null ==> force "unknown"
// 03/03/12 13:45 NDP : FileNameFromBlobBuilder: different file name for Clob

package org.awakefw.sql.jdbc.util;

import java.util.Vector;

import org.awakefw.file.util.AwakeFileUtil;
import org.awakefw.file.util.Tag;
import org.awakefw.sql.api.server.StatementAnalyser;

/**
 * @author Nicolas de Pomereu Build the raw filename that will contain the Blob
 *         or Clob contant
 */
public class FileNameFromBlobBuilder {
    /** The statement sql string */
    private String sqlOrder = null;

    /** The column to dump */
    private String columnName = null;

    /** The parameter index */
    private int parameterIndex = 0;

    /** true if the Blob is a Clob */
    private boolean isClob = false;

    /**
     * Default constructor
     * 
     * @param sqlOrder
     *            The statement sql string
     * @param columnName
     *            The column to dump
     * @param isClob
     *            true if the Blob as a Clob
     */
    public FileNameFromBlobBuilder(String sqlOrder, String columnName,
	    boolean isClob) {
	if (sqlOrder == null) {
	    throw new IllegalArgumentException(Tag.AWAKE_PRODUCT_FAIL
		    + "sqlOrder can not be null!");
	}

	if (columnName == null) {
	    throw new IllegalArgumentException(Tag.AWAKE_PRODUCT_FAIL
		    + "columnName can not be null!");
	}

	this.sqlOrder = sqlOrder;
	this.columnName = columnName;
	this.isClob = isClob;
    }

    /**
     * Default constructor
     * 
     * @param sqlOrder
     *            The statement sql string
     * @param parameterIndex
     *            the index of the prepared statement
     * @param isClob
     *            true if the Blob as a Clob
     */
    public FileNameFromBlobBuilder(String sqlOrder, int parameterIndex,
	    boolean isClob) {
	if (sqlOrder == null) {
	    throw new IllegalArgumentException(Tag.AWAKE_PRODUCT_FAIL
		    + "sqlOrder can not be null!");
	}

	if (parameterIndex < 1) {
	    throw new IllegalArgumentException(Tag.AWAKE_PRODUCT_FAIL
		    + "Prepared statement parameterIndex must be >= 1");
	}

	this.sqlOrder = sqlOrder;
	this.parameterIndex = parameterIndex;
	this.isClob = isClob;
    }

    /**
     * @return the raw file name of the file to create from the blob/clob column
     *         with the content
     */
    public String getFileName() {
	StatementAnalyser analyser = new StatementAnalyser(sqlOrder,
		new Vector());
	String fileName = null;

	String unique = AwakeFileUtil.getUniqueId();

	String tableName = analyser.getTableNameFromDmlStatement();
	String statementType = analyser.getStatementType();

	if (tableName == null) {
	    tableName = "unknown";
	}

	if (statementType == null) {
	    statementType = "unknown";
	}

	String blobHeader = "blob-";
	if (isClob) {
	    blobHeader = "clob-";
	}

	if (columnName != null) {
	    fileName = blobHeader + statementType.toLowerCase() + "-"
		    + tableName.toLowerCase() + "." + columnName + "-" + unique
		    + ".awake";
	} else {
	    fileName = blobHeader + statementType.toLowerCase() + "-"
		    + tableName.toLowerCase() + "-index-" + parameterIndex
		    + "-" + unique + ".awake";
	}

	if (isClob) {
	    fileName += ".txt";
	}

	return fileName;
    }
}