org.nuiton.topia.generator
Class ServiceTransformer

java.lang.Object
  extended by org.nuiton.eugene.Template<I>
      extended by org.nuiton.eugene.Transformer<org.nuiton.eugene.models.object.ObjectModel,O>
          extended by org.nuiton.eugene.models.object.ObjectModelTransformer<org.nuiton.eugene.models.object.ObjectModel>
              extended by org.nuiton.eugene.java.ObjectModelTransformerToJava
                  extended by org.nuiton.topia.generator.ServiceTransformer
All Implemented Interfaces:
org.nuiton.eugene.TemplateConfiguration

public class ServiceTransformer
extends org.nuiton.eugene.java.ObjectModelTransformerToJava

This Template is used to create the skeleton of services for a final application which using Topia.

Generation from interfaces with stereotype <<service>> :
Exemple of ServiceImpl utils method implementation. (The AppException is considered if defined in model tagvalue "exceptionClass") :
   public class ServiceImpl implements ServiceAbstract  {

      // properties for Topia configuration
      protected Properties properties;
      ...
 
      @Override
      public void treateError(TopiaContext transaction, Exception eee,
              String message, Object... args) throws AppException {

          // Note that the message from service doesn't directly use _() for
          // i18 messages but n_(). In this log, the _() is used to translate
          // correctly the message. But the message must be translate when
          // catching the AppException in UI.
          if (log.isErrorEnabled()) {
              log.error(_(message, args), eee);
          }

          // rollback of current transaction
          if (transaction != null) {
              try {
                  transaction.rollbackTransaction();
              } catch (TopiaException ex) {
                  if (log.isErrorEnabled()) {
                      log.error(_("app.error.context.rollback"), ex);
                  }
              }
          }
          // wrapping the exception in a AppException with message and
          // arguments for i18n translation
          throw new AppException(eee, message, args);
      }

      @Override
      public void closeTransaction(TopiaContext transaction) {
          if (transaction != null) {
              try {
                  transaction.closeContext();
              } catch (TopiaException eee) {
                  if (log.isErrorEnabled()) {
                      log.error(_("app.error.context.close"), eee);
                  }
              }
          }
      }

      @Override
      public TopiaContext beginTransaction() throws TopiaException {
          TopiaContext rootContext = null;
          try {
              // You have to manage the properties using ApplicationConfig
              // or other lib to have configuration for Topia
              rootContext = TopiaContextFactory.getContext(properties);

              return getTopiaRootContext().beginTransaction();

          // only catch exception for rootContext
          } catch (TopiaNotFoundException eee) {
              treateError(eee, n_("app.error.context.getTopiaRootContext"));
          }
          return null;
      }

      // Implementation of abstract method, the interface method is
      // called 'createMyEntity(MyEntity entity)' in this case.
      @Override
      public void executeCreateMyEntity(TopiaContext transaction, 
                      MyEntity entity) throws TopiaException {

          MyEntityDAO dao = AppDAOHelper.getMyEntityDAO(transaction);
          dao.create(entity);
          // That's it, no need to manage errors or transaction, the abstract
          // service will do this job.
      }
 }
 

TAG_TRANSACTION

Default value : true

You can use the tagValue 'transaction=false' to specify that a method doesn't need any TopiaContext, so no need to instantiate a new one. This tagValue can only be put directly in the model and not in properties file (because of multiple methods with same name problem).

TAG_ERROR_ARGS

Default value : false

You can use the tagValue 'errorArgs=true' to specify that a method need arguments for error message. This tagValue can only be put directly in the model and not in properties file.

TAG_EXCEPTION_CLASS

Default value : null

You can use the tagValue 'exceptionClass=my.exception.full.qualified.Name' to specify that all contract methods will throw this exception.

It is smooth, isn't it :p ?

TODO : may be refactor to integrate JTA or webservice or may be not in this transformer.

TODO : find a good way to change log level

Created: 23 mars 2010

Since:
2.3.1
Version:
$Id: ServiceTransformer.java 1993 2010-06-03 17:28:18Z fdesbois $
Author:
fdesbois
Plexus component:
role:
"org.nuiton.eugene.Template"
role-hint:
"org.nuiton.topia.generator.ServiceTransformer"

Nested Class Summary
protected static class ServiceTransformer.Primitive
           
 
Field Summary
protected  java.lang.String defaultPackageName
           
protected  java.lang.String exceptionName
           
protected  java.lang.String modelName
           
 
Fields inherited from class org.nuiton.eugene.java.ObjectModelTransformerToJava
builder
 
Fields inherited from class org.nuiton.eugene.Transformer
outputModel, outputProperties, outputTemplate, previousTransformer
 
Fields inherited from class org.nuiton.eugene.Template
configuration, excludeTemplates, generatedPackages, model
 
Fields inherited from interface org.nuiton.eugene.TemplateConfiguration
PROP_CLASS_LOADER, PROP_DEFAULT_PACKAGE, PROP_ENCODING, PROP_EXCLUDE_TEMPLATES, PROP_GENERATED_PACKAGES, PROP_LAST_MODIFIED_SOURCE, PROP_OVERWRITE, PROP_VERBOSE, PROP_WRITER_REPORT
 
Constructor Summary
ServiceTransformer()
           
 
Method Summary
protected  org.nuiton.eugene.models.object.ObjectModelOperation createOperationExecuteAbstract(org.nuiton.eugene.models.object.ObjectModelClass serviceAbstract, org.nuiton.eugene.models.object.ObjectModelOperation source)
          Create an operation abstract to execute in contract implementation.
protected  void createOperationImplementation(org.nuiton.eugene.models.object.ObjectModelClass serviceAbstract, org.nuiton.eugene.models.object.ObjectModelOperation abstOp, org.nuiton.eugene.models.object.ObjectModelOperation source, java.lang.String serviceContractName)
          Create an operation implementation.
protected  void createServiceAbstract(org.nuiton.eugene.models.object.ObjectModelInterface source, org.nuiton.eugene.models.object.ObjectModelInterface serviceContract)
          Create the service abstract for serviceContract using source interface defined in model.
protected  org.nuiton.eugene.models.object.ObjectModelInterface createServiceContract(org.nuiton.eugene.models.object.ObjectModelInterface source)
          Create the service contract using source interface defined in model.
protected  java.lang.String getAbstractOperationKeyMap(org.nuiton.eugene.models.object.ObjectModelOperation op)
          Create a key for the abstractOperations map from op.
protected  java.lang.String getReturnValue(java.lang.String returnType)
          This method give the return string for an operation returnType.
protected  java.lang.String getServiceAbstractClassName(java.lang.String serviceName)
           
protected  boolean isErrorArgsNeeded(org.nuiton.eugene.models.object.ObjectModelOperation op)
          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.
protected  boolean isTransactionNeeded(org.nuiton.eugene.models.object.ObjectModelOperation op)
          boolean to specify if the method need a transaction or not.
 void transformFromInterface(org.nuiton.eugene.models.object.ObjectModelInterface interfacez)
           
 void transformFromModel(org.nuiton.eugene.models.object.ObjectModel model)
           
 
Methods inherited from class org.nuiton.eugene.java.ObjectModelTransformerToJava
addAnnotation, addAttribute, addAttribute, addAttribute, addAttribute, addBlock, addConstant, addConstant, addConstantsFromDependency, addConstructor, addConstructor, addException, addException, addImport, addImport, addImport, addInnerClassifier, addInterface, addInterface, addLiteral, addOperation, addOperation, addParameter, addParameter, cloneOperationSignature, createAbstractClass, createClass, createEnumeration, createInterface, debugOutputModel, getConstantName, getConstantPrefix, getConstantPrefix, getFileInClassPath, initConstantPrefixFromModel, initOutputModel, initOutputTemplate, setConstantPrefix, setDocumentation, setOperationBody, setSuperClass, setSuperClass
 
Methods inherited from class org.nuiton.eugene.models.object.ObjectModelTransformer
transform, transformFromClass, transformFromClassifier, transformFromElement, transformFromEnumeration
 
Methods inherited from class org.nuiton.eugene.Transformer
applyTemplate, getOutputModel, getOutputProperties, getOutputTemplate, init, initPreviousTransformer, setProperties, transform
 
Methods inherited from class org.nuiton.eugene.Template
getClassLoader, getConfiguration, getEncoding, getExcludeTemplates, getGeneratedPackages, getLastModifiedSource, getModel, getOverwrite, getProperties, getProperty, getProperty, getWriterReport, isOverwrite, isVerbose, setConfiguration, setProperty
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

modelName

protected java.lang.String modelName

defaultPackageName

protected java.lang.String defaultPackageName

exceptionName

protected java.lang.String exceptionName
Constructor Detail

ServiceTransformer

public ServiceTransformer()
Method Detail

getServiceAbstractClassName

protected java.lang.String getServiceAbstractClassName(java.lang.String serviceName)

transformFromModel

public void transformFromModel(org.nuiton.eugene.models.object.ObjectModel model)
Overrides:
transformFromModel in class org.nuiton.eugene.models.object.ObjectModelTransformer<org.nuiton.eugene.models.object.ObjectModel>

transformFromInterface

public void transformFromInterface(org.nuiton.eugene.models.object.ObjectModelInterface interfacez)
Overrides:
transformFromInterface in class org.nuiton.eugene.models.object.ObjectModelTransformer<org.nuiton.eugene.models.object.ObjectModel>

createServiceContract

protected org.nuiton.eugene.models.object.ObjectModelInterface createServiceContract(org.nuiton.eugene.models.object.ObjectModelInterface source)
Create the service contract using source interface defined in model.

Parameters:
source - interface from model
Returns:
the ObjectModelInterface created

createServiceAbstract

protected void createServiceAbstract(org.nuiton.eugene.models.object.ObjectModelInterface source,
                                     org.nuiton.eugene.models.object.ObjectModelInterface serviceContract)
Create the service abstract for serviceContract using source interface defined in model.

Parameters:
source - interface from model
serviceContract - to implement

getAbstractOperationKeyMap

protected java.lang.String getAbstractOperationKeyMap(org.nuiton.eugene.models.object.ObjectModelOperation op)
Create a key for the abstractOperations map from op. Concat opName + params as String.

Parameters:
op - used as reference to construct the keyMap
Returns:
the keyMap

createOperationExecuteAbstract

protected org.nuiton.eugene.models.object.ObjectModelOperation createOperationExecuteAbstract(org.nuiton.eugene.models.object.ObjectModelClass serviceAbstract,
                                                                                              org.nuiton.eugene.models.object.ObjectModelOperation source)
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.

Parameters:
serviceAbstract - where the operation will be created
source - ObjectModelOperation from model
Returns:
the abstract operation created
See Also:
isErrorArgsNeeded(ObjectModelOperation), isTransactionNeeded(ObjectModelOperation)

createOperationImplementation

protected void createOperationImplementation(org.nuiton.eugene.models.object.ObjectModelClass serviceAbstract,
                                             org.nuiton.eugene.models.object.ObjectModelOperation abstOp,
                                             org.nuiton.eugene.models.object.ObjectModelOperation source,
                                             java.lang.String serviceContractName)
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 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 serviceAbstract class.

Parameters:
serviceAbstract - where the operation will be created
abstOp - to execute into the implementation body
source - ObjectModelOperation from model
serviceContractName - where the signature method is defined
See Also:
isErrorArgsNeeded(ObjectModelOperation), isTransactionNeeded(ObjectModelOperation)

isTransactionNeeded

protected boolean isTransactionNeeded(org.nuiton.eugene.models.object.ObjectModelOperation op)
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.

Parameters:
op - where the tagvalue is set
Returns:
true if transaction is needed

isErrorArgsNeeded

protected boolean isErrorArgsNeeded(org.nuiton.eugene.models.object.ObjectModelOperation op)
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.

Parameters:
op - where the tagvalue is set
Returns:
true if errorArgs are needed

getReturnValue

protected java.lang.String getReturnValue(java.lang.String returnType)
This method give the return string for an operation returnType. This use ServiceTransformer.Primitive enum to provide default values for primitive type. For all other object type, this method will return null.

Parameters:
returnType -
Returns:
the defaultValue of the returnType


Copyright © 2004-2010 CodeLutin. All Rights Reserved.