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

package org.ellectra.gui.managebeans;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

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

    
    private static byte[] getByteData (String path) {
		byte[] result = null;
		try {
			byte[] temp = new byte[1024];
			int len = 0;
			FileInputStream inputFile = new FileInputStream (path);
			ByteArrayOutputStream outputFile = new ByteArrayOutputStream();
			while (inputFile.available() != 0) {
				len = inputFile.read(temp);
				outputFile.write(temp, 0, len);
			}
			inputFile.close();
			result = outputFile.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
    
    public String GetTechDoc(int tenderid)
    {
        String filename="ERROR";
        
try { // Call Web Service Operation
   org.ellectra.wsproposaldocs.ProposalDocumentManagementService service = new org.ellectra.wsproposaldocs.ProposalDocumentManagementService();
   org.ellectra.wsproposaldocs.ProposalDocumentManagement port = service.getProposalDocumentManagementPort();
      // TODO process result here
   java.util.List<org.ellectra.wsproposaldocs.ProposalDocument> result = port.queryProposalDocumentFindAll();
   //System.out.println("Result = "+result);
   for (int ddi=0;ddi<result.size();ddi++)
   {
       org.ellectra.wsproposaldocs.ProposalDocument ditem=result.get(ddi);
       if(ditem.getTender().getId()==tenderid)
       {
           if (ditem.getDocumentType().equalsIgnoreCase("TECHNICAL_PROPOSAL"))
           {
               return ditem.getFilename();
           }
       }
   }
} catch (Exception ex) {
   // TODO handle custom exceptions here
}

        return filename;
    }
    
    public String GetFinaDoc(int tenderid)
    {
        String filename="ERROR";
        
try { // Call Web Service Operation
   org.ellectra.wsproposaldocs.ProposalDocumentManagementService service = new org.ellectra.wsproposaldocs.ProposalDocumentManagementService();
   org.ellectra.wsproposaldocs.ProposalDocumentManagement port = service.getProposalDocumentManagementPort();
      // TODO process result here
   java.util.List<org.ellectra.wsproposaldocs.ProposalDocument> result = port.queryProposalDocumentFindAll();
   //System.out.println("Result = "+result);
   for (int ddi=0;ddi<result.size();ddi++)
   {
       org.ellectra.wsproposaldocs.ProposalDocument ditem=result.get(ddi);
       if(ditem.getTender().getId()==tenderid)
       {
           if (ditem.getDocumentType().equalsIgnoreCase("FINANCIAL_PROPOSAL"))
           {
               return ditem.getFilename();
           }
       }
   }
} catch (Exception ex) {
   // TODO handle custom exceptions here
}

        return filename;
    }
    
    
    public byte[] GetPrivateKey(String username,String org,String ten,int doctype)
    {
      
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 = username;
	java.lang.String arg1 = org;
	java.lang.String arg2 = "GUEST";
	java.lang.String arg3 = ten;
	int arg4 = doctype;
   // TODO process result here
   byte[] result = port.getUserPrivateKey(arg0, arg1, arg2, arg3, arg4);
   //System.out.println("Result = "+result);
   return result;
} catch (Exception ex) {
   // TODO handle custom exceptions here
    return null;
}
  
    }
    public String DecryptTechDocs(String tenderid,int phase)
    {
   String szName="";     
   String szOrgname="";     
try { // Call Web Service Operation
   org.ellectra.wstender.TenderManagementService service = new org.ellectra.wstender.TenderManagementService();
   org.ellectra.wstender.TenderManagement port = service.getTenderManagementPort();
   	 // TODO initialize WS operation arguments here
	java.lang.Integer arg0 = Integer.valueOf(tenderid);
   // TODO process result here
   java.util.List<org.ellectra.wstender.Tender> result = port.queryTenderFindById(arg0);
   //System.out.println("Result = "+result);
   org.ellectra.wstender.Tender dTender=result.get(0);
    szName=dTender.getUserAccount().getUserName();
   szOrgname=dTender.getUserAccount().getBelong().getOrganisationName();
   System.err.print("Name and Org"+szName+"  "+szOrgname);
} catch (Exception ex) {
   // TODO handle custom exceptions here
}

if (phase==1)
{
    String filenamedd=GetTechDoc(Integer.valueOf(tenderid));
    
    String fileName=trimFilePath(filenamedd);
    deletefile(filenamedd);
        pathSettings dset=new pathSettings();
        String szPath=dset.GetUploadbDirectory();
        
    renamefile(szPath+fileName+"b",filenamedd);
    
//        
//        byte[] secretkey=getByteData(filenamedd+"k");
//        System.err.print("Secret"+secretkey);
//        System.err.print("Private"+GetPrivateKey(szName,szOrgname,tenderid,2));
//try { // Call Web Service Operation
//   org.ellectra.directenw.EllectraCryptoService service = new org.ellectra.directenw.EllectraCryptoService();
//   org.ellectra.directenw.EllectraCrypto port = service.getEllectraCryptoPort();
//   	 // TODO initialize WS operation arguments here
//	byte[] arg0 = secretkey;
//	byte[] arg1 = GetPrivateKey(szName,szOrgname,tenderid,2);
//	java.lang.String arg2 = filenamedd;
//	java.lang.String arg3 = filenamedd+"out";
//   // TODO process result here
//   java.lang.String result = port.ellectraCryptoDecrypt(arg0, arg1, arg2, arg3);
//   System.out.println("Result = "+result);
//} catch (Exception ex) {
//   // TODO handle custom exceptions here
//}
    

}
   
   if (phase==2)
{
    String filenamedd=GetFinaDoc(Integer.valueOf(tenderid));
    
    String fileName=trimFilePath(filenamedd);
    deletefile(filenamedd);
        pathSettings dset=new pathSettings();
        String szPath=dset.GetUploadDirectory();
        
    renamefile(szPath+fileName+"b",filenamedd);
    
//        
//        byte[] secretkey=getByteData(filenamedd+"k");
//        System.err.print("Secret"+secretkey);
//        System.err.print("Private"+GetPrivateKey(szName,szOrgname,tenderid,2));
//try { // Call Web Service Operation
//   org.ellectra.directenw.EllectraCryptoService service = new org.ellectra.directenw.EllectraCryptoService();
//   org.ellectra.directenw.EllectraCrypto port = service.getEllectraCryptoPort();
//   	 // TODO initialize WS operation arguments here
//	byte[] arg0 = secretkey;
//	byte[] arg1 = GetPrivateKey(szName,szOrgname,tenderid,2);
//	java.lang.String arg2 = filenamedd;
//	java.lang.String arg3 = filenamedd+"out";
//   // TODO process result here
//   java.lang.String result = port.ellectraCryptoDecrypt(arg0, arg1, arg2, arg3);
//   System.out.println("Result = "+result);
//} catch (Exception ex) {
//   // TODO handle custom exceptions here
//}
    

}
        return "OK";
    }

 public  String trimFilePath(String fileName) {
        return fileName
            .substring(fileName.lastIndexOf("/") + 1)
            .substring(fileName.lastIndexOf("\\") + 1);
    }
public boolean deletefile(String Filename)
    {
        File ftemp= new File(Filename);
        return ftemp.delete();
        
    }
    
    public boolean renamefile(String Filename,String NewFilename)
    {
        File ftemp= new File(Filename);
        File ftemp2= new File(NewFilename);
        
        return ftemp.renameTo(ftemp2);
    }
}
