1
|
package eu.dnetlib.oai.conf;
|
2
|
|
3
|
import java.util.List;
|
4
|
import javax.annotation.Resource;
|
5
|
|
6
|
import eu.dnetlib.data.information.oai.publisher.OaiPublisherRuntimeException;
|
7
|
import eu.dnetlib.data.information.oai.publisher.info.MDFInfo;
|
8
|
import eu.dnetlib.data.information.oai.publisher.info.SetInfo;
|
9
|
import eu.dnetlib.data.oai.store.mongo.MongoPublisherStore;
|
10
|
import eu.dnetlib.data.oai.store.mongo.MongoPublisherStoreDAO;
|
11
|
import eu.dnetlib.data.oai.store.sets.MongoSetCollection;
|
12
|
import org.apache.commons.lang.StringUtils;
|
13
|
import org.apache.commons.logging.Log;
|
14
|
import org.apache.commons.logging.LogFactory;
|
15
|
|
16
|
public class OAISetHelper {
|
17
|
|
18
|
private static final Log log = LogFactory.getLog(OAISetHelper.class); // NOPMD by marko on 11/24/08 5:02 PM
|
19
|
|
20
|
@Resource(name = "oaiConfigurationExistReader")
|
21
|
private OAIConfigurationReader configurationReader;
|
22
|
@Resource
|
23
|
private MongoPublisherStoreDAO mongoPublisherStoreDAO;
|
24
|
@Resource
|
25
|
private MongoSetCollection mongoSetCollection;
|
26
|
|
27
|
public void loadConfiguration(final String dbName) {
|
28
|
loadConfigurationSets(dbName);
|
29
|
}
|
30
|
|
31
|
private void loadConfigurationSets(final String dbName) {
|
32
|
log.debug("*****Dropping and re-creating configuration sets******");
|
33
|
this.mongoSetCollection.dropConfigurationSets(dbName);
|
34
|
List<SetInfo> oaiConfigSets = configurationReader.getSets();
|
35
|
for (SetInfo setInfo : oaiConfigSets) {
|
36
|
this.mongoSetCollection.upsertSet(setInfo, true, dbName);
|
37
|
}
|
38
|
log.info("Configuration Sets updated succesfully on db: " + dbName);
|
39
|
}
|
40
|
|
41
|
/*
|
42
|
* Helper method to count elements in OAI sets.
|
43
|
*/
|
44
|
public void updateAllCounts(final MDFInfo mdFormat, final String dbName) {
|
45
|
this.mongoSetCollection.ensureIndexes(dbName);
|
46
|
|
47
|
MongoPublisherStore store = getStore(mdFormat, dbName);
|
48
|
if (store == null) throw new OaiPublisherRuntimeException("Can't count elements for not yet created store (" + mdFormat + ") on db " + dbName);
|
49
|
|
50
|
updateTotalCount(store, mdFormat, dbName);
|
51
|
|
52
|
updateProvenanceSetsCount(store, mdFormat, dbName);
|
53
|
|
54
|
updateConfiguredSetsCount(store, mdFormat, dbName);
|
55
|
}
|
56
|
|
57
|
public void updateConfiguredSetsCount(final MDFInfo mdFormat, final String dbName) {
|
58
|
this.mongoSetCollection.ensureIndexes(dbName);
|
59
|
|
60
|
MongoPublisherStore store = getStore(mdFormat, dbName);
|
61
|
if (store == null) throw new OaiPublisherRuntimeException("Can't count elements for not yet created store (" + mdFormat + ") on db " + dbName);
|
62
|
|
63
|
updateConfiguredSetsCount(store, mdFormat, dbName);
|
64
|
}
|
65
|
|
66
|
protected void updateConfiguredSetsCount(final MongoPublisherStore store, final MDFInfo mdFormat, final String dbName) {
|
67
|
List<SetInfo> sets = mongoSetCollection.getConfiguredSets(dbName);
|
68
|
this.updateCountForSets(store, sets, mdFormat, dbName);
|
69
|
}
|
70
|
|
71
|
protected void updateProvenanceSetsCount(final MongoPublisherStore store, final MDFInfo mdFormat, final String dbName) {
|
72
|
// now we need to get all distinct set names in the store:
|
73
|
List<String> distinctSetSpecs = store.getDistinctSetNamesFromRecords();
|
74
|
store.upsertSets(distinctSetSpecs);
|
75
|
List<SetInfo> sets = mongoSetCollection.getSetsFromData(dbName);
|
76
|
this.updateCountForSets(store, sets, mdFormat, dbName);
|
77
|
}
|
78
|
|
79
|
protected void updateTotalCount(final MongoPublisherStore store, final MDFInfo mdFormat, final String dbName) {
|
80
|
String baseQuery = mdFormat.getBaseQuery();
|
81
|
int total = store.count(baseQuery);
|
82
|
mongoSetCollection.updateCounts("ALL", mdFormat.getPrefix(), total, dbName);
|
83
|
log.info("Got total for " + mdFormat.getPrefix() + " with query: " + baseQuery + " on db " + dbName);
|
84
|
}
|
85
|
|
86
|
protected void updateCountForSets(final MongoPublisherStore store, final List<SetInfo> oaiSets, final MDFInfo mdFormat, final String dbName) {
|
87
|
|
88
|
String baseQuery = mdFormat.getBaseQuery();
|
89
|
boolean hasBaseQuery = !StringUtils.isBlank(baseQuery);
|
90
|
for (SetInfo setInfo : oaiSets) {
|
91
|
String setQuery = "(" + setInfo.getQuery() + ")";
|
92
|
if (hasBaseQuery) {
|
93
|
setQuery += " AND (" + baseQuery + ")";
|
94
|
}
|
95
|
log.info("Counting total for " + mdFormat.getPrefix() + " set " + setInfo + " with query: " + setQuery + " on db " + dbName);
|
96
|
int setCount = store.count(setQuery);
|
97
|
mongoSetCollection.updateCounts(setInfo.getSetSpec(), mdFormat.getPrefix(), setCount, dbName);
|
98
|
}
|
99
|
}
|
100
|
|
101
|
private MongoPublisherStore getStore(final MDFInfo mdFormat, final String dbName) {
|
102
|
String format = mdFormat.getSourceFormatName();
|
103
|
String layout = mdFormat.getSourceFormatLayout();
|
104
|
String interpretation = mdFormat.getSourceFormatInterpretation();
|
105
|
String sourceKey = format + "-" + layout + "-" + interpretation;
|
106
|
MongoPublisherStore store = this.mongoPublisherStoreDAO.getStore(format, interpretation, layout, dbName);
|
107
|
log.info("Got OAI store " + sourceKey + " via metadata prefix " + mdFormat.getPrefix() + " on db " + dbName);
|
108
|
|
109
|
return store;
|
110
|
}
|
111
|
|
112
|
public OAIConfigurationReader getConfigurationReader() {
|
113
|
return configurationReader;
|
114
|
}
|
115
|
|
116
|
public void setConfigurationReader(final OAIConfigurationReader configurationReader) {
|
117
|
this.configurationReader = configurationReader;
|
118
|
}
|
119
|
|
120
|
public MongoPublisherStoreDAO getMongoPublisherStoreDAO() {
|
121
|
return mongoPublisherStoreDAO;
|
122
|
}
|
123
|
|
124
|
public void setMongoPublisherStoreDAO(final MongoPublisherStoreDAO mongoPublisherStoreDAO) {
|
125
|
this.mongoPublisherStoreDAO = mongoPublisherStoreDAO;
|
126
|
}
|
127
|
|
128
|
public MongoSetCollection getMongoSetCollection() {
|
129
|
return mongoSetCollection;
|
130
|
}
|
131
|
|
132
|
public void setMongoSetCollection(final MongoSetCollection mongoSetCollection) {
|
133
|
this.mongoSetCollection = mongoSetCollection;
|
134
|
}
|
135
|
|
136
|
}
|