Project

General

Profile

1
package eu.dnetlib.msro.workflows.sarasvati.loader;
2

    
3
import java.io.StringReader;
4

    
5
import javax.annotation.Resource;
6

    
7
import org.apache.commons.lang.math.NumberUtils;
8
import org.apache.commons.logging.Log;
9
import org.apache.commons.logging.LogFactory;
10
import org.dom4j.Document;
11
import org.dom4j.DocumentException;
12
import org.dom4j.io.SAXReader;
13
import org.springframework.beans.factory.annotation.Required;
14

    
15
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
16
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
17
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
18
import eu.dnetlib.miscutils.functional.xml.ApplyXslt;
19
import eu.dnetlib.msro.workflows.util.WorkflowsConstants;
20

    
21
public class ProfileToSarasvatiConverter {
22

    
23
	@Resource
24
	private UniqueServiceLocator serviceLocator;
25

    
26
	private org.springframework.core.io.Resource xslt;
27

    
28
	private static final Log log = LogFactory.getLog(ProfileToSarasvatiConverter.class);
29

    
30
	public WfProfileDescriptor getSarasvatiWorkflow(final String id) throws DocumentException {
31
		final String s = getProfile(id);
32
		final Document doc = new SAXReader().read(new StringReader(s));
33
		final WfProfileDescriptor desc = new WfProfileDescriptor();
34
		desc.setName(doc.valueOf("//WORKFLOW_NAME"));
35
		desc.setType(doc.valueOf("//WORKFLOW_TYPE"));
36
		desc.setPriority(NumberUtils.toInt("//WORKFLOW_PRIORITY", WorkflowsConstants.DEFAULT_WF_PRIORITY));
37
		desc.setWorkflowXml(new ApplyXslt(xslt).evaluate(s));
38
		desc.setReady(doc.selectNodes("//PARAM[@required='true' and string-length(normalize-space(.)) = 0]").isEmpty());
39
		return desc;
40
	}
41

    
42
	public String getProfile(final String id) {
43
		try {
44
			return serviceLocator.getService(ISLookUpService.class).getResourceProfile(id);
45
		} catch (ISLookUpException e) {
46
			log.error("Error finding profile: " + id, e);
47
			return null;
48
		}
49
	}
50

    
51
	public org.springframework.core.io.Resource getXslt() {
52
		return xslt;
53
	}
54

    
55
	@Required
56
	public void setXslt(final org.springframework.core.io.Resource xslt) {
57
		this.xslt = xslt;
58
	}
59
}
(6-6/8)