/*
 * $Id: VKCDocumentFormatSerializer.java,v 1.10 2006/03/27 22:08:15 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: VKCDocumentFormatSerializer.java,v $
 * Revision 1.10  2006/03/27 22:08:15  marbobo
 * + Bugfixes und Refactoring
 *
 * Revision 1.9  2005/03/21 20:21:45  marbobo
 * + DocSize auf Type Long umgestellt
 *
 * Revision 1.8  2004/12/02 14:20:19  marbobo
 * + Statement auf PreparedStatement umgestellt
 *
 * Revision 1.7  2004/11/24 09:25:53  lawern
 * Hibernate ist VKC-Geschichte!
 *
 * Revision 1.6  2004/07/20 08:23:21  jonyp
 * added copyright info + cvs log
 *
 *
 */
package vkc.db.serializer;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import java.sql.Connection;
import vkc.db.entities.VKCAdditionalDocumentFormat;
import vkc.db.helper.DBHelper;
import vkc.exception.DBException;
import vkc.view.WebShare;

public class VKCDocumentFormatSerializer {
	public static final Collection retrieveAdditionalDocumentFormats(Integer docId) throws DBException {
		Connection conn = null;

		try {
			conn = WebShare.database.getConnection();

			return retrieveAdditionalDocumentFormats(docId, conn);
		} catch (Exception e) {
			throw new DBException(e);
		} finally {
			DBHelper.close(conn);
		}
	}

	public static final Collection retrieveAdditionalDocumentFormats(Integer docId, Connection conn) throws DBException {
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		
		try {		
			ArrayList list = new ArrayList();

			if (docId == null) { 		
				return list;
			}

			pStmt = conn.prepareStatement("SELECT * FROM ADDITIONALDOCUMENTFORMATS WHERE DOCUMENTID = ?");
			pStmt.setInt(1, docId.intValue());
			rs = pStmt.executeQuery();
			
			while (rs.next()) {
				VKCAdditionalDocumentFormat format = new VKCAdditionalDocumentFormat();

				format.setAddDocFormatId(new Integer(rs.getInt("ADDDOCFORMATID")));
				format.setDocumentId(new Integer(rs.getInt("DOCUMENTID")));
				format.setDocTypeId(new Integer(rs.getInt("DOCTYPEID")));
				format.setDocType(VKCDocumentTypeSerializer.getDocumentTypeById(format.getDocTypeId()));

				format.setDocSize(new Long(rs.getLong("DOCSIZE")));
				if (rs.wasNull()) format.setDocSize(null);

				format.setArchiveId(rs.getString("ARCHIVEID"));
				format.setZipped(new Integer(rs.getInt("ZIPPED")));
				format.setInternalDocFormat(new Integer(rs.getInt("INTERNALDOCFORMAT")));

				list.add(format);
			}

			return list;
		} catch (Exception ex) {
			throw new DBException(ex);
		} finally {
			DBHelper.close(rs);
			DBHelper.close(pStmt);
		}
	}
	
	/**
	 * Speichert für die gegebene Dokumenten ID
	 * die zusätzlichen Formate.
	 *
	 * @param documentId für diese Dokumenten ID
	 * @param addFormats Sammlung der konvertierten Dokumentenformate (Typ: VKCAdditionalDocumentFormat)
	 * @throws DBException
	 */
	public final static void saveAdditionalDocumentFormats(Integer documentId,	Collection addFormats)	throws DBException {
		Connection conn = null;

		try {
			conn = WebShare.database.getConnection();
			saveAdditionalDocumentFormats(conn, documentId, addFormats);
			conn.commit();
		} catch (Exception e) {
			DBHelper.rollBack(conn);
			throw new DBException(e);
		} finally {
			DBHelper.close(conn);
		}
	}

	/**
	 * (non-Javadoc)
	 * @see vkc.db.serializer.VKCAdditionalDocumentFormatSerializer#saveAdditionalDocumentFormats(java.lang.Integer, java.util.Collection)
	 */
	public final static void saveAdditionalDocumentFormats(Connection conn,
											   			   Integer documentId, 
											   			   Collection addFormats)
	throws DBException {
		PreparedStatement pStmt = null;

		try {        
			if (addFormats == null || documentId == null) {
				return;
			}
	
			String statement = " INSERT INTO AdditionalDocumentFormats ( addDocFormatId, documentId, docTypeId, archiveId, zipped, docSize, internalDocFormat )" +
							   " VALUES ( ?, ?, ?, ?, ?, ?, ? )";
			pStmt = conn.prepareStatement(statement);
			
			for (Iterator i = addFormats.iterator(); i.hasNext();) {
				VKCAdditionalDocumentFormat f = (VKCAdditionalDocumentFormat) i.next();
				f.setAddDocFormatId(DBHelper.getNewId(conn, "AdditionalDocumentFormats", "addDocFormatId"));
				f.setDocumentId(documentId);
	
				pStmt.setInt(1, f.getAddDocFormatId().intValue());
				pStmt.setInt(2, f.getDocumentId().intValue());
				pStmt.setInt(3, f.getDocTypeId().intValue());
				pStmt.setString(4, f.getArchiveId());
				pStmt.setInt(5, f.getZipped().intValue());
				pStmt.setLong(6, f.getDocSize().longValue());
				pStmt.setInt(7, f.getInternalDocFormat().intValue());
				pStmt.execute();
			}
		} catch (Exception e) {
			throw new DBException(e);
		} finally {
			DBHelper.close(pStmt);
		}
	}
}