Ottieni lettera

Il codice seguente è stato scritto per recuperare la bozza di lettera salvata.
Per caricare le istanze di una lettera salvata è necessario fornire il valore draftID. In base a questo ID bozza, eseguiamo una query sul database per ottenere i metadati aggiuntivi sulla lettera. Lo stesso draftID viene utilizzato per creare i dati della lettera leggendo l'xml appropriato dal file system. Viene quindi creato e restituito un oggetto CCRDocumentInstance.

@Override
public CCRDocumentInstance get(String draftID) throws CCRDocumentException {

  String selectStatement = "Select documentID from aemformstutorial.icdrafts where draftID='" + draftID + "'";
  log.debug("The select statement is " + selectStatement);
  Connection connection = getConnection();
  Statement statement = null;
  String documentID = "";
  try {
    statement = connection.createStatement();
    ResultSet rs = statement.executeQuery(selectStatement);
    while (rs.next()) {
      documentID = rs.getString("documentID");

    }
  } catch (SQLException e) {
    log.debug("The error is " + e.getMessage());
  }
  Document draftData = new Document(new File(draftID + ".xml"));
  CCRDocumentInstance draftInstance = new CCRDocumentInstance(draftData, "abc", documentID, CCRDocumentInstance.Status.DRAFT);
  draftInstance.setId(draftID);
  return draftInstance;
}

Aggiorna lettera

Il codice seguente è stato utilizzato per aggiornare l’istanza della lettera salvata. I dati della lettera aggiornata vengono scritti nel file system utilizzando l'ID della lettera.

public void update(CCRDocumentInstance letterInstanceToUpdate) throws CCRDocumentException {
        Document icData = letterInstanceToUpdate.getData();
        String draftID = letterInstanceToUpdate.getId();
        log.debug("updating letter instance with draft id =  "+draftID);
        try
            {
                icData.copyToFile(new File(draftID+".xml"));
            }
        catch (IOException e)
            {
                log.debug("Error updating "+e.getMessage());;
            }

    }

Ottieni tutte le lettere salvate

AEM Forms non fornisce alcuna interfaccia utente predefinita per elencare le lettere salvate. Per questo articolo elenco le istanze di lettere salvate in formato tabulare utilizzando un modulo adattivo.
È possibile personalizzare la query per recuperare le istanze di lettere salvate. In questo esempio, sto cercando l’istanza della lettera salvata da "admin".

    public List < CCRDocumentInstance > getAll(String arg0, Date arg1, Date arg2, Map < String, Object > arg3) throws CCRDocumentException {
      String selectStatement = "Select * from aemformstutorial.icdrafts where owner = 'admin'";
      Connection connection = getConnection();
      Statement statement = null;
      String documentID = "";
      List < CCRDocumentInstance > listOfDrafts = new ArrayList < CCRDocumentInstance > ();
      String draftID;
      String savedInstanceName = "";
      try {
        statement = connection.createStatement();
        ResultSet rs = statement.executeQuery(selectStatement);
        while (rs.next()) {
          documentID = rs.getString("documentID");
          draftID = rs.getString("draftID");
          savedInstanceName = rs.getString("name");
          Document draftData = new Document(new File(draftID + ".xml"));
          CCRDocumentInstance draftLetter = new CCRDocumentInstance(draftData, savedInstanceName, documentID, CCRDocumentInstance.Status.DRAFT);
          listOfDrafts.add(draftLetter);
        }
      } catch (SQLException e) {
        log.debug("The error is " + e.getMessage());
      } finally {
        if (statement != null) {
          try {
            statement.close();
          } catch (SQLException e) {
            log.debug("error in closing statement" + e.getMessage());
          }
        }
        if (connection != null) {
          try {
            connection.close();
          } catch (SQLException e) {
            log.debug("error in closing connection" + e.getMessage());
          }
        }
      }

      return listOfDrafts;
    }