HOWTO - Sashipa development
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.
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.
Here is the method:
a) Build your application (s2m command).
b) Take the Java source file in <melbalab-home>/work/src_java/. For architecture 'clientDatabase' it's the unique Java file. Otherwise it's the Servlet Java file.
c) Edit it with an editor that accepts 'LF' character for changing line (ie. not Notepad !). Then search for the class that extends PacketFromClientAnalyser.
Now you are ready to add Java code for deleting in cascade.
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).
The method:
a) Because you've just generated your application, there is a file <melbalab-home>/work/classes/melba_classes.jar.
b) Include this file in the compilation class-path. If you work with shell command, your command is like this one:
javac -classpath .;"<melbalab>/work/classes/melba_classes.jar;path_to_servlet.jar" MyFile.java |
c) If you are in client-servlet-database mode, you just have to copy '.class' compiled Java files to the right place in your servlets container. In client-database mode a jar file must be created (cf jar command in the jdk).
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.