Class ServiceTransformer
- All Implemented Interfaces:
org.nuiton.eugene.TemplateConfiguration
- 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.
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 -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Stringprotected Stringprotected Stringstatic final StringFields inherited from class org.nuiton.eugene.java.ObjectModelTransformerToJava
builder, eugeneTagValuesFields inherited from class org.nuiton.eugene.Transformer
outputModel, outputProperties, outputTemplate, previousTransformerFields inherited from class org.nuiton.eugene.Template
configuration, excludeTemplates, generatedPackages, model, resourcesHelperFields 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 -
Method Summary
Modifier and TypeMethodDescriptionprotected voidcreateBeginTransactionMethod(org.nuiton.eugene.models.object.ObjectModelInterface source, org.nuiton.eugene.models.object.ObjectModelInterface serviceContract, org.nuiton.eugene.models.object.ObjectModelClass serviceAbstract) protected voidcreateCloseTransactionMethod(org.nuiton.eugene.models.object.ObjectModelInterface source, org.nuiton.eugene.models.object.ObjectModelInterface serviceContract, org.nuiton.eugene.models.object.ObjectModelClass serviceAbstract) protected voidcreateCommitTransactionMethod(org.nuiton.eugene.models.object.ObjectModelClass serviceAbstract) protected org.nuiton.eugene.models.object.ObjectModelOperationcreateOperationExecuteAbstract(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 voidcreateOperationImplementation(org.nuiton.eugene.models.object.ObjectModelClass serviceAbstract, org.nuiton.eugene.models.object.ObjectModelOperation abstOp, org.nuiton.eugene.models.object.ObjectModelOperation source, String serviceContractName, boolean needTransaction) Create an operation implementation.protected voidcreateServiceAbstract(org.nuiton.eugene.models.object.ObjectModelInterface source, org.nuiton.eugene.models.object.ObjectModelInterface serviceContract, boolean needTransaction) Create the service abstract forserviceContractusingsourceinterface defined in model.protected org.nuiton.eugene.models.object.ObjectModelInterfacecreateServiceContract(org.nuiton.eugene.models.object.ObjectModelInterface source) Create the service contract usingsourceinterface defined in model.protected voidcreateTreateErrorMethod(org.nuiton.eugene.models.object.ObjectModelInterface source, org.nuiton.eugene.models.object.ObjectModelInterface serviceContract, org.nuiton.eugene.models.object.ObjectModelClass serviceAbstract, boolean needTransaction) protected StringgetReturnValue(String returnType) This method give the return string for an operationreturnType.protected StringgetServiceAbstractClassName(String serviceName) protected booleanisCommit(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 booleanisErrorArgsNeeded(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 booleanisTransactionNeeded(org.nuiton.eugene.models.object.ObjectModelInterface op) boolean to specify if the method need a transaction or not.protected booleanisTransactionNeeded(org.nuiton.eugene.models.object.ObjectModelOperation op) boolean to specify if the method need a transaction or not.voidtransformFromInterface(org.nuiton.eugene.models.object.ObjectModelInterface input) voidtransformFromModel(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, setSuperClassMethods inherited from class org.nuiton.eugene.models.object.ObjectModelTransformer
transform, transformFromClass, transformFromClassifier, transformFromElement, transformFromEnumerationMethods inherited from class org.nuiton.eugene.Transformer
addOutputProperty, applyTemplate, getDefaultPackageName, getOutputModel, getOutputProperties, getOutputTemplate, init, initPreviousTransformer, transformMethods inherited from class org.nuiton.eugene.Template
getClassLoader, getConfiguration, getEncoding, getExcludeTemplates, getGeneratedPackages, getLastModifiedSource, getModel, getOverwrite, getProperties, getProperty, getProperty, getResourcesHelper, getWriterReport, isOverwrite, isVerbose, setConfiguration, setProperty
-
Field Details
-
modelName
-
defaultPackageName
-
exceptionName
-
PARAMETER_TRANSACTION
- See Also:
-
-
Constructor Details
-
ServiceTransformer
public ServiceTransformer()
-
-
Method Details
-
getServiceAbstractClassName
-
transformFromModel
public void transformFromModel(org.nuiton.eugene.models.object.ObjectModel model) - Overrides:
transformFromModelin classorg.nuiton.eugene.models.object.ObjectModelTransformer<org.nuiton.eugene.models.object.ObjectModel>
-
transformFromInterface
public void transformFromInterface(org.nuiton.eugene.models.object.ObjectModelInterface input) - Overrides:
transformFromInterfacein classorg.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 usingsourceinterface 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 forserviceContractusingsourceinterface defined in model.- Parameters:
source- interface from modelserviceContract- to implementneedTransaction- 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 createdsource- ObjectModelOperation from modelneedTransaction- flag to know if service globally use transaction- Returns:
- the abstract operation created
- See Also:
-
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, 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 methodabstOp. 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 inserviceAbstractclass.- Parameters:
serviceAbstract- where the operation will be createdabstOp- to execute into the implementation bodysource- ObjectModelOperation from modelserviceContractName- where the signature method is definedneedTransaction- flag to know if service globally use transaction- See Also:
-
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:
trueif 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:
trueif 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 setmodel- model where to tagvalue can be also set- Returns:
trueif a commit must be generated after the executeXXX invocation- Since:
- 2.5
- See Also:
-
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
This method give the return string for an operationreturnType. This useServiceTransformer.Primitiveenum 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
-