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