Class ServiceTransformer

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

@Component(role=org.nuiton.eugene.Template.class,
           hint="org.nuiton.topia.generator.ServiceTransformer")
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>> :
  • Service : interface of the service defined in model.
  • ServiceAbstract : abstract class which contains :

    * treateError : abstract method used to catch all exception from a service method.

    * closeTransaction : abstract method used to finally the try/catch of a service method

    * beginTransaction : abstract method used to start the transaction using rootContext.

    * constructor with AppContextImplementor in argument

    * for each method : the implementation of the method (skeleton with try/catch and beginTransaction call to open a new TopiaContext from AppContextImplementor). Usage of i18n keys for error messages in exception.

    * for each method : an abstract method used to execute the business code of the method : need to be implemented in subclass.

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 ?

Created: 23 mars 2010

Since:
2.3.1
Version:
$Id$
Author:
fdesbois <fdesbois@codelutin.com>
  • Nested Class Summary

    Nested Classes 
    Modifier and Type Class Description
    protected static class  ServiceTransformer.Primitive  
  • Field Summary

    Fields 
    Modifier and Type Field Description
    protected java.lang.String defaultPackageName  
    protected java.lang.String exceptionName  
    protected java.lang.String modelName  
    static java.lang.String PARAMETER_TRANSACTION  

    Fields inherited from class org.nuiton.eugene.java.ObjectModelTransformerToJava

    builder, eugeneTagValues

    Fields inherited from class org.nuiton.eugene.Transformer

    outputModel, outputProperties, outputTemplate, previousTransformer

    Fields inherited from class org.nuiton.eugene.Template

    configuration, excludeTemplates, generatedPackages, model, resourcesHelper

    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

    Constructors 
    Constructor Description
    ServiceTransformer()  
  • Method Summary

    Modifier and Type Method Description
    protected void createBeginTransactionMethod​(org.nuiton.eugene.models.object.ObjectModelInterface source, org.nuiton.eugene.models.object.ObjectModelInterface serviceContract, org.nuiton.eugene.models.object.ObjectModelClass serviceAbstract)  
    protected void createCloseTransactionMethod​(org.nuiton.eugene.models.object.ObjectModelInterface source, org.nuiton.eugene.models.object.ObjectModelInterface serviceContract, org.nuiton.eugene.models.object.ObjectModelClass serviceAbstract)  
    protected void createCommitTransactionMethod​(org.nuiton.eugene.models.object.ObjectModelClass serviceAbstract)  
    protected org.nuiton.eugene.models.object.ObjectModelOperation createOperationExecuteAbstract​(org.nuiton.eugene.models.object.ObjectModelClass serviceAbstract, org.nuiton.eugene.models.object.ObjectModelOperation source, boolean needTransaction)
    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, boolean needTransaction)
    Create an operation implementation.
    protected void createServiceAbstract​(org.nuiton.eugene.models.object.ObjectModelInterface source, org.nuiton.eugene.models.object.ObjectModelInterface serviceContract, boolean needTransaction)
    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 void createTreateErrorMethod​(org.nuiton.eugene.models.object.ObjectModelInterface source, org.nuiton.eugene.models.object.ObjectModelInterface serviceContract, org.nuiton.eugene.models.object.ObjectModelClass serviceAbstract, boolean needTransaction)  
    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 isCommit​(org.nuiton.eugene.models.object.ObjectModelOperation op, org.nuiton.eugene.models.object.ObjectModel model)
    boolean to specify if method needs a commit after the executeXXX code invoked.
    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.ObjectModelInterface op)
    boolean to specify if the method need a transaction or not.
    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 input)  
    void transformFromModel​(org.nuiton.eugene.models.object.ObjectModel model)  

    Methods inherited from class org.nuiton.eugene.java.ObjectModelTransformerToJava

    addAnnotation, addAnnotation, addAnnotationParameter, addAttribute, addAttribute, addAttribute, addAttribute, addBlock, addComment, addConstant, addConstant, addConstantsFromDependency, addConstructor, addConstructor, addException, addException, addGeneratedAnnotation, addI18n, addImport, addImport, addImport, addInnerClassifier, addInterface, addInterface, addLiteral, addOperation, addOperation, addOperation, addParameter, addParameter, addStereotype, addTagValue, addTagValue, cloneAttribute, cloneClass, cloneClassifier, cloneClassifier, cloneEnumeration, cloneInterface, cloneOperation, cloneOperationSignature, cloneStereotypes, cloneTagValues, copyClassifier, createAbstractClass, createClass, createEnumeration, createInterface, debugOutputModel, generateI18nBlock, getConstantName, getConstantPrefix, getConstantPrefix, getFileInClassPath, getJavaBeanMethodName, getJavaBeanMethodName, getPackage, getPackage, initConstantPrefixFromModel, initOutputModel, initOutputTemplate, isInClassPath, isInClassPath, isInClassPath, setConstantPrefix, setDocumentation, setMaxMultiplicity, setMinMultiplicity, setNavigable, 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

    addOutputProperty, applyTemplate, getDefaultPackageName, getOutputModel, getOutputProperties, getOutputTemplate, init, initPreviousTransformer, transform

    Methods inherited from class org.nuiton.eugene.Template

    getClassLoader, getConfiguration, getEncoding, getExcludeTemplates, getGeneratedPackages, getLastModifiedSource, getModel, getOverwrite, getProperties, getProperty, getProperty, getResourcesHelper, getWriterReport, isOverwrite, isVerbose, setConfiguration, setProperty

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

  • Method Details

    • 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 input)
      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
    • createBeginTransactionMethod

      protected void createBeginTransactionMethod​(org.nuiton.eugene.models.object.ObjectModelInterface source, org.nuiton.eugene.models.object.ObjectModelInterface serviceContract, org.nuiton.eugene.models.object.ObjectModelClass serviceAbstract)
    • createCommitTransactionMethod

      protected void createCommitTransactionMethod​(org.nuiton.eugene.models.object.ObjectModelClass serviceAbstract)
    • createCloseTransactionMethod

      protected void createCloseTransactionMethod​(org.nuiton.eugene.models.object.ObjectModelInterface source, org.nuiton.eugene.models.object.ObjectModelInterface serviceContract, org.nuiton.eugene.models.object.ObjectModelClass serviceAbstract)
    • createTreateErrorMethod

      protected void createTreateErrorMethod​(org.nuiton.eugene.models.object.ObjectModelInterface source, org.nuiton.eugene.models.object.ObjectModelInterface serviceContract, org.nuiton.eugene.models.object.ObjectModelClass serviceAbstract, boolean needTransaction)
    • createServiceAbstract

      protected void createServiceAbstract​(org.nuiton.eugene.models.object.ObjectModelInterface source, org.nuiton.eugene.models.object.ObjectModelInterface serviceContract, boolean needTransaction)
      Create the service abstract for serviceContract using source interface defined in model.
      Parameters:
      source - interface from model
      serviceContract - to implement
      needTransaction - flag to know if service globally use transaction
    • createOperationExecuteAbstract

      protected org.nuiton.eugene.models.object.ObjectModelOperation createOperationExecuteAbstract​(org.nuiton.eugene.models.object.ObjectModelClass serviceAbstract, org.nuiton.eugene.models.object.ObjectModelOperation source, boolean needTransaction)
      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
      needTransaction - flag to know if service globally use transaction
      Returns:
      the abstract operation created
      See Also:
      isErrorArgsNeeded(ObjectModelOperation), isTransactionNeeded(ObjectModelOperation), isTransactionNeeded(ObjectModelInterface)
    • 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, boolean needTransaction)
      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
      needTransaction - flag to know if service globally use transaction
      See Also:
      isErrorArgsNeeded(ObjectModelOperation), isTransactionNeeded(ObjectModelInterface)
    • isTransactionNeeded

      protected boolean isTransactionNeeded​(org.nuiton.eugene.models.object.ObjectModelInterface 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
    • 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
    • isCommit

      protected boolean isCommit​(org.nuiton.eugene.models.object.ObjectModelOperation op, org.nuiton.eugene.models.object.ObjectModel model)
      boolean to specify if method needs a commit after the executeXXX code invoked.
      Parameters:
      op - model element where the tagvalue is set
      model - model where to tagvalue can be also set
      Returns:
      true if a commit must be generated after the executeXXX invocation
      Since:
      2.5
      See Also:
      TopiaTagValues.TAG_DO_COMMIT
    • 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