Project

General

Profile

1
package eu.dnetlib.oai.actions;
2

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

    
6
import eu.dnetlib.data.information.oai.publisher.OaiPublisherRuntimeException;
7
import eu.dnetlib.data.oai.store.mongo.MongoPublisherStore;
8
import eu.dnetlib.data.oai.store.mongo.MongoPublisherStoreDAO;
9
import eu.dnetlib.enabling.tools.blackboard.BlackboardJob;
10
import eu.dnetlib.enabling.tools.blackboard.BlackboardServerHandler;
11
import org.apache.commons.lang.StringUtils;
12

    
13
public class CreateOAIIndexAction extends AbstractOAIStoreAction {
14

    
15
	@Resource
16
	private MongoPublisherStoreDAO mongoPublisherStoreDAO;
17

    
18
	@Override
19
	public void execute(final BlackboardServerHandler handler, final BlackboardJob job) throws Exception {
20
		String storeId = job.getParameters().get("collection");
21
		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
		String fieldNames = job.getParameters().get("oai_index_fieldNames");
25
		if (StringUtils.isBlank(storeId) || StringUtils.isBlank(fieldNames)) throw new IllegalArgumentException(
26
				"Job parameters collection and oai_index_fieldNames are mandatory");
27
		else {
28
			MongoPublisherStore store = this.mongoPublisherStoreDAO.getStore(storeId, dbName);
29
			if (store != null) {
30
				String[] indexFieldList = fieldNames.replaceAll(" ", "").split(";");
31
				for (String idx : indexFieldList) {
32
					String[] fields = idx.split(",");
33
					store.createCompoundIndex(Arrays.asList(fields));
34
				}
35
				handler.done(job);
36
			} else throw new OaiPublisherRuntimeException("store " + storeId + " does not exist on db " + dbName + ": can't create compound indices");
37
		}
38
	}
39

    
40
	public MongoPublisherStoreDAO getMongoPublisherStoreDAO() {
41
		return mongoPublisherStoreDAO;
42
	}
43

    
44
	public void setMongoPublisherStoreDAO(final MongoPublisherStoreDAO mongoPublisherStoreDAO) {
45
		this.mongoPublisherStoreDAO = mongoPublisherStoreDAO;
46
	}
47

    
48
}
(3-3/9)