use of org.hibernate.envers.exception.AuditException in project hibernate-orm by hibernate.
the class ComponentPropertyMapper method mapToEntityFromMap.
@Override
public void mapToEntityFromMap(EnversService enversService, Object obj, Map data, Object primaryKey, AuditReaderImplementor versionsReader, Number revision) {
if (data == null || obj == null) {
return;
}
if (propertyData.getBeanName() == null) {
// If properties are not encapsulated in a component but placed directly in a class
// (e.g. by applying <properties> tag).
delegate.mapToEntityFromMap(enversService, obj, data, primaryKey, versionsReader, revision);
return;
}
final Setter setter = ReflectionTools.getSetter(obj.getClass(), propertyData, enversService.getServiceRegistry());
// If all properties are null and single, then the component has to be null also.
boolean allNullAndSingle = true;
for (Map.Entry<PropertyData, PropertyMapper> property : delegate.getProperties().entrySet()) {
if (data.get(property.getKey().getName()) != null || !(property.getValue() instanceof SinglePropertyMapper)) {
allNullAndSingle = false;
break;
}
}
if (allNullAndSingle) {
// single property, but default value need not be null, so we'll set it to null anyway
setter.set(obj, null, null);
} else {
// set the component
try {
final Object subObj = ReflectHelper.getDefaultConstructor(componentClass).newInstance();
setter.set(obj, subObj, null);
delegate.mapToEntityFromMap(enversService, subObj, data, primaryKey, versionsReader, revision);
} catch (Exception e) {
throw new AuditException(e);
}
}
}
use of org.hibernate.envers.exception.AuditException in project hibernate-orm by hibernate.
the class EmbeddedIdMapper method mapToEntityFromMap.
@Override
public boolean mapToEntityFromMap(Object obj, Map data) {
if (data == null || obj == null) {
return false;
}
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);
}
}
use of org.hibernate.envers.exception.AuditException in project hibernate-orm by hibernate.
the class AbstractOneToOneMapper method nullSafeMapToEntityFromMap.
@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);
}
use of org.hibernate.envers.exception.AuditException in project hibernate-orm by hibernate.
the class MiddleEmbeddableComponentMapper method mapToObjectFromFullMap.
@Override
public Object mapToObjectFromFullMap(EntityInstantiator entityInstantiator, Map<String, Object> data, Object dataObject, Number revision) {
try {
final Object componentInstance = dataObject != null ? dataObject : ReflectHelper.getDefaultConstructor(componentClass).newInstance();
delegate.mapToEntityFromMap(entityInstantiator.getEnversService(), componentInstance, data, null, entityInstantiator.getAuditReaderImplementor(), revision);
return componentInstance;
} catch (Exception e) {
throw new AuditException(e);
}
}
use of org.hibernate.envers.exception.AuditException in project hibernate-orm by hibernate.
the class AuditReaderImpl method getRevisionDate.
@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);
}
}
Aggregations