Project

General

Profile

1
package eu.dnetlib.oai.utils;
2

    
3
import java.util.List;
4
import javax.annotation.Resource;
5

    
6
import eu.dnetlib.oai.conf.OAIConfigurationReader;
7
import eu.dnetlib.oai.info.SetInfo;
8
import eu.dnetlib.oai.mongo.MongoPublisherStore;
9
import eu.dnetlib.oai.mongo.MongoPublisherStoreDAO;
10
import eu.dnetlib.oai.sets.MongoSetCollection;
11
import eu.dnetlib.rmi.provision.MDFInfo;
12
import eu.dnetlib.rmi.provision.OaiPublisherRuntimeException;
13
import org.apache.commons.lang3.StringUtils;
14
import org.apache.commons.logging.Log;
15
import org.apache.commons.logging.LogFactory;
16

    
17
/**
18
 * Created by alessia on 18/01/17.
19
 */
20
public class OAIHelper {
21
	private static final Log log = LogFactory.getLog(OAIHelper.class); // NOPMD by marko on 11/24/08 5:02 PM
22

    
23
	@Resource(name = "oaiConfigurationExistReader")
24
	private OAIConfigurationReader configurationReader;
25
	@Resource
26
	private MongoPublisherStoreDAO mongoPublisherStoreDAO;
27
	@Resource
28
	private MongoSetCollection mongoSetCollection;
29

    
30
	public void loadConfiguration(final String dbName) {
31
		loadConfigurationSets(dbName);
32
	}
33

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

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

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

    
53
		updateTotalCount(store, mdFormat, dbName);
54

    
55
		updateProvenanceSetsCount(store, mdFormat, dbName);
56

    
57
		updateConfiguredSetsCount(store, mdFormat, dbName);
58
	}
59

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

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

    
66
		updateConfiguredSetsCount(store, mdFormat, dbName);
67
	}
68

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

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

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

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

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

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

    
112
		return store;
113
	}
114

    
115
	public OAIConfigurationReader getConfigurationReader() {
116
		return configurationReader;
117
	}
118

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

    
123
	public MongoPublisherStoreDAO getMongoPublisherStoreDAO() {
124
		return mongoPublisherStoreDAO;
125
	}
126

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

    
131
	public MongoSetCollection getMongoSetCollection() {
132
		return mongoSetCollection;
133
	}
134

    
135
	public void setMongoSetCollection(final MongoSetCollection mongoSetCollection) {
136
		this.mongoSetCollection = mongoSetCollection;
137
	}
138
}
(1-1/3)