@Override public void add(PropertyData propertyData) { if ( this.propertyData != null ) { throw new AuditException( "Only one property can be added!" ); } this.propertyData = propertyData; }
private void checkEntityAudited(String entityName) { if ( !auditReaderImplementor.isEntityNameAudited( entityName ) ) { throw new NotAuditedException( entityName, "Cannot query audit history on a non-audited entity [" + entityName + "]." ); } } }
@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 ); } }
@Override public void add(PropertyData propertyData) { if ( this.propertyData != null ) { throw new AuditException( "Only one property can be added!" ); } this.propertyData = propertyData; }
@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 ); } }
private Object getComponentInstance(Object dataObject) { if ( dataObject != null ) { return dataObject; } return AccessController.doPrivileged( new PrivilegedAction<Object>() { @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 ); } } } ); }
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 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 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 ); } } }
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" ); } } }
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 ); }
@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( 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 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 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 ); } }
@Override public void nullSafeMapToEntityFromMap( EnversService enversService, Object obj, Map data, Object primaryKey, AuditReaderImplementor versionsReader, Number revision) { final EntityInfo referencedEntity = getEntityInfo( enversService, referencedEntityName ); Object value; try { value = queryForReferencedEntity( versionsReader, referencedEntity, (Serializable) primaryKey, revision ); } catch (NoResultException e) { value = null; } catch (NonUniqueResultException e) { throw new AuditException( "Many versions results for one-to-one relationship " + entityName + "." + getPropertyData().getBeanName() + ".", e ); } setPropertyValue( obj, value ); }
/** * 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!" ); } }