Project

General

Profile

1
package eu.dnetlib.oai.actions;
2

    
3
import java.util.Arrays;
4

    
5
import eu.dnetlib.enabling.tools.blackboard.BlackboardJob;
6
import eu.dnetlib.enabling.tools.blackboard.BlackboardServerHandler;
7
import eu.dnetlib.oai.mongo.MongoPublisherStore;
8
import eu.dnetlib.oai.mongo.MongoPublisherStoreDAO;
9
import eu.dnetlib.oai.utils.OAIParameterNames;
10
import eu.dnetlib.rmi.provision.OaiPublisherRuntimeException;
11
import org.apache.commons.lang3.StringUtils;
12
import org.springframework.beans.factory.annotation.Autowired;
13

    
14
/**
15
 * Create the indices on fields specified in the job parameter 'oai_index_fieldNames'.
16
 * Example: field1,field2 -- one compound index on the given two fields
17
 * field1,field2;field3,field4 -- two compound indices: one on field1 and field2, the other on field3 and field4
18
 * field5;field6 -- two indices: one on field5, one on field6
19
 *
20
 * @author alessia
21
 */
22
public class CreateOAIIndexAction extends AbstractOAIStoreAction {
23

    
24
	@Autowired
25
	private MongoPublisherStoreDAO mongoPublisherStoreDAO;
26

    
27
	@Override
28
	public void execute(final BlackboardServerHandler handler, final BlackboardJob job) throws Exception {
29
		String storeId = job.getParameters().get(OAIParameterNames.OAI_COLLECTON);
30
		String dbName = job.getParameters().get(OAIParameterNames.OAI_DB);
31
		// Examples: field1,field2 -- one compound index on the given two fields
32
		// field1,field2;field3,field4 -- two compound indices: one on field1 and field2, the other on field3 and field4
33
		String fieldNames = job.getParameters().get(OAIParameterNames.OAI_INDEXES);
34
		if (StringUtils.isBlank(dbName) || StringUtils.isBlank(storeId) || StringUtils.isBlank(fieldNames))
35
			throw new IllegalArgumentException(String.format(
36
					"Job parameters %s, %s and %s are mandatory", OAIParameterNames.OAI_DB, OAIParameterNames.OAI_COLLECTON, OAIParameterNames.OAI_INDEXES));
37
		else {
38
			MongoPublisherStore store = this.mongoPublisherStoreDAO.getStore(storeId, dbName);
39
			if (store != null) {
40
				String[] indexFieldList = fieldNames.replaceAll(" ", "").split(";");
41
				for (String idx : indexFieldList) {
42
					String[] fields = idx.split(",");
43
					store.createCompoundIndex(Arrays.asList(fields));
44
				}
45
				handler.done(job);
46
			} else throw new OaiPublisherRuntimeException("store " + storeId + " does not exist on db " + dbName + ": can't create compound indices");
47
		}
48
	}
49

    
50
	public MongoPublisherStoreDAO getMongoPublisherStoreDAO() {
51
		return this.mongoPublisherStoreDAO;
52
	}
53

    
54
	public void setMongoPublisherStoreDAO(final MongoPublisherStoreDAO mongoPublisherStoreDAO) {
55
		this.mongoPublisherStoreDAO = mongoPublisherStoreDAO;
56
	}
57

    
58
}
(3-3/8)