Code Monkey home page Code Monkey logo

searchable-multi-tenant-bridge's Introduction

Searchable - Multi Tenant Bridge (SMTB)

There are many developers who struggle to overcome the problems that exist when the two essential Grails Plugins Searchable and Multi Tenant (Core) for an application come together. Here is the solution for it.

This Plugin is to overcome the ClassCastException that occurs while the two plugins Searchable [http://grails.org/plugin/searchable] and Multi-Tenant Plugin (Core) [http://grails.org/plugin/multi-tenant-core] co-exist together.

This particular exception occurs due to the dependent plugin of the Multi-Tenant Plugin (Core) called Falcone Util [http://grails.org/plugin/falcone-util]. This is caused due to the modification of the session in com.infusion.util.event.spring.InterceptableSessionFactoryPostProcessor. Due to this modification, the session (SessionFactoryImpl) needed by the Indexing mechanism (Compass) is modified as the session (InterceptableSessionFactory) and hence throws the Exception at the below point of code:

public class DefaultHibernateEntityLifecycleInjector implements HibernateEntityLifecycleInjector {

		public void injectLifecycle(SessionFactory sessionFactory, HibernateGpsDevice device) throws HibernateGpsDeviceException {

    		SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) sessionFactory;
    		.....
 		}
  		.....
}

So what this plugin does is, it performs the indexing required for search by the Searchable Plugin first and then the session modification for Multi Tenant (Core) happens.

Due to this, we need to accomodate the below few drawbacks that happens with Multi Tenant Features:

Refer http://multi-tenant.github.com/grails-multi-tenant-core/guide/4.%20Behind%20the%20scenes.html

Just install this Plugin and do the following modifications specific to Multi-Tenant (Core).

  1. Dynamically injected Named Parameter ":tenantId" will not work =================================================================

Earlier GORM functions were automatically injected with tenantId without explicitly specifying it. Now this will not happen.

All GORM functions backed by a Criteria object (createCriteria, countBy, exists, find, get, list, listOrderBy, withCriteria) will have to be passed with the tenantId explicitly.

Workaround:

Instead of ‘:tenantId’ in queries, use ‘TenantUtils.getCurrentTenant()’.

E.g.:

Before SMTB:

Domain.executeQuery("select d from Domain as d where d.tenantId = :tenantId")

After SMTB:

Domain.executeQuery("select d from Domain as d where d.tenantId = " + TenantUtils.getCurrentTenant())

The same holds good for executeUpdate() too.

  1. TenantId has to be artificially injected in createCriteria and withCriteria ==============================================================================

Before SMTB:

def listDomain(params) { 

        def d = Domain.createCriteria()

        def results = d.list(max: params.max, offset: params.offset)  {
	        
            order(params.sort, params.order)
        }
    }

After SMTB:

def listDomain(params) { 

        def d = Domain.createCriteria()

        def results = d.list(max: params.max, offset: params.offset)  {

            eq('tenantId',TenantUtils.getCurrentTenant())

	        order(params.sort, params.order)
        }
    }
  1. All GORM function has to be artificially injected with TenantId with the help of NamedQueries ================================================================================================

For all the Domain classes which are annotated as "@MultiTenant" add namedQuery "domain":

static namedQueries = { 
  domain {
	 eq 'tenantId', TenantUtils.getCurrentTenant()
  }
}

The name ‘domain’ is the name of the Named Query and can be named as anything.

Now the NamedQuery "domain" is added to each of the GORM function to retrieve the right records for the Tenant.

Before SMTB:

Domain.list()
Domain.get(id)
Domain.countByTitleAndAuthor("The Sum of All Fears", "Tom Clancy")
Domain.exists(id)
Domain.find(String query)
Domain.listOrderByAuthor()

After SMTB:

Domain.domain.list()
Domain.domain.get(id)
Domain.domain.countByTitleAndAuthor("The Sum of All Fears", "Tom Clancy")
Domain.domain.exists(id)
Domain.domain.find(String query)
Domain.domain.listOrderByAuthor()

searchable-multi-tenant-bridge's People

Contributors

archanaa-karun avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.