Project

General

Profile

1
package eu.dnetlib.data.search.app;
2

    
3
import eu.dnetlib.api.enabling.ISLookUpService;
4
import eu.dnetlib.api.enabling.ISRegistryService;
5
import eu.dnetlib.enabling.tools.blackboard.BlackboardJob;
6
import eu.dnetlib.enabling.tools.blackboard.BlackboardNotificationHandler;
7
import eu.dnetlib.enabling.tools.blackboard.BlackboardServerHandler;
8
import gr.uoa.di.driver.util.ServiceLocator;
9
import org.apache.log4j.Logger;
10
import org.w3c.dom.Document;
11
import org.w3c.dom.Node;
12
import org.xml.sax.InputSource;
13
import org.xml.sax.SAXException;
14

    
15
import javax.xml.parsers.DocumentBuilder;
16
import javax.xml.parsers.DocumentBuilderFactory;
17
import javax.xml.parsers.ParserConfigurationException;
18
import javax.xml.transform.Transformer;
19
import javax.xml.transform.TransformerFactory;
20
import javax.xml.transform.dom.DOMSource;
21
import javax.xml.transform.stream.StreamResult;
22
import javax.xml.xpath.*;
23
import java.io.IOException;
24
import java.io.StringReader;
25
import java.io.StringWriter;
26

    
27
public class SearchServiceBlackboardHandler extends
28
        BlackboardNotificationHandler<BlackboardServerHandler> {
29

    
30
    private static Logger logger = Logger.getLogger(SearchServiceBlackboardHandler.class);
31

    
32
    private SearchServiceImpl searchService = null;
33
    private ServiceLocator<ISLookUpService> lookUpServiceServiceLocator = null;
34
    private ServiceLocator<ISRegistryService> registryServiceServiceLocator = null;
35

    
36
    private XPathExpression indexMdFormatExpression = null;
37
    private XPathExpression indexLayoutExpression = null;
38
    private XPathExpression searchMdFormatExpression = null;
39

    
40

    
41
    public SearchServiceBlackboardHandler() throws XPathExpressionException {
42
        XPathFactory factory = XPathFactory.newInstance();
43
        XPath xpath = factory.newXPath();
44

    
45
        indexMdFormatExpression = xpath.compile("//METADATA_FORMAT");
46
        indexLayoutExpression = xpath.compile("//METADATA_FORMAT_LAYOUT");
47
        searchMdFormatExpression = xpath.compile("//SERVICE_PROPERTIES/PROPERTY[@key='mdformat']");
48
    }
49

    
50
    @Override
51
    protected void processJob(BlackboardJob job) {
52
        logger.info("Got Blackboard Message");
53
        String action = job.getAction();
54

    
55
        try {
56
            getBlackboardHandler().ongoing(job);
57

    
58
            if (action.equals("UpdateIndex")) {
59
                String indexProfileId = job.getParameters().get("IndexId");
60
                logger.info("Updating the service to use index " + indexProfileId);
61

    
62
                String indexProfile = lookUpServiceServiceLocator.getService().getResourceProfile(indexProfileId);
63
                String mdFormat = getMDFormat(indexProfile);
64
                String indexLayout = getIndexLayout(indexProfile);
65
                logger.info("New mdFormat: " + mdFormat + ", new layout: " + indexLayout);
66

    
67
                searchService.setMdFormat(mdFormat);
68
                searchService.setIndexLayout(indexLayout);
69

    
70
                String searchProfileId = searchService.getServiceEPR().getParameter("serviceId");
71

    
72
                updateSearchServiceProfile(searchProfileId, mdFormat);
73

    
74
            } else {
75
                throw new Exception("Don't know what to do with " + action);
76
            }
77

    
78
            getBlackboardHandler().done(job);
79
        } catch (Exception e) {
80
            getBlackboardHandler().failed(job, e);
81

    
82
            logger.error("Error processing job", e);
83
        }
84
    }
85

    
86
    private String getIndexLayout(String indexProfile) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
87
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
88
        dbf.setNamespaceAware(true);
89
        DocumentBuilder db = dbf.newDocumentBuilder();
90
        Document doc = db.parse(new InputSource(new StringReader(indexProfile)));
91

    
92
        Node layoutNode = (Node) indexLayoutExpression.evaluate(doc, XPathConstants.NODE);
93

    
94
        return layoutNode.getTextContent();
95
    }
96

    
97
    private String getMDFormat(String indexProfile) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {
98
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
99
        dbf.setNamespaceAware(true);
100
        DocumentBuilder db = dbf.newDocumentBuilder();
101
        Document doc = db.parse(new InputSource(new StringReader(indexProfile)));
102

    
103
        Node layoutNode = (Node) indexMdFormatExpression.evaluate(doc, XPathConstants.NODE);
104

    
105
        return layoutNode.getTextContent();
106
    }
107

    
108
    private void updateSearchServiceProfile(String searchProfileId, String mdFormat)  {
109
        try {
110
            String searchProfile = lookUpServiceServiceLocator.getService().getResourceProfile(searchProfileId);
111

    
112
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
113
            dbf.setNamespaceAware(true);
114
            DocumentBuilder db = dbf.newDocumentBuilder();
115
            Document doc = db.parse(new InputSource(new StringReader(searchProfile)));
116

    
117

    
118
            Node node = (Node) searchMdFormatExpression.evaluate(doc,XPathConstants.NODE);
119
            node.getAttributes().getNamedItem("value").setNodeValue(mdFormat);
120

    
121
            DOMSource domSource = new DOMSource(doc);
122
            StringWriter writer = new StringWriter();
123
            StreamResult result = new StreamResult(writer);
124
            TransformerFactory tf = TransformerFactory.newInstance();
125
            Transformer transformer = tf.newTransformer();
126
            transformer.transform(domSource, result);
127

    
128
            registryServiceServiceLocator.getService().updateProfile(searchProfileId, writer.toString(), "SearchServiceResourceType");
129

    
130
        } catch (Exception e) {
131
            logger.error("Fail to update search service profile with id " + searchProfileId, e);
132
        }
133

    
134
        logger.info("Updated search service profile with id " +searchProfileId);
135

    
136
    }
137

    
138

    
139
    public ServiceLocator<ISLookUpService> getLookUpServiceServiceLocator() {
140
        return lookUpServiceServiceLocator;
141
    }
142

    
143
    public void setLookUpServiceServiceLocator(ServiceLocator<ISLookUpService> lookUpServiceServiceLocator) {
144
        this.lookUpServiceServiceLocator = lookUpServiceServiceLocator;
145
    }
146

    
147
    public void setRegistryServiceServiceLocator(ServiceLocator<ISRegistryService> registryServiceServiceLocator) {
148
        this.registryServiceServiceLocator = registryServiceServiceLocator;
149
    }
150

    
151
    public SearchServiceImpl getSearchService() {
152
        return searchService;
153
    }
154

    
155
    public void setSearchService(SearchServiceImpl searchService) {
156
        this.searchService = searchService;
157
    }
158
}
(1-1/2)