10.1 Building SaaS applications
This section explains how to create a SaaS application and the main variations between a SaaS and non-SaaS application.
All the images diagrams and Class objects are taken form the MultiTenancy example which can be found at:
/examples/CloudTran/MultiTenanacy
Generating a SaaS application is achieved simply by setting the 'SaaS' flag to 'true' on the application artefact in the model.
Having set this flag the application will need to be (re)built. Some of the objects in an existing application will change, so whilst it
is feasible to switch between SaaS and non-SaaS modes any business logic may need to be amended.
The generated variations in a SaaS application are shown in the following sections
10.1.1 DataObjects
|
As CloudTran uses a shared schema for the space data, every data object (derived from an entity) in a SaaS application will be generated with an additional
attribute. This attribute is call 'tenantId' is of type String.
public String getTenantId()
{
return tenantId;
}
public void setTenantId( String tenantId )
{
this.tenantId = tenantId;
}
This tenantId is therefore held as a field in the SOR. Access to the underlying data is gained through the ORM.
10.1.2 ORM
|
The ORM - the interface between client-side entity objects and the backend data objects - will be generated slightly differently too. The ORM is
responsible for saving, retrieving, updating and deleting entity objects. It does this by converting the client-side entity objects into Data Objects.
As the Data Objects need the TenantId the ORM is amende to retreive the TenantId from the SecurityContext and adds it to the Data Object. For example the following
code shows the readById for the Employee entity. Before performing the read the tenantId is retrieved from the SecurityUtils and added to the template.
public Employee readById( DistributedTxAttributes distTx, Object pk ) throws TransactionExceptionRetriable
{
if ( pk == null )
{
throw new TransactionExceptionRetriable( "Error calling readById on Employee as pk = null ");
}
EmployeeData template = new EmployeeData( ( Long )pk );
template.setTenantId( SecurityUtils.getTenantId() );
return read( distTx, template, null, ReadModifiers.READ_COMMITTED, true ) ;
}
10.1.3 SecurityUtils
|
The SecurityUtils is generated for both SaaS and non-SaaS applications. Further details can be found in the section on security Security Concepts.
In SaaS applications the TenantId is also held in the SecurityContext and so an additional method 'getTenantId' is generated on the SecurityUtils.
public static String getTenantId()
{
int ix = getPrincipalName().lastIndexOf( ":" );
if( ix < 0 )
return getPrincipalName();
return getPrincipalName().substring( ix+":".length() ); // start copying from after the end of the separator
}
|