Difference between revisions of "Documentation/DevGuide/Forms/Automatic Key Generation"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (Robot: Changing Category:Documentation/Developers Guide/Forms)
 
(One intermediate revision by one other user not shown)
Line 5: Line 5:
 
|NextPage=Documentation/DevGuide/Forms/Data Validation
 
|NextPage=Documentation/DevGuide/Forms/Data Validation
 
}}
 
}}
{{DISPLAYTITLE:Automatic Key Generation}}
+
{{Documentation/DevGuideLanguages|Documentation/DevGuide/Forms/{{SUBPAGENAME}}}}
 +
{{DISPLAYTITLE:Automatic Key Generation}}
 
<!--<idltopic>com.sun.star.sdb.XRowSetApproveBroadcaster</idltopic>-->
 
<!--<idltopic>com.sun.star.sdb.XRowSetApproveBroadcaster</idltopic>-->
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. {{PRODUCTNAME}} is currently limited in re-fetching the server-side generated value ''after'' a record has been inserted.
+
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. {{AOo}} is currently limited in re-fetching the server-side generated value ''after'' a record has been inserted.
  
 
Assume that you have a method called <code>generateUniqueKey()</code> 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:  
 
Assume that you have a method called <code>generateUniqueKey()</code> 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:  
 
<!--[SOURCE:Forms/KeyGenerator.java]-->
 
<!--[SOURCE:Forms/KeyGenerator.java]-->
<source lang="java">
+
<syntaxhighlight lang="java">
 
   public void insertUniqueKey(XPropertySet xForm, String sFieldName) throws com.sun.star.uno.Exception {
 
   public void insertUniqueKey(XPropertySet xForm, String sFieldName) throws com.sun.star.uno.Exception {
 
       // get the column object
 
       // get the column object
Line 22: Line 23:
 
       xCol.updateInt(generateUniqueKey(xForm, sFieldName));
 
       xCol.updateInt(generateUniqueKey(xForm, sFieldName));
 
   }
 
   }
</source>
+
</syntaxhighlight>
 
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.
 
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 <idl>com.sun.star.sdb.XRowSetApproveBroadcaster</idl> is used. It notifies listeners when rows are inserted, the listeners can veto this, and final changes can be made to the new record:  
 
Another approach is to step in immediately before the record is inserted. For this, the <idl>com.sun.star.sdb.XRowSetApproveBroadcaster</idl> is used. It notifies listeners when rows are inserted, the listeners can veto this, and final changes can be made to the new record:  
 
<!--[SOURCE:Forms/KeyGenerator.java]-->
 
<!--[SOURCE:Forms/KeyGenerator.java]-->
<source lang="java">
+
<syntaxhighlight lang="java">
 
   public boolean approveRowChange(RowChangeEvent aEvent) throws com.sun.star.uno.RuntimeException {
 
   public boolean approveRowChange(RowChangeEvent aEvent) throws com.sun.star.uno.RuntimeException {
 
       if (RowChangeAction.INSERT == aEvent.Action) {
 
       if (RowChangeAction.INSERT == aEvent.Action) {
Line 38: Line 39:
 
       return true;
 
       return true;
 
   }
 
   }
</source>
+
</syntaxhighlight>
 
{{PDL1}}
 
{{PDL1}}
  
 
[[Category:Documentation/Developer's Guide/Forms]]
 
[[Category:Documentation/Developer's Guide/Forms]]

Latest revision as of 15:59, 21 December 2020



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 com.sun.star.uno.Exception {
      // 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 com.sun.star.sdb.XRowSetApproveBroadcaster 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 com.sun.star.uno.RuntimeException {
      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