@Override public void insertInBatch(final List<? extends PersistentObject> entities) throws SPersistenceException { for (final PersistentObject entity : entities) { setTenant(entity); } super.insertInBatch(entities); }
protected void setTenant(final PersistentObject entity) throws SPersistenceException { if (entity == null) { return; } // if this entity has no id, set it Long tenantId = null; try { tenantId = ClassReflector.invokeGetter(entity, "getTenantId"); } catch (final Exception e) { // this is a new object to save } if (tenantId == null || tenantId == -1 || tenantId == 0) { setTenantByClassReflector(entity, tenantId); } }
@Override protected Session getSession(final boolean useTenant) throws SPersistenceException { final Session session = super.getSession(useTenant); updateTenantFilter(session, useTenant); return session; }
@Override public void deleteByTenant(final Class<? extends PersistentObject> entityClass, final List<FilterOption> filters) throws SPersistenceException { try { final Session session = getSession(true); final String entityClassName = entityClass.getCanonicalName(); final boolean enableWordSearch = isWordSearchEnabled(entityClass); boolean hasFilters = filters != null && !filters.isEmpty(); String baseQuery = "DELETE FROM " + entityClassName + " " + (hasFilters ? getClassAliasMappings().get(entityClassName) : "") + " WHERE tenantId= :tenantId"; QueryBuilder queryBuilder = new HQLQueryBuilder(baseQuery, orderByBuilder, getClassAliasMappings(), interfaceToClassMapping, likeEscapeCharacter); if (hasFilters) { queryBuilder.appendFilters(filters, null, enableWordSearch); } final Query query = queryBuilder.buildQuery(session); query.setLong(TENANT_ID, getTenantId()); query.executeUpdate(); if (logger.isLoggable(getClass(), TechnicalLogSeverity.DEBUG)) { logger.log(this.getClass(), TechnicalLogSeverity.DEBUG, "[Tenant] Deleting all instance of class " + entityClass.getClass().getSimpleName()); } } catch (final STenantIdNotSetException e) { throw new SPersistenceException(e); } }
@Override public void delete(final PersistentObject entity) throws SPersistenceException { try { if (logger.isLoggable(getClass(), TechnicalLogSeverity.DEBUG)) { logger.log(this.getClass(), TechnicalLogSeverity.DEBUG, "[Tenant] Deleting instance of class " + entity.getClass().getSimpleName() + " with id=" + entity.getId()); } final Class<? extends PersistentObject> mappedClass = getMappedClass(entity.getClass()); final Session session = getSession(true); final Object pe = session.get(mappedClass, new PersistentObjectId(entity.getId(), getTenantId())); session.delete(pe); } catch (final AssertionFailure | LockAcquisitionException | StaleStateException e) { throw new SRetryableException(e); } catch (final STenantIdNotSetException | HibernateException e) { throw new SPersistenceException(e); } }
@SuppressWarnings("unchecked") @Override <T extends PersistentObject> T selectById(final Session session, final SelectByIdDescriptor<T> selectDescriptor) throws SBonitaReadException { try { final PersistentObjectId id = new PersistentObjectId(selectDescriptor.getId(), getTenantId()); Class<? extends PersistentObject> mappedClass = null; mappedClass = getMappedClass(selectDescriptor.getEntityType()); return (T) session.get(mappedClass, id); } catch (final STenantIdNotSetException e) { return super.selectById(session, selectDescriptor); } catch (final AssertionFailure | LockAcquisitionException | StaleStateException e) { throw new SRetryableException(e); } catch (final SPersistenceException | HibernateException e) { throw new SBonitaReadException(e); } }
private void setTenantByClassReflector(final PersistentObject entity, Long tenantId) throws SPersistenceException { try { tenantId = getTenantId(); ClassReflector.invokeSetter(entity, "setTenantId", long.class, tenantId); } catch (final SReflectException e) { throw new SPersistenceException("Can't set tenantId = <" + tenantId + "> on entity." + entity, e); } catch (final STenantIdNotSetException e) { throw new SPersistenceException("Can't set tenantId = <" + tenantId + "> on entity." + entity, e); } }
@Override public void deleteByTenant(final Class<? extends PersistentObject> entityClass, final List<FilterOption> filters) throws SPersistenceException { try { final Session session = getSession(true); final String entityClassName = entityClass.getCanonicalName(); final boolean enableWordSearch = isWordSearchEnabled(entityClass); boolean hasFilters = filters != null && !filters.isEmpty(); String baseQuery = "DELETE FROM " + entityClassName + " " + (hasFilters ? getClassAliasMappings().get(entityClassName) : "") + " WHERE tenantId= :tenantId"; QueryBuilder queryBuilder = new HQLQueryBuilder(baseQuery, orderByBuilder, getClassAliasMappings(), interfaceToClassMapping, likeEscapeCharacter); if (hasFilters) { queryBuilder.appendFilters(filters, null, enableWordSearch); } final Query query = queryBuilder.buildQuery(session); query.setLong(TENANT_ID, getTenantId()); query.executeUpdate(); if (logger.isLoggable(getClass(), TechnicalLogSeverity.DEBUG)) { logger.log(this.getClass(), TechnicalLogSeverity.DEBUG, "[Tenant] Deleting all instance of class " + entityClass.getClass().getSimpleName()); } } catch (final STenantIdNotSetException e) { throw new SPersistenceException(e); } }
@Override public void delete(final PersistentObject entity) throws SPersistenceException { try { if (logger.isLoggable(getClass(), TechnicalLogSeverity.DEBUG)) { logger.log(this.getClass(), TechnicalLogSeverity.DEBUG, "[Tenant] Deleting instance of class " + entity.getClass().getSimpleName() + " with id=" + entity.getId()); } final Class<? extends PersistentObject> mappedClass = getMappedClass(entity.getClass()); final Session session = getSession(true); final Object pe = session.get(mappedClass, new PersistentObjectId(entity.getId(), getTenantId())); session.delete(pe); } catch (final AssertionFailure | LockAcquisitionException | StaleStateException e) { throw new SRetryableException(e); } catch (final STenantIdNotSetException | HibernateException e) { throw new SPersistenceException(e); } }
@SuppressWarnings("unchecked") @Override <T extends PersistentObject> T selectById(final Session session, final SelectByIdDescriptor<T> selectDescriptor) throws SBonitaReadException { try { final PersistentObjectId id = new PersistentObjectId(selectDescriptor.getId(), getTenantId()); Class<? extends PersistentObject> mappedClass = null; mappedClass = getMappedClass(selectDescriptor.getEntityType()); return (T) session.get(mappedClass, id); } catch (final STenantIdNotSetException e) { return super.selectById(session, selectDescriptor); } catch (final AssertionFailure | LockAcquisitionException | StaleStateException e) { throw new SRetryableException(e); } catch (final SPersistenceException | HibernateException e) { throw new SBonitaReadException(e); } }
private void setTenantByClassReflector(final PersistentObject entity, Long tenantId) throws SPersistenceException { try { tenantId = getTenantId(); ClassReflector.invokeSetter(entity, "setTenantId", long.class, tenantId); } catch (final SReflectException e) { throw new SPersistenceException("Can't set tenantId = <" + tenantId + "> on entity." + entity, e); } catch (final STenantIdNotSetException e) { throw new SPersistenceException("Can't set tenantId = <" + tenantId + "> on entity." + entity, e); } }
@Override public void insert(final PersistentObject entity) throws SPersistenceException { setTenant(entity); super.insert(entity); }
protected void updateTenantFilter(final Session session, final boolean useTenant) throws SPersistenceException { if (useTenant) { try { session.enableFilter(TENANT_FILTER).setParameter(TENANT_ID, getTenantId()); } catch (final STenantIdNotSetException e) { throw new SPersistenceException(e); } } else { session.disableFilter(TENANT_FILTER); } }
@Override protected Session getSession(final boolean useTenant) throws SPersistenceException { final Session session = super.getSession(useTenant); updateTenantFilter(session, useTenant); return session; }
protected void setTenant(final PersistentObject entity) throws SPersistenceException { if (entity == null) { return; } // if this entity has no id, set it Long tenantId = null; try { tenantId = ClassReflector.invokeGetter(entity, "getTenantId"); } catch (final Exception e) { // this is a new object to save } if (tenantId == null || tenantId == -1 || tenantId == 0) { setTenantByClassReflector(entity, tenantId); } }
@Override public void insert(final PersistentObject entity) throws SPersistenceException { setTenant(entity); super.insert(entity); }
protected void updateTenantFilter(final Session session, final boolean useTenant) throws SPersistenceException { if (useTenant) { try { session.enableFilter(TENANT_FILTER).setParameter(TENANT_ID, getTenantId()); } catch (final STenantIdNotSetException e) { throw new SPersistenceException(e); } } else { session.disableFilter(TENANT_FILTER); } }
@Override public void insertInBatch(final List<? extends PersistentObject> entities) throws SPersistenceException { for (final PersistentObject entity : entities) { setTenant(entity); } super.insertInBatch(entities); }