Automatic Key Generation

From Apache OpenOffice Wiki
Jump to: navigation, search

Another problem frequently encountered is the automatic generation of unique keys. There are reasons for doing this on the client side, and missing support, for example, auto-increment fields in your database backend, or you need this value before inserting the row. Apache OpenOffice is currently limited in re-fetching the server-side generated value after a record has been inserted.

Assume that you have a method called generateUniqueKey() to generate a unique key that could be queried from a key generator on a database server, or in a single-user-environment by selecting the maximum of the existing keys and incrementing it by 1. This fragment inserts the generated value into the given column of a given form:

  public void insertUniqueKey(XPropertySet xForm, String sFieldName) throws {
      // get the column object
      XColumnsSupplier xSuppCols = (XColumnsSupplier)UnoRuntime.queryInterface(
          XColumnsSupplier.class, xForm);
      XNameAccess xCols = xSuppCols.getColumns();
      XColumnUpdate xCol = (XColumnUpdate)UnoRuntime.queryInterface(
          XColumnUpdate.class, xCols.getByName(sFieldName));
      xCol.updateInt(generateUniqueKey(xForm, sFieldName));

A solution to determine when the insertion is to happen has been introduced in a previous chapter, that is, we could fill in the value as soon as the form is positioned on the insert row, wait for the user's input in the other fields, and save the record.

Another approach is to step in immediately before the record is inserted. For this, the is used. It notifies listeners when rows are inserted, the listeners can veto this, and final changes can be made to the new record:

  public boolean approveRowChange(RowChangeEvent aEvent) throws {
      if (RowChangeAction.INSERT == aEvent.Action) {
          // the affected form
          XPropertySet xFormProps = (XpropertySet)UnoRuntime.queryInterface(
              XpropertySet.class, aEvent.Source);
          // insert a new unique value
          insertUniqueKey(xFormProps, m_sFieldName);
      return true;
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages