/**
 * $Id: DocumentService.java,v 1.1 2004/08/23 10:54:21 bigmac Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 */

package vkc.ws.axis.service;

import org.apache.axis.AxisFault;
import org.apache.axis.MessageContext;
import org.w3c.dom.Document;

import vkc.db.entities.VKCCategory;
import vkc.db.entities.VKCDocument;
import vkc.db.entities.VKCUser;
import vkc.db.serializer.VKCDocumentSerializer;
import vkc.security.PermissionManager;
import vkc.ws.axis.response.*;
import vkc.xml.XmlCategoryUtils;
import vkc.xml.XmlDocumentUtils;
import vkc.xml.XmlUtils;

public class DocumentService {

	/**
	 * Method returns the meta data of a document as an xml document
	 */
	public String getDocumentMeta( Integer documentId ) 
	throws AxisFault {
		
		try {

			MessageContext messageContext = MessageContext.getCurrentContext();
			VKCUser user = (VKCUser)messageContext.getProperty( MessageContext.AUTHUSER );
			if ( user==null ) {
				// Benutzer hat sich nicht authentifiziert
				return XmlUtils.convertToString( 
					XmlResponseFactory.createErrorResponseDocument( 
						"Unauthorized access not allowed" ) );
			}
				
			if ( documentId==null ) {
				// Fehlende Dokument-ID in der Anfrage
				return XmlUtils.convertToString( 
					XmlResponseFactory.createErrorResponseDocument( 
						"No document id specified" ) );
			}
	
			VKCDocument vkcDoc = VKCDocumentSerializer.retrieveDocument( documentId );
			if ( vkcDoc==null ) {
				// kein Dokument mit der ID in der DB vorhanden
				return XmlUtils.convertToString( 
					XmlResponseFactory.createErrorResponseDocument( 
						"Document not found: docId=" + documentId ) );
			}
				
			VKCCategory category = vkcDoc.getCategory();
			if ( category == null ) {

				// Kategorie existiert nicht
				return XmlUtils.convertToString( 
					XmlResponseFactory.createErrorResponseDocument( 
						"Document category could not be loaded: docId=" + documentId + ", catId=" + vkcDoc.getCategoryId() ) );
			}

			if ( !PermissionManager.hasPermission( category.getAcl(), user, 'r' ) ) {

				// Benutzer hat keine Leseberechtigung
				return XmlUtils.convertToString( 
					XmlResponseFactory.createErrorResponseDocument( 
						"No read permission for document category: docId=" + documentId + ", catId=" + vkcDoc.getCategoryId() ) );
			}

			// Versionsdaten des Dokuments in XML-Form als Antwort liefern
			Document responseDoc = XmlResponseFactory.createResponseDocument();
			XmlDocumentUtils.addDocumentMetaData( responseDoc, vkcDoc );
			return XmlUtils.convertToString( responseDoc );

		} catch (Exception ex) {

			AxisFault fault = AxisFault.makeFault( ex );
			fault.addFaultDetailString( "Internal server error" );
			throw fault;
		}
	}
	
	/**
	 * Method returns the version information of a document as an xml document
	 */
	public String getDocumentVersionInfo( Integer documentId ) 
	throws AxisFault {

		try {

			MessageContext messageContext = MessageContext.getCurrentContext();
			VKCUser user = (VKCUser)messageContext.getProperty( MessageContext.AUTHUSER );
			if ( user==null ) {
				// Benutzer hat sich nicht authentifiziert
				return XmlUtils.convertToString( 
					XmlResponseFactory.createErrorResponseDocument( 
						"Unauthorized access not allowed" ) );
			}
			
			if ( documentId==null ) {
				// Fehlende Dokument-ID in der Anfrage
				return XmlUtils.convertToString( 
					XmlResponseFactory.createErrorResponseDocument( 
						"No document id specified" ) );
			}

			VKCDocument vkcDoc = VKCDocumentSerializer.retrieveDocument( documentId );
			if ( vkcDoc==null ) {
				// kein Dokument mit der ID in der DB vorhanden
				return XmlUtils.convertToString( 
					XmlResponseFactory.createErrorResponseDocument( 
						"Document not found: docId=" + documentId ) );
			}
			
			VKCCategory category = vkcDoc.getCategory();
			if ( category == null ) {

				// Kategorie existiert nicht
				return XmlUtils.convertToString( 
					XmlResponseFactory.createErrorResponseDocument( 
						"Document category could not be loaded: docId=" + documentId + ", catId=" + vkcDoc.getCategoryId() ) );
			}

			if ( !PermissionManager.hasPermission( category.getAcl(), user, 'r' ) ) {

				// Benutzer hat keine Leseberechtigung
				return XmlUtils.convertToString( 
					XmlResponseFactory.createErrorResponseDocument( 
						"No read permission for document category: docId=" + documentId + ", catId=" + vkcDoc.getCategoryId() ) );
			}

			// Versionsdaten des Dokuments in XML-Form als Antwort liefern
			Document responseDoc = XmlResponseFactory.createResponseDocument();
			XmlDocumentUtils.addDocumentVersionData( responseDoc, vkcDoc );
			return XmlUtils.convertToString( responseDoc );

		} catch (Exception ex) {

			AxisFault fault = AxisFault.makeFault( ex );
			fault.addFaultDetailString( "Internal server error" );
			throw fault;
		}
	}
	
	/**
	 * Method returns the meta data of a document as an xml document
	 */
	public String getDocumentCategoryMeta( Integer documentId ) 
	throws AxisFault {
		
		try {

			MessageContext messageContext = MessageContext.getCurrentContext();
			VKCUser user = (VKCUser)messageContext.getProperty( MessageContext.AUTHUSER );
			if ( user==null ) {
				// Benutzer hat sich nicht authentifiziert
				return XmlUtils.convertToString( 
					XmlResponseFactory.createErrorResponseDocument( 
						"Unauthorized access not allowed" ) );
			}
				
			if ( documentId==null ) {
				// Fehlende Dokument-ID in der Anfrage
				return XmlUtils.convertToString( 
					XmlResponseFactory.createErrorResponseDocument( 
						"No document id specified" ) );
			}
	
			VKCDocument vkcDoc = VKCDocumentSerializer.retrieveDocument( documentId );
			if ( vkcDoc==null ) {

				// kein Dokument mit der ID in der DB vorhanden
				return XmlUtils.convertToString( 
					XmlResponseFactory.createErrorResponseDocument( 
						"Document not found: docId=" + documentId ) );
			}

			VKCCategory category = vkcDoc.getCategory();
			if ( category == null ) {

				// Kategorie existiert nicht
				return XmlUtils.convertToString( 
					XmlResponseFactory.createErrorResponseDocument( 
						"Document category could not be loaded: docId=" + documentId + ", catId=" + vkcDoc.getCategoryId() ) );
			}

			if ( !PermissionManager.hasPermission( category.getAcl(), user, 'r' ) ) {

				// Benutzer hat keine Leseberechtigung
				return XmlUtils.convertToString( 
					XmlResponseFactory.createErrorResponseDocument( 
						"No read permission for document category: docId=" + documentId + ", catId=" + vkcDoc.getCategoryId() ) );
			}
			
			// Versionsdaten des Dokuments in XML-Form als Antwort liefern
			Document responseDoc = XmlResponseFactory.createResponseDocument();
			XmlCategoryUtils.addCategoryMetaData( responseDoc, category );
			return XmlUtils.convertToString( responseDoc );

		} catch (Exception ex) {

			AxisFault fault = AxisFault.makeFault( ex );
			fault.addFaultDetailString( "Internal server error" );
			throw fault;
		}
	}
	
}
