/*
 * $Id: AudiService.java,v 1.6 2006/03/23 11:47:20 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * $Log: AudiService.java,v $
 * Revision 1.6  2006/03/23 11:47:20  marbobo
 * + Mögliche NPE abgefangen
 *
 * Revision 1.5  2005/07/21 20:16:08  marbobo
 * + Unicode-Unterstützung
 * + saveAllDocumentData() hinzugefügt
 *
 * Revision 1.4  2005/07/19 22:03:48  marbobo
 * + setErrorCategory() hinzugefügt
 *
 * Revision 1.3  2005/07/19 13:23:27  marbobo
 * + kleine anpassung
 *
 * Revision 1.2  2005/07/18 00:03:26  marbobo
 * + Methoden zum Speichern von Fehlerdateien implementiert
 *
 * Revision 1.1  2005/07/15 13:06:50  marbobo
 * + Initiale Version
 *
 */

package vkc.ws.axis.service;

import java.io.ByteArrayInputStream;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;

import vkc.db.entities.VKCCategory;
import vkc.db.entities.VKCDocument;
import vkc.db.entities.VKCUser;
import vkc.db.serializer.VKCAclSerializer;
import vkc.db.serializer.VKCCategorySerializer;
import vkc.db.serializer.VKCDocumentSerializer;
import vkc.db.serializer.VKCDocumentTypeSerializer;
import vkc.db.serializer.VKCUserSerializer;
import vkc.misc.VKCLogManager;
import vkc.search.Search;
import vkc.search.SearchResultEntry;
import vkc.util.VKCStringUtils;
import vkc.view.WebShare;

public class AudiService {
    public static final String ERROR_CATEGORY_NAME = "Fehlermeldung";

    public static final String FILENAME_NOTIFICATION = "Fehlermeldung";
    public static final String FILENAME_ANALYSE = "Fehleranalyse";
    public static final String FILENAME_CORRECTION = "Fehlerbehandlung";
    public static final String FILENAME_CAUSATION = "Fehlerursache";
    public static final String FILENAME_COMPOUNDING = "Abstellmaßnahmen";
    public static final String FILENAME_EFFICIENCY = "Wirksamkeit";
    public static final String FILENAME_DISPATCH = "Ursachenbeseitigung";
    public static final String FILENAME_ALLDOCUMENTS = "Gesamtfehler_Bericht";

    public static final String NOTIFICATION_DESCRIPTION = "Beschreibung";
    public static final String NOTIFICATION_TIME = "Zeit";
    public static final String NOTIFICATION_LOCATION = "Ort";
    public static final String NOTIFICATION_PRIORITY = "Priorität";
    public static final String NOTIFICATION_CONTACT = "Bearbeiter";
    
    public static final String ANALYSE_DESCRIPTION = "Beschreibung";
    public static final String ANALYSE_PARAMETERSET = "Parametersätze";
    public static final String ANALYSE_TESTPLANTSTATE = "Prüfstandszustand";
    public static final String ANALYSE_ERRORTYPE = "Fehlertyp";
    public static final String ANALYSE_ERROREFFECT = "Fehlerauswirkung";
    public static final String ANALYSE_CONTACT = "Bearbeiter";

    public static final String CORRECTION_ARRANGEMENTS = "Massnahmen";
    public static final String CORRECTION_RESTRICTIONS = "Einschränkungen";

    public static final String CAUSATION_ERRORCAUSE = "Fehlerursache";
    public static final String CAUSATION_DESCRIPTION = "Beschreibung";
    public static final String CAUSATION_VERIFICATION = "Nachweis";

    public static final String COMPOUNDING_DESCRIPTION = "Beschreibung";

    public static final String EFFICIENCY_DESCRIPTION = "Beschreibung";

    public static final String DISPATCH_DESCRIPTION = "Beschreibung";

    private boolean authenticate(){
	    MessageContext messageContext = MessageContext.getCurrentContext();
		VKCUser user = (VKCUser)messageContext.getProperty(MessageContext.AUTHUSER);
		
		if (user != null) {
		    return true;
		}
		
		// Benutzer hat sich nicht authentifiziert
		VKCLogManager.infoLog(this, "Anmeldung fehlgeschlagen!");

		return false;
	}
    
    public Integer setErrorCategory(Integer categoryId) throws AxisFault {
	    VKCLogManager.infoLog(this, "Methode setErrorCategory called...");		
		
		try {
		    if (!authenticate())  {
				return new Integer(-1);
		    }
		    
		    MessageContext messageContext = MessageContext.getCurrentContext();
			VKCUser user = (VKCUser)messageContext.getProperty(MessageContext.AUTHUSER);
			VKCCategory category = new VKCCategory();
			Integer parentCategoryId = null;
		    
			if (categoryId == null || categoryId.intValue() <= 0) {
			    parentCategoryId = new Integer(VKCCategory.ROOT_CATEGORY_ID);
			} else {
			    parentCategoryId = categoryId;
			}
			
			category.setName(ERROR_CATEGORY_NAME + "_" + new Timestamp(System.currentTimeMillis()));
			category.setParentCategoryId(parentCategoryId);
			category.setCreatorId(user.getUserId());
			category.setCreated(new Timestamp(System.currentTimeMillis()));
		    category.setAcl(null);
			category.setAcl(VKCAclSerializer.getAclByCategoryId(parentCategoryId));
			VKCCategorySerializer.saveCategory(category);
			categoryId = category.getCategoryId();
		} catch (Exception ex) {
			VKCLogManager.errorLog(this, "", ex.getMessage(), ex);
			AxisFault fault = AxisFault.makeFault(ex);
			fault.addFaultDetailString("Internal server error");
			throw fault;
	    }
	
		return categoryId;	
    }
	
	public Integer saveNotificationData(Integer categoryId, Integer documentId, String description, String time, String location, String priority, String contact) throws AxisFault {
	    VKCLogManager.infoLog(this, "Methode saveNotificationData called...");		
			
		try {
		    if (!authenticate())  {
				return new Integer(-1);
		    }
		    
		    MessageContext messageContext = MessageContext.getCurrentContext();
			VKCUser user = (VKCUser)messageContext.getProperty(MessageContext.AUTHUSER);
		    VKCDocument document = VKCDocumentSerializer.retrieveDocument(documentId);
		    String text = NOTIFICATION_DESCRIPTION + ": " + VKCStringUtils.decodeXmlEntities(description) + "\n";
		    text += NOTIFICATION_TIME + ": " + VKCStringUtils.decodeXmlEntities(time) + "\n";
		    text += NOTIFICATION_LOCATION + ": " + VKCStringUtils.decodeXmlEntities(location) + "\n";
		    text += NOTIFICATION_PRIORITY + ": " + VKCStringUtils.decodeXmlEntities(priority) + "\n";
		    text += NOTIFICATION_CONTACT + ": " + VKCStringUtils.decodeXmlEntities(contact);
		    
		    if (document == null) {
		        document = new VKCDocument();

		        document.setCategoryId(categoryId);
		        document.setPublished(new Timestamp(System.currentTimeMillis()));
		        document.setLastAccessed(null);
		        document.setLastAccessorId(null);
		        document.setLastChanged(null);
		        document.setLastChangerId(null);
				
		        document.setPublisherId(user.getUserId());
		        document.setTitle(FILENAME_NOTIFICATION + "_" + new Timestamp(System.currentTimeMillis()));
				document.setName(document.getTitle() + ".txt");        
				 
		        document.setArchiveId(WebShare.archive.store(new ByteArrayInputStream(text.getBytes("UTF-8"))));
		        document.setDocSize(new Long(text.getBytes("UTF-8").length));

		        document.setZipped(new Integer(0));
		        document.setDocumentType(VKCDocumentTypeSerializer.getDocumentTypeByExtension("TXT"));
			
		        VKCDocumentSerializer.saveDocument(document, false, false);
		        documentId = document.getDocumentId();
		    } else {
		        document.setLastChanged(new Timestamp(System.currentTimeMillis()));
		        document.setLastChangerId(user.getUserId());
		        document.setTitle(FILENAME_NOTIFICATION + "_" + new Timestamp(System.currentTimeMillis()));
				document.setName(document.getTitle() + ".txt");        
				WebShare.archive.delete(document.getArchiveId());
		        document.setArchiveId(WebShare.archive.store(new ByteArrayInputStream(text.getBytes("UTF-8"))));
		        document.setDocSize(new Long(text.getBytes("UTF-8").length));

		        VKCDocumentSerializer.saveDocument(document, false, false);
		    }		    
		} catch (Exception ex) {
			VKCLogManager.errorLog(this, "", ex.getMessage(), ex);
			AxisFault fault = AxisFault.makeFault(ex);
			fault.addFaultDetailString("Internal server error");
			throw fault;
	    }

		return documentId;	
	}

	public Integer saveAnalyseData(Integer categoryId, Integer documentId, String description, String parameterSet, String testPlantState, String errorType, String errorEffect, String contact) throws AxisFault{
	    VKCLogManager.infoLog(this, "Methode saveAnalyseData called...");		
			
		try {
		    if (!authenticate())  {
				return new Integer(-1);
		    }
		    
		    MessageContext messageContext = MessageContext.getCurrentContext();
			VKCUser user = (VKCUser)messageContext.getProperty(MessageContext.AUTHUSER);
		    VKCDocument document = VKCDocumentSerializer.retrieveDocument(documentId);
		    String text = ANALYSE_DESCRIPTION + ": " + VKCStringUtils.decodeXmlEntities(description) + "\n";
		    text += ANALYSE_PARAMETERSET + ": " + VKCStringUtils.decodeXmlEntities(parameterSet) + "\n";
		    text += ANALYSE_TESTPLANTSTATE + ": " + VKCStringUtils.decodeXmlEntities(testPlantState) + "\n";
		    text += ANALYSE_ERRORTYPE + ": " + VKCStringUtils.decodeXmlEntities(errorType) + "\n";
		    text += ANALYSE_ERROREFFECT + ": " + VKCStringUtils.decodeXmlEntities(errorEffect) + "\n";
		    text += ANALYSE_CONTACT + ": " + VKCStringUtils.decodeXmlEntities(contact);
		    
		    if (document == null) {
		        document = new VKCDocument();

		        document.setCategoryId(categoryId);
		        document.setPublished(new Timestamp(System.currentTimeMillis()));
		        document.setLastAccessed(null);
		        document.setLastAccessorId(null);
		        document.setLastChanged(null);
		        document.setLastChangerId(null);
				
		        document.setPublisherId(user.getUserId());
		        document.setTitle(FILENAME_ANALYSE + "_" + new Timestamp(System.currentTimeMillis()));
				document.setName(document.getTitle() + ".txt");        
				 
		        document.setArchiveId(WebShare.archive.store(new ByteArrayInputStream(text.getBytes("UTF-8"))));
		        document.setDocSize(new Long(text.getBytes("UTF-8").length));

		        document.setZipped(new Integer(0));
		        document.setDocumentType(VKCDocumentTypeSerializer.getDocumentTypeByExtension("TXT"));
			
		        VKCDocumentSerializer.saveDocument(document, false, false);
		        documentId = document.getDocumentId();
		    } else {
		        document.setLastChanged(new Timestamp(System.currentTimeMillis()));
		        document.setLastChangerId(user.getUserId());
		        document.setTitle(FILENAME_ANALYSE + "_" + new Timestamp(System.currentTimeMillis()));
				document.setName(document.getTitle() + ".txt");        
				WebShare.archive.delete(document.getArchiveId());
		        document.setArchiveId(WebShare.archive.store(new ByteArrayInputStream(text.getBytes("UTF-8"))));
		        document.setDocSize(new Long(text.getBytes("UTF-8").length));

		        VKCDocumentSerializer.saveDocument(document, false, false);
		    }		    
		} catch (Exception ex) {
			VKCLogManager.errorLog(this, "", ex.getMessage(), ex);
			AxisFault fault = AxisFault.makeFault(ex);
			fault.addFaultDetailString("Internal server error");
			throw fault;
	    }

		return documentId;	
	}

	public Integer saveCorrectionData(Integer categoryId, Integer documentId, String arrangements, String restrictions) throws AxisFault{
	    VKCLogManager.infoLog(this, "Methode saveCorrectionData called...");		
			
		try {
		    if (!authenticate())  {
				return new Integer(-1);
		    }
		    
		    MessageContext messageContext = MessageContext.getCurrentContext();
			VKCUser user = (VKCUser)messageContext.getProperty(MessageContext.AUTHUSER);
		    VKCDocument document = VKCDocumentSerializer.retrieveDocument(documentId);
		    String text = CORRECTION_ARRANGEMENTS + ": " + VKCStringUtils.decodeXmlEntities(arrangements) + "\n";
		    text += CORRECTION_RESTRICTIONS + ": " + VKCStringUtils.decodeXmlEntities(restrictions);
		    
		    if (document == null) {
		        document = new VKCDocument();

		        document.setCategoryId(categoryId);
		        document.setPublished(new Timestamp(System.currentTimeMillis()));
		        document.setLastAccessed(null);
		        document.setLastAccessorId(null);
		        document.setLastChanged(null);
		        document.setLastChangerId(null);
				
		        document.setPublisherId(user.getUserId());
		        document.setTitle(FILENAME_CORRECTION + "_" + new Timestamp(System.currentTimeMillis()));
				document.setName(document.getTitle() + ".txt");        
				 
		        document.setArchiveId(WebShare.archive.store(new ByteArrayInputStream(text.getBytes("UTF-8"))));
		        document.setDocSize(new Long(text.getBytes("UTF-8").length));

		        document.setZipped(new Integer(0));
		        document.setDocumentType(VKCDocumentTypeSerializer.getDocumentTypeByExtension("TXT"));
			
		        VKCDocumentSerializer.saveDocument(document, false, false);
		        documentId = document.getDocumentId();
		    } else {
		        document.setLastChanged(new Timestamp(System.currentTimeMillis()));
		        document.setLastChangerId(user.getUserId());
		        document.setTitle(FILENAME_CORRECTION + "_" + new Timestamp(System.currentTimeMillis()));
				document.setName(document.getTitle() + ".txt");        
				WebShare.archive.delete(document.getArchiveId());
		        document.setArchiveId(WebShare.archive.store(new ByteArrayInputStream(text.getBytes("UTF-8"))));
		        document.setDocSize(new Long(text.getBytes("UTF-8").length));

		        VKCDocumentSerializer.saveDocument(document, false, false);
		    }		    
		} catch (Exception ex) {
			VKCLogManager.errorLog(this, "", ex.getMessage(), ex);
			AxisFault fault = AxisFault.makeFault(ex);
			fault.addFaultDetailString("Internal server error");
			throw fault;
	    }

		return documentId;	
	}

	public Integer saveCausationData(Integer categoryId, Integer documentId, String errorCause, String description, String verification) throws AxisFault{
	    VKCLogManager.infoLog(this, "Methode saveCausationData called...");		
			
		try {
		    if (!authenticate())  {
				return new Integer(-1);
		    }
		    
		    MessageContext messageContext = MessageContext.getCurrentContext();
			VKCUser user = (VKCUser)messageContext.getProperty(MessageContext.AUTHUSER);
		    VKCDocument document = VKCDocumentSerializer.retrieveDocument(documentId);
		    String text = CAUSATION_ERRORCAUSE + ": " + VKCStringUtils.decodeXmlEntities(errorCause) + "\n";
		    text += CAUSATION_DESCRIPTION + ": " + VKCStringUtils.decodeXmlEntities(description) + "\n";
		    text += CAUSATION_VERIFICATION + ": " + VKCStringUtils.decodeXmlEntities(verification);
		    
		    if (document == null) {
		        document = new VKCDocument();

		        document.setCategoryId(categoryId);
		        document.setPublished(new Timestamp(System.currentTimeMillis()));
		        document.setLastAccessed(null);
		        document.setLastAccessorId(null);
		        document.setLastChanged(null);
		        document.setLastChangerId(null);
				
		        document.setPublisherId(user.getUserId());
		        document.setTitle(FILENAME_CAUSATION + "_" + new Timestamp(System.currentTimeMillis()));
				document.setName(document.getTitle() + ".txt");        
				 
		        document.setArchiveId(WebShare.archive.store(new ByteArrayInputStream(text.getBytes("UTF-8"))));
		        document.setDocSize(new Long(text.getBytes("UTF-8").length));

		        document.setZipped(new Integer(0));
		        document.setDocumentType(VKCDocumentTypeSerializer.getDocumentTypeByExtension("TXT"));
			
		        VKCDocumentSerializer.saveDocument(document, false, false);
		        documentId = document.getDocumentId();
		    } else {
		        document.setLastChanged(new Timestamp(System.currentTimeMillis()));
		        document.setLastChangerId(user.getUserId());
		        document.setTitle(FILENAME_CAUSATION + "_" + new Timestamp(System.currentTimeMillis()));
				document.setName(document.getTitle() + ".txt");        
				WebShare.archive.delete(document.getArchiveId());
		        document.setArchiveId(WebShare.archive.store(new ByteArrayInputStream(text.getBytes("UTF-8"))));
		        document.setDocSize(new Long(text.getBytes("UTF-8").length));

		        VKCDocumentSerializer.saveDocument(document, false, false);
		    }		    
		} catch (Exception ex) {
			VKCLogManager.errorLog(this, "", ex.getMessage(), ex);
			AxisFault fault = AxisFault.makeFault(ex);
			fault.addFaultDetailString("Internal server error");
			throw fault;
	    }

		return documentId;	
	}

	public Integer saveCompoundingData(Integer categoryId, Integer documentId, String description) throws AxisFault{
	    VKCLogManager.infoLog(this, "Methode saveCompoundingData called...");		
			
		try {
		    if (!authenticate())  {
				return new Integer(-1);
		    }
		    
		    MessageContext messageContext = MessageContext.getCurrentContext();
			VKCUser user = (VKCUser)messageContext.getProperty(MessageContext.AUTHUSER);
		    VKCDocument document = VKCDocumentSerializer.retrieveDocument(documentId);
		    String text = COMPOUNDING_DESCRIPTION + ": " + VKCStringUtils.decodeXmlEntities(description);
		    
		    if (document == null) {
		        document = new VKCDocument();

		        document.setCategoryId(categoryId);
		        document.setPublished(new Timestamp(System.currentTimeMillis()));
		        document.setLastAccessed(null);
		        document.setLastAccessorId(null);
		        document.setLastChanged(null);
		        document.setLastChangerId(null);
				
		        document.setPublisherId(user.getUserId());
		        document.setTitle(FILENAME_COMPOUNDING + "_" + new Timestamp(System.currentTimeMillis()));
				document.setName(document.getTitle() + ".txt");        
				 
		        document.setArchiveId(WebShare.archive.store(new ByteArrayInputStream(text.getBytes("UTF-8"))));
		        document.setDocSize(new Long(text.getBytes("UTF-8").length));

		        document.setZipped(new Integer(0));
		        document.setDocumentType(VKCDocumentTypeSerializer.getDocumentTypeByExtension("TXT"));
			
		        VKCDocumentSerializer.saveDocument(document, false, false);
		        documentId = document.getDocumentId();
		    } else {
		        document.setLastChanged(new Timestamp(System.currentTimeMillis()));
		        document.setLastChangerId(user.getUserId());
		        document.setTitle(FILENAME_COMPOUNDING + "_" + new Timestamp(System.currentTimeMillis()));
				document.setName(document.getTitle() + ".txt");        
				WebShare.archive.delete(document.getArchiveId());
		        document.setArchiveId(WebShare.archive.store(new ByteArrayInputStream(text.getBytes("UTF-8"))));
		        document.setDocSize(new Long(text.getBytes("UTF-8").length));

		        VKCDocumentSerializer.saveDocument(document, false, false);
		    }		    
		} catch (Exception ex) {
			VKCLogManager.errorLog(this, "", ex.getMessage(), ex);
			AxisFault fault = AxisFault.makeFault(ex);
			fault.addFaultDetailString("Internal server error");
			throw fault;
	    }

		return documentId;	
	}

	public Integer saveEfficiencyData(Integer categoryId, Integer documentId, String description) throws AxisFault{
	    VKCLogManager.infoLog(this, "Methode saveEfficiencyData called...");		
			
		try {
		    if (!authenticate())  {
				return new Integer(-1);
		    }
		    
		    MessageContext messageContext = MessageContext.getCurrentContext();
			VKCUser user = (VKCUser)messageContext.getProperty(MessageContext.AUTHUSER);
		    VKCDocument document = VKCDocumentSerializer.retrieveDocument(documentId);
		    String text = EFFICIENCY_DESCRIPTION + ": " + VKCStringUtils.decodeXmlEntities(description);
		    
		    if (document == null) {
		        document = new VKCDocument();

		        document.setCategoryId(categoryId);
		        document.setPublished(new Timestamp(System.currentTimeMillis()));
		        document.setLastAccessed(null);
		        document.setLastAccessorId(null);
		        document.setLastChanged(null);
		        document.setLastChangerId(null);
				
		        document.setPublisherId(user.getUserId());
		        document.setTitle(FILENAME_EFFICIENCY + "_" + new Timestamp(System.currentTimeMillis()));
				document.setName(document.getTitle() + ".txt");        
				 
		        document.setArchiveId(WebShare.archive.store(new ByteArrayInputStream(text.getBytes("UTF-8"))));
		        document.setDocSize(new Long(text.getBytes("UTF-8").length));

		        document.setZipped(new Integer(0));
		        document.setDocumentType(VKCDocumentTypeSerializer.getDocumentTypeByExtension("TXT"));
			
		        VKCDocumentSerializer.saveDocument(document, false, false);
		        documentId = document.getDocumentId();
		    } else {
		        document.setLastChanged(new Timestamp(System.currentTimeMillis()));
		        document.setLastChangerId(user.getUserId());
		        document.setTitle(FILENAME_EFFICIENCY + "_" + new Timestamp(System.currentTimeMillis()));
				document.setName(document.getTitle() + ".txt");        
				WebShare.archive.delete(document.getArchiveId());
		        document.setArchiveId(WebShare.archive.store(new ByteArrayInputStream(text.getBytes("UTF-8"))));
		        document.setDocSize(new Long(text.getBytes("UTF-8").length));

		        VKCDocumentSerializer.saveDocument(document, false, false);
		    }		    
		} catch (Exception ex) {
			VKCLogManager.errorLog(this, "", ex.getMessage(), ex);
			AxisFault fault = AxisFault.makeFault(ex);
			fault.addFaultDetailString("Internal server error");
			throw fault;
	    }

		return documentId;	
	}

	public Integer saveDispatchData(Integer categoryId, Integer documentId, String description) throws AxisFault{
	    VKCLogManager.infoLog(this, "Methode saveDispatchData called...");		
			
		try {
		    if (!authenticate())  {
				return new Integer(-1);
		    }
		    
		    MessageContext messageContext = MessageContext.getCurrentContext();
			VKCUser user = (VKCUser)messageContext.getProperty(MessageContext.AUTHUSER);
		    VKCDocument document = VKCDocumentSerializer.retrieveDocument(documentId);
		    String text = DISPATCH_DESCRIPTION + ": " + VKCStringUtils.decodeXmlEntities(description);
		    
		    if (document == null) {
		        document = new VKCDocument();

		        document.setCategoryId(categoryId);
		        document.setPublished(new Timestamp(System.currentTimeMillis()));
		        document.setLastAccessed(null);
		        document.setLastAccessorId(null);
		        document.setLastChanged(null);
		        document.setLastChangerId(null);
				
		        document.setPublisherId(user.getUserId());
		        document.setTitle(FILENAME_DISPATCH + "_" + new Timestamp(System.currentTimeMillis()));
				document.setName(document.getTitle() + ".txt");        
				 
		        document.setArchiveId(WebShare.archive.store(new ByteArrayInputStream(text.getBytes("UTF-8"))));
		        document.setDocSize(new Long(text.getBytes("UTF-8").length));

		        document.setZipped(new Integer(0));
		        document.setDocumentType(VKCDocumentTypeSerializer.getDocumentTypeByExtension("TXT"));
			
		        VKCDocumentSerializer.saveDocument(document, false, false);
		        documentId = document.getDocumentId();
		    } else {
		        document.setLastChanged(new Timestamp(System.currentTimeMillis()));
		        document.setLastChangerId(user.getUserId());
		        document.setTitle(FILENAME_DISPATCH + "_" + new Timestamp(System.currentTimeMillis()));
				document.setName(document.getTitle() + ".txt");        
				WebShare.archive.delete(document.getArchiveId());
		        document.setArchiveId(WebShare.archive.store(new ByteArrayInputStream(text.getBytes("UTF-8"))));
		        document.setDocSize(new Long(text.getBytes("UTF-8").length));

		        VKCDocumentSerializer.saveDocument(document, false, false);
		    }		    
		} catch (Exception ex) {
			VKCLogManager.errorLog(this, "", ex.getMessage(), ex);
			AxisFault fault = AxisFault.makeFault(ex);
			fault.addFaultDetailString("Internal server error");
			throw fault;
	    }

		return documentId;	
	}

	public Integer searchRelatedDocument(Integer notificationDocumentId, Integer analyseDocumentId) throws AxisFault{
	    VKCLogManager.infoLog(this, "Methode searchRelatedDocument called...");		
	    Integer documentId = new Integer(-1);
			
		try {
		    if (!authenticate())  {
				return new Integer(-1);
		    }
		    
		    //MessageContext messageContext = MessageContext.getCurrentContext();
			//VKCUser user = (VKCUser)messageContext.getProperty(MessageContext.AUTHUSER);
		    Collection relatedDocs = Search.search("contents:(fehler*)", null, null, "*", VKCUserSerializer.retrieveUser(VKCUser.ADMIN_USER_ID), null);
		    
		    if (!relatedDocs.isEmpty()) {
		        List documents = new ArrayList(relatedDocs);
		        documentId = new Integer(((SearchResultEntry)documents.get(0)).getDocumentId());
		    }
		} catch (Exception ex) {
			VKCLogManager.errorLog(this, "", ex.getMessage(), ex);
			AxisFault fault = AxisFault.makeFault(ex);
			fault.addFaultDetailString("Internal server error");
			throw fault;
	    }

		return documentId;	
	}

	public void saveAllDocumentData(Integer categoryId, Integer notificationDocumentId, Integer analyseDocumentId, Integer correctionDocumentId, Integer causationDocumentId, Integer compoundingDocumentId, Integer efficiencyDocumentId, Integer dispatchDocumentId) throws AxisFault{
	    VKCLogManager.infoLog(this, "Methode saveAllDocumentData called...");		
			
		try {
		    if (!authenticate())  {
				return;
		    }
		    
		    MessageContext messageContext = MessageContext.getCurrentContext();
			VKCUser user = (VKCUser)messageContext.getProperty(MessageContext.AUTHUSER);

			VKCDocument notificationDocument = VKCDocumentSerializer.retrieveDocument(notificationDocumentId);
			VKCDocument analyseDocument = VKCDocumentSerializer.retrieveDocument(analyseDocumentId);
			VKCDocument correctionDocument = VKCDocumentSerializer.retrieveDocument(correctionDocumentId);
			VKCDocument causationDocument = VKCDocumentSerializer.retrieveDocument(causationDocumentId);
			VKCDocument compoundingDocument = VKCDocumentSerializer.retrieveDocument(compoundingDocumentId);
			VKCDocument efficiencyDocument = VKCDocumentSerializer.retrieveDocument(efficiencyDocumentId);
			VKCDocument dispatchDocument = VKCDocumentSerializer.retrieveDocument(dispatchDocumentId);

			byte[] b = null;
			String text = "";
			
			if (notificationDocument != null) {
			    b = WebShare.archive.load(notificationDocument.getArchiveId());
				text += VKCStringUtils.decodeXmlEntities(notificationDocument.getTitle());
				text += "\n----------------------------------------\n";
			    text += new String(b, "UTF-8");
				text += "\n----------------------------------------\n";
			}
			
			if (analyseDocument != null) {
			    b = WebShare.archive.load(analyseDocument.getArchiveId());
				text += "\n\n" + VKCStringUtils.decodeXmlEntities(analyseDocument.getTitle());
				text += "\n----------------------------------------\n";
				text += new String(b, "UTF-8");
				text += "\n----------------------------------------\n";
			}
			
			if (correctionDocument != null) {
			    b = WebShare.archive.load(correctionDocument.getArchiveId());
				text += "\n\n" + VKCStringUtils.decodeXmlEntities(correctionDocument.getTitle());
				text += "\n----------------------------------------\n";
				text += new String(b, "UTF-8");
				text += "\n----------------------------------------\n";
			}
			
			if (causationDocument != null) {
			    b = WebShare.archive.load(causationDocument.getArchiveId());
				text += "\n\n" + VKCStringUtils.decodeXmlEntities(causationDocument.getTitle());
				text += "\n----------------------------------------\n";
				text += new String(b, "UTF-8");
				text += "\n----------------------------------------\n";
			}
			
			if (compoundingDocument != null) {
			    b = WebShare.archive.load(compoundingDocument.getArchiveId());
				text += "\n\n" + VKCStringUtils.decodeXmlEntities(compoundingDocument.getTitle());
				text += "\n----------------------------------------\n";
				text += new String(b, "UTF-8");
				text += "\n----------------------------------------\n";
			}
			
			if (efficiencyDocument != null) {
			    b = WebShare.archive.load(efficiencyDocument.getArchiveId());
				text += "\n\n" + VKCStringUtils.decodeXmlEntities(efficiencyDocument.getTitle());
				text += "\n----------------------------------------\n";
				text += new String(b, "UTF-8");
				text += "\n----------------------------------------\n";
			}
			
			if (dispatchDocument != null) {
			    b = WebShare.archive.load(dispatchDocument.getArchiveId());
				text += "\n\n" + VKCStringUtils.decodeXmlEntities(dispatchDocument.getTitle());
				text += "\n----------------------------------------\n";
				text += new String(b, "UTF-8");
				text += "\n----------------------------------------\n";
			}
			
	        VKCDocument document = new VKCDocument();

	        document.setCategoryId(categoryId);
	        document.setPublished(new Timestamp(System.currentTimeMillis()));
	        document.setLastAccessed(null);
	        document.setLastAccessorId(null);
	        document.setLastChanged(null);
	        document.setLastChangerId(null);
			
	        document.setPublisherId(user.getUserId());
	        document.setTitle(FILENAME_ALLDOCUMENTS + "_" + new Timestamp(System.currentTimeMillis()));
			document.setName(document.getTitle() + ".txt");        
			 
	        document.setArchiveId(WebShare.archive.store(new ByteArrayInputStream(text.getBytes("UTF-8"))));
	        document.setDocSize(new Long(text.getBytes("UTF-8").length));

	        document.setZipped(new Integer(0));
	        document.setDocumentType(VKCDocumentTypeSerializer.getDocumentTypeByExtension("TXT"));
		
	        VKCDocumentSerializer.saveDocument(document, true, false);
		} catch (Exception ex) {
			VKCLogManager.errorLog(this, "", ex.getMessage(), ex);
			AxisFault fault = AxisFault.makeFault(ex);
			fault.addFaultDetailString("Internal server error");
			throw fault;
	    }
	}
}