HOWTO - Sashipa development

 Delete in cascade with Sashipa-Melba.

Your user doesn't want to delete each dependent record for deleting a card. He prefers all dependent records are automatically deleted when he deletes the card.

Warning !

I recommand you to use the feature of your DBMS if it exists. Then you can use this HOWTO.

This feature isn't implemented by the compiler Sashipa2Melba. So it's required to manually modify the generated Java code, then to compile this java code. This operation is only for Java programers.

Add to this, the modified Java file has to be modified again after each generation. So it's not convenient for client-database architecture. But if your architecture is client-servlet-database you can do it because the file to modify is the Servlet file. The servlet has to be updated only when there are changes in the environment or the architecture element, in your Sashipa code.

Step 1: take the good Java source file

Here is the method:

Now you are ready to add Java code for deleting in cascade.

Step 2: Modify Java code

In the class XxxAnalyser, several methods return null. Among these methods, there is a doActionBeforeExecute method that has a deleted query as parameter (object MpDeleteQuery). We are going to add our Java code in this method.

In our example, all contact addresses are deleted when a contact is deleted.

  protected String doActionBeforeExecute(
        ClientSession session, MpDeleteQuery query,
        MpBufferUpdate bufferUpdate
        ) throws FatalException { 
    String dbTable = query.getSchemaTable();
    if (dbTable.equalsIgnoreCase("CONTACT")) {
      try {
        ConstList lstPkValue = query.getPkValueList();
        for (int i=0; i < lstPkValue.size(); i++) {
          MpPkValue pkValue = (MpPkValue) lstPkValue.get(i);
          // - delete dependencies in CONTACTADDRESS
          deleteDependentRecord(
            query.getDbms(), query.getDatabase(),
            "CONTACTADDRESS",
            new String[] {"ContactRef"},
            pkValue, 
            bufferUpdate
          );
        }
        return null;            // ok, no error
      }
      catch (Throwable x) {
        return "Error when deleting addresses\n\n" +
               x.getMessage();
      }
    }
    return null;
  }

This method returns null for continuing the delete process. An error message is returned for aborting the delete process. Error message will be logged and displayed to user. (It's not a fatal error, just a working error).

A MpDeleteQuery object contains a set of primary key values to delete. Our code executes a delete query like "DELETE FROM CONTACTADDRESS WHERE ContactRef=XXX" for each primary key value (XXX is the primary key value).

Step 3: Compile Java code

The method:

Once this operation is terminated, a good habitude is to copy/paste the added Java code, into an XML comment at the end of your Sashipa file.

Ok I know it's a little bit complex, but it's existing...

© Copyright 2003 Sashipa-Melba Team. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License as much as this note clearly appears.