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.rmi.provision.OaiPublisherRuntimeException;
10
import org.apache.commons.lang3.StringUtils;
11
import org.springframework.beans.factory.annotation.Autowired;
12

    
13
public class CreateOAIIndexAction extends AbstractOAIStoreAction {
14

    
15
	@Autowired
16
	private MongoPublisherStoreDAO mongoPublisherStoreDAO;
17

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

    
43
	public MongoPublisherStoreDAO getMongoPublisherStoreDAO() {
44
		return this.mongoPublisherStoreDAO;
45
	}
46

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

    
51
}
(3-3/8)