/
AuditProcess.java
158 lines (131 loc) · 5.55 KB
/
AuditProcess.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.envers.synchronization;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import org.hibernate.action.BeforeTransactionCompletionProcess;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.envers.revisioninfo.RevisionInfoGenerator;
import org.hibernate.envers.synchronization.work.AuditWorkUnit;
import org.hibernate.envers.tools.Pair;
import org.hibernate.FlushMode;
import org.hibernate.Session;
/**
* @author Adam Warski (adam at warski dot org)
*/
public class AuditProcess implements BeforeTransactionCompletionProcess {
private final RevisionInfoGenerator revisionInfoGenerator;
private final SessionImplementor session;
private final LinkedList<AuditWorkUnit> workUnits;
private final Queue<AuditWorkUnit> undoQueue;
private final Map<Pair<String, Object>, AuditWorkUnit> usedIds;
private Object revisionData;
public AuditProcess(RevisionInfoGenerator revisionInfoGenerator, SessionImplementor session) {
this.revisionInfoGenerator = revisionInfoGenerator;
this.session = session;
workUnits = new LinkedList<AuditWorkUnit>();
undoQueue = new LinkedList<AuditWorkUnit>();
usedIds = new HashMap<Pair<String, Object>, AuditWorkUnit>();
}
private void removeWorkUnit(AuditWorkUnit vwu) {
workUnits.remove(vwu);
if (vwu.isPerformed()) {
// If this work unit has already been performed, it must be deleted (undone) first.
undoQueue.offer(vwu);
}
}
public void addWorkUnit(AuditWorkUnit vwu) {
if (vwu.containsWork()) {
Object entityId = vwu.getEntityId();
if (entityId == null) {
// Just adding the work unit - it's not associated with any persistent entity.
workUnits.offer(vwu);
} else {
String entityName = vwu.getEntityName();
Pair<String, Object> usedIdsKey = Pair.make(entityName, entityId);
if (usedIds.containsKey(usedIdsKey)) {
AuditWorkUnit other = usedIds.get(usedIdsKey);
AuditWorkUnit result = vwu.dispatch(other);
if (result != other) {
removeWorkUnit(other);
if (result != null) {
usedIds.put(usedIdsKey, result);
workUnits.offer(result);
} // else: a null result means that no work unit should be kept
} // else: the result is the same as the work unit already added. No need to do anything.
} else {
usedIds.put(usedIdsKey, vwu);
workUnits.offer(vwu);
}
}
}
}
private void executeInSession(Session session) {
// Making sure the revision data is persisted.
getCurrentRevisionData(session, true);
AuditWorkUnit vwu;
// First undoing any performed work units
while ((vwu = undoQueue.poll()) != null) {
vwu.undo(session);
}
while ((vwu = workUnits.poll()) != null) {
vwu.perform(session, revisionData);
}
}
public Object getCurrentRevisionData(Session session, boolean persist) {
// Generating the revision data if not yet generated
if (revisionData == null) {
revisionData = revisionInfoGenerator.generate();
}
// Saving the revision data, if not yet saved and persist is true
if (!session.contains(revisionData) && persist) {
revisionInfoGenerator.saveRevisionData(session, revisionData);
}
return revisionData;
}
public void doBeforeTransactionCompletion(SessionImplementor session) {
if (workUnits.size() == 0 && undoQueue.size() == 0) {
return;
}
// see: http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4178431
if (FlushMode.isManualFlushMode(session.getFlushMode())) {
Session temporarySession = null;
try {
temporarySession = session.getFactory().openTemporarySession();
executeInSession(temporarySession);
temporarySession.flush();
} finally {
if (temporarySession != null) {
temporarySession.close();
}
}
} else {
executeInSession((Session) session);
// Explicity flushing the session, as the auto-flush may have already happened.
session.flush();
}
}
}