/*
 * $Id: CORBARepositoryManager.java,v 1.2 2004/09/20 10:26:11 koppy Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 * 
 * Created on 13.07.2004
 *
 * $Log: CORBARepositoryManager.java,v $
 * Revision 1.2  2004/09/20 10:26:11  koppy
 * +javadoc Dokumentation
 *
 * Revision 1.1  2004/08/30 14:49:56  koppy
 * Initiale Version der Workflow-Komponente
 *
 */
package vkc.workflow.service.corba;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

import vkc.workflow.WorkflowException;
import vkc.workflow.model.PackageInfo;
import vkc.workflow.service.CORBAWorkflowService;
import vkc.workflow.service.VKCWorkflowService;
import vkc.workflow.service.WfMConnection;
import vkc.workflow.service.manager.RepositoryManager;
import vkc.workflow.util.SharkUtils;
import WorkflowService.PackageAdministration;
import WorkflowService.RepositoryInvalid;
import WorkflowService.RepositoryMgr;

/**
 * This class represents the CORBA client version
 * of the repository manager.
 * 
 * @author Andreas Koop
 */
public class CORBARepositoryManager implements RepositoryManager {

    private CORBAWorkflowService wfService = null;
    
    public CORBARepositoryManager( CORBAWorkflowService wfService ){
        this.wfService = wfService;
    }

    /** 
     * (non-Javadoc)
     * @see vkc.workflow.service.manager.RepositoryManager#getPackageInfos(vkc.workflow.service.WfMConnection)
     */
    public List getPackageInfos( WfMConnection sConn) throws WorkflowException{
        
        List l = new ArrayList();
        PackageAdministration pa = wfService.getShark()
								        .getAdminInterface()
								        .getPackageAdministration();
		RepositoryMgr rm = wfService.getShark()
										.getRepositoryManager();

        try {
            pa.connect( sConn.getUsername(), sConn.getPassword(), wfService.getEngine(), "");
            rm.connect( sConn.getUsername(), sConn.getPassword(), wfService.getEngine(), "");
            String [] paths = rm.getPackagePaths();

            for (int i = 0; i < paths.length; i++) {
                String string = paths[i];
                String pkgId = rm.getPackageId(string);
                
                PackageInfo pinfo = new PackageInfo();
                pinfo.setPath( string );
                pinfo.setPackageId( pkgId );
                pinfo.setOpened( pa.isPackageOpened(pkgId) );
                
                l.add(pinfo);
            }
            
        } catch (Exception e) {
            throw new WorkflowException( "wf.error.notConnected",e);
        }finally {
            SharkUtils.disconnect( pa );
            SharkUtils.disconnect( rm );
        }
        
        return l;
    }

    /** 
     * (non-Javadoc)
     * @see vkc.workflow.service.manager.RepositoryManager#deletePackage(vkc.workflow.service.WfMConnection, java.lang.String)
     */
    public void deletePackage(WfMConnection sConn, String path) throws WorkflowException{
        
        RepositoryMgr rm = wfService.getShark().getRepositoryManager();
        if(VKCWorkflowService.debug)
            System.err.println( "Deleting " + path );
        
        try {
            rm.connect(sConn.getUsername(), sConn.getPassword(), wfService
                    .getEngine(), "");
            rm.deletePkg(path);
        } catch (RepositoryInvalid e) {
            throw new WorkflowException("wf.error.repositoryInvalid", e);
        } catch (Exception e) {
            throw new WorkflowException("wf.error.notConnected", e);
        }finally {
            SharkUtils.disconnect( rm );
        }
        
    }

    /** 
     * (non-Javadoc)
     * @see vkc.workflow.service.manager.RepositoryManager#uploadPackage(vkc.workflow.service.WfMConnection, byte[], java.lang.String)
     */
    public void uploadPackage(WfMConnection sConn, byte[] utf8bytes, String path) throws WorkflowException{
        
        if(utf8bytes == null)
            return;
        
        if(VKCWorkflowService.debug)
            System.err.println( "Uploading " + path );
        
        RepositoryMgr rm = wfService.getShark().getRepositoryManager();
       
        try {
            rm.connect( sConn.getUsername(), sConn.getPassword(), wfService.getEngine(), "");
            rm.uploadPkg( utf8bytes, path);
        } catch (RepositoryInvalid e) {
            throw new WorkflowException("wf.error.repositoryInvalid", e);
        } catch (Exception e) {
            throw new WorkflowException("wf.error.notConnected", e);
        }finally {
            SharkUtils.disconnect( rm );
        }
       
        
    }

    /** 
     * (non-Javadoc)
     * @see vkc.workflow.service.manager.RepositoryManager#uploadPackage(vkc.workflow.service.WfMConnection, java.io.File, java.lang.String)
     */
    public void uploadPackage(WfMConnection sConn, File file, String path) throws WorkflowException{
        
        byte[] utf8Bytes = null;
        try {
            RandomAccessFile raf = new RandomAccessFile(file, "r");
            
            long noOfBytes = raf.length();
            if (noOfBytes > 0) {
                utf8Bytes = new byte[(int) noOfBytes];
                raf.seek(0);
                raf.readFully(utf8Bytes);
            }

            
        } catch (FileNotFoundException e) {
            throw new WorkflowException( "wf.error.fileNotFound", e);
        } catch (IOException e) {
            throw new WorkflowException( "wf.error.io", e);
        }
        
        this.uploadPackage(sConn, utf8Bytes, path);
        
    }


}
