Skip to content

Commit

Permalink
Cleanup name clash between methods that have the same erasure, yet ne…
Browse files Browse the repository at this point in the history
…ither hides the other between

grails.gorm.multitenancy.Tenant and grails.gorm.rx.multitenancy.Tenant by removing inheritance
  • Loading branch information
jamesfredley committed Aug 22, 2024
1 parent 57eca6b commit d7068bb
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import org.grails.gorm.rx.api.RxGormEnhancer
*/
@CompileStatic
@Slf4j
class Tenants extends grails.gorm.multitenancy.Tenants {
class Tenants {
/**
* Execute the given closure for each tenant.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Tenants {
* @param callable The closure
* @return The result of the closure
*/
static void eachTenantFromDatasource(Class<? extends Datastore> datastoreClass, Closure callable) {
static void eachTenant(Class<? extends Datastore> datastoreClass, Closure callable) {
eachTenantInternal(GormEnhancer.findDatastoreByType(datastoreClass), callable)
}

Expand Down Expand Up @@ -81,7 +81,7 @@ class Tenants {
*
* @throws org.grails.datastore.mapping.multitenancy.exceptions.TenantNotFoundException if no current tenant is found
*/
static Serializable currentIdFromDatasource(Class<? extends Datastore> datastoreClass) {
static Serializable currentId(Class<? extends Datastore> datastoreClass) {
Datastore datastore = GormEnhancer.findDatastoreByType(datastoreClass)
if(datastore instanceof MultiTenantCapableDatastore) {
MultiTenantCapableDatastore multiTenantCapableDatastore = (MultiTenantCapableDatastore)datastore
Expand Down Expand Up @@ -125,17 +125,17 @@ class Tenants {
* @param callable The closure
* @return The result of the closure
*/
// static <T> T withCurrent(Closure<T> callable) {
// Serializable tenantIdentifier = currentId()
// Datastore datastore = GormEnhancer.findSingleDatastore()
// if(datastore instanceof MultiTenantCapableDatastore) {
// MultiTenantCapableDatastore multiTenantCapableDatastore = (MultiTenantCapableDatastore)datastore
// return withId(multiTenantCapableDatastore, tenantIdentifier, callable)
// }
// else {
// throw new UnsupportedOperationException("Datastore implementation does not support multi-tenancy")
// }
// }
static <T> T withCurrent(Closure<T> callable) {
Serializable tenantIdentifier = currentId()
Datastore datastore = GormEnhancer.findSingleDatastore()
if(datastore instanceof MultiTenantCapableDatastore) {
MultiTenantCapableDatastore multiTenantCapableDatastore = (MultiTenantCapableDatastore)datastore
return withId(multiTenantCapableDatastore, tenantIdentifier, callable)
}
else {
throw new UnsupportedOperationException("Datastore implementation does not support multi-tenancy")
}
}

/**
* Execute the given closure with the current tenant
Expand All @@ -144,45 +144,12 @@ class Tenants {
* @param callable The closure
* @return The result of the closure
*/
// static <T> T withCurrent(Class<? extends Datastore> datastoreClass, Closure<T> callable) {
// Serializable tenantIdentifier = currentIdFromDatasource(datastoreClass)
// Datastore datastore = GormEnhancer.findDatastoreByType(datastoreClass)
// if(datastore instanceof MultiTenantCapableDatastore) {
// MultiTenantCapableDatastore multiTenantCapableDatastore = (MultiTenantCapableDatastore)datastore
// return withId(multiTenantCapableDatastore, tenantIdentifier, callable)
// }
// else {
// throw new UnsupportedOperationException("Datastore implementation does not support multi-tenancy")
// }
// }

/**
* Execute the given closure with given tenant id
* @param tenantId The tenant id
* @param callable The closure
* @return The result of the closure
*/
// static <T> T withId(Serializable tenantId, Closure<T> callable) {
// Datastore datastore = GormEnhancer.findSingleDatastore()
// if(datastore instanceof MultiTenantCapableDatastore) {
// MultiTenantCapableDatastore multiTenantCapableDatastore = (MultiTenantCapableDatastore)datastore
// return withId(multiTenantCapableDatastore, tenantId, callable)
// }
// else {
// throw new UnsupportedOperationException("Datastore implementation does not support multi-tenancy")
// }
// }
/**
* Execute the given closure with given tenant id
* @param tenantId The tenant id
* @param callable The closure
* @return The result of the closure
*/
static <T> T withIdFromDatastore(Class<? extends Datastore> datastoreClass, Serializable tenantId, Closure callable) {
static <T> T withCurrent(Class<? extends Datastore> datastoreClass, Closure<T> callable) {
Serializable tenantIdentifier = currentId(datastoreClass)
Datastore datastore = GormEnhancer.findDatastoreByType(datastoreClass)
if(datastore instanceof MultiTenantCapableDatastore) {
MultiTenantCapableDatastore multiTenantCapableDatastore = (MultiTenantCapableDatastore)datastore
return withId(multiTenantCapableDatastore, tenantId, callable)
return withId(multiTenantCapableDatastore, tenantIdentifier, callable)
}
else {
throw new UnsupportedOperationException("Datastore implementation does not support multi-tenancy")
Expand All @@ -194,11 +161,9 @@ class Tenants {
* @param tenantId The tenant id
* @param callable The closure
* @return The result of the closure
*
* method duplicated to address name clash: <T#1>withId(Class<? extends RxDatastoreClient>,Serializable,Closure<T#1>) in grails.gorm.rx.multitenancy.Tenants and <T#2>withId(Class<? extends Datastore>,Serializable,Closure<T#2>) in grails.gorm.multitenancy.Tenants have the same erasure, yet neither hides the other
*/
static <T> T withIdSharedConnection(Class<? extends Datastore> datastoreClass, Serializable tenantId, Closure<T> callable) {
Datastore datastore = GormEnhancer.findDatastoreByType(datastoreClass)
static <T> T withId(Serializable tenantId, Closure<T> callable) {
Datastore datastore = GormEnhancer.findSingleDatastore()
if(datastore instanceof MultiTenantCapableDatastore) {
MultiTenantCapableDatastore multiTenantCapableDatastore = (MultiTenantCapableDatastore)datastore
return withId(multiTenantCapableDatastore, tenantId, callable)
Expand All @@ -207,17 +172,13 @@ class Tenants {
throw new UnsupportedOperationException("Datastore implementation does not support multi-tenancy")
}
}


/**
* Execute the given closure with given tenant id
* @param tenantId The tenant id
* @param callable The closure
* @return The result of the closure
*
* method duplicated to address name clash: <T#1>withId(Class<? extends RxDatastoreClient>,Serializable,Closure<T#1>) in grails.gorm.rx.multitenancy.Tenants and <T#2>withId(Class<? extends Datastore>,Serializable,Closure<T#2>) in grails.gorm.multitenancy.Tenants have the same erasure, yet neither hides the other
*/
static <T> T withIdMultiTenancyDatabase(Class<? extends Datastore> datastoreClass, Serializable tenantId, Closure<T> callable) {
static <T> T withId(Class<? extends Datastore> datastoreClass, Serializable tenantId, Closure<T> callable) {
Datastore datastore = GormEnhancer.findDatastoreByType(datastoreClass)
if(datastore instanceof MultiTenantCapableDatastore) {
MultiTenantCapableDatastore multiTenantCapableDatastore = (MultiTenantCapableDatastore)datastore
Expand Down Expand Up @@ -266,12 +227,12 @@ class Tenants {
}

/**
* Execute the given closure with given tenant id for the given datastore. This method will create a new datastore session for the scope of the call and hence is designed to be used to manage the connection life cycle
* @param tenantId The tenant id
* @param callable The closure
* @return The result of the closure
*/
static <T> T withId(MultiTenantCapableDatastore multiTenantCapableDatastore, Serializable tenantId, Closure callable) {
* Execute the given closure with given tenant id for the given datastore. This method will create a new datastore session for the scope of the call and hence is designed to be used to manage the connection life cycle
* @param tenantId The tenant id
* @param callable The closure
* @return The result of the closure
*/
static <T> T withId(MultiTenantCapableDatastore multiTenantCapableDatastore, Serializable tenantId, Closure<T> callable) {
return CurrentTenant.withTenant(tenantId) {
if(multiTenantCapableDatastore.getMultiTenancyMode().isSharedConnection()) {
def i = callable.parameterTypes.length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class GormStaticApi<D> extends AbstractGormApi<D> implements GormAllOperations<D
PersistentEntity getGormPersistentEntity() {
persistentEntity
}

List<FinderMethod> getGormDynamicFinders() {
gormDynamicFinders
}
Expand Down Expand Up @@ -253,7 +253,7 @@ class GormStaticApi<D> extends AbstractGormApi<D> implements GormAllOperations<D
*/
List<Serializable> saveAll(Object... objectsToSave) {
(List<Serializable>)execute({ Session session ->
session.persist Arrays.asList(objectsToSave)
session.persist Arrays.asList(objectsToSave)
} as SessionCallback)
}

Expand Down Expand Up @@ -337,7 +337,7 @@ class GormStaticApi<D> extends AbstractGormApi<D> implements GormAllOperations<D
*/
D get(Serializable id) {
(D)execute({ Session session ->
session.retrieve((Class)persistentClass, id)
session.retrieve((Class)persistentClass, id)
} as SessionCallback)
}

Expand All @@ -349,7 +349,7 @@ class GormStaticApi<D> extends AbstractGormApi<D> implements GormAllOperations<D
*/
D read(Serializable id) {
(D)execute ({ Session session ->
session.retrieve((Class)persistentClass, id)
session.retrieve((Class)persistentClass, id)
} as SessionCallback)
}

Expand All @@ -358,7 +358,7 @@ class GormStaticApi<D> extends AbstractGormApi<D> implements GormAllOperations<D
*/
D load(Serializable id) {
(D)execute ({ Session session ->
session.proxy((Class)persistentClass, id)
session.proxy((Class)persistentClass, id)
} as SessionCallback)
}

Expand All @@ -368,7 +368,7 @@ class GormStaticApi<D> extends AbstractGormApi<D> implements GormAllOperations<D
D proxy(Serializable id) {
load(id)
}

/**
* Retrieve all the objects for the given identifiers
* @param ids The identifiers to operate against
Expand All @@ -385,7 +385,7 @@ class GormStaticApi<D> extends AbstractGormApi<D> implements GormAllOperations<D
*/
List<D> getAll(Serializable... ids) {
(List<D>)execute ({ Session session ->
session.retrieveAll(persistentClass, ids.flatten())
session.retrieveAll(persistentClass, ids.flatten())
} as SessionCallback)
}

Expand Down Expand Up @@ -446,7 +446,7 @@ class GormStaticApi<D> extends AbstractGormApi<D> implements GormAllOperations<D
*/
D lock(Serializable id) {
(D)execute ({ Session session ->
session.lock((Class)persistentClass, id)
session.lock((Class)persistentClass, id)
} as SessionCallback)
}

Expand Down Expand Up @@ -709,14 +709,14 @@ class GormStaticApi<D> extends AbstractGormApi<D> implements GormAllOperations<D
}

/**
* Finds the last object. If queryParams includes 'sort', that will
* dictate the sort order, otherwise natural sort order will be used.
* queryParams may include any of the same parameters that might be passed
* to the list(Map) method. This method will ignore 'order' and 'max' as
* those are always 'asc' and 1, respectively.
*
* @return the last object in the datastore, null if none exist
*/
* Finds the last object. If queryParams includes 'sort', that will
* dictate the sort order, otherwise natural sort order will be used.
* queryParams may include any of the same parameters that might be passed
* to the list(Map) method. This method will ignore 'order' and 'max' as
* those are always 'asc' and 1, respectively.
*
* @return the last object in the datastore, null if none exist
*/
D last(Map queryParams) {
queryParams.max = 1
queryParams.order = 'desc'
Expand Down Expand Up @@ -818,23 +818,23 @@ class GormStaticApi<D> extends AbstractGormApi<D> implements GormAllOperations<D
} as SessionCallback)
}

/**
* Finds a single result matching all of the given conditions. Eg. Book.findWhere(author:"Stephen King", title:"The Stand"). If
* a matching persistent entity is not found a new entity is created and returned.
*
* @param queryMap The map of conditions
* @return A single result
/**
* Finds a single result matching all of the given conditions. Eg. Book.findWhere(author:"Stephen King", title:"The Stand"). If
* a matching persistent entity is not found a new entity is created and returned.
*
* @param queryMap The map of conditions
* @return A single result
*/
D findOrCreateWhere(Map queryMap) {
internalFindOrCreate(queryMap, false)
}

/**
* Finds a single result matching all of the given conditions. Eg. Book.findWhere(author:"Stephen King", title:"The Stand"). If
* a matching persistent entity is not found a new entity is created, saved and returned.
*
* @param queryMap The map of conditions
* @return A single result
/**
* Finds a single result matching all of the given conditions. Eg. Book.findWhere(author:"Stephen King", title:"The Stand"). If
* a matching persistent entity is not found a new entity is created, saved and returned.
*
* @param queryMap The map of conditions
* @return A single result
*/
D findOrSaveWhere(Map queryMap) {
internalFindOrCreate(queryMap, true)
Expand Down Expand Up @@ -881,10 +881,10 @@ class GormStaticApi<D> extends AbstractGormApi<D> implements GormAllOperations<D
@Override
def <T> T withTenant(Serializable tenantId, Closure<T> callable) {
if(multiTenancyMode == MultiTenancyMode.DATABASE) {
Tenants.withIdMultiTenancyDatabase((Class<Datastore>)GormEnhancer.findDatastore(persistentClass, tenantId.toString()).getClass(), tenantId, callable)
Tenants.withId((Class<Datastore>)GormEnhancer.findDatastore(persistentClass, tenantId.toString()).getClass(), tenantId, callable)
}
else if(multiTenancyMode.isSharedConnection()) {
Tenants.withIdSharedConnection((Class<Datastore>)GormEnhancer.findDatastore(persistentClass, ConnectionSource.DEFAULT).getClass(), tenantId, callable)
Tenants.withId((Class<Datastore>)GormEnhancer.findDatastore(persistentClass, ConnectionSource.DEFAULT).getClass(), tenantId, callable)
}
else {
throw new UnsupportedOperationException("Method not supported in multi tenancy mode $multiTenancyMode")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import org.grails.datastore.mapping.multitenancy.MultiTenantCapableDatastore;
import org.grails.datastore.mapping.multitenancy.exceptions.TenantException;
import org.grails.datastore.mapping.query.Query;
import org.grails.datastore.mapping.query.event.PostQueryEvent;
import org.grails.datastore.mapping.query.event.PreQueryEvent;
import org.grails.datastore.mapping.reflect.EntityReflector;
import org.springframework.context.ApplicationEvent;

import java.io.Serializable;
Expand Down Expand Up @@ -64,7 +66,7 @@ public void onApplicationEvent(ApplicationEvent event) {
currentId = Tenants.currentId((MultiTenantCapableDatastore) datastore);
}
else {
currentId = Tenants.currentIdFromDatasource(datastore.getClass());
currentId = Tenants.currentId(datastore.getClass());
}
query.eq(tenantId.getName(), currentId );
}
Expand All @@ -86,7 +88,7 @@ else if((event instanceof ValidationEvent) || (event instanceof PreInsertEvent)
currentId = Tenants.currentId((MultiTenantCapableDatastore) datastore);
}
else {
currentId = Tenants.currentIdFromDatasource(datastore.getClass());
currentId = Tenants.currentId(datastore.getClass());
}
if(currentId != null) {
try {
Expand Down
Loading

0 comments on commit d7068bb

Please sign in to comment.