/**
   * preparing data to send to applet for sign process
   *
   * @param dataFromApplet encoded64 data received from applet certificate choice
   * @return the data to passa to sign applet
   */
  protected void prepareDataToAppletSign() {

    // calcolo impronte documenti selezionati
    SinekartaUtility su = SinekartaUtility.getCurrentInstance();

    try {

      // copio il file per la conversione
      NodeRef folder = su.getNodeService().getPrimaryParent(selectedDocumentNode).getParentRef();

      FileInfo newFile =
          su.getFileFolderService().copy(selectedDocumentNode, folder, documentNamePdf);

      selectedDocumentNodePdf = newFile.getNodeRef();

      // conversione documento in PDF/A
      Action documentToPDFA =
          su.getActionService().createAction(DocumentToPDFA.ACTION_NAME_DOCUMENT_TO_PDFA);
      try {
        su.getActionService().executeAction(documentToPDFA, selectedDocumentNodePdf, false, false);
      } catch (Throwable t) {
        tracer.error("Unable to execute PDF/A conversion : " + t.getMessage(), t);
        throw new SignFailedException("Unable to execute PDF/A conversion : " + t.getMessage(), t);
      }

      Action digitalSignaturePrepareAndAddDocument =
          su.getActionService()
              .createAction(
                  DocumentDigitalSignaturePrepareAndAddDocument
                      .ACTION_NAME_DOCUMENT_DIGITAL_SIGNATURE_PREPARE_AND_ADD_DOCUMENT);
      digitalSignaturePrepareAndAddDocument.setParameterValue(
          DocumentDigitalSignaturePrepareAndAddDocument.PARAM_SIGN_DESCRIPTION, description);
      digitalSignaturePrepareAndAddDocument.setParameterValue(
          DocumentDigitalSignaturePrepareAndAddDocument.PARAM_SIGN_LOCATION,
          Constants.SIGN_LOCATION_ITALY);
      digitalSignaturePrepareAndAddDocument.setParameterValue(
          DocumentDigitalSignaturePrepareAndAddDocument.PARAM_CLIENT_AREA,
          su.getDataFromAppletCertificateChoice());
      try {
        su.getActionService()
            .executeAction(
                digitalSignaturePrepareAndAddDocument, selectedDocumentNodePdf, false, false);
      } catch (Throwable t) {
        tracer.error("Unable to prepare data for document sign : " + t.getMessage(), t);
        throw new SignFailedException(
            "Unable to prepare data for document sign : " + t.getMessage(), t);
      }
      su.setDataToAppletSign(
          (String)
              digitalSignaturePrepareAndAddDocument.getParameterValue(
                  DocumentDigitalSignaturePrepareAndAddDocument.PARAM_RESULT));
    } catch (SignFailedException e) {
      tracer.error(e.getMessage(), e);
      throw e;
    } catch (Exception e) {
      tracer.error("Unable to calculate document fingerprint.", e);
      throw new SignFailedException(
          "Unable to calculate document fingerprint : " + e.getMessage(), e);
    }
  }
  @Override
  public String back() {
    String stepName = Application.getWizardManager().getCurrentStepName();
    SinekartaUtility su = SinekartaUtility.getCurrentInstance();
    if (stepName.equals(STEP_NAME_CERTIFICATE_CHOICE)) {

      prepareDataToAppletCertificateChoice();

      su.setDataFromAppletCertificateChoice(null);

    } else if (stepName.equals(STEP_NAME_SIGN)) {

      prepareDataToAppletSign();

      su.setDataFromAppletSign(null);
    }

    return null;
  }
 /**
  * prepare data to pass to applet for certificate choice
  *
  * @return encoded64 string to pass to applet
  */
 protected void prepareDataToAppletCertificateChoice() {
   // calcolo impronte documenti selezionati
   SinekartaUtility su = SinekartaUtility.getCurrentInstance();
   // conversione documento in PDF/A
   Action digitalSignatureInit =
       su.getActionService()
           .createAction(DocumentDigitalSignatureInit.ACTION_NAME_DOCUMENT_DIGITAL_SIGNATURE_INIT);
   try {
     su.getActionService()
         .executeAction(digitalSignatureInit, selectedDocumentNodeParent, false, false);
   } catch (Throwable t) {
     tracer.error(
         "Unable to initialize digital signature alfresco action service : " + t.getMessage(), t);
     throw new SignFailedException(
         "Unable to initialize digital signature alfresco action service : " + t.getMessage(), t);
   }
   su.setDataToAppletCertificateChoice(
       (String) digitalSignatureInit.getParameterValue(DocumentDigitalSignatureInit.PARAM_RESULT));
 }
  public String next() {
    String stepName = Application.getWizardManager().getCurrentStepName();
    SinekartaUtility su = SinekartaUtility.getCurrentInstance();

    if (stepName.equals(STEP_NAME_CERTIFICATE_CHOICE)) {

      prepareDataToAppletCertificateChoice();

      su.setDataFromAppletCertificateChoice(null);

    } else if (stepName.equals(STEP_NAME_SIGN)) {

      // adjust documentName
      documentName = PDFTools.calculatePdfName(documentNamePdf);

      // verify if a signed document version of this document already exists
      NodeRef folder = su.getNodeService().getPrimaryParent(selectedDocumentNode).getParentRef();

      if (su.getFileFolderService().searchSimple(folder, documentName) != null) {
        Util.addFatalMessage(BUNDLE_GENERIC_SIGN, "fileAlreadyExists", "fileAlreadyExistsDesc");
        Application.getWizardManager()
            .getState()
            .setCurrentStep(Application.getWizardManager().getCurrentStep() - 1);
        return null;
      }

      prepareDataToAppletSign();

      su.setDataFromAppletSign(null);
    }

    return null;
  }
  /** this method is called when finish button is pressed applying the sign returned from applet */
  protected String finishImpl(FacesContext context, String outcome) throws Exception {
    SinekartaUtility su = SinekartaUtility.getCurrentInstance();

    // applicao firma ai documenti selezionati

    try {

      // invoking generic sign apply action
      Action documentDigitalSignatureApply =
          su.getActionService()
              .createAction(
                  DocumentDigitalSignatureApply.ACTION_NAME_DOCUMENT_DIGITAL_SIGNATURE_APPLY);
      documentDigitalSignatureApply.setParameterValue(
          DocumentDigitalSignatureApply.PARAM_CLIENT_AREA, su.getDataFromAppletSign());
      try {
        su.getActionService()
            .executeAction(documentDigitalSignatureApply, selectedDocumentNodeParent, false, false);
      } catch (Throwable t) {
        tracer.error("Unable to apply sign to document : " + t.getMessage(), t);
        throw new SignFailedException("Unable to apply sign to document : " + t.getMessage(), t);
      }

      su.getNodeService()
          .setProperty(selectedDocumentNodePdf, ContentModel.PROP_NAME, documentName);

    } catch (SignFailedException e) {
      tracer.error(e.getMessage(), e);
      throw e;
    } catch (Exception e) {
      tracer.error("Unable to apply sign to document : " + e.getMessage(), e);
      throw new SignFailedException("Unable to apply sign to document : " + e.getMessage(), e);
    }

    return outcome;
  }
  private void init() {
    error = false;
    SinekartaUtility su = SinekartaUtility.getCurrentInstance();

    su.setDataToAppletCertificateChoice(null);
    su.setDataFromAppletCertificateChoice(null);
    su.setDataToAppletSign(null);
    su.setDataFromAppletSign(null);
    description = null;
    selectedDocumentNodePdf = null;

    selectedDocumentNode = browseBean.getActionSpace().getNodeRef();

    documentName =
        (String) su.getNodeService().getProperty(selectedDocumentNode, ContentModel.PROP_NAME);

    documentPath =
        NodeTools.translatePath(
            su.getNodeService(), su.getNodeService().getPath(selectedDocumentNode));
    documentPath = documentPath.substring(0, documentPath.indexOf(documentName));

    // verify if a signed document version of this document already exists
    selectedDocumentNodeParent =
        su.getNodeService().getPrimaryParent(selectedDocumentNode).getParentRef();

    NodeRef exist = null;
    int i = 0;
    do {
      // calculating new filename
      documentNamePdf =
          (String) su.getNodeService().getProperty(selectedDocumentNode, ContentModel.PROP_NAME);
      int dotIdx = documentNamePdf.lastIndexOf('.');
      if (i == 0) {
        documentNamePdf =
            documentNamePdf.substring(0, dotIdx)
                + Configuration.getInstance().getSignedSuffix()
                + documentNamePdf.substring(dotIdx);
      } else {
        documentNamePdf =
            documentNamePdf.substring(0, dotIdx)
                + " ("
                + i
                + ")"
                + Configuration.getInstance().getSignedSuffix()
                + documentNamePdf.substring(dotIdx);
      }

      // adjust documentName
      documentName = PDFTools.calculatePdfName(documentNamePdf);

      exist = su.getFileFolderService().searchSimple(selectedDocumentNodeParent, documentName);
      i++;
    } while (exist != null);

    // preparazione area di input per applet

    prepareDataToAppletCertificateChoice();

    su.setDataFromAppletCertificateChoice(null);
  }