Revision 44354
Added by Michele Artini over 7 years ago
DelegatingDiffTrigger.java | ||
---|---|---|
4 | 4 |
import java.util.List; |
5 | 5 |
import java.util.Map; |
6 | 6 |
|
7 |
import eu.dnetlib.miscutils.functional.xml.XMLIndenter; |
|
8 |
import eu.dnetlib.xml.database.Trigger; |
|
9 | 7 |
import org.apache.commons.logging.Log; |
10 | 8 |
import org.apache.commons.logging.LogFactory; |
11 | 9 |
import org.exist.collections.Collection; |
... | ... | |
16 | 14 |
import org.exist.storage.txn.Txn; |
17 | 15 |
import org.exist.xmldb.XmldbURI; |
18 | 16 |
|
17 |
import eu.dnetlib.miscutils.functional.xml.XMLIndenter; |
|
18 |
import eu.dnetlib.xml.database.Trigger; |
|
19 |
|
|
19 | 20 |
/** |
20 | 21 |
* this trigger delegates diffs for CRUDE events to an non-exist dependent eu.dnetlib.xml.database.Trigger instance. |
21 |
* |
|
22 |
* Since eXist triggers are instantiated by eXist, we need to locate the Trigger instances which are registered to the |
|
23 |
* database instance. |
|
24 |
* |
|
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 | 25 |
* @author marko |
26 |
*
|
|
26 |
* |
|
27 | 27 |
*/ |
28 | 28 |
public class DelegatingDiffTrigger extends SAXTrigger { |
29 |
|
|
29 | 30 |
/** |
30 | 31 |
* logger. |
31 | 32 |
*/ |
... | ... | |
36 | 37 |
*/ |
37 | 38 |
private String triggerName; |
38 | 39 |
|
39 |
private Map<String, ExistsTriggerEvent> existsTriggerEventMap= new HashMap<>(); |
|
40 |
private Map<String, ExistsTriggerEvent> existsTriggerEventMap = new HashMap<>();
|
|
40 | 41 |
|
41 | 42 |
private XMLIndenter indenter = new XMLIndenter(); |
42 | 43 |
|
43 | 44 |
/** |
44 | 45 |
* {@inheritDoc} |
45 |
*
|
|
46 |
* @see org.exist.collections.triggers.FilteringTrigger#configure(org.exist.storage.DBBroker, |
|
47 |
* org.exist.collections.Collection, java.util.Map)
|
|
46 |
* |
|
47 |
* @see org.exist.collections.triggers.FilteringTrigger#configure(org.exist.storage.DBBroker, org.exist.collections.Collection,
|
|
48 |
* java.util.Map) |
|
48 | 49 |
*/ |
49 | 50 |
@SuppressWarnings("unchecked") |
50 | 51 |
@Override |
51 |
public void configure(DBBroker dbBroker, Collection parent, Map<String, List<?>> parameters) throws TriggerException {
|
|
52 |
public void configure(final DBBroker dbBroker, final Collection parent, final Map<String, List<?>> parameters) throws TriggerException {
|
|
52 | 53 |
super.configure(dbBroker, parent, parameters); |
53 | 54 |
|
54 |
if (parameters == null) |
|
55 |
return; |
|
55 |
if (parameters == null) { return; } |
|
56 | 56 |
|
57 | 57 |
final Map<String, List<?>> params = parameters; |
58 |
if(parameters.containsKey("triggerName")){
|
|
59 |
final List<?> myTriggerName = parameters.get("triggerName");
|
|
60 |
if(myTriggerName!= null && myTriggerName.size()==1){
|
|
58 |
if (parameters.containsKey("triggerName")) {
|
|
59 |
final List<?> myTriggerName = parameters.get("triggerName"); |
|
60 |
if ((myTriggerName != null) && (myTriggerName.size() == 1)) {
|
|
61 | 61 |
setTriggerName(myTriggerName.get(0).toString()); |
62 | 62 |
} |
63 | 63 |
} |
... | ... | |
69 | 69 |
|
70 | 70 |
/** |
71 | 71 |
* find the trigger instance associated with this delegation. |
72 |
*
|
|
72 |
* |
|
73 | 73 |
* @return trigger instance |
74 | 74 |
*/ |
75 | 75 |
protected Trigger getTrigger() { |
... | ... | |
87 | 87 |
} |
88 | 88 |
|
89 | 89 |
public void setTriggerName(final String triggerId) { |
90 |
this.triggerName = triggerId;
|
|
90 |
triggerName = triggerId; |
|
91 | 91 |
} |
92 | 92 |
|
93 |
|
|
94 |
|
|
95 | 93 |
@Override |
96 | 94 |
public void beforeCreateDocument(final DBBroker dbBroker, final Txn txn, final XmldbURI xmldbURI) throws TriggerException { |
97 | 95 |
|
... | ... | |
104 | 102 |
final Trigger trigger = getTrigger(); |
105 | 103 |
final String collection = document.getCollection().getURI().toString(); |
106 | 104 |
final String fileName = document.getFileURI().toString(); |
107 |
if (trigger != null) |
|
105 |
if (trigger != null) {
|
|
108 | 106 |
trigger.created(fileName, collection, document); |
109 |
} catch (Exception e) { |
|
107 |
} |
|
108 |
} catch (final Exception e) { |
|
110 | 109 |
throw new TriggerException(e); |
111 | 110 |
} |
112 | 111 |
} |
... | ... | |
123 | 122 |
event.setCollection(collection); |
124 | 123 |
event.setName(fileName); |
125 | 124 |
existsTriggerEventMap.put(String.valueOf(txn.getId()), event); |
126 |
|
|
127 |
} catch (Exception e) { |
|
128 |
throw new TriggerException((e)); |
|
125 |
} catch (final Exception e) { |
|
126 |
throw new TriggerException(e); |
|
129 | 127 |
} |
130 | 128 |
} |
131 | 129 |
|
... | ... | |
133 | 131 |
public void afterUpdateDocument(final DBBroker dbBroker, final Txn txn, final DocumentImpl document) throws TriggerException { |
134 | 132 |
|
135 | 133 |
try { |
136 |
if (!existsTriggerEventMap.containsKey(String.valueOf(txn.getId()))) { |
|
137 |
throw new TriggerException("Error on trigger missing previous operation beforeUpdateTrigger"); |
|
138 |
} |
|
134 |
if (!existsTriggerEventMap.containsKey( |
|
135 |
String.valueOf(txn.getId()))) { throw new TriggerException("Error on trigger missing previous operation beforeUpdateTrigger"); } |
|
139 | 136 |
final ExistsTriggerEvent existsTriggerEvent = existsTriggerEventMap.remove(String.valueOf(txn.getId())); |
140 | 137 |
|
141 | 138 |
existsTriggerEvent.setDocument(indenter.parseXmlString(XMLIndenter.indent(document))); |
142 | 139 |
final Trigger trigger = getTrigger(); |
143 |
if (trigger != null) |
|
140 |
if (trigger != null) {
|
|
144 | 141 |
trigger.updated(existsTriggerEvent.getName(), existsTriggerEvent.getCollection(), existsTriggerEvent.getOldDocument(), |
145 | 142 |
existsTriggerEvent.getDocument()); |
146 |
} catch (Exception e) { |
|
147 |
throw new TriggerException((e)); |
|
143 |
} |
|
144 |
} catch (final Exception e) { |
|
145 |
throw new TriggerException(e); |
|
148 | 146 |
} |
149 | 147 |
} |
150 | 148 |
|
... | ... | |
185 | 183 |
final Trigger trigger = getTrigger(); |
186 | 184 |
final String collection = document.getCollection().getURI().toString(); |
187 | 185 |
final String fileName = document.getFileURI().toString(); |
188 |
if (trigger != null) |
|
186 |
if (trigger != null) {
|
|
189 | 187 |
trigger.deleted(fileName, collection, indenter.parseXmlString(XMLIndenter.indent(document))); |
190 |
} catch (Exception e) { |
|
188 |
} |
|
189 |
} catch (final Exception e) { |
|
191 | 190 |
throw new TriggerException(e); |
192 | 191 |
} |
193 | 192 |
} |
Also available in: Unified diff
fix