Created
February 28, 2022 17:26
-
-
Save onurcill/ff67f58b7b49c72f8150eea44067144d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class TenantInterceptor extends EmptyInterceptor { | |
@Override | |
public boolean onSave( | |
final Object entity, | |
final Serializable id, | |
final Object[] state, | |
final String[] propertyNames, | |
final Type[] types) { | |
return this.addTenantIdIfObjectIsTenantEntity(entity, state, propertyNames); | |
} | |
@Override | |
public boolean onFlushDirty( | |
final Object entity, | |
final Serializable id, | |
final Object[] currentState, | |
final Object[] previousState, | |
final String[] propertyNames, | |
final Type[] types) { | |
return this.addTenantIdIfObjectIsTenantEntity(entity, currentState, propertyNames); | |
} | |
@Override | |
public void onDelete( | |
final Object entity, | |
final Serializable id, | |
final Object[] state, | |
final String[] propertyNames, | |
final Type[] types) { | |
this.addTenantIdIfObjectIsTenantEntity(entity, state, propertyNames); | |
} | |
private boolean addTenantIdIfObjectIsTenantEntity( | |
final Object entity, final Object[] state, final String[] propertyName) { | |
final Optional<NoMultiTenancy> noMultiTenancy = this.findAnnotation(entity); | |
if (noMultiTenancy.isPresent() && entity instanceof TenantEntity) { | |
throw new IllegalArgumentException( | |
"NoMultiTenancy annotation can not be used in combination of TenantEntity"); | |
} | |
if (noMultiTenancy.isEmpty() && entity instanceof TenantEntity) { | |
for (int index = 0; index < propertyName.length; index++) { | |
if (propertyName[index].equals(TENANT_FILTER_ARGUMENT_NAME)) { | |
state[index] = TenantAssistance.resolveCurrentTenantIdentifier(); | |
return true; | |
} | |
} | |
} | |
if (noMultiTenancy.isPresent()) { | |
return true; | |
} | |
throw new UnknownTenantException("Tenant interceptor did not detect a valid tenant"); | |
} | |
private Optional<NoMultiTenancy> findAnnotation(final Object entity) { | |
return Arrays.stream(entity.getClass().getAnnotations()) | |
.filter(NoMultiTenancy.class::isInstance) | |
.map(NoMultiTenancy.class::cast) | |
.findFirst(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment