1
|
package eu.dnetlib.oai.conf;
|
2
|
|
3
|
import java.util.List;
|
4
|
import javax.annotation.Resource;
|
5
|
|
6
|
import eu.dnetlib.oai.info.SetInfo;
|
7
|
import eu.dnetlib.oai.mongo.MongoPublisherStore;
|
8
|
import eu.dnetlib.oai.mongo.MongoPublisherStoreDAO;
|
9
|
import eu.dnetlib.oai.sets.MongoSetCollection;
|
10
|
import eu.dnetlib.rmi.provision.MDFInfo;
|
11
|
import eu.dnetlib.rmi.provision.OaiPublisherRuntimeException;
|
12
|
import org.apache.commons.lang3.StringUtils;
|
13
|
import org.apache.commons.logging.Log;
|
14
|
import org.apache.commons.logging.LogFactory;
|
15
|
import org.springframework.beans.factory.annotation.Autowired;
|
16
|
|
17
|
public class OAISetHelper {
|
18
|
|
19
|
private static final Log log = LogFactory.getLog(OAISetHelper.class); // NOPMD by marko on 11/24/08 5:02 PM
|
20
|
|
21
|
@Resource(name = "oaiConfigurationExistReader")
|
22
|
private OAIConfigurationReader configurationReader;
|
23
|
@Autowired
|
24
|
private MongoPublisherStoreDAO mongoPublisherStoreDAO;
|
25
|
@Autowired
|
26
|
private MongoSetCollection mongoSetCollection;
|
27
|
|
28
|
public void loadConfiguration(final String dbName) {
|
29
|
loadConfigurationSets(dbName);
|
30
|
}
|
31
|
|
32
|
private void loadConfigurationSets(final String dbName) {
|
33
|
log.info("***** Updating configuration sets ******");
|
34
|
final List<SetInfo> oaiConfigSets = this.configurationReader.getSets();
|
35
|
for (final 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
|
final 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
|
final 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
|
final List<SetInfo> sets = this.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
|
final List<String> distinctSetSpecs = store.getDistinctSetNamesFromRecords();
|
74
|
store.upsertSets(distinctSetSpecs);
|
75
|
final List<SetInfo> sets = this.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
|
final String baseQuery = mdFormat.getBaseQuery();
|
81
|
final int total = store.count(baseQuery);
|
82
|
this.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
|
final String baseQuery = mdFormat.getBaseQuery();
|
89
|
final boolean hasBaseQuery = !StringUtils.isBlank(baseQuery);
|
90
|
for (final 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
|
final int setCount = store.count(setQuery);
|
97
|
this.mongoSetCollection.updateCounts(setInfo.getSetSpec(), mdFormat.getPrefix(), setCount, dbName);
|
98
|
}
|
99
|
}
|
100
|
|
101
|
public void updateSetCount(final MDFInfo mdFormat, final String dbName, final String set) {
|
102
|
|
103
|
}
|
104
|
|
105
|
private MongoPublisherStore getStore(final MDFInfo mdFormat, final String dbName) {
|
106
|
final String format = mdFormat.getSourceFormatName();
|
107
|
final String layout = mdFormat.getSourceFormatLayout();
|
108
|
final String interpretation = mdFormat.getSourceFormatInterpretation();
|
109
|
final String sourceKey = format + "-" + layout + "-" + interpretation;
|
110
|
final MongoPublisherStore store = this.mongoPublisherStoreDAO.getStore(format, interpretation, layout, dbName);
|
111
|
log.info("Got OAI store " + sourceKey + " via metadata prefix " + mdFormat.getPrefix() + " on db " + dbName);
|
112
|
|
113
|
return store;
|
114
|
}
|
115
|
|
116
|
public OAIConfigurationReader getConfigurationReader() {
|
117
|
return this.configurationReader;
|
118
|
}
|
119
|
|
120
|
public void setConfigurationReader(final OAIConfigurationReader configurationReader) {
|
121
|
this.configurationReader = configurationReader;
|
122
|
}
|
123
|
|
124
|
public MongoPublisherStoreDAO getMongoPublisherStoreDAO() {
|
125
|
return this.mongoPublisherStoreDAO;
|
126
|
}
|
127
|
|
128
|
public void setMongoPublisherStoreDAO(final MongoPublisherStoreDAO mongoPublisherStoreDAO) {
|
129
|
this.mongoPublisherStoreDAO = mongoPublisherStoreDAO;
|
130
|
}
|
131
|
|
132
|
public MongoSetCollection getMongoSetCollection() {
|
133
|
return this.mongoSetCollection;
|
134
|
}
|
135
|
|
136
|
public void setMongoSetCollection(final MongoSetCollection mongoSetCollection) {
|
137
|
this.mongoSetCollection = mongoSetCollection;
|
138
|
}
|
139
|
|
140
|
|
141
|
}
|