first fix (must still be cleaned up)

This commit is contained in:
Oliver Lorenz 2011-05-25 11:53:30 +02:00 committed by adamw
parent 74b83b06f2
commit 735899da92
1 changed files with 32 additions and 1 deletions

View File

@ -71,8 +71,39 @@ public class ValidityAuditStrategy implements AuditStrategy {
@SuppressWarnings({"unchecked"})
public void performCollectionChange(Session session, AuditConfiguration auditCfg,
PersistentCollectionChangeData persistentCollectionChangeData, Object revision) {
final boolean lastRevisionCanBeUpdated;
// TODO clean this up, remove duplicate code
// Update the end date of the previous row if this operation is expected to have a previous row
if (getRevisionType(auditCfg, persistentCollectionChangeData.getData()) != RevisionType.ADD) {
if (getRevisionType(auditCfg, persistentCollectionChangeData.getData()) == RevisionType.ADD) {
// ADD: special checking is necessary to see if this is really the first insert with the given primary key
final QueryBuilder qb = new QueryBuilder(persistentCollectionChangeData.getEntityName(), "e");
// Adding a parameter for each id component, except the rev number
String originalIdPropName = auditCfg.getAuditEntCfg().getOriginalIdPropName();
Map<String, Object> originalId = (Map<String, Object>) persistentCollectionChangeData.getData().get(
originalIdPropName);
for (Map.Entry<String, Object> originalIdEntry : originalId.entrySet()) {
if (!auditCfg.getAuditEntCfg().getRevisionFieldName().equals(originalIdEntry.getKey())) {
qb.getRootParameters().addWhereWithParam(originalIdPropName + "." + originalIdEntry.getKey(),
true, "=", originalIdEntry.getValue());
}
}
// e.end_rev is null
String revisionEndFieldName = auditCfg.getAuditEntCfg().getRevisionEndFieldName();
qb.getRootParameters().addWhere(revisionEndFieldName, true, "is", "null", false);
// last revision can be updated if the query returned at least one row
final List<Object> l = qb.toQuery(session).list();
lastRevisionCanBeUpdated = l.size() > 0;
} else {
// revision type is something else than ADD: last revision can be safely updated
lastRevisionCanBeUpdated = true;
}
if (lastRevisionCanBeUpdated) {
/*
Constructing a query (there are multiple id fields):
select e from audited_middle_ent e where e.end_rev is null and e.id1 = :id1 and e.id2 = :id2 ...