Project

General

Profile

1
package eu.dnetlib.index;
2

    
3
import java.io.StringReader;
4
import java.util.List;
5
import java.util.concurrent.Executor;
6
import java.util.concurrent.Executors;
7

    
8
import eu.dnetlib.clients.index.utils.MetadataReference;
9
import eu.dnetlib.clients.index.utils.MetadataReferenceFactory;
10
import eu.dnetlib.clients.index.utils.ServiceTools;
11
import eu.dnetlib.enabling.actions.AbstractSubscriptionAction;
12
import org.apache.commons.logging.Log;
13
import org.apache.commons.logging.LogFactory;
14
import org.dom4j.Document;
15
import org.dom4j.DocumentException;
16
import org.dom4j.Element;
17
import org.dom4j.Node;
18
import org.dom4j.io.SAXReader;
19
import org.springframework.beans.factory.annotation.Autowired;
20

    
21
/**
22
 * MdFormatNotificationHandler updates index schema in response to metadata formats modifications.
23
 *
24
 * @author claudio
25
 */
26
public class MdFormatNotificationHandler extends AbstractSubscriptionAction {
27

    
28
	/**
29
	 * Logger.
30
	 */
31
	private static final Log log = LogFactory.getLog(MdFormatNotificationHandler.class); // NOPMD by marko on 11/24/08 5:02 PM
32
	private final transient ThreadLocal<SAXReader> domFactory = new ThreadLocal<SAXReader>() {
33

    
34
		@Override
35
		protected SAXReader initialValue() {
36
			return new SAXReader();
37
		}
38
	};
39
	@Autowired
40
	private transient MetadataReferenceFactory mdFactory;
41
	@Autowired
42
	private IndexServerDAOMap indexServerDAOMap;
43
	@Autowired
44
	private ServiceTools serviceTools;
45
	private Executor executor = Executors.newSingleThreadExecutor();
46
	/**
47
	 * {@inheritDoc}
48
	 */
49
	private boolean enabled;
50

    
51
	@Override
52
	public void notified(final String subscrId, final String topic, final String rsId, final String profile) {
53
		if (!topic.startsWith(getTopicPrefix())) return;
54
		executor.execute(() -> {
55
			try {
56
				final Document doc = parse(profile);
57
				final Node formatNode = doc.selectSingleNode("//CONFIGURATION/NAME/text()");
58

    
59
				if ((formatNode != null) && !formatNode.asXML().isEmpty()) {
60

    
61
					final String format = formatNode.asXML();
62
					final Iterable<Element> layouts = (Iterable<Element>) doc.selectNodes("//STATUS/LAYOUTS/LAYOUT");
63

    
64
					final Node interpretationNode = doc.selectSingleNode("//CONFIGURATION/INTERPRETATION/text()");
65
					final String interpretation = interpretationNode.asXML();
66

    
67
					log.info("found a change in mdFormat: " + format);
68
					for (Element element : layouts) {
69
						final String layout = element.attributeValue("name");
70

    
71
						// updates index schema based on mdFormat and layout
72
						final MetadataReference mdRef = mdFactory.getMetadata(format, layout, interpretation);
73
						final Document fields = parse(element.selectSingleNode("./FIELDS").asXML());
74

    
75
						List<String> backends = serviceTools.getBackendIds(mdRef);
76

    
77
						if ((backends == null) || backends.isEmpty()) {
78
							log.warn("There is no backendId in profiles for mdref " + mdRef);
79
						}
80

    
81
						for (String backendId : backends) {
82
							IndexServerDAO idxDao = indexServerDAOMap.getIndexServerDAO(backendId);
83
							if (idxDao == null) throw new RuntimeException("No index found for the mdformat " + mdRef);
84
							log.info("Found index DAO which serve this mdFormat");
85
							idxDao.updateIndexCollection(mdRef, fields);
86
						}
87
					}
88
				}
89
				log.info("Upload schema completed");
90
			} catch (Exception e) {
91
				throw new RuntimeException(e); // NOPMD
92
			}
93
		});
94
	}
95

    
96
	/**
97
	 * Helper method, parses an xml string.
98
	 *
99
	 * @param source the xml.
100
	 * @return the parsed xml.
101
	 * @throws DocumentException could happen
102
	 */
103
	private Document parse(final String source) throws DocumentException {
104
		return domFactory.get().read(new StringReader(source));
105
	}
106

    
107
}
(9-9/9)