Project

General

Profile

1 39920 sandro.lab
import urllib2
2
3 40330 sandro.lab
import pysolr
4 39920 sandro.lab
from flask import json
5
6 40330 sandro.lab
from DLI_ObjectResolver import DLIObject
7 39920 sandro.lab
8
9
class QueryResolver(object):
10
    def __init__(self, solr_url, base_url_dnet):
11
        self.solr_url = solr_url
12
        self.solr_server = pysolr.Solr(self.solr_url)
13
        self.base_url_dnet = base_url_dnet
14
        self.facet_renaming = {'entitytype': "Typology", 'provenance': "Content Provider",
15 40969 sandro.lab
                               "Content Provider": 'provenance', "Typology": 'entitytype',
16
                               "publisher":"publisher"}
17 39920 sandro.lab
18
    def get_facet(self, facet, query=None):
19
        data = []
20
21
        f = 'entitytype'
22
        if query is not None:
23
            data.append({'facet': self.facet_renaming[f], 'query_name': f, 'values': [
24
                {'key': facet[f][i], 'value': facet[f][i + 1]} for i in range(0, len(facet[f]), 2)]})
25
        else:
26
            data.append({'facet': self.facet_renaming[f], 'query_name': f,
27
                         'values': [{'key': facet[f][i], 'value': facet[f][i + 1]} for i in
28
                                    range(0, len(facet[f]), 2)]})
29
        f = 'provenance'
30
        data.append({'facet': self.facet_renaming[f], 'query_name': f,
31
                     'values': [{'key': facet[f][i], 'value': facet[f][i + 1]} for i in range(0, len(facet[f]), 2)]})
32
33 40969 sandro.lab
        f = 'publisher'
34
        data.append({'facet': self.facet_renaming[f], 'query_name': f,
35
                     'values': [{'key': facet[f][i], 'value': facet[f][i + 1]} for i in range(0, len(facet[f]), 2)]})
36
37 39920 sandro.lab
        return data
38
39
    def get_item(self, persistent_identifier):
40
        query = 'objidentifier:"%s"' % persistent_identifier
41
        response = self.solr_server.search(q=query)
42
        query_response = []
43
        for hit in response:
44 40330 sandro.lab
            query_response.append(DLIObject(hit['dnetresourceidentifier'], self.base_url_dnet))
45 39920 sandro.lab
46
        return {'objects': query_response}
47
48
    def get_stats_detail(self):
49
        data = urllib2.urlopen(self.base_url_dnet + "/mvc/ui/stats/getLastStats")
50
        result = []
51
        d = json.loads(data.read())
52
        for k in d:
53
            if 'Deduction' not in k['datasource']:
54
                result.append(k)
55
        return result
56
57
    def get_stats(self):
58
59
        data = urllib2.urlopen(self.base_url_dnet + "/mvc/ui/stats/getAllStats")
60
        result = {}
61
        d = json.loads(data.read())
62
        for item in d:
63
            result[item] = []
64
            for k in d[item]:
65
                if 'Deduction' not in k['datasource']:
66
                    result[item].append(k)
67
        return result
68
69
    def query(self, q, browse_fields, start=None, page_size=10, only_result=False, filter_query=None):
70
        params = {}
71
        if browse_fields is not None:
72
            params['facet'] = 'true',
73
            params['facet.field'] = browse_fields
74
            params['facet.mincount'] = 1
75
76
        if filter_query is not None:
77
            params['fq'] = filter_query
78
        if start is not None:
79
            params['start'] = start
80 40422 sandro.lab
81
        if len(q) > 0:
82
            w_query = '%s OR (%s AND completionstatus:"complete")^2.0' % (q, q)
83
84
        response = self.solr_server.search(w_query, rows=page_size, **params)
85 39920 sandro.lab
        query_response = []
86
        for hit in response:
87 40330 sandro.lab
            dnet_id = hit['dnetresourceidentifier']
88
89
            query_response.append(DLIObject(hit['dnetresourceidentifier'], self.base_url_dnet))
90 39920 sandro.lab
        if not only_result:
91
            facet = self.get_facet(response.facets['facet_fields'], q)
92
        else:
93
            facet = self.get_facet(response.facets['facet_fields'])
94
        return {'objects': query_response, 'facet': facet, 'total': response.hits}
95
96
    def get_ds_info(self, id, name):
97 40330 sandro.lab
        param = ""
98 39920 sandro.lab
        value = ""
99 40330 sandro.lab
        if (len(id) > 0):
100
            param = "id"
101
            value = id
102
        if len(name) > 0:
103
            param = "name"
104
            value = name.replace(" ", '%20')
105
        url_query = self.base_url_dnet + "/mvc/ui/dli/datasource/info.do?%s=%s" % (param, value)
106 39920 sandro.lab
        data = urllib2.urlopen(url_query)
107
        d = json.loads(data.read())
108
        return d
109
110
    def get_all_ds(self):
111
        data = urllib2.urlopen(self.base_url_dnet + "/mvc/ui/dli/datasource/datasources.do")
112
        d = json.loads(data.read())
113
        return d