Project

General

Profile

1
package eu.dnetlib.functionality.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 javax.annotation.Resource;
9

    
10
import org.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
12
import org.dom4j.Document;
13
import org.dom4j.DocumentException;
14
import org.dom4j.Element;
15
import org.dom4j.Node;
16
import org.dom4j.io.SAXReader;
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.beans.factory.annotation.Required;
19

    
20
import eu.dnetlib.enabling.actions.AbstractSubscriptionAction;
21
import eu.dnetlib.functionality.index.utils.MetadataReference;
22
import eu.dnetlib.functionality.index.utils.MetadataReferenceFactory;
23
import eu.dnetlib.functionality.index.utils.ServiceTools;
24
import eu.dnetlib.miscutils.dom4j.XPathHelper;
25

    
26
/**
27
 * MdFormatNotificationHandler updates index schema in response to metadata formats modifications.
28
 * 
29
 * @author claudio
30
 * 
31
 */
32
public class MdFormatNotificationHandler extends AbstractSubscriptionAction {
33

    
34
	/**
35
	 * Logger.
36
	 */
37
	private static final Log log = LogFactory.getLog(MdFormatNotificationHandler.class); // NOPMD by marko on 11/24/08 5:02 PM
38

    
39
	@Resource
40
	private transient MetadataReferenceFactory mdFactory;
41

    
42
	@Autowired
43
	private IndexServerDAOMap indexServerDAOMap;
44

    
45
	@Autowired
46
	private ServiceTools serviceTools;
47

    
48
	private Executor executor = Executors.newSingleThreadExecutor();
49

    
50
	private final transient ThreadLocal<SAXReader> domFactory = new ThreadLocal<SAXReader>() {
51

    
52
		@Override
53
		protected SAXReader initialValue() {
54
			return new SAXReader();
55
		}
56
	};
57

    
58
	/**
59
	 * {@inheritDoc}
60
	 * 
61
	 * @see eu.dnetlib.enabling.tools.Enableable
62
	 */
63
	private boolean enabled;
64

    
65
	@Override
66
	public void notified(final String subscrId, final String topic, final String rsId, final String profile) {
67
		if (!topic.startsWith(getTopicPrefix()) || !isEnabled()) return;
68
		executor.execute(new Runnable() {
69

    
70
			@Override
71
			public void run() {
72
				try {
73
					final Document doc = parse(profile);
74
					final Node formatNode = doc.selectSingleNode("//CONFIGURATION/NAME/text()");
75

    
76
					if ((formatNode != null) && !formatNode.asXML().isEmpty()) {
77

    
78
						final String format = formatNode.asXML();
79
						final Iterable<Element> layouts = XPathHelper.selectElements(doc, "//STATUS/LAYOUTS/LAYOUT");
80
						final Node interpretationNode = doc.selectSingleNode("//CONFIGURATION/INTERPRETATION/text()");
81
						final String interpretation = interpretationNode.asXML();
82

    
83
						log.info("found a change in mdFormat: " + format);
84
						for (Element element : layouts) {
85
							final String layout = element.attributeValue("name");
86

    
87
							// updates index schema based on mdFormat and layout
88
							final MetadataReference mdRef = mdFactory.getMetadata(format, layout, interpretation);
89
							final Document fields = parse(element.selectSingleNode("./FIELDS").asXML());
90

    
91
							List<String> backends = serviceTools.getBackendIds(mdRef);
92

    
93
							if ((backends == null) || backends.isEmpty()) {
94
								log.warn("There is no backendId in profiles for mdref " + mdRef);
95
							}
96

    
97
							for (String backendId : backends) {
98
								IndexServerDAO idxDao = indexServerDAOMap.getIndexServerDAO(backendId);
99
								if (idxDao == null) throw new RuntimeException("No index found for the mdformat " + mdRef);
100
								log.info("Found index DAO which serve this mdFormat");
101
								idxDao.updateIndexCollection(mdRef, fields);
102
							}
103
						}
104
					}
105
					log.info("Upload schema completed");
106
				} catch (Exception e) {
107
					throw new RuntimeException(e); // NOPMD
108
				}
109
			}
110
		});
111
	}
112

    
113
	@Override
114
	@Required
115
	public void setEnabled(final boolean enabled) {
116
		this.enabled = enabled;
117
	}
118

    
119
	/**
120
	 * Helper method, parses an xml string.
121
	 * 
122
	 * @param source
123
	 *            the xml.
124
	 * @return the parsed xml.
125
	 * @throws DocumentException
126
	 *             could happen
127
	 */
128
	private Document parse(final String source) throws DocumentException {
129
		return domFactory.get().read(new StringReader(source));
130
	}
131

    
132
	@Override
133
	public boolean isEnabled() {
134
		return enabled;
135
	}
136

    
137
}
(9-9/9)