Project

General

Profile

1
package eu.dnetlib.msro.workflows.dedup.conf;
2

    
3
import java.io.StringReader;
4
import java.util.List;
5
import java.util.Queue;
6

    
7
import javax.annotation.Resource;
8

    
9
import org.apache.commons.logging.Log;
10
import org.apache.commons.logging.LogFactory;
11
import org.dom4j.Document;
12
import org.dom4j.DocumentException;
13
import org.dom4j.Element;
14
import org.dom4j.io.SAXReader;
15

    
16
import com.google.common.collect.Lists;
17

    
18
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
19
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
20
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
21
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
22
import eu.dnetlib.pace.config.DedupConfig;
23

    
24
/**
25
 * The Class DedupConfigurationOrchestrationLoader.
26
 */
27
public class DedupConfigurationOrchestrationLoader {
28

    
29
	/** The Constant log. */
30
	private static final Log log = LogFactory.getLog(DedupConfigurationOrchestrationLoader.class);
31

    
32
	/** The service locator. */
33
	@Resource
34
	private UniqueServiceLocator serviceLocator;
35

    
36
	/**
37
	 * Load the dedup orchestration profile from the IS.
38
	 *
39
	 * @param id
40
	 *            the id
41
	 * @return the dedup configuration orchestration
42
	 * @throws ISLookUpDocumentNotFoundException
43
	 *             the IS look up document not found exception
44
	 * @throws ISLookUpException
45
	 *             the IS look up exception
46
	 * @throws DocumentException
47
	 *             the document exception
48
	 */
49
	public DedupConfigurationOrchestration loadByActionSetId(final String id) throws Exception {
50

    
51
		final ISLookUpService isLookUpService = serviceLocator.getService(ISLookUpService.class);
52

    
53
		final String xquery = String.format("/RESOURCE_PROFILE[.//DEDUPLICATION/ACTION_SET/@id = '%s']", id);
54
		log.info("loading dedup orchestration: " + xquery);
55

    
56
		return parseOrchestrationProfile(isLookUpService, isLookUpService.getResourceProfileByQuery(xquery));
57
	}
58

    
59
	public List<DedupConfigurationOrchestration> loadByEntityName(final String entityName) throws Exception {
60

    
61
		final ISLookUpService isLookUpService = serviceLocator.getService(ISLookUpService.class);
62

    
63
		final String xquery = String.format("/RESOURCE_PROFILE[.//DEDUPLICATION/ENTITY/@name = '%s']", entityName);
64
		log.info("loading dedup orchestration: " + xquery);
65

    
66
		final List<DedupConfigurationOrchestration> res = Lists.newArrayList();
67

    
68
		for (final String profile : isLookUpService.quickSearchProfile(xquery)) {
69
			res.add(parseOrchestrationProfile(isLookUpService, profile));
70
		}
71

    
72
		return res;
73
	}
74

    
75
	private DedupConfigurationOrchestration parseOrchestrationProfile(final ISLookUpService isLookUpService, final String dedupOrchestation)
76
			throws DocumentException,
77
			ISLookUpException, ISLookUpDocumentNotFoundException {
78
		final Document doc = new SAXReader().read(new StringReader(dedupOrchestation));
79

    
80
		final Element e = (Element) doc.selectSingleNode("//DEDUPLICATION/ENTITY");
81
		final Entity entity = new Entity(e.attributeValue("name"), e.attributeValue("code"), e.attributeValue("label"));
82

    
83
		final String actionSetId = doc.valueOf("//DEDUPLICATION/ACTION_SET/@id");
84
		final Queue<DedupConfig> configurations = Lists.newLinkedList();
85

    
86
		for (final Object o : doc.selectNodes("//SCAN_SEQUENCE/SCAN")) {
87
			configurations.add(loadConfig(isLookUpService, actionSetId, o));
88
		}
89

    
90
		final DedupConfigurationOrchestration dco = new DedupConfigurationOrchestration(entity, actionSetId, configurations);
91

    
92
		log.debug("loaded dedup configuration orchestration: " + dco.toString());
93
		log.info("loaded dedup configuration orchestration, size: " + dco.getConfigurations().size());
94
		return dco;
95
	}
96

    
97
	private DedupConfig loadConfig(final ISLookUpService isLookUpService, final String actionSetId, final Object o) throws ISLookUpException,
98
			ISLookUpDocumentNotFoundException {
99
		final Element s = (Element) o;
100
		final String configProfileId = s.attributeValue("id");
101
		final String conf =
102
				isLookUpService.getResourceProfileByQuery(String.format(
103
						"for $x in /RESOURCE_PROFILE[.//RESOURCE_IDENTIFIER/@value = '%s'] return $x//DEDUPLICATION/text()",
104
						configProfileId));
105
		log.debug("loaded dedup configuration from IS profile: " + conf);
106
		final DedupConfig dedupConfig = DedupConfig.load(conf);
107
		dedupConfig.getWf().setConfigurationId(actionSetId);
108
		return dedupConfig;
109
	}
110
}
(2-2/3)