Project

General

Profile

1
package eu.dnetlib.oai.actions;
2

    
3
import java.util.Arrays;
4

    
5
import javax.annotation.Resource;
6

    
7
import org.apache.commons.lang3.StringUtils;
8

    
9
import eu.dnetlib.enabling.tools.blackboard.BlackboardJob;
10
import eu.dnetlib.enabling.tools.blackboard.BlackboardServerHandler;
11
import eu.dnetlib.oai.mongo.MongoPublisherStore;
12
import eu.dnetlib.oai.mongo.MongoPublisherStoreDAO;
13
import eu.dnetlib.rmi.provision.OaiPublisherRuntimeException;
14

    
15
public class CreateOAIIndexAction extends AbstractOAIStoreAction {
16

    
17
	@Resource
18
	private MongoPublisherStoreDAO mongoPublisherStoreDAO;
19

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

    
45
	public MongoPublisherStoreDAO getMongoPublisherStoreDAO() {
46
		return this.mongoPublisherStoreDAO;
47
	}
48

    
49
	public void setMongoPublisherStoreDAO(final MongoPublisherStoreDAO mongoPublisherStoreDAO) {
50
		this.mongoPublisherStoreDAO = mongoPublisherStoreDAO;
51
	}
52

    
53
}
(3-3/9)