In my application, I have a several audited entity classes for example the following.
It contains multiple HAS-IS relations to other entities with various hibernate annotations.
@Entity
@Audited
public class Entity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Integer Id;
@ManyToMany
private Set < Country> affectedCountries;
@OneToOne
private Impact impact;
@Enumerated(EnumType.STRING)
private Owner owner;
...
}
I am analyzing the the audit-trail with the following code snipplet, which return all the attribute values from the audit table entity.
public List< AuditTrailForm> getAuditTrailEntries(Class< ?> clazz, Serializable id) {
AuditReader reader = AuditReaderFactory.get(this.getSession());
List < Number> revNumbers = reader.getRevisions(clazz, id);
List< AuditTrailForm> forms = new ArrayList();
Iterator< Number> it = revNumbers.iterator();
while(it.hasNext()) {
Number item = it.next();
Object obj = reader.find(clazz, id, item);
AuditInfo revision = reader.findRevision(AuditInfo.class, item);
BeanMap beanMap = new BeanMap(obj);
HashMap map = new HashMap();
Set keys = beanMap.keySet( );
Iterator keyIterator = keys.iterator( );
while( keyIterator.hasNext( ) ) {
String propertyName = (String) keyIterator.next( );
if (beanMap.getType(propertyName).equals(String.class)) {
String propertyValue = (String) beanMap.get( propertyName );
map.put(propertyName, propertyValue);
}
}
Date createdAt = revision.getTimestamp();
String user = revision.getUser();
AuditTrailForm form = new AuditTrailForm(user, createdAt, map);
forms.add(form);
}
return forms;
}
Works fine, however this doesn't take into account the traversing the relations in the class.
Could I somehow develop a recursive algorithm, which would detect the type of the object attribute and then do the recursive call?
Is there perhaps a better way to do this altogether?