Consultor Eletrônico



Kbase 18492: Apptivity: Avoiding A Switch Statement In ServerRequests
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   10/16/2008
Status: Unverified

FACT(s) (Environment):

Apptivity

FIX:

When using multiple ServerRequests, it is usually the case that in the
ServerEvent Java class, a long Switch statement is needed to determine the method that should be called.

This can be changed to a generic routine that calls the methods based on the reflection API. Through this mechanism you only need to specify the exact "NAME" (Case sensitive) of the method to call. In this example the "id" field is used to specify the name of the method to call.

The ServerRequest call on the client would be something like:


abVector tmp = theApp.serverRequest(1, "computeTotal", arg);

The Server code could look like following:


public class ServerEventReflectionServerEvents extends abServerEvents

{
public abVector computeTotal(abVector arg)
{
abVector returnValue = new abVector();
returnValue.addElement(new abInteger(2703)); // example
return(returnValue);
}

public abVector serverRequest (abServer server,
int reqType,
String id,
abQuery query,
abVector arg) throws abException
{
abVector returnValue = new abVector();
System.out.println("The id = " + id);

Object result = null;
Object[] args = { arg };
boolean error = false;

final Class[] params = { abVector.class };

try {
result = this.getClass().getMethod(id, params).invoke(this, args);
}
catch (Exception e) {
System.out.println(" --> error: "+e);
error = true;
}

if (!error) {
// YES! We found the appropriate method (probably)!
System.out.println("FOUND IT!!!!!!!!!!!!!");
}
if (result != null && result instanceof abVector) {
return (abVector)result;
}

return null;
}
}