/*
 * $Id: ConversionRequest.java,v 1.7 2006/04/05 18:38:03 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: ConversionRequest.java,v $
 * Revision 1.7  2006/04/05 18:38:03  marbobo
 * +Berücksichtigung der Licensedatei
 *
 * Revision 1.6  2006/04/04 17:01:47  marbobo
 * + Einige Anpassungen bzgl. neu eingeführter Konvertierungsformate
 *
 * Revision 1.5  2006/04/04 07:37:07  korowa
 * Eine Funktion hinzugefügt , die es einem Konverter ein Zugrif auf Systemparameter VKCSTREAMS gewährt.
 *
 * Revision 1.4  2004/08/24 13:22:09  jonyp
 * normalize dbUrl to lowercase
 *
 * Revision 1.3  2004/08/05 07:16:20  koppy
 * Ein Konverter-Server kann jetzt auch mehrere VKC-WebApps bedienen
 *
 * Revision 1.2  2004/02/25 18:35:08  koppy
 * Bestimmung der docTypeId
 *
 * Revision 1.1  2004/01/16 14:19:40  koppy
 * Initiale Version
 *
 */
package vkc.converter.common;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.io.Serializable;

import vkc.db.PersistenceManager;
import vkc.db.entities.VKCSysParameter;
import vkc.db.helper.DBHelper;
import vkc.db.serializer.VKCSysParameterSerializer;
import vkc.exception.DBException;
import vkc.misc.VKCLogManager;
import vkc.view.WebShare;

/**
 * This class represents a request for the ConverterServer.
 * It contains all neccessary information for a conversion
 * 
 * @author koppy
 */
public abstract class ConversionRequest implements Serializable {
	private Collection conversions = null;

	private String assocDbUrl = null;

	protected ConversionRequest() {
		this.assocDbUrl = WebShare.database.getDbUrl().toLowerCase();
	}

	/**
	 * Constructs a converter request 
	 * 
	 * @param conversions for this desired output conversions
	 * (collection of type VKCDocumentType)
	 */
	public ConversionRequest(Collection conversions) {
		this.conversions = conversions;
	}

	/**
	 * Returns the collection of desired conversion formats
	 * @return collection of type: String
	 * @throws DBException 
	 * @throws DBException 
	 */
	public Collection getConversions() {
		if (conversions == null) {
			try {
				conversions = retrieveEnabledVKCStreams();
			} catch (DBException e) {
				conversions = null;
			}
		}

		return conversions;
	}

	public abstract int getDocTypeId();

	/**
	 * @param dbUrl
	 */
	public String getAssocDbUrl() {
		return this.assocDbUrl;
	}

	private Collection retrieveEnabledVKCStreams() throws DBException {
		ArrayList list= new ArrayList();
		
		if (retrieveIntValue("VKCSTREAMS", "EnableHTML", 0) != 0) {
			list.add("EnableHTML");
		}
		
		if (retrieveIntValue("VKCSTREAMS", "EnableTDF", 0) != 0) {
			list.add("EnableTDF");
		}
		
		if (retrieveIntValue("VKCSTREAMS", "EnablePDF", 0) != 0) {
			list.add("EnablePDF");
		}
		
		return list;
	}

	private int retrieveIntValue(String context, String name, int defaultValue) throws DBException {
		String value = retrieveValue(context, name);

		if (value != null) {
			return Integer.parseInt(value);
		} 

		return defaultValue;
	}

	private String retrieveValue(String context, String name) throws DBException {
		Connection conn = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;

		VKCSysParameter pParam = WebShare.getPermanentSystemParameter(context, name);
		
		if (pParam != null) {
			return pParam.getValuestring();
		}

		try {
			conn = PersistenceManager.getInstance().getConnection();
			pStmt = conn.prepareStatement("SELECT * FROM SysParameters WHERE context = ? AND name = ?");
			pStmt.setString(1, context);
			pStmt.setString(2, name);
			rs = pStmt.executeQuery();

			if (rs.next()) {
				String value = rs.getString("valuestring");
				
				return value;
			} 

			return null;
		} catch (Exception e) {
			VKCLogManager.errorLog(VKCSysParameterSerializer.class, "1", "Error retrieving VKC sysparameter '" + name + "' (Context: '" + context + "')", e);
			throw new DBException(e);
		} finally {
			DBHelper.close(rs);
			DBHelper.close(pStmt);
			DBHelper.close(conn);
		}
	}
}
