1
|
package eu.dnetlib.xml.database.exist;
|
2
|
|
3
|
import java.util.HashMap;
|
4
|
import java.util.List;
|
5
|
import java.util.Map;
|
6
|
|
7
|
import eu.dnetlib.miscutils.functional.xml.IndentXmlString;
|
8
|
import org.apache.commons.logging.Log;
|
9
|
import org.apache.commons.logging.LogFactory;
|
10
|
import org.exist.collections.Collection;
|
11
|
import org.exist.collections.triggers.SAXTrigger;
|
12
|
import org.exist.collections.triggers.TriggerException;
|
13
|
import org.exist.dom.persistent.DocumentImpl;
|
14
|
import org.exist.storage.DBBroker;
|
15
|
import org.exist.storage.txn.Txn;
|
16
|
import org.exist.xmldb.XmldbURI;
|
17
|
|
18
|
import eu.dnetlib.xml.database.Trigger;
|
19
|
|
20
|
/**
|
21
|
* this trigger delegates diffs for CRUDE events to an non-exist dependent eu.dnetlib.xml.database.Trigger instance.
|
22
|
*
|
23
|
* Since eXist triggers are instantiated by eXist, we need to locate the Trigger instances which are registered to the database instance.
|
24
|
*
|
25
|
* @author marko
|
26
|
*
|
27
|
*/
|
28
|
public class DelegatingDiffTrigger extends SAXTrigger {
|
29
|
|
30
|
/**
|
31
|
* logger.
|
32
|
*/
|
33
|
private static final Log log = LogFactory.getLog(DelegatingDiffTrigger.class); // NOPMD by marko on 11/24/08 5:02 PM
|
34
|
|
35
|
/**
|
36
|
* trigger identifier, from configuration.
|
37
|
*/
|
38
|
private String triggerName;
|
39
|
|
40
|
private Map<String, ExistsTriggerEvent> existsTriggerEventMap = new HashMap<>();
|
41
|
|
42
|
/**
|
43
|
* {@inheritDoc}
|
44
|
*
|
45
|
* @see org.exist.collections.triggers.FilteringTrigger#configure(org.exist.storage.DBBroker, org.exist.collections.Collection,
|
46
|
* java.util.Map)
|
47
|
*/
|
48
|
@SuppressWarnings("unchecked")
|
49
|
@Override
|
50
|
public void configure(final DBBroker dbBroker, final Collection parent, final Map<String, List<?>> parameters) throws TriggerException {
|
51
|
super.configure(dbBroker, parent, parameters);
|
52
|
|
53
|
if (parameters == null) { return; }
|
54
|
|
55
|
final Map<String, List<?>> params = parameters;
|
56
|
if (parameters.containsKey("triggerName")) {
|
57
|
final List<?> myTriggerName = parameters.get("triggerName");
|
58
|
if ((myTriggerName != null) && (myTriggerName.size() == 1)) {
|
59
|
setTriggerName(myTriggerName.get(0).toString());
|
60
|
}
|
61
|
}
|
62
|
if (getTriggerName() == null) {
|
63
|
log.fatal("trigger id not configured");
|
64
|
throw new TriggerException("trigger name not configured");
|
65
|
}
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* find the trigger instance associated with this delegation.
|
70
|
*
|
71
|
* @return trigger instance
|
72
|
*/
|
73
|
protected Trigger getTrigger() {
|
74
|
final Trigger trigger = ExistTriggerRegistry.defaultInstance().getTrigger(getTriggerName());
|
75
|
if (trigger == null) {
|
76
|
log.fatal("no trigger " + triggerName + " but there is a registered callback for it");
|
77
|
return null;
|
78
|
}
|
79
|
|
80
|
return trigger;
|
81
|
}
|
82
|
|
83
|
public String getTriggerName() {
|
84
|
return triggerName;
|
85
|
}
|
86
|
|
87
|
public void setTriggerName(final String triggerId) {
|
88
|
triggerName = triggerId;
|
89
|
}
|
90
|
|
91
|
@Override
|
92
|
public void beforeCreateDocument(final DBBroker dbBroker, final Txn txn, final XmldbURI xmldbURI) throws TriggerException {
|
93
|
|
94
|
}
|
95
|
|
96
|
@Override
|
97
|
public void afterCreateDocument(final DBBroker dbBroker, final Txn txn, final DocumentImpl document) throws TriggerException {
|
98
|
|
99
|
try {
|
100
|
final Trigger trigger = getTrigger();
|
101
|
final String collection = document.getCollection().getURI().toString();
|
102
|
final String fileName = document.getFileURI().toString();
|
103
|
if (trigger != null) {
|
104
|
trigger.created(fileName, collection, document);
|
105
|
}
|
106
|
} catch (final Exception e) {
|
107
|
throw new TriggerException(e);
|
108
|
}
|
109
|
}
|
110
|
|
111
|
@Override
|
112
|
public void beforeUpdateDocument(final DBBroker dbBroker, final Txn txn, final DocumentImpl document) throws TriggerException {
|
113
|
|
114
|
try {
|
115
|
final String collection = document.getCollection().getURI().toString();
|
116
|
final String fileName = document.getFileURI().toString();
|
117
|
final ExistsTriggerEvent event = new ExistsTriggerEvent();
|
118
|
event.setOldDocument(IndentXmlString.apply(document));
|
119
|
event.setEventType(EventType.UPDATE);
|
120
|
event.setCollection(collection);
|
121
|
event.setName(fileName);
|
122
|
existsTriggerEventMap.put(String.valueOf(txn.getId()), event);
|
123
|
} catch (final Exception e) {
|
124
|
throw new TriggerException(e);
|
125
|
}
|
126
|
}
|
127
|
|
128
|
@Override
|
129
|
public void afterUpdateDocument(final DBBroker dbBroker, final Txn txn, final DocumentImpl document) throws TriggerException {
|
130
|
|
131
|
try {
|
132
|
if (!existsTriggerEventMap.containsKey(
|
133
|
String.valueOf(txn.getId()))) { throw new TriggerException("Error on trigger missing previous operation beforeUpdateTrigger"); }
|
134
|
final ExistsTriggerEvent existsTriggerEvent = existsTriggerEventMap.remove(String.valueOf(txn.getId()));
|
135
|
|
136
|
existsTriggerEvent.setDocument(IndentXmlString.apply(document));
|
137
|
final Trigger trigger = getTrigger();
|
138
|
if (trigger != null) {
|
139
|
trigger.updated(existsTriggerEvent.getName(), existsTriggerEvent.getCollection(), existsTriggerEvent.getOldDocument(),
|
140
|
existsTriggerEvent.getDocument());
|
141
|
}
|
142
|
} catch (final Exception e) {
|
143
|
throw new TriggerException(e);
|
144
|
}
|
145
|
}
|
146
|
|
147
|
@Override
|
148
|
public void beforeUpdateDocumentMetadata(final DBBroker dbBroker, final Txn txn, final DocumentImpl document) throws TriggerException {
|
149
|
|
150
|
}
|
151
|
|
152
|
@Override
|
153
|
public void afterUpdateDocumentMetadata(final DBBroker dbBroker, final Txn txn, final DocumentImpl document) throws TriggerException {
|
154
|
|
155
|
}
|
156
|
|
157
|
@Override
|
158
|
public void beforeCopyDocument(final DBBroker dbBroker, final Txn txn, final DocumentImpl document, final XmldbURI xmldbURI) throws TriggerException {
|
159
|
|
160
|
}
|
161
|
|
162
|
@Override
|
163
|
public void afterCopyDocument(final DBBroker dbBroker, final Txn txn, final DocumentImpl document, final XmldbURI xmldbURI) throws TriggerException {
|
164
|
|
165
|
}
|
166
|
|
167
|
@Override
|
168
|
public void beforeMoveDocument(final DBBroker dbBroker, final Txn txn, final DocumentImpl document, final XmldbURI xmldbURI) throws TriggerException {
|
169
|
|
170
|
}
|
171
|
|
172
|
@Override
|
173
|
public void afterMoveDocument(final DBBroker dbBroker, final Txn txn, final DocumentImpl document, final XmldbURI xmldbURI) throws TriggerException {
|
174
|
|
175
|
}
|
176
|
|
177
|
@Override
|
178
|
public void beforeDeleteDocument(final DBBroker dbBroker, final Txn txn, final DocumentImpl document) throws TriggerException {
|
179
|
|
180
|
try {
|
181
|
final Trigger trigger = getTrigger();
|
182
|
final String collection = document.getCollection().getURI().toString();
|
183
|
final String fileName = document.getFileURI().toString();
|
184
|
if (trigger != null) {
|
185
|
trigger.deleted(fileName, collection, IndentXmlString.apply(document));
|
186
|
}
|
187
|
} catch (final Exception e) {
|
188
|
throw new TriggerException(e);
|
189
|
}
|
190
|
}
|
191
|
|
192
|
@Override
|
193
|
public void afterDeleteDocument(final DBBroker dbBroker, final Txn txn, final XmldbURI xmldbURI) throws TriggerException {
|
194
|
|
195
|
}
|
196
|
|
197
|
}
|