1
|
package eu.dnetlib.oai.conf;
|
2
|
|
3
|
import java.util.List;
|
4
|
import java.util.concurrent.Callable;
|
5
|
|
6
|
import javax.annotation.Resource;
|
7
|
|
8
|
import org.apache.commons.lang3.StringUtils;
|
9
|
import org.apache.commons.logging.Log;
|
10
|
import org.apache.commons.logging.LogFactory;
|
11
|
|
12
|
import com.google.common.collect.Lists;
|
13
|
|
14
|
import eu.dnetlib.oai.sets.MongoSetCollection;
|
15
|
import eu.dnetlib.rmi.provision.MDFInfo;
|
16
|
|
17
|
/**
|
18
|
* Updates the counts for all sets in the special sets collection of OAI store.
|
19
|
*
|
20
|
* @author alessia
|
21
|
*/
|
22
|
public class OAISetsCounter {
|
23
|
|
24
|
private static final Log log = LogFactory.getLog(OAISetsCounter.class); // NOPMD by marko on 11/24/08 5:02 PM
|
25
|
|
26
|
@Resource(name = "oaiConfigurationExistReader")
|
27
|
private OAIConfigurationReader configurationReader;
|
28
|
@Resource
|
29
|
private MongoSetCollection mongoSetCollection;
|
30
|
@Resource
|
31
|
private OAISetHelper oaiSetHelper;
|
32
|
|
33
|
/**
|
34
|
* Update counts for all OAI sets.
|
35
|
* <p>
|
36
|
* If a non blank storeId is given, counts are updated only for those mdPrefix served by the given store.
|
37
|
* </p>
|
38
|
* <p>
|
39
|
* Otherwise all counts are updated.
|
40
|
* </p>
|
41
|
*
|
42
|
* @param storeId
|
43
|
* oai store id. Can be blank to execute over all metadata served by OAI.
|
44
|
* @param callback
|
45
|
* callback to execute when the execution is successful
|
46
|
* @param failCallbackcallback
|
47
|
* to execute when the execution fails
|
48
|
*/
|
49
|
public void updateCounts(final String storeId, final String dbName, final Callable<?> callback, final Callable<?> failCallback) {
|
50
|
new Thread() {
|
51
|
|
52
|
@Override
|
53
|
public void run() {
|
54
|
try {
|
55
|
final List<MDFInfo> metadataFormats = listProcessableMDFInfo(storeId);
|
56
|
for (final MDFInfo mdFormat : metadataFormats) {
|
57
|
OAISetsCounter.this.oaiSetHelper.updateAllCounts(mdFormat, dbName);
|
58
|
}
|
59
|
log.info("All sets count updated succesfully on db: " + dbName);
|
60
|
executeCallback(callback);
|
61
|
} catch (final Exception e) {
|
62
|
log.error(e);
|
63
|
executeCallback(failCallback);
|
64
|
}
|
65
|
}
|
66
|
}.start();
|
67
|
}
|
68
|
|
69
|
public void updateCounts(final MDFInfo mdf, final String dbName, final Callable<?> callback, final Callable<?> failCallback) {
|
70
|
new Thread() {
|
71
|
|
72
|
@Override
|
73
|
public void run() {
|
74
|
try {
|
75
|
OAISetsCounter.this.oaiSetHelper.updateAllCounts(mdf, dbName);
|
76
|
log.info("All sets count updated succesfully for metadata format " + mdf + " on db " + dbName);
|
77
|
executeCallback(callback);
|
78
|
} catch (final Exception e) {
|
79
|
log.error(e);
|
80
|
executeCallback(failCallback);
|
81
|
}
|
82
|
}
|
83
|
}.start();
|
84
|
}
|
85
|
|
86
|
/**
|
87
|
* Update counts for configured OAI sets and all md formats.
|
88
|
*
|
89
|
* @param callback
|
90
|
* callback to execute when the execution is successful
|
91
|
* @param failCallbackcallback
|
92
|
* to execute when the execution fails
|
93
|
*/
|
94
|
public void updateConfigurationCounts(final String storeId, final String dbName, final Callable<?> callback, final Callable<?> failCallback) {
|
95
|
new Thread() {
|
96
|
|
97
|
@Override
|
98
|
public void run() {
|
99
|
try {
|
100
|
final List<MDFInfo> metadataFormats = listProcessableMDFInfo(storeId);
|
101
|
|
102
|
for (final MDFInfo mdFormat : metadataFormats) {
|
103
|
OAISetsCounter.this.oaiSetHelper.updateConfiguredSetsCount(mdFormat, dbName);
|
104
|
}
|
105
|
log.info("All configured sets count updated succesfully on db " + dbName);
|
106
|
executeCallback(callback);
|
107
|
} catch (final Exception e) {
|
108
|
log.error(e);
|
109
|
executeCallback(failCallback);
|
110
|
}
|
111
|
}
|
112
|
}.start();
|
113
|
}
|
114
|
|
115
|
protected void executeCallback(final Callable<?> callback) {
|
116
|
if (callback != null) {
|
117
|
try {
|
118
|
callback.call();
|
119
|
} catch (final Exception e) {
|
120
|
log.error("Error executing callback", e);
|
121
|
}
|
122
|
}
|
123
|
}
|
124
|
|
125
|
protected List<MDFInfo> listProcessableMDFInfo(final String storeId) {
|
126
|
List<MDFInfo> metadataFormats = Lists.newArrayList();
|
127
|
if (StringUtils.isBlank(storeId)) {
|
128
|
metadataFormats = this.configurationReader.getMetadataFormatInfo();
|
129
|
} else {
|
130
|
final String[] splitted = storeId.split("-");
|
131
|
final String format = splitted[0];
|
132
|
final String layout = splitted[1];
|
133
|
final String inter = splitted[2];
|
134
|
metadataFormats = this.configurationReader.getFormatsServedBy(format, layout, inter);
|
135
|
}
|
136
|
return metadataFormats;
|
137
|
}
|
138
|
|
139
|
public OAIConfigurationReader getConfigurationReader() {
|
140
|
return this.configurationReader;
|
141
|
}
|
142
|
|
143
|
public void setConfigurationReader(final OAIConfigurationReader configurationReader) {
|
144
|
this.configurationReader = configurationReader;
|
145
|
}
|
146
|
|
147
|
public MongoSetCollection getMongoSetCollection() {
|
148
|
return this.mongoSetCollection;
|
149
|
}
|
150
|
|
151
|
public void setMongoSetCollection(final MongoSetCollection mongoSetCollection) {
|
152
|
this.mongoSetCollection = mongoSetCollection;
|
153
|
}
|
154
|
|
155
|
public OAISetHelper getOaiSetCounterHelper() {
|
156
|
return this.oaiSetHelper;
|
157
|
}
|
158
|
|
159
|
public void setOaiSetCounterHelper(final OAISetHelper oaiSetHelper) {
|
160
|
this.oaiSetHelper = oaiSetHelper;
|
161
|
}
|
162
|
|
163
|
}
|