/*
 * $Id: SharkUtils.java,v 1.4 2004/11/24 09:26:03 lawern Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * Created on 09.07.2004
 *
 * $Log: SharkUtils.java,v $
 * Revision 1.4  2004/11/24 09:26:03  lawern
 * Hibernate ist VKC-Geschichte!
 *
 * Revision 1.3  2004/11/10 15:02:54  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.2  2004/09/20 10:26:12  koppy
 * +javadoc Dokumentation
 *
 * Revision 1.1  2004/08/30 14:50:00  koppy
 * Initiale Version der Workflow-Komponente
 *
 */
package vkc.workflow.util;

import org.enhydra.shark.api.RootException;
import org.enhydra.shark.api.client.wfbase.BaseException;
import org.enhydra.shark.api.client.wfservice.ExecutionAdministration;
import org.enhydra.shark.api.client.wfservice.NotConnected;
import org.enhydra.shark.api.internal.working.CallbackUtilities;

import vkc.db.VKCDatabaseManager;
import vkc.view.WebShare;
import WorkflowService.AdminMisc;
import WorkflowService.MappingAdministration;
import WorkflowService.PackageAdministration;
import WorkflowService.RepositoryMgr;
import WorkflowService.SharkConnection;

/**
 * This utility class provides some 
 * useful methods for the work with shark's client APIs.
 * 
 * @author Andreas Koop
 */
public final class SharkUtils {

   /**
    * Disconnects the ExecutionAdministration interface (POJO).
    * @param ea ExecutionAdministration
    */
    public static final void disconnect( ExecutionAdministration ea ){
        try {
            ea.disconnect();
        } catch (BaseException e) {
        } catch (NotConnected e) {
        }
    }
    
    /**
     * Disconnects the ExecutionAdministration interface (CORBA).
     * @param ea ExecutionAdministration
     */
    public static final void disconnect( WorkflowService.ExecutionAdministration ea ){
        try {
            ea.disconnect();
        } catch (WfBase.BaseException e) {
        } catch (WorkflowService.NotConnected e) {
        }
    }

    /**
     * Disconnects the AdminMisc interface.
     * @param adminMisc AdminMisc
     */
    public static final void disconnect(AdminMisc adminMisc) {
        try {
            adminMisc.disconnect();
        } catch (WfBase.BaseException e) {
        } catch (WorkflowService.NotConnected e) {
        }
        
    }

    /**
     * Disconnects the PackageAdministration interface.
     * @param pkgAdmin PackageAdministration
     */
    public static final void disconnect(PackageAdministration pkgAdmin) {
        try {
            pkgAdmin.disconnect();
        } catch (WfBase.BaseException e) {
        } catch (WorkflowService.NotConnected e) {
        }
        
    }

    /**
     * Disconnects the Repository manager interface.
     * @param rm repository manager
     */
    public static final void disconnect(RepositoryMgr rm) {
        try {
            rm.disconnect();
        } catch (WfBase.BaseException e) {
        } catch (WorkflowService.NotConnected e) {
        }
    }

    /**
     * Disconnects the SharkConnection.
     * @param conn SharkConnection
     */
    public static final void disconnect(SharkConnection conn) {
        try {
            conn.disconnect();
        } catch (WfBase.BaseException e) {
        } catch (WorkflowService.NotConnected e) {
        }
        
    }

    /**
     * Disconnects the MappingAdministration interface.
     * @param ma MappingAdministration
     */
    public static void disconnect(MappingAdministration ma) {
        try {
            ma.disconnect();
        } catch (WfBase.BaseException e) {
        } catch (WorkflowService.NotConnected e) {
        }
        
    }

    /**
     * Initializes the VKC application from within shark.
     * 
     * @param properties vkc db properties, etc...
     * @throws RootException
     */
    public static synchronized void initVKC(CallbackUtilities cus) throws RootException {
        if(WebShare.database != null )
		    return;
		
		if( cus.getProperties().getProperty( "vkc.db.url") == null)
		    throw new RootException( "Properties for connecting to VKC database not defined!" );
		
		try {
			// Database Initialization       
			WebShare.database = VKCDatabaseManager.getDatabase(cus.getProperties());

		} catch (Exception e) {
			cus.error( "Error while initialization of VKC!", new RootException(e));
			throw new RootException(e);
		}
    }
    
    /**
     * This method provides a very crude means of parsing Shark's XML
     * representation of extended attributes in a case-insensitive manner.
     * 
     * @param extAttribs string containig the XML extended attributes
     * @param name name of an extended attribute
     */
    public static String getExtendedAttribValue(String extAttribs, String name)
    {
       String result = null;

       String lcExtAttribs = extAttribs.toLowerCase();

       String strToMatch = "<extendedattribute name=\"" + name.toLowerCase()
          + "\" value=\"";


       int beginIndex = lcExtAttribs.indexOf(strToMatch);
       if (beginIndex>=0) {
          beginIndex+=strToMatch.length();

          int endIndex = lcExtAttribs.indexOf("\"", beginIndex);

          if (endIndex >= 0)
          {
             result = extAttribs.substring(beginIndex, endIndex).trim();
          }
       }
       return result;
    }
}
