package net.catcert.psa.test.query;

import net.catcert.psa.services.PsaServices;
import net.catcert.psa.util.Utils;
import schema.core._0._1.dss.tc.names.oasis.InternationalStringType;
import schema.core._0._1.dss.tc.names.oasis.ResultDocument.Result;

import com.sadiel.catcert.psa.ws.client.QueryServiceStub;
import com.sadiel.catcert.psa.ws.client.factory.PSARequestFactory;
import com.sadiel.catcert.psa.ws.query.DownloadSignatureAtPSADocument;
import com.sadiel.catcert.psa.ws.query.DownloadSignatureAtPSAResponseDocument;

import dss_directsign.profiles._0._1.psa.catcert.DocumentHashDocument.DocumentHash;
import dss_directsign.profiles._0._1.psa.catcert.DocumentOrDocumentHashType;
import dss_directsign.profiles._0._1.psa.catcert.DocumentType;
import dss_getdata.profiles._0._1.psa.catcert.DownloadSignatureAtPSARSPNSDocument.DownloadSignatureAtPSARSPNS;


/**
 * Invokes Download Signature at psa query service
 * @author suport tčcnic CATCert
 *
 */
public class DownloadSignatureAtPSASample {

	public static void main(String[] args){

		try{

			//política de procediment
			String signedDocId = "1ffa2f8c-5117-4e89-b64c-82be788cb331";
			String[] oids = new String[1];
			oids[0] = signedDocId;

			//localització (idioma)
			String locale = "ES";

			//petició
			DownloadSignatureAtPSADocument request = PSARequestFactory._().createDownloadSignatureAtPSA(locale, oids);

			//servei query
			QueryServiceStub queryServiceStub = PsaServices.getInstance().getQueryServiceStub();

			//enviament de la petició
			//resposta
			DownloadSignatureAtPSAResponseDocument responseDoc = queryServiceStub.downloadSignatureAtPSA(request);
			DownloadSignatureAtPSARSPNS response = responseDoc.getDownloadSignatureAtPSAResponse().getDownloadSignatureAtPSARSPNS();

			//resultat de l'operació sol·licitada
			System.out.println("Result:");
			Result result = response.getResult();
			String major = result.getResultMajor();
			String minor = result.getResultMinor();
			InternationalStringType message = result.getResultMessage();
			System.out.println("Major: "+major);
			System.out.println("Minor: "+minor);
			if(message!=null)
				System.out.println("Message: "+message.getStringValue());

			//document signat
			DocumentOrDocumentHashType outDocs = response.getOutputDocuments();
			if(outDocs!=null){
				DocumentType[] outDocsList = outDocs.getDocumentArray();
				DocumentHash[] outDocsHashList = outDocs.getDocumentHashArray();

				if(outDocsList!=null){
					DocumentType doc = outDocsList[0];
					System.out.println("Doc ID: "+doc.getID());
					System.out.println("Doc filename: "+doc.getFilename());
					Utils.writeDataToFile("C:/temp/"+signedDocId+".tmp", doc.getData());
				} else if(outDocsHashList!=null){
					DocumentHash doc = outDocsHashList[0];
					System.out.println("Doc Hash ID: "+doc.getID());
					System.out.println("Doc Hash digest: "+doc.getDigestValue());
					System.out.println("Doc Hash algorithm: "+doc.getDigestMethod().getAlgorithm());
					Utils.writeDataToFile("C:/temp/"+signedDocId+".tmp", doc.getDigestValue());
				}

			}

		} catch (Throwable e) {
			e.printStackTrace();
		}
	}	

}
