Project

General

Profile

1
package eu.dnetlib.msro.workflows.nodes.dedup.utils;
2

    
3
import java.io.StringReader;
4
import java.util.List;
5
import java.util.Queue;
6
import javax.annotation.Resource;
7

    
8
import com.google.common.collect.Lists;
9
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
10
import eu.dnetlib.pace.config.DedupConfig;
11
import eu.dnetlib.rmi.enabling.ISLookUpDocumentNotFoundException;
12
import eu.dnetlib.rmi.enabling.ISLookUpException;
13
import eu.dnetlib.rmi.enabling.ISLookUpService;
14
import org.apache.commons.lang3.StringUtils;
15
import org.apache.commons.logging.Log;
16
import org.apache.commons.logging.LogFactory;
17
import org.dom4j.Document;
18
import org.dom4j.DocumentException;
19
import org.dom4j.Element;
20
import org.dom4j.io.SAXReader;
21

    
22
/**
23
 * The Class DedupConfigurationOrchestrationLoader.
24
 */
25
public class DedupConfigurationOrchestrationLoader {
26

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

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

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

    
49
		final ISLookUpService isLookUpService = serviceLocator.getService(ISLookUpService.class);
50

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

    
54
		return parseOrchestrationProfile(isLookUpService, isLookUpService.getResourceProfileByQuery(xquery));
55
	}
56

    
57
	public List<DedupConfigurationOrchestration> loadByEntityName(final String entityName) throws Exception {
58

    
59
		final ISLookUpService isLookUpService = serviceLocator.getService(ISLookUpService.class);
60

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

    
64
		final List<DedupConfigurationOrchestration> res = Lists.newArrayList();
65

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

    
70
		return res;
71
	}
72

    
73
	private DedupConfigurationOrchestration parseOrchestrationProfile(final ISLookUpService isLookUpService, final String dedupOrchestation)
74
			throws DocumentException, ISLookUpException {
75

    
76
		final Document doc = new SAXReader().read(new StringReader(dedupOrchestation));
77

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

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

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

    
88
		final DedupConfigurationOrchestration dco = new DedupConfigurationOrchestration(entity, actionSetId, configurations);
89

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

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