/*
 * $Id: VKCKeywordSerializer.java,v 1.9 2006/03/27 22:08:13 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: VKCKeywordSerializer.java,v $
 * Revision 1.9  2006/03/27 22:08:13  marbobo
 * + Bugfixes und Refactoring
 *
 * Revision 1.8  2005/01/18 16:53:48  jonyp
 * deleted logic for userregsiteredkeywords
 *
 * Revision 1.7  2004/12/05 22:57:46  marbobo
 * + Statement auf PreparedStatement umgestellt
 *
 * Revision 1.6  2004/11/24 13:01:11  lawern
 * Global "conn.setAutoCommit( false );" enfernt, weil überflüssig.
 *
 * Revision 1.5  2004/11/24 09:25:56  lawern
 * Hibernate ist VKC-Geschichte!
 *
 * Revision 1.4  2004/07/20 08:23:20  jonyp
 * added copyright info + cvs log
 *
 *
 */
package vkc.db.serializer;

import java.sql.Connection;
import vkc.db.entities.VKCCategory;
import vkc.db.entities.VKCDocument;
import vkc.db.entities.VKCEntity;
import vkc.db.entities.VKCKeyword;
import vkc.db.helper.DBHelper;
import vkc.exception.DBException;
import vkc.view.WebShare;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class VKCKeywordSerializer {
	private VKCKeywordSerializer() {
	}

	public static Set retrieveKeywords(Connection conn, String associationTable, String keyColumn, Integer keyId) throws SQLException {
		PreparedStatement pStmt = null;
		ResultSet rs = null;
		Set keywords = new HashSet();

		try {

			pStmt = conn.prepareStatement("SELECT term, termDescriptor, weight, internal FROM " + associationTable + " WHERE " + keyColumn + " = ?");
			pStmt.setInt(1, keyId.intValue());
			rs = pStmt.executeQuery();

			while (rs.next()) {
				VKCKeyword keyword = new VKCKeyword(rs.getString("term"), rs.getString("termDescriptor"), rs.getDouble("weight"), new Integer(rs.getInt("internal")));
				keywords.add(keyword);
			}
		} finally {
			DBHelper.close(rs);
			DBHelper.close(pStmt);
		}

		return keywords;
	}

	/**
	 * Speichert Schlüsselbegriffe einer Entität
	 *
	 * @param sess
	 * @param document
	 *
	 * @throws SQLException
	 */
	public static void saveKeywords(Connection conn, Set keywords, String associationTable, String keyColumn, Integer keyId) throws DBException {
		PreparedStatement pStmt = null;

		try {
			// Erstmal evtl. vorhandene Schlüsselbegriffe löschen
			DBHelper.deleteEntry(conn, associationTable, keyColumn, keyId);

			if (!keywords.isEmpty()) {
				pStmt = conn.prepareStatement("INSERT INTO " + associationTable + " (" + keyColumn + ", term , termDescriptor, weight, internal ) VALUES ( ?, ?, ?, ?, ? )");

				for (Iterator it = keywords.iterator(); it.hasNext();) {
					VKCKeyword keyword = (VKCKeyword) it.next();
					pStmt.setInt(1, keyId.intValue());
					pStmt.setString(2, keyword.getTerm());
					pStmt.setString(3, keyword.getTermDescriptor());
					pStmt.setDouble(4, keyword.getWeight());
					pStmt.setInt(5, keyword.getInternal());
					pStmt.execute();
				}
			}
		} catch (Exception ex) {
			throw new DBException(ex);
		} finally {
			DBHelper.close(pStmt);
		}
	}

	/**
	 * Speichert eine Menge(Set) von Schlagworten in der DB ab. Schon
	 * vorhandene Schlagworte werden nicht noch einmal gespeichert.
	 *
	 * @param entity Referenz auf eine VKCEntity (VKCCategory, VKCDocument,
	 *        VKCUser)
	 */
	public final static void saveKeywords(VKCEntity entity) throws DBException {
		Connection conn = null;

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

			if (entity instanceof VKCCategory) {
				VKCKeywordSerializer.saveKeywords(conn, entity.getKeywords(), "CategoryKeywords", "categoryId", ((VKCCategory) entity).getCategoryId());
			} else if (entity instanceof VKCDocument) {
				VKCKeywordSerializer.saveKeywords(conn, entity.getKeywords(), "DocumentKeywords", "documentId", ((VKCDocument) entity).getDocumentId());
			}

			conn.commit();
		} catch (Exception e) {
			DBHelper.rollBack(conn);
			throw new DBException(e);
		} finally {
			DBHelper.close(conn);
		}
	}
}