@Component(role=org.nuiton.eugene.Template.class,
hint="org.nuiton.topia.generator.ServiceTransformer")
public class ServiceTransformer
extends org.nuiton.eugene.java.ObjectModelTransformerToJava
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. } }
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).
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.
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
Modifier and Type | Class and Description |
---|---|
protected static class |
ServiceTransformer.Primitive |
Modifier and Type | Field and Description |
---|---|
protected String |
defaultPackageName |
protected String |
exceptionName |
protected String |
modelName |
static String |
PARAMETER_TRANSACTION |
builder, eugeneTagValues
outputModel, outputProperties, outputTemplate, previousTransformer
Constructor and Description |
---|
ServiceTransformer() |
Modifier and Type | Method and 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,
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 String |
getReturnValue(String returnType)
This method give the return string for an operation
returnType . |
protected String |
getServiceAbstractClassName(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) |
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
transform, transformFromClass, transformFromClassifier, transformFromElement, transformFromEnumeration
addOutputProperty, applyTemplate, getDefaultPackageName, getOutputModel, getOutputProperties, getOutputTemplate, init, initPreviousTransformer, transform
getClassLoader, getConfiguration, getEncoding, getExcludeTemplates, getGeneratedPackages, getLastModifiedSource, getModel, getOverwrite, getProperties, getProperty, getProperty, getResourcesHelper, getWriterReport, isOverwrite, isVerbose, setConfiguration, setProperty
protected String modelName
protected String defaultPackageName
protected String exceptionName
public static final String PARAMETER_TRANSACTION
public void transformFromModel(org.nuiton.eugene.models.object.ObjectModel model)
transformFromModel
in class org.nuiton.eugene.models.object.ObjectModelTransformer<org.nuiton.eugene.models.object.ObjectModel>
public void transformFromInterface(org.nuiton.eugene.models.object.ObjectModelInterface input)
transformFromInterface
in class org.nuiton.eugene.models.object.ObjectModelTransformer<org.nuiton.eugene.models.object.ObjectModel>
protected org.nuiton.eugene.models.object.ObjectModelInterface createServiceContract(org.nuiton.eugene.models.object.ObjectModelInterface source)
source
interface defined
in model.source
- interface from modelprotected 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 createCommitTransactionMethod(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 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 void createServiceAbstract(org.nuiton.eugene.models.object.ObjectModelInterface source, org.nuiton.eugene.models.object.ObjectModelInterface serviceContract, boolean needTransaction)
serviceContract
using source
interface defined
in model.source
- interface from modelserviceContract
- to implementneedTransaction
- flag to know if service globally use transactionprotected org.nuiton.eugene.models.object.ObjectModelOperation createOperationExecuteAbstract(org.nuiton.eugene.models.object.ObjectModelClass serviceAbstract, org.nuiton.eugene.models.object.ObjectModelOperation source, boolean needTransaction)
serviceAbstract
- where the operation will be createdsource
- ObjectModelOperation from modelneedTransaction
- flag to know if service globally use transactionisErrorArgsNeeded(ObjectModelOperation)
,
isTransactionNeeded(ObjectModelOperation)
,
isTransactionNeeded(ObjectModelInterface)
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)
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.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 transactionisErrorArgsNeeded(ObjectModelOperation)
,
isTransactionNeeded(ObjectModelInterface)
protected boolean isTransactionNeeded(org.nuiton.eugene.models.object.ObjectModelInterface op)
op
- where the tagvalue is settrue
if transaction is neededprotected boolean isTransactionNeeded(org.nuiton.eugene.models.object.ObjectModelOperation op)
op
- where the tagvalue is settrue
if transaction is neededprotected boolean isCommit(org.nuiton.eugene.models.object.ObjectModelOperation op, org.nuiton.eugene.models.object.ObjectModel model)
op
- model element where the tagvalue is setmodel
- model where to tagvalue can be also settrue
if a commit must be generated after the executeXXX invocationTopiaTagValues.TAG_DO_COMMIT
protected boolean isErrorArgsNeeded(org.nuiton.eugene.models.object.ObjectModelOperation op)
op
- where the tagvalue is setprotected String getReturnValue(String returnType)
returnType
.
This use ServiceTransformer.Primitive
enum to provide default values for primitive
type. For all other object type, this method will return null.returnType
- Copyright © 2004–2022 Code Lutin. All rights reserved.