/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ellectra.clientside;

import java.io.FileOutputStream;
import java.io.IOException;
import org.ellectra.gui.managebeans.*;

/**
 *
 * @author tdi
 */
public class generatekeys {

	String szPath="";
    private void SaveOnFile(String StoragePath,byte[] signature)
    {
        try{
            System.err.print("save"+StoragePath+signature);
        FileOutputStream outSig = new FileOutputStream(StoragePath);
        outSig.write(signature);
        outSig.flush();
        outSig.close();
        }
        catch(java.io.IOException e)
        {
            
        }
    }
    
    private boolean SaveSecretTech()
    {
       
        try { // Call Web Service Operation
           org.ellectra.wskm.KeyManagerService service = new org.ellectra.wskm.KeyManagerService();
           org.ellectra.wskm.KeyManager port = service.getKeyManagerPort();
                 // TODO initialize WS operation arguments here
                 java.lang.String arg0 = "ELLECTRA";
                        java.lang.String arg1 = "CLIENTSIDE";
                        java.lang.String arg2 = "TECHNICAL";
                        java.lang.String arg3 = "ALL";
                        int arg4 = 2;
           // TODO process result here
           byte[] result = port.getUserSecretKey(arg0, arg1, arg2, arg3, arg4);
           SaveOnFile(szPath+"secrettech.key",result);
           
        } catch (Exception ex) {
           // TODO handle custom exceptions here
            return false;
        }
        return true;
    }
    
    private boolean SaveSecretFina()
    {
       
        try { // Call Web Service Operation
           org.ellectra.wskm.KeyManagerService service = new org.ellectra.wskm.KeyManagerService();
           org.ellectra.wskm.KeyManager port = service.getKeyManagerPort();
                 // TODO initialize WS operation arguments here
                 java.lang.String arg0 = "ELLECTRA";
                        java.lang.String arg1 = "CLIENTSIDE";
                        java.lang.String arg2 = "FINANCE";
                        java.lang.String arg3 = "ALL";
                        int arg4 = 2;
           // TODO process result here
           byte[] result = port.getUserSecretKey(arg0, arg1, arg2, arg3, arg4);
           SaveOnFile(szPath+"secretfina.key",result);
           
        } catch (Exception ex) {
           // TODO handle custom exceptions here
            return false;
        }
        return true;
    }
    
    private boolean SavePrivate()
    {
       
        try { // Call Web Service Operation
           org.ellectra.wskm.KeyManagerService service = new org.ellectra.wskm.KeyManagerService();
           org.ellectra.wskm.KeyManager port = service.getKeyManagerPort();
                 // TODO initialize WS operation arguments here
                java.lang.String arg0 = "ELLECTRA";
                java.lang.String arg1 = "CLIENTSIDE";
                java.lang.String arg2 = "ADMIN";
                java.lang.String arg3 = "ALL";
                int arg4 = 2;
           // TODO process result here
           byte[] result = port.getUserPrivateKey(arg0, arg1, arg2, arg3, arg4);
           SaveOnFile(szPath+"private.key",result);
           
        } catch (Exception ex) {
           // TODO handle custom exceptions here
            return false;
        }
         return true;
    }
    
    private boolean SavePublic()
    {
        try { // Call Web Service Operation
            org.ellectra.wskm.KeyManagerService service = new org.ellectra.wskm.KeyManagerService();
            org.ellectra.wskm.KeyManager port = service.getKeyManagerPort();
   	 // TODO initialize WS operation arguments here
	java.lang.String arg0 = "ELLECTRA";
	java.lang.String arg1 = "CLIENTSIDE";
	java.lang.String arg2 = "ADMIN";
	java.lang.String arg3 = "ALL";
	int arg4 = 2;
        // TODO process result here
        byte[] result = port.getUserPublicKey(arg0, arg1, arg2, arg3, arg4);
        SaveOnFile(szPath+"public.key",result);
        
        } catch (Exception ex) {
            // TODO handle custom exceptions here
            return false;
        }
        return true;

    }
    public boolean GenerateAllKeys()
    {
    	pathSettings dset=new pathSettings();
        szPath=dset.GetUploadDirectory();
        SaveSecretFina();
        SavePublic();
        SavePrivate();    
        SaveSecretTech();
        return false;
    }
}
