Concepten opslaan en ophalen

De volgende code wordt gebruikt om de letterinstantie op te slaan. De metagegevens van de letterinstantie worden opgeslagen in het dialoogvenster pictogrammen tabel. Er wordt een unieke tekenreeks (conceptID) gegenereerd en geretourneerd. Deze unieke tekenreeks wordt vervolgens gebruikt om opgeslagen letterinstantie op te halen.

public String save(CCRDocumentInstance letterToSave) throws CCRDocumentException {
  String insertRowSQL = "INSERT INTO aemformstutorial.icdrafts(draftID,documentID,status,owner,name) VALUES(?,?,?,?,?)";
  log.debug(" in save IC Draft" + letterToSave.getDocumentId() + letterToSave.getName());
  UUID uuid = UUID.randomUUID();
  String uuidString = uuid.toString();
  Connection connection = getConnection();
  PreparedStatement pstmt = null;
  Document icData = letterToSave.getData();
  try {
    pstmt = connection.prepareStatement(insertRowSQL);
    pstmt.setString(1, uuidString);
    pstmt.setString(2, letterToSave.getDocumentId());
    pstmt.setString(3, "DRAFT");
    pstmt.setString(4, letterToSave.getCreatedBy());
    pstmt.setString(5, letterToSave.getName());
    icData.copyToFile(new File(uuidString + ".xml"));
    log.debug("Executing the insert statment  " + pstmt.executeUpdate());
    connection.commit();
  } catch (IOException | SQLException e) {
    log.debug("The error is " + e.getMessage());
  } finally {
    if (pstmt != null) {
      try {
        pstmt.close();
      } catch (SQLException e) {
        log.debug("Error in closing prepared statment" + e.getMessage());
      }
    }
    if (connection != null) {
      try {
        log.debug("Closing the connection in Save Letter Draft");
        connection.close();
      } catch (SQLException e) {
        log.debug("Error in closing connection" + e.getMessage());
      }
    }

  }

  return uuidString;
}

Letter ophalen

De volgende code is geschreven om de opgeslagen conceptbrief op te halen.
Als u een opgeslagen letter-instantie wilt laden, moet u de conceptID opgeven. Gebaseerd op dit conceptID vragen wij het gegevensbestand om de extra meta-gegevens over de brief te krijgen. Dezelfde conceptID wordt gebruikt om de gegevens van de letter te maken door de juiste xml uit het bestandssysteem te lezen. Een voorwerp CCRDocumentInstance wordt dan gebouwd en teruggekeerd.

@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;
}

Letter bijwerken

De volgende code is gebruikt om opgeslagen letter-instantie bij te werken. De gegevens van de bijgewerkte brief worden naar het bestandssysteem geschreven met de letter-id.

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());;
            }

    }

Alle opgeslagen letters ophalen

AEM Forms biedt geen gebruikersinterface buiten het vak om de opgeslagen letters weer te geven. In dit artikel worden de opgeslagen lettertypen weergegeven in een tabelindeling met behulp van een adaptief formulier.
U kunt de query aanpassen om de opgeslagen letter-instanties op te halen. In dit voorbeeld, vraag ik voor bewaarde brieveninstantie door "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;
    }

Eclipse Project

Het eclipse-project met voorbeeldimplementatie kan hier gedownload

recommendation-more-help
8de24117-1378-413c-a581-01e660b7163e