Project

General

Profile

1
package eu.dnetlib.index.action;
2

    
3
import eu.dnetlib.clients.index.utils.MetadataReference;
4
import eu.dnetlib.enabling.tools.blackboard.BlackboardJob;
5
import eu.dnetlib.enabling.tools.blackboard.BlackboardServerAction;
6
import eu.dnetlib.enabling.tools.blackboard.BlackboardServerHandler;
7
import eu.dnetlib.index.IndexBackendDescriptor;
8
import eu.dnetlib.index.IndexBackendEnumerator;
9
import eu.dnetlib.index.IndexServerDAOMap;
10
import eu.dnetlib.rmi.provision.IndexServiceException;
11
import org.antlr.stringtemplate.StringTemplate;
12
import org.apache.commons.logging.Log;
13
import org.apache.commons.logging.LogFactory;
14
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.beans.factory.annotation.Required;
16

    
17
/**
18
 * The Class manage the blackboard action of Create Index.
19
 */
20
public class CreateIndexAction extends AbstractIndexAction implements BlackboardServerAction<IndexAction> {
21

    
22
	private static final Log log = LogFactory.getLog(CreateIndexAction.class);
23

    
24
	@Autowired
25
	private IndexServerDAOMap indexServerDAOMap;
26

    
27
	@Autowired
28
	private IndexBackendEnumerator backendEnumerator;
29

    
30
	/**
31
	 * index ds template.
32
	 */
33
	private transient StringTemplate indexDsTemplate;
34

    
35
	/**
36
	 * {@inheritDoc}
37
	 *
38
	 * @see BlackboardServerAction#execute(BlackboardServerHandler,
39
	 * BlackboardJob)
40
	 */
41
	@Override
42
	public void execute(final BlackboardServerHandler handler, final BlackboardJob job) throws IndexServiceException {
43
		final MetadataReference mdRef = getMetadataReference(job);
44
		if ((mdRef.getFormat() == null) || (mdRef.getLayout() == null))
45
			throw new IndexServiceException("some Blackboard parameter is missing in CREATE message");
46
		final String fields = getMDFormatFields(mdRef);
47

    
48
		if (fields.isEmpty()) throw new IndexServiceException("No result getting index layout informations");
49

    
50
		final String backendId = getBackend(job);
51
		if (backendId == null) throw new IndexServiceException("No backend identifier information in CREATE message");
52

    
53
		final String dataStructure = getDataStructure(mdRef, backendId);
54
		final String newId = serviceTools.registerProfile(dataStructure);
55
		log.info("registered DsId: " + newId + "\n" + dataStructure);
56

    
57
		indexServerDAOMap.getIndexServerDAO(backendId).createIndexCollection(mdRef, fields);
58

    
59
		serviceTools.incrementHandledDataStructures(backendId);
60
		job.getParameters().put("id", newId);
61
		handler.done(job);
62

    
63
	}
64

    
65
	/**
66
	 * Method applies given format, layout and interpretation to the index dataStructure template.
67
	 *
68
	 * @return String representation of the indexDataStructure
69
	 * @throws IndexServiceException
70
	 */
71
	private String getDataStructure(final MetadataReference mdRef, final String backendIdentifier) throws IndexServiceException {
72

    
73
		final StringTemplate ds = new StringTemplate(indexDsTemplate.getTemplate());
74

    
75
		IndexBackendDescriptor backendDescriptor = backendEnumerator.getDescriptor(backendIdentifier);
76
		if (backendDescriptor == null) throw new IndexServiceException("No backend identifier found id:" + backendIdentifier);
77
		String backendName = backendDescriptor.getEndpoint().get(IndexBackendDescriptor.ID);
78
		if (backendName == null) throw new IndexServiceException("No backend name associtated to the  identifier with id:" + backendIdentifier);
79
		ds.setAttribute("serviceUri", getServiceAddress(backendIdentifier));
80
		ds.setAttribute("format", mdRef.getFormat());
81
		ds.setAttribute("layout", mdRef.getLayout());
82
		ds.setAttribute("interpretation", mdRef.getInterpretation());
83
		ds.setAttribute("backendID", backendIdentifier);
84
		ds.setAttribute("backendNAME", backendName);
85
		return ds.toString();
86
	}
87

    
88
	@Required
89
	public void setIndexDsTemplate(final StringTemplate indexDsTemplate) {
90
		this.indexDsTemplate = indexDsTemplate;
91
	}
92

    
93
}
(3-3/8)