Project

General

Profile

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.debug("*****Dropping and re-creating configuration sets******");
34
		this.mongoSetCollection.dropConfigurationSets(dbName);
35
		final List<SetInfo> oaiConfigSets = this.configurationReader.getSets();
36
		for (final SetInfo setInfo : oaiConfigSets) {
37
			this.mongoSetCollection.upsertSet(setInfo, true, dbName);
38
		}
39
		log.info("Configuration Sets updated succesfully on db: " + dbName);
40
	}
41

    
42
	/*
43
	 * Helper method to count elements in OAI sets.
44
	 */
45
	public void updateAllCounts(final MDFInfo mdFormat, final String dbName) {
46
		this.mongoSetCollection.ensureIndexes(dbName);
47

    
48
		final MongoPublisherStore store = getStore(mdFormat, dbName);
49
		if (store == null) { throw new OaiPublisherRuntimeException("Can't count elements for not yet created store (" + mdFormat + ") on db " + dbName); }
50

    
51
		updateTotalCount(store, mdFormat, dbName);
52

    
53
		updateProvenanceSetsCount(store, mdFormat, dbName);
54

    
55
		updateConfiguredSetsCount(store, mdFormat, dbName);
56
	}
57

    
58
	public void updateConfiguredSetsCount(final MDFInfo mdFormat, final String dbName) {
59
		this.mongoSetCollection.ensureIndexes(dbName);
60

    
61
		final MongoPublisherStore store = getStore(mdFormat, dbName);
62
		if (store == null) { throw new OaiPublisherRuntimeException("Can't count elements for not yet created store (" + mdFormat + ") on db " + dbName); }
63

    
64
		updateConfiguredSetsCount(store, mdFormat, dbName);
65
	}
66

    
67
	protected void updateConfiguredSetsCount(final MongoPublisherStore store, final MDFInfo mdFormat, final String dbName) {
68
		final List<SetInfo> sets = this.mongoSetCollection.getConfiguredSets(dbName);
69
		this.updateCountForSets(store, sets, mdFormat, dbName);
70
	}
71

    
72
	protected void updateProvenanceSetsCount(final MongoPublisherStore store, final MDFInfo mdFormat, final String dbName) {
73
		// now we need to get all distinct set names in the store:
74
		final List<String> distinctSetSpecs = store.getDistinctSetNamesFromRecords();
75
		store.upsertSets(distinctSetSpecs);
76
		final List<SetInfo> sets = this.mongoSetCollection.getSetsFromData(dbName);
77
		this.updateCountForSets(store, sets, mdFormat, dbName);
78
	}
79

    
80
	protected void updateTotalCount(final MongoPublisherStore store, final MDFInfo mdFormat, final String dbName) {
81
		final String baseQuery = mdFormat.getBaseQuery();
82
		final int total = store.count(baseQuery);
83
		this.mongoSetCollection.updateCounts("ALL", mdFormat.getPrefix(), total, dbName);
84
		log.info("Got total for " + mdFormat.getPrefix() + " with query: " + baseQuery + " on db " + dbName);
85
	}
86

    
87
	protected void updateCountForSets(final MongoPublisherStore store, final List<SetInfo> oaiSets, final MDFInfo mdFormat, final String dbName) {
88

    
89
		final String baseQuery = mdFormat.getBaseQuery();
90
		final boolean hasBaseQuery = !StringUtils.isBlank(baseQuery);
91
		for (final SetInfo setInfo : oaiSets) {
92
			String setQuery = "(" + setInfo.getQuery() + ")";
93
			if (hasBaseQuery) {
94
				setQuery += " AND (" + baseQuery + ")";
95
			}
96
			log.info("Counting total for " + mdFormat.getPrefix() + " set " + setInfo + " with query: " + setQuery + " on db " + dbName);
97
			final int setCount = store.count(setQuery);
98
			this.mongoSetCollection.updateCounts(setInfo.getSetSpec(), mdFormat.getPrefix(), setCount, dbName);
99
		}
100
	}
101

    
102
	private MongoPublisherStore getStore(final MDFInfo mdFormat, final String dbName) {
103
		final String format = mdFormat.getSourceFormatName();
104
		final String layout = mdFormat.getSourceFormatLayout();
105
		final String interpretation = mdFormat.getSourceFormatInterpretation();
106
		final String sourceKey = format + "-" + layout + "-" + interpretation;
107
		final MongoPublisherStore store = this.mongoPublisherStoreDAO.getStore(format, interpretation, layout, dbName);
108
		log.info("Got OAI store " + sourceKey + " via metadata prefix " + mdFormat.getPrefix() + " on db " + dbName);
109

    
110
		return store;
111
	}
112

    
113
	public OAIConfigurationReader getConfigurationReader() {
114
		return this.configurationReader;
115
	}
116

    
117
	public void setConfigurationReader(final OAIConfigurationReader configurationReader) {
118
		this.configurationReader = configurationReader;
119
	}
120

    
121
	public MongoPublisherStoreDAO getMongoPublisherStoreDAO() {
122
		return this.mongoPublisherStoreDAO;
123
	}
124

    
125
	public void setMongoPublisherStoreDAO(final MongoPublisherStoreDAO mongoPublisherStoreDAO) {
126
		this.mongoPublisherStoreDAO = mongoPublisherStoreDAO;
127
	}
128

    
129
	public MongoSetCollection getMongoSetCollection() {
130
		return this.mongoSetCollection;
131
	}
132

    
133
	public void setMongoSetCollection(final MongoSetCollection mongoSetCollection) {
134
		this.mongoSetCollection = mongoSetCollection;
135
	}
136

    
137
}
(8-8/9)