@Override public void add(PropertyData propertyData) { if ( this.propertyData != null ) { throw new AuditException( "Only one property can be added!" ); } this.propertyData = propertyData; }
@Override public void add(PropertyData propertyData) { if ( this.propertyData != null ) { throw new AuditException( "Only one property can be added!" ); } this.propertyData = propertyData; }
@Override public T run() { try { return (T) ReflectHelper.getDefaultConstructor( collectionClass ).newInstance(); } catch (InstantiationException e) { throw new AuditException( e ); } catch (IllegalAccessException e) { throw new AuditException( e ); } catch (InvocationTargetException e) { throw new AuditException( e ); } } }
protected AbstractCollectionMapper( CommonCollectionMapperData commonCollectionMapperData, Class<? extends T> collectionClass, Class<? extends T> proxyClass, boolean ordinalInId, boolean revisionTypeInId) { this.commonCollectionMapperData = commonCollectionMapperData; this.collectionClass = collectionClass; this.ordinalInId = ordinalInId; this.revisionTypeInId = revisionTypeInId; try { proxyConstructor = proxyClass.getConstructor( Initializor.class ); } catch (NoSuchMethodException e) { throw new AuditException( e ); } }
@Override public T run() { try { return (T) ReflectHelper.getDefaultConstructor( collectionClass ).newInstance(); } catch (InstantiationException e) { throw new AuditException( e ); } catch (IllegalAccessException e) { throw new AuditException( e ); } catch (InvocationTargetException e) { throw new AuditException( e ); } } }
@Override protected SortedMap initializeCollection(int size) { if ( comparator == null ) { return super.initializeCollection( size ); } try { return collectionClass.getConstructor( Comparator.class ).newInstance( comparator ); } catch (InstantiationException e) { throw new AuditException( e ); } catch (IllegalAccessException e) { throw new AuditException( e ); } catch (NoSuchMethodException e) { throw new AuditException( e ); } catch (InvocationTargetException e) { throw new AuditException( e ); } }
@Override public Object run() { try { return ReflectHelper.getDefaultConstructor( componentClass ).newInstance(); } catch ( InstantiationException e ) { throw new AuditException( e ); } catch ( IllegalAccessException e ) { throw new AuditException( e ); } catch ( InvocationTargetException e ) { throw new AuditException( e ); } } }
public void cacheEntityState(Object id, String entityName, Object[] snapshot) { final Pair<String, Object> key = new Pair<>( entityName, id ); if ( entityStateCache.containsKey( key ) ) { throw new AuditException( "The entity [" + entityName + "] with id [" + id + "] is already cached." ); } entityStateCache.put( key, snapshot ); }
protected void checkIfTransactionInProgress(SessionImplementor session) { if ( !session.isTransactionInProgress() ) { // Historical data would not be flushed to audit tables if outside of active transaction // (AuditProcess#doBeforeTransactionCompletion(SessionImplementor) not executed). throw new AuditException( "Unable to create revision because of non-active transaction" ); } } }
@Override public Object run() { try { return ReflectHelper.getDefaultConstructor( compositeIdClass ).newInstance(); } catch ( Exception e ) { throw new AuditException( e ); } } }
@Override public CrossTypeRevisionChangesReader getCrossTypeRevisionChangesReader() throws AuditException { if ( !enversService.getGlobalConfiguration().isTrackEntitiesChangedInRevision() ) { throw new AuditException( "This API is designed for Envers default mechanism of tracking entities modified in a given revision." + " Extend DefaultTrackingModifiedEntitiesRevisionEntity, utilize @ModifiedEntityNames annotation or set " + "'org.hibernate.envers.track_entities_changed_in_revision' parameter to true." ); } return crossTypeRevisionChangesReader; }
public static void checkPropertyNotARelation( EnversService enversService, String entityName, String propertyName) throws AuditException { if ( enversService.getEntitiesConfigurations().get( entityName ).isRelation( propertyName ) ) { throw new AuditException( "This criterion cannot be used on a property that is " + "a relation to another property." ); } }
public static Class<?> getType(Class cls, PropertyData propertyData, ServiceRegistry serviceRegistry) { final Setter setter = getSetter( cls, propertyData, serviceRegistry ); if ( setter.getMethod() != null && setter.getMethod().getParameterCount() > 0 ) { return setter.getMethod().getParameterTypes()[0]; } final Field field = getField( cls, propertyData ); if ( field != null ) { return field.getType(); } throw new AuditException( String.format( Locale.ROOT, "Failed to determine type for field [%s] on class [%s].", propertyData.getName(), cls.getName() ) ); }
public static RelationDescription getRelatedEntity( EnversService enversService, String entityName, String propertyName) throws AuditException { RelationDescription relationDesc = enversService.getEntitiesConfigurations().getRelationDescription( entityName, propertyName ); if ( relationDesc == null ) { return null; } if ( relationDesc.getRelationType() == RelationType.TO_ONE ) { return relationDesc; } throw new AuditException( "This type of relation (" + entityName + "." + propertyName + ") isn't supported and can't be used in queries." ); }
@Override public Boolean run() { final Getter getter = ReflectionTools.getGetter( obj.getClass(), idPropertyData, getServiceRegistry() ); final Setter setter = ReflectionTools.getSetter( obj.getClass(), idPropertyData, getServiceRegistry() ); try { final Object subObj = ReflectHelper.getDefaultConstructor( getter.getReturnType() ).newInstance(); boolean ret = true; for ( IdMapper idMapper : ids.values() ) { ret &= idMapper.mapToEntityFromMap( subObj, data ); } if ( ret ) { setter.set( obj, subObj, null ); } return ret; } catch (Exception e) { throw new AuditException( e ); } } }
@Override protected void addToQuery( EnversService enversService, AuditReaderImplementor versionsReader, String entityName, String alias, QueryBuilder qb, Parameters parameters) { String propertyName = CriteriaTools.determinePropertyName( enversService, versionsReader, entityName, propertyNameGetter ); RelationDescription relatedEntity = CriteriaTools.getRelatedEntity( enversService, entityName, propertyName ); if ( relatedEntity == null ) { throw new AuditException( "This criterion can only be used on a property that is a relation to another property." ); } relatedEntity.getIdMapper().addIdEqualsToQuery( parameters, id, alias, null, equals ); } }
private Object getQueryResultRowValue(Map versionsData, Object revisionData, String entityName) { final Number revision = getRevisionNumber( versionsData ); final Object entity = entityInstantiator.createInstanceFromVersionsEntity( entityName, versionsData, revision ); if ( selectEntitiesOnly ) { return entity; } final String revisionTypePropertyName = enversService.getAuditEntitiesConfiguration().getRevisionTypePropName(); Object revisionType = versionsData.get( revisionTypePropertyName ); if ( !includePropertyChanges ) { return new Object[] { entity, revisionData, revisionType }; } if ( !isEntityUsingModifiedFlags() ) { throw new AuditException( String.format( Locale.ROOT, "The specified entity [%s] does not support or use modified flags.", getEntityConfiguration().getEntityClassName() ) ); } final Set<String> changedPropertyNames = getChangedPropertyNames( versionsData, revisionType ); return new Object[] { entity, revisionData, revisionType, changedPropertyNames }; } }
@Override public Date getRevisionDate(Number revision) throws IllegalArgumentException, RevisionDoesNotExistException, IllegalStateException { checkNotNull( revision, "Entity revision" ); checkPositive( revision, "Entity revision" ); checkSession(); final Query<?> query = enversService.getRevisionInfoQueryCreator().getRevisionDateQuery( session, revision ); try { final Object timestampObject = query.uniqueResult(); if ( timestampObject == null ) { throw new RevisionDoesNotExistException( revision ); } // The timestamp object is either a date or a long return timestampObject instanceof Date ? (Date) timestampObject : new Date( (Long) timestampObject ); } catch (NonUniqueResultException e) { throw new AuditException( e ); } }
@Override public Number getRevisionNumberForDate(Date date) { checkNotNull( date, "Date of revision" ); checkSession(); final Query<?> query = enversService.getRevisionInfoQueryCreator().getRevisionNumberForDateQuery( session, date ); try { final Number res = (Number) query.uniqueResult(); if ( res == null ) { throw new RevisionDoesNotExistException( date ); } return res; } catch (NonUniqueResultException e) { throw new AuditException( e ); } }
/** * Create an audit reader associated with an open entity manager. * * @param entityManager An open entity manager. * * @return An audit reader associated with the given entity manager. It shouldn't be used * after the entity manager is closed. * * @throws AuditException When the given entity manager is not based on Hibernate, or if the required * listeners aren't installed. */ public static AuditReader get(EntityManager entityManager) throws AuditException { if ( entityManager.getDelegate() instanceof Session ) { return get( (Session) entityManager.getDelegate() ); } if ( entityManager.getDelegate() instanceof EntityManager ) { return get( (EntityManager) entityManager.getDelegate() ); } throw new AuditException( "Hibernate EntityManager not present!" ); } }