Author: fdesbois
Date: 2010-04-02 17:44:28 +0200 (Fri, 02 Apr 2010)
New Revision: 1871
Log:
Evo #439 : don't generate context and exception class + clean methods, abstract order and add javadoc
Modified:
trunk/pom.xml
trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/ServiceTransformer.java
trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/TopiaGeneratorUtil.java
Modified: trunk/pom.xml
===================================================================
--- trunk/pom.xml 2010-04-02 09:19:54 UTC (rev 1870)
+++ trunk/pom.xml 2010-04-02 15:44:28 UTC (rev 1871)
@@ -190,7 +190,7 @@
<eugene.version>2.0</eugene.version>
<lutinutil.version>1.2</lutinutil.version>
<processor.version>1.0.3</processor.version>
- <i18n.version>1.2</i18n.version>
+ <i18n.version>1.2.1-SNAPSHOT</i18n.version>
<xmlrpc.version>3.1.2</xmlrpc.version>
<hibernate.version>3.3.2.GA</hibernate.version>
Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/ServiceTransformer.java
===================================================================
--- trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/ServiceTransformer.java 2010-04-02 09:19:54 UTC (rev 1870)
+++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/ServiceTransformer.java 2010-04-02 15:44:28 UTC (rev 1871)
@@ -2,12 +2,13 @@
package org.nuiton.topia.generator;
import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DurationFormatUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuiton.eugene.GeneratorUtil;
-import org.nuiton.eugene.Template;
import org.nuiton.eugene.java.ObjectModelTransformerToJava;
import org.nuiton.eugene.models.object.ObjectModel;
import org.nuiton.eugene.models.object.ObjectModelClass;
@@ -27,33 +28,16 @@
* This Template is used to create the skeleton of services for a final
* application which using Topia.
* <div>
- * Generation from a model named 'App' :
- * <ul>
- * <li>AppContext : empty super interface to used in application UI. Can
- * be override in model if put in defaultPackage (ex : org.chorem.app) set
- * in maven-eugene-plugin configuration.
- * </li>
- * <li><p>AppContextImplementor : interface which extends AppContext to add
- * technical methods for the application. Generation of methods :</p>
- * <p>* doCatch : used to catch all exception from a service method.</p>
- * <p>* doFinally : used to finally the try/catch of a service method</p>
- * <p>* beginTransaction : start the transaction using rootContext.</p>
- * <p>These three methods have to be implemented in a class which implements
- * AppContextImplementor (ex : AppContextImpl). You can also add others
- * methods to AppContextImplementor in the same way as AppContext.</p>
- * </li>
- * <li>AppException : exception class which extends RuntimeException for all
- * technical exceptions which appears in service method. If you want to
- * manage some specific exceptions, you have to managed them in doCatch
- * implementation.
- * </li>
- * </ul>
- * </div>
- * <div>
* Generation from interfaces with stereotype <<service>> :
* <ul>
* <li>Service : interface of the service defined in model.</li>
* <li><p>ServiceAbstract : abstract class which contains :</p>
+ * <p>* treateError : abstract method used to catch all exception from a
+ * service method.</p>
+ * <p>* closeTransaction : abstract method used to finally the try/catch
+ * of a service method</p>
+ * <p>* beginTransaction : abstract method used to start the transaction
+ * using rootContext.</p>
* <p>* constructor with AppContextImplementor in argument</p>
* <p>* for each method : the implementation of the method (skeleton with
* try/catch and beginTransaction call to open a new TopiaContext from
@@ -65,9 +49,10 @@
* </ul>
* </div>
* <div>
- * Exemple of AppContextImpl :<br />
+ * Exemple of ServiceImpl utils method implementation. (The AppException
+ * is considered if defined in model tagvalue "exceptionClass") : <br />
* <pre>
- * public class AppContextImpl implements AppContextImplementor {
+ * public class ServiceImpl implements ServiceAbstract {
*
* // properties for Topia configuration
* protected Properties properties;
@@ -129,19 +114,7 @@
* }
* return null;
* }
- * ...
- * }
- * </pre>
- * </div>
- * <div>
- * Exemple of ServiceImpl :<br />
- * <pre>
- * public class ServiceImpl extends ServiceAbstract {
*
- * public ServiceImpl(AppContextImplementor context) {
- * super(context);
- * }
- *
* // Implementation of abstract method, the interface method is
* // called 'createMyEntity(MyEntity entity)' in this case.
* @Override
@@ -170,6 +143,12 @@
* need arguments for error message. This tagValue can only be put directly
* in the model and not in properties file.</p>
* </div>
+ * <div>
+ * <h2>TAG_EXCEPTION_CLASS</h2>
+ * <p>Default value : null</p>
+ * <p>You can use the tagValue 'exceptionClass=my.exception.full.qualified.Name'
+ * to specify that all contract methods will throw this exception.</p>
+ * </div>
* <p>It is smooth, isn't it :p ?</p>
* <p>TODO : may be refactor to integrate JTA or webservice or may be not in this
* transformer.</p>
@@ -189,149 +168,67 @@
protected String defaultPackageName;
- protected String getContextInterfaceName() {
- return StringUtils.capitalize(modelName) + "Context";
- }
+ protected String exceptionName;
- protected String getContextImplementorInterfaceName() {
- return getContextInterfaceName() + "Implementor";
- }
+ private static final String OP_NAME_BEGIN_TRANSACTION = "beginTransaction";
- protected String getExceptionClassName() {
- return StringUtils.capitalize(modelName) + "Exception";
- }
+ private static final String OP_NAME_CLOSE_TRANSACTION = "closeTransaction";
+ private static final String OP_NAME_TREATE_ERROR = "treateError";
+
+// protected String getContextInterfaceName() {
+// return StringUtils.capitalize(modelName) + "Context";
+// }
+//
+// protected String getContextImplementorInterfaceName() {
+// return getContextInterfaceName() + "Implementor";
+// }
+//
+// protected String getExceptionClassName() {
+// return StringUtils.capitalize(modelName) + "Exception";
+// }
+
protected String getServiceAbstractClassName(String serviceName) {
return serviceName + "Abstract";
}
@Override
public void transformFromModel(ObjectModel model) {
+ exceptionName =
+ model.getTagValue(TopiaGeneratorUtil.TAG_EXCEPTION_CLASS);
modelName = model.getName();
- defaultPackageName = getOutputProperties().
- getProperty(Template.PROP_DEFAULT_PACKAGE);
+ }
- ObjectModelInterface contextImplementor =
- model.getInterface(defaultPackageName + "." +
- getContextImplementorInterfaceName());
-
- ObjectModelInterface context =
- model.getInterface(defaultPackageName + "." +
- getContextInterfaceName());
-
- ObjectModelClass exception = createExceptionClass();
-
- ObjectModelInterface newContextImplementor =
- createInterface(getContextImplementorInterfaceName(),
- defaultPackageName);
- ObjectModelInterface newContext =
- createInterface(getContextInterfaceName(),
- defaultPackageName);
-
- addInterface(newContextImplementor,
- newContext.getQualifiedName());
-
- if (contextImplementor != null) {
- // Copy of defined operations
- // interfaces of contextImplementor are not copied
- copyInterfaceOperations(
- contextImplementor, newContextImplementor, false);
- }
-
- if (context != null) {
- // Copy of defined operations
- // interfaces of context are not copied
- copyInterfaceOperations(context, newContext, false);
+ @Override
+ public void transformFromInterface(ObjectModelInterface interfacez) {
+ if (!interfacez.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_SERVICE)) {
+ return;
}
- ObjectModelOperation beginTransaction =
- addOperation(newContextImplementor,
- "beginTransaction", TopiaContext.class);
- addException(beginTransaction, TopiaException.class);
+ ObjectModelInterface serviceContract =
+ createServiceContract(interfacez);
- ObjectModelOperation doCatch1 =
- addOperation(newContextImplementor, "treateError", "void");
- addParameter(doCatch1, Exception.class, "eee");
- addParameter(doCatch1, String.class, "message");
- addParameter(doCatch1, "Object...", "args");
- addException(doCatch1, exception.getQualifiedName());
-
- ObjectModelOperation doCatch2 =
- addOperation(newContextImplementor, "treateError", "void");
- addParameter(doCatch2, TopiaContext.class, "transaction");
- addParameter(doCatch2, Exception.class, "eee");
- addParameter(doCatch2, String.class, "message");
- addParameter(doCatch2, "Object...", "args");
- addException(doCatch2, exception.getQualifiedName());
-
- ObjectModelOperation doFinally =
- addOperation(newContextImplementor, "closeTransaction", "void");
- addParameter(doFinally, TopiaContext.class, "transaction");
+ createServiceAbstract(interfacez, serviceContract);
}
- protected ObjectModelClass createExceptionClass() {
-
- ObjectModelClass exception =
- createClass(getExceptionClassName(), defaultPackageName);
-
- setSuperClass(exception, RuntimeException.class);
- addAttribute(exception, "args", "Object[]", null,
- ObjectModelModifier.PROTECTED);
-
- ObjectModelOperation constructor =
- addConstructor(exception, ObjectModelModifier.PUBLIC);
-
- addParameter(constructor, Throwable.class, "eee");
- addParameter(constructor, String.class, "message");
- addParameter(constructor, "Object...", "args");
-
- setOperationBody(constructor, ""
- /*{
- super(message, eee);
- this.args = args;
- }*/
- );
-
- ObjectModelOperation getArgs =
- addOperation(exception, "getArgs", "Object[]",
- ObjectModelModifier.PUBLIC);
-
- setOperationBody(getArgs, ""
- /*{
- return args;
- }*/
- );
-
- ObjectModelOperation hasArgs =
- addOperation(exception, "hasArgs", "boolean",
- ObjectModelModifier.PUBLIC);
-
- setOperationBody(hasArgs, ""
- /*{
- return args.length > 0;
- }*/
- );
-
- return exception;
- }
-
/**
- * Used to simply copy the {@code source} interface signature to the
- * {@code dest} interface.
+ * Create the service contract using {@code source} interface defined
+ * in model.
*
- * @param source interface
- * @param dest interface
- * @param throwException if generated exception is thrown
+ * @param source interface from model
+ * @return the ObjectModelInterface created
*/
- protected void copyInterfaceOperations(ObjectModelInterface source,
- ObjectModelInterface dest, boolean throwException) {
- setDocumentation(dest, source.getDocumentation());
- if (throwException) {
- addImport(dest, defaultPackageName + "." + getExceptionClassName());
- }
+ protected ObjectModelInterface createServiceContract(
+ ObjectModelInterface source) {
+
+ ObjectModelInterface serviceContract =
+ createInterface(source.getName(),
+ source.getPackageName());
+
+ setDocumentation(serviceContract, source.getDocumentation());
for (ObjectModelOperation op : source.getOperations()) {
ObjectModelOperation newOp =
- addOperation(dest,
+ addOperation(serviceContract,
op.getName(), op.getReturnType());
setDocumentation(newOp.getReturnParameter(),
op.getReturnParameter().getDocumentation());
@@ -344,36 +241,33 @@
for (String ex : op.getExceptions()) {
addException(newOp, ex);
}
- if (throwException) {
- addException(newOp, getExceptionClassName());
+ if (exceptionName != null) {
+ addException(newOp, exceptionName);
}
setDocumentation(newOp, op.getDocumentation());
}
+ return serviceContract;
}
- @Override
- public void transformFromInterface(ObjectModelInterface interfacez) {
- // skip ContextImplementor and Context interfaces
- if (!interfacez.hasStereotype(TopiaGeneratorUtil.STEREOTYPE_SERVICE)) {
- return;
- }
+ /**
+ * Create the service abstract for {@code serviceContract}
+ * using {@code source} interface defined
+ * in model.
+ *
+ * @param source interface from model
+ * @param serviceContract to implement
+ */
+ protected void createServiceAbstract(ObjectModelInterface source,
+ ObjectModelInterface serviceContract) {
- // Create INTERFACE
- ObjectModelInterface serviceInterface =
- createInterface(interfacez.getName(),
- interfacez.getPackageName());
+ ObjectModelClass serviceAbstract = createAbstractClass(
+ getServiceAbstractClassName(serviceContract.getName()),
+ serviceContract.getPackageName());
- copyInterfaceOperations(interfacez, serviceInterface, true);
+ // Implements contract interface
+ addInterface(serviceAbstract, serviceContract.getQualifiedName());
-
- // Create ABSTRACT CLASS
- ObjectModelClass service = createAbstractClass(
- getServiceAbstractClassName(interfacez.getName()),
- interfacez.getPackageName());
-
- addInterface(service, serviceInterface.getQualifiedName());
-
- // Add Logger
+ // Add Logger attribute
// FIXME in EUGene, we want the default value not to be managed
// for import.
// addAttribute(service, "log",
@@ -382,243 +276,352 @@
// ObjectModelModifier.PRIVATE,
// ObjectModelModifier.STATIC,
// ObjectModelModifier.FINAL);
- addAttribute(service, "log",
+ addAttribute(serviceAbstract, "log",
Log.class, null,
ObjectModelModifier.PRIVATE,
ObjectModelModifier.FINAL);
- addImport(service, Log.class);
- addImport(service, LogFactory.class);
+ addImport(serviceAbstract, Log.class);
+ addImport(serviceAbstract, LogFactory.class);
- String contextFqn = defaultPackageName + "." +
- getContextImplementorInterfaceName();
-
- // Add Context Attribute + constructor
- addAttribute(service, "context", contextFqn, null,
- ObjectModelModifier.PROTECTED);
-
// Constructor
ObjectModelOperation constructor =
- addConstructor(service, ObjectModelModifier.PUBLIC);
+ addConstructor(serviceAbstract, ObjectModelModifier.PUBLIC);
setOperationBody(constructor, ""
/*{
// FIXME : must be fixed attribute value in EUGene
- this.log = LogFactory.getLog(<%=interfacez.getName()%>.class);
+ this.log = LogFactory.getLog(<%=serviceAbstract.getName()%>.class);
}*/
);
- ObjectModelOperation setContext =
- addOperation(service, "setContext", "void",
- ObjectModelModifier.PUBLIC);
- addParameter(setContext, contextFqn, "context");
- setOperationBody(setContext, ""
- /*{
- this.context = context;
- }*/
- );
+ // Create abstract methods
+ ObjectModelOperation beginTransaction =
+ addOperation(serviceAbstract, OP_NAME_BEGIN_TRANSACTION,
+ TopiaContext.class,
+ ObjectModelModifier.ABSTRACT,
+ ObjectModelModifier.PROTECTED);
+ addException(beginTransaction, TopiaException.class);
- // Prepare operation generations
- String first = modelName.substring(0, 1);
- String serviceName =
- GeneratorUtil.toLowerCaseFirstLetter(interfacez.getName());
+ ObjectModelOperation treateError1 =
+ addOperation(serviceAbstract, OP_NAME_TREATE_ERROR, "void",
+ ObjectModelModifier.ABSTRACT,
+ ObjectModelModifier.PROTECTED);
+ addParameter(treateError1, Exception.class, "eee");
+ addParameter(treateError1, String.class, "message");
+ addParameter(treateError1, "Object...", "args");
+ if (exceptionName != null) {
+ addException(treateError1, exceptionName);
+ }
- addImport(service, TopiaContext.class);
- addImport(service, I18n.class);
- addImport(service, ArrayList.class);
- addImport(service, DurationFormatUtils.class);
+ ObjectModelOperation treateError2 =
+ addOperation(serviceAbstract, OP_NAME_TREATE_ERROR, "void",
+ ObjectModelModifier.ABSTRACT,
+ ObjectModelModifier.PROTECTED);
+ addParameter(treateError2, TopiaContext.class, "transaction");
+ addParameter(treateError2, Exception.class, "eee");
+ addParameter(treateError2, String.class, "message");
+ addParameter(treateError2, "Object...", "args");
+ if (exceptionName != null) {
+ addException(treateError2, exceptionName);
+ }
- for (ObjectModelOperation op : interfacez.getOperations()) {
+ ObjectModelOperation closeTransaction =
+ addOperation(serviceAbstract, OP_NAME_CLOSE_TRANSACTION, "void",
+ ObjectModelModifier.ABSTRACT,
+ ObjectModelModifier.PROTECTED);
+ addParameter(closeTransaction, TopiaContext.class, "transaction");
- // boolean to specify if the method need a transaction or not
- // Default set to true but can be override by a tagvalue on the
- // method
- boolean needTransaction = true;
+ // Create abstract execute methods
+ Map<String, ObjectModelOperation> abstOps =
+ new HashMap<String, ObjectModelOperation>();
- String transactionTag =
- op.getTagValue(TopiaGeneratorUtil.TAG_TRANSACTION);
+ // Use source interfacez to have tagvalues from model
+ for (ObjectModelOperation op : source.getOperations()) {
+ ObjectModelOperation abstOp =
+ createOperationExecuteAbstract(serviceAbstract, op);
+ // Keep abstract methods to use them in operationImplementation
+ // generation
+ abstOps.put(op.getName(), abstOp);
+ }
- if (transactionTag != null) {
- needTransaction = Boolean.parseBoolean(transactionTag);
- }
+ // Imports for implementations
+ addImport(serviceAbstract, TopiaContext.class);
+ addImport(serviceAbstract, I18n.class);
+ addImport(serviceAbstract, ArrayList.class);
+ addImport(serviceAbstract, DurationFormatUtils.class);
- // boolean to specify if the method need error arguments or not
- // Default set to true but can be override by a tagvalue on the
- // method
- boolean needErrorArgs = false;
+ // Create abstract execute methods
+ for (ObjectModelOperation op : source.getOperations()) {
+ createOperationImplementation(serviceAbstract,
+ abstOps.get(op.getName()), op, source.getName());
+ }
+ }
- String errorArgsTag =
- op.getTagValue(TopiaGeneratorUtil.TAG_ERROR_ARGS);
+ /**
+ * Create an operation abstract to execute in contract implementation.
+ * You can use tagvalues "errorArgs" (default = false) and "transaction"
+ * (default = true) to generate appropriate parameters. This abstract
+ * method will throw all exceptions (Exception.class). This is the method
+ * which will be implemented by the developper in service implementation
+ * class.
+ *
+ * @param serviceAbstract where the operation will be created
+ * @param source ObjectModelOperation from model
+ * @return the abstract operation created
+ * @see #isErrorArgsNeeded(org.nuiton.eugene.models.object.ObjectModelOperation)
+ * @see #isTransactionNeeded(org.nuiton.eugene.models.object.ObjectModelOperation)
+ */
+ protected ObjectModelOperation createOperationExecuteAbstract(
+ ObjectModelClass serviceAbstract, ObjectModelOperation source) {
+ String opName = StringUtils.capitalize(source.getName());
- if (errorArgsTag != null) {
- needErrorArgs = Boolean.parseBoolean(errorArgsTag);
- }
+ // Abstract operation to execute method content
+ ObjectModelOperation abstOp =
+ addOperation(serviceAbstract, "execute" + opName,
+ source.getReturnType(),
+ ObjectModelModifier.ABSTRACT,
+ ObjectModelModifier.PROTECTED);
- // Implementation of interface operation
- ObjectModelOperation implOp =
- addOperation(service,
- op.getName(), op.getReturnType(),
- ObjectModelModifier.PUBLIC);
- addAnnotation(service, implOp, Override.class.getSimpleName());
-
- String opName = StringUtils.capitalize(op.getName());
+ // Throw all exception from abstract method
+ // They will be catched by interface method to use treateError
+ addException(abstOp, Exception.class);
- // Abstract operation to execute method content
- ObjectModelOperation abstOp =
- addOperation(service, "execute" + opName,
- op.getReturnType(),
- ObjectModelModifier.ABSTRACT,
- ObjectModelModifier.PROTECTED);
+ if (isTransactionNeeded(source)) {
+ addParameter(abstOp, TopiaContext.class, "transaction");
+ }
- // Throw all exception from abstract method
- // They will be catched by interface method to use doCatch
- addException(abstOp, Exception.class);
+ if (isErrorArgsNeeded(source)) {
+ // Add errorArgs to abstract operation
+ addParameter(abstOp,
+ "java.util.List<Object>", "errorArgs");
+ }
- if (needTransaction) {
- addParameter(abstOp, TopiaContext.class, "transaction");
- //addException(abstOp, TopiaException.class);
- }
+ // Copy other operation parameters
+ for (ObjectModelParameter param : source.getParameters()) {
+ addParameter(abstOp, param.getType(), param.getName());
+ }
+ return abstOp;
+ }
- String toStringAppend = "";
- String separatorLog = " : ";
- // Prepare operation parameters
- String opParams = "";
- String separatorParams = "";
- if (needErrorArgs) {
- opParams += "errorArgs";
- separatorParams = ", ";
- // Add errorArgs to abstract operation
- addParameter(abstOp,
- "java.util.List<Object>", "errorArgs");
- }
+ /**
+ * Create an operation implementation. This is the skeleton of the operation
+ * defined from model. This will put a try/catch block over an abstract
+ * method {@code abstOp}. You can use tagvalues "errorArgs" and
+ * "transaction" for abstract method parameters to call. If the transaction
+ * is needed, this will use the beginTransaction() and closeTransaction()
+ * methods defined in {@code serviceAbstract} class.
+ *
+ * @param serviceAbstract where the operation will be created
+ * @param abstOp to execute into the implementation body
+ * @param source ObjectModelOperation from model
+ * @param serviceContractName where the signature method is defined
+ * @see #isErrorArgsNeeded(org.nuiton.eugene.models.object.ObjectModelOperation)
+ * @see #isTransactionNeeded(org.nuiton.eugene.models.object.ObjectModelOperation)
+ */
+ protected void createOperationImplementation(
+ ObjectModelClass serviceAbstract,
+ ObjectModelOperation abstOp, ObjectModelOperation source,
+ String serviceContractName) {
- // Copy other operation parameters
- for (ObjectModelParameter param : op.getParameters()) {
- String paramName = param.getName();
- addParameter(implOp, param.getType(), param.getName());
- addParameter(abstOp, param.getType(), param.getName());
+ // boolean to specify if the method need a transaction or not
+ // Default set to true but can be override by a tagvalue on the
+ // method
+ boolean needTransaction = isTransactionNeeded(source);
- // Prepare Log
- toStringAppend +=
- "\n\t\t\t.append(\"" + separatorLog + paramName + " = \")" +
- ".append(" + paramName + ")";
- separatorLog = " _ ";
+ // boolean to specify if the method need error arguments or not
+ // Default set to true but can be override by a tagvalue on the
+ // method
+ boolean needErrorArgs = isErrorArgsNeeded(source);
- // Prepare Abstract method params
- opParams += separatorParams + param.getName();
- separatorParams = ", ";
- }
+ // Implementation of interface operation
+ ObjectModelOperation implOp =
+ addOperation(serviceAbstract, source.getName(),
+ source.getReturnType(),
+ ObjectModelModifier.PUBLIC);
+ addAnnotation(serviceAbstract, implOp, Override.class.getSimpleName());
- // Use buffer for operation body
- StringBuilder buffer = new StringBuilder();
-
- // Error key for i18n
- String errorKey = StringUtils.lowerCase(modelName) + ".error." +
- serviceName + "." + op.getName();
+ String toStringAppend = "";
+ String separatorLog = " : ";
+ // Copy operation parameters
+ for (ObjectModelParameter param : source.getParameters()) {
+ String paramName = param.getName();
+ addParameter(implOp, param.getType(), paramName);
- String treateErrorParams = "eee, I18n.n_(\"" + errorKey + "\")";
- treateErrorParams += needErrorArgs ? ", errorArgs.toArray()" : "";
+ // Prepare Log
+ toStringAppend +=
+ "\n\t\t\t.append(\"" + separatorLog + paramName + " = \")" +
+ ".append(" + paramName + ")";
+ separatorLog = " _ ";
+ }
- // Return managment
- String opReturnType = "";
- String opReturn = "";
- String finalReturn = "";
- if (!op.getReturnType().equals("void")) {
- opReturnType = GeneratorUtil.getSimpleName(op.getReturnType()) +
- " result = ";
- opReturn = "return result;";
- finalReturn = "return " +
- getReturnValue(op.getReturnType()) + ";";
- }
+ // Use buffer for operation body
+ StringBuilder buffer = new StringBuilder();
- if (needErrorArgs) {
- // Init errorArgs
- buffer.append(""
+ // Abstract operation parameters
+ String abstName = abstOp.getName();
+ String abstParams = "";
+ String separator = "";
+ for (ObjectModelParameter param : abstOp.getParameters()) {
+ abstParams += separator + param.getName();
+ separator = ", ";
+ }
+
+ // Abstract operation return managment
+ String abstReturnType = "";
+ String abstReturn = "";
+ String finalReturn = "";
+ if (!abstOp.getReturnType().equals("void")) {
+ abstReturnType = GeneratorUtil.getSimpleName(abstOp.getReturnType())
+ + " result = ";
+ abstReturn = "return result;";
+ finalReturn = "return " +
+ getReturnValue(abstOp.getReturnType()) + ";";
+ }
+
+ // Error key for i18n
+ String contract =
+ GeneratorUtil.toLowerCaseFirstLetter(serviceContractName);
+ String errorKey = StringUtils.lowerCase(modelName) + ".error." +
+ contract + "." + source.getName();
+
+ String treateErrorParams = "eee, I18n.n_(\"" + errorKey + "\")";
+
+ if (needErrorArgs) {
+ // Init errorArgs
+ buffer.append(""
/*{
List<Object> errorArgs = new ArrayList<Object>();
}*/ );
- }
+ treateErrorParams += ", errorArgs.toArray()";
+ }
- if (needTransaction) {
- // Open the transaction
- buffer.append(""
+ if (needTransaction) {
+ // Open the transaction
+ buffer.append(""
/*{
TopiaContext transaction = null;
try {
- transaction = context.beginTransaction();
+ transaction = beginTransaction();
}*/
- );
- // Add transaction in the execute operation parameters
- // and doCatch parameters
- opParams = "transaction" + separatorParams + opParams;
- treateErrorParams = "transaction, " + treateErrorParams;
- } else {
- buffer.append(""
+ );
+ // Add transaction in treateError parameters
+ treateErrorParams = "transaction, " + treateErrorParams;
+ } else {
+ buffer.append(""
/*{
try {
}*/
- );
- }
+ );
+ }
- buffer.append(""
+ String implName = StringUtils.capitalize(implOp.getName());
+ String first = modelName.substring(0, 1);
+
+ buffer.append(""
/*{
long startTime = 0;
if (log.isDebugEnabled()) {
- log.debug("<%=first%>:[ begin <%=opName%> ]");
+ log.debug("<%=first%>:[ begin <%=implName%> ]");
startTime = System.currentTimeMillis();
}
if (log.isTraceEnabled()) {
String message = new StringBuilder("# ARGS >")<%=toStringAppend%>.
toString();
log.trace(message);
- }
+ }
- <%=opReturnType%>execute<%=opName%>(<%=opParams%>);
+ <%=abstReturnType%><%=abstName%>(<%=abstParams%>);
if (log.isDebugEnabled()) {
long stopTime = System.currentTimeMillis();
- log.debug("<%=first%>:[ end <%=opName%> ] Time = " +
+ log.debug("<%=first%>:[ end <%=implName%> ] Time = " +
DurationFormatUtils.formatDurationHMS(
stopTime - startTime));
}
- <%=opReturn%> }*/);
+ <%=abstReturn%> }*/);
- // Copy exceptions
- for (String ex : op.getExceptions()) {
- addException(implOp, ex);
- //addException(abstOp, ex);
- // Add catch block for known exceptions we want to throw
- String exName = GeneratorUtil.getSimpleName(ex);
- buffer.append(""
+ // Copy exceptions
+ for (String ex : source.getExceptions()) {
+ addException(implOp, ex);
+ // Add catch block for known exceptions we want to throw
+ String exName = GeneratorUtil.getSimpleName(ex);
+ buffer.append(""
/*{
} catch (<%=exName%> eee) {
throw eee; }*/);
- }
+ }
- buffer.append(""
+ buffer.append(""
/*{
} catch (Exception eee) {
- context.treateError(<%=treateErrorParams%>); }*/);
+ treateError(<%=treateErrorParams%>); }*/);
- if (needTransaction) {
- // Finally block to close transaction
- buffer.append(""
+ if (needTransaction) {
+ // Finally block to close transaction
+ buffer.append(""
/*{
} finally {
- context.closeTransaction(transaction); }*/);
- }
+ closeTransaction(transaction); }*/);
+ }
- buffer.append(""
+ buffer.append(""
/*{
}
- <%=finalReturn%>
+ <%=finalReturn%>
}*/
- );
+ );
- setOperationBody(implOp, buffer.toString());
+ setOperationBody(implOp, buffer.toString());
+ }
+
+ /**
+ * boolean to specify if the method need a transaction or not.
+ * Default set to true but can be override using a tagvalue "transaction"
+ * on the method from model.
+ *
+ * @param op where the tagvalue is set
+ * @return true if transaction is needed
+ */
+ protected boolean isTransactionNeeded(ObjectModelOperation op) {
+ boolean needTransaction = true;
+
+ String transactionTag =
+ op.getTagValue(TopiaGeneratorUtil.TAG_TRANSACTION);
+
+ if (transactionTag != null) {
+ needTransaction = Boolean.parseBoolean(transactionTag);
}
-
+ return needTransaction;
}
+ /**
+ * boolean to specify if the method need error arguments or not
+ * Default set to false but can be override using a tagvalue "errorArgs" on
+ * the method from model.
+ *
+ * @param op where the tagvalue is set
+ * @return true if errorArgs are needed
+ */
+ protected boolean isErrorArgsNeeded(ObjectModelOperation op) {
+ //
+ boolean needErrorArgs = false;
+ String errorArgsTag =
+ op.getTagValue(TopiaGeneratorUtil.TAG_ERROR_ARGS);
+
+ if (errorArgsTag != null) {
+ needErrorArgs = Boolean.parseBoolean(errorArgsTag);
+ }
+ return needErrorArgs;
+ }
+
+ /**
+ * This method give the return string for an operation {@code returnType}.
+ * This use {@link Primitive} enum to provide default values for primitive
+ * type. For all other object type, this method will return null.
+ *
+ * @param returnType
+ * @return the defaultValue of the returnType
+ */
protected String getReturnValue(String returnType) {
try {
Primitive prim =
@@ -630,7 +633,7 @@
}
}
- public enum Primitive {
+ protected enum Primitive {
BYTE("0"),
SHORT("0"),
INT("0"),
Modified: trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/TopiaGeneratorUtil.java
===================================================================
--- trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/TopiaGeneratorUtil.java 2010-04-02 09:19:54 UTC (rev 1870)
+++ trunk/topia-persistence/src/main/java/org/nuiton/topia/generator/TopiaGeneratorUtil.java 2010-04-02 15:44:28 UTC (rev 1871)
@@ -148,6 +148,12 @@
*/
public static final String TAG_ERROR_ARGS = "errorArgs";
/**
+ * Tag pour specifier l'exception principale de l'application.
+ * Utiliser dans le ServiceTransformer pour etre automatiquement jeter
+ * depuis les methodes des services.
+ */
+ public static final String TAG_EXCEPTION_CLASS = "exceptionClass";
+ /**
* Tag pour permettre de choisir qui contrôle la relation N-N
* bidirectionnelle. A utiliser sur les deux extremités de l'association.
* Mettre inverse=false sur le rôle fils et inverse=true sur le rôle père.