forked from DSpace/DSpace
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOAIIndexEventConsumer.java
168 lines (147 loc) · 5.91 KB
/
OAIIndexEventConsumer.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
159
160
161
162
163
164
165
166
167
168
/**
* The contents of this file are subject to the license and copyright
* detailed in the LICENSE and NOTICE files at the root of the source
* tree and available online at
*
* http://www.dspace.org/license/
*/
package org.dspace.event;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Objects;
import java.util.Set;
import org.apache.log4j.Logger;
import org.dspace.content.Bitstream;
import org.dspace.content.Bundle;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.ItemService;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.xoai.app.BasicConfiguration;
import org.dspace.xoai.app.XOAI;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* The OAIIndexEventConsumer determining which items need to be indexed or updated based on the event type and subject.
* It listens for changes to items, collections, communities,
* bundles, and bitstreams, and updates the OAI index accordingly.
* The indexing is done using the XOAI indexer after all relevant items are collected.
*/
public class OAIIndexEventConsumer implements Consumer {
/**
* log4j logger
*/
private static Logger log = Logger.getLogger(OAIIndexEventConsumer.class);
ItemService itemService = ContentServiceFactory.getInstance().getItemService();
// collect Items, Collections, Communities that need indexing
private Set<Item> itemsToUpdate = null;
public void initialize() throws Exception {
// No-op
}
/**
* Consume a content event -- just build the sets of objects to add (new) to
* the index, update, and delete.
*
* @param ctx DSpace context
* @param event Content event
*/
public void consume(Context ctx, Event event) throws Exception {
if (itemsToUpdate == null) {
itemsToUpdate = new HashSet<Item>();
}
int st = event.getSubjectType();
if (!(st == Constants.ITEM || st == Constants.BUNDLE
|| st == Constants.COLLECTION || st == Constants.COMMUNITY || st == Constants.BITSTREAM)) {
log
.warn("IndexConsumer should not have been given this kind of Subject in an event, skipping: "
+ event.toString());
return;
}
DSpaceObject subject = event.getSubject(ctx);
DSpaceObject object = event.getObject(ctx);
int et = event.getEventType();
if (object != null && event.getObjectType() == Constants.ITEM) {
//just update the object
itemsToUpdate.add((Item)object);
return;
}
if (Objects.isNull(subject)) {
return;
}
if (event.getSubjectType() == Constants.COLLECTION || event.getSubjectType() == Constants.COMMUNITY) {
if (et == Event.MODIFY || et == Event.MODIFY_METADATA || et == Event.REMOVE || et == Event.DELETE) {
//must update all the items
if (subject.getType() == Constants.COMMUNITY) {
for (Collection col : ((Community)subject).getCollections()) {
addAll(ctx, col);
}
} else {
addAll(ctx, (Collection)subject);
}
}
} else if (event.getSubjectType() == Constants.BITSTREAM || event.getSubjectType() == Constants.BUNDLE) {
//must update owning items regardless the event
if (subject.getType() == Constants.BITSTREAM) {
for (Bundle bun : ((Bitstream)subject).getBundles()) {
itemsToUpdate.addAll(bun.getItems());
}
} else {
itemsToUpdate.addAll(((Bundle)subject).getItems());
}
} else if (event.getSubjectType() == Constants.ITEM) {
//any event reindex this item
itemsToUpdate.add((Item)subject);
}
}
private void addAll(Context context, Collection col) throws SQLException {
Iterator<Item> i = itemService.findByCollection(context, col);
while (i.hasNext()) {
itemsToUpdate.add(i.next());
}
}
/**
* Process sets of objects to add, update, and delete in index. Correct for
* interactions between the sets -- e.g. objects which were deleted do not
* need to be added or updated, new objects don't also need an update, etc.
*/
public void end(Context ctx) throws Exception {
Context anonymousContext = null;
try {
if (Objects.isNull(itemsToUpdate)) {
return;
}
Set<Item> filtered = new HashSet<Item>(itemsToUpdate.size());
for (Item item : itemsToUpdate) {
if (item.getHandle() == null) {
// probably submission item, skip
continue;
}
filtered.add(item);
}
// "free" the resources
itemsToUpdate = null;
anonymousContext = new Context();
XOAI indexer = new XOAI(anonymousContext, false, false);
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(
new Class[] { BasicConfiguration.class });
applicationContext.getAutowireCapableBeanFactory()
.autowireBean(indexer);
indexer.indexItems(filtered);
applicationContext.close();
} catch (Exception e) {
itemsToUpdate = null;
throw e;
} finally {
if (anonymousContext != null) {
anonymousContext.complete();
}
}
}
public void finish(Context ctx) throws Exception {
// No-op
}
}