Project

General

Profile

1
DROP VIEW organizations_view;
2
DROP VIEW organizations_info_view;
3
DROP VIEW organizations_simple_view;
4
DROP VIEW users_view;
5
DROP VIEW conflicts_view;
6

    
7
DROP TABLE IF EXISTS other_ids;
8
DROP TABLE IF EXISTS other_names;
9
DROP TABLE IF EXISTS acronyms;
10
DROP TABLE IF EXISTS relationships;
11
DROP TABLE IF EXISTS urls;
12
DROP TABLE IF EXISTS openaire_simrels;
13
DROP TABLE IF EXISTS organizations;
14
DROP TABLE IF EXISTS org_types;
15

    
16
DROP TABLE IF EXISTS user_countries;
17
DROP TABLE IF EXISTS users;
18

    
19
DROP TABLE IF EXISTS user_roles;
20
DROP TABLE IF EXISTS countries;
21
DROP TABLE IF EXISTS id_types;
22
DROP TABLE IF EXISTS languages;
23

    
24
DROP SEQUENCE IF EXISTS organizations_id_seq;
25

    
26
CREATE TABLE org_types (val text PRIMARY KEY);
27
INSERT INTO org_types VALUES ('Archive'), ('Company'), ('Education'), ('Facility'), ('Government'), ('Healthcare'), ('Nonprofit'), ('Other'), ('UNKNOWN');
28

    
29
CREATE TABLE id_types (val text PRIMARY KEY);
30
INSERT INTO id_types VALUES ('CNRS'), ('FundRef'), ('HESA'), ('ISNI'), ('OrgRef'), ('UCAS'), ('UKPRN'), ('Wikidata'), ('grid.ac');
31

    
32
CREATE TABLE languages (val text PRIMARY KEY);
33
INSERT INTO languages VALUES ('UNKNOWN'), ('aa'), ('af'), ('am'), ('ar'), ('as'), ('az'), ('ba'), ('be'), ('bg'), ('bn'), ('br'), ('bs'), ('ca'), ('ch'), ('co'), ('cs'), ('cy'), ('da'),
34
			('de'), ('dv'), ('dz'), ('el'), ('en'), ('eo'), ('es'), ('et'), ('eu'), ('fa'), ('fi'), ('fr'), ('fy'), ('ga'), ('gd'), ('gl'), ('gu'), ('he'), ('hi'), ('hr'), ('hu'), ('hy'), ('id'), ('is'),
35
			('it'), ('iu'), ('ja'), ('jv'), ('ka'), ('kk'), ('kl'), ('km'), ('kn'), ('ko'), ('ku'), ('ky'), ('la'), ('lb'), ('lo'), ('lt'), ('lv'), ('mg'), ('mi'), ('mk'), ('ml'), ('mn'), ('mr'), ('ms'),
36
			('mt'), ('my'), ('nb'), ('ne'), ('nl'), ('nn'), ('no'), ('oc'), ('or'), ('pa'), ('pl'), ('ps'), ('pt'), ('rm'), ('ro'), ('ru'), ('rw'), ('sa'), ('sd'), ('si'), ('sk'), ('sl'), ('so'), ('sq'),
37
			('sr'), ('sv'), ('sw'), ('ta'), ('te'), ('tg'), ('th'), ('tk'), ('tl'), ('tr'), ('tt'), ('ug'), ('uk'), ('ur'), ('uz'), ('vi'), ('xh'), ('yo'), ('zh'), ('zu');
38

    
39
CREATE TABLE countries (val text PRIMARY KEY);
40
INSERT INTO countries VALUES ('UNKNOWN'), ('AD'), ('AE'), ('AF'), ('AG'), ('AL'), ('AM'), ('AO'), ('AR'), ('AT'), ('AU'), ('AW'), ('AX'), ('AZ'), ('BA'), ('BB'), ('BD'), ('BE'), ('BF'), ('BG'), ('BH'), ('BI'),
41
			('BJ'), ('BM'), ('BN'), ('BO'), ('BQ'), ('BR'), ('BS'), ('BT'), ('BW'), ('BY'), ('BZ'), ('CA'), ('CD'), ('CF'), ('CG'), ('CH'), ('CI'), ('CL'), ('CM'), ('CN'), ('CO'), ('CR'),
42
			('CU'), ('CV'), ('CW'), ('CY'), ('CZ'), ('DE'), ('DJ'), ('DK'), ('DM'), ('DO'), ('DZ'), ('EC'), ('EE'), ('EG'), ('EH'), ('ER'), ('ES'), ('ET'), ('FI'), ('FJ'), ('FM'), ('FO'),
43
			('FR'), ('GA'), ('GB'), ('GD'), ('GE'), ('GF'), ('GH'), ('GI'), ('GL'), ('GM'), ('GN'), ('GP'), ('GQ'), ('GR'), ('GT'), ('GW'), ('GY'), ('HN'), ('HR'), ('HT'), ('HU'), ('ID'),
44
			('IE'), ('IL'), ('IM'), ('IN'), ('IQ'), ('IR'), ('IS'), ('IT'), ('JE'), ('JM'), ('JO'), ('JP'), ('KE'), ('KG'), ('KH'), ('KN'), ('KP'), ('KR'), ('KW'), ('KY'), ('KZ'), ('LA'),
45
			('LB'), ('LC'), ('LI'), ('LK'), ('LR'), ('LS'), ('LT'), ('LU'), ('LV'), ('LY'), ('MA'), ('MC'), ('MD'), ('ME'), ('MG'), ('MK'), ('ML'), ('MM'), ('MN'), ('MO'), ('MQ'), ('MR'),
46
			('MS'), ('MT'), ('MU'), ('MV'), ('MW'), ('MX'), ('MY'), ('MZ'), ('NA'), ('NC'), ('NE'), ('NG'), ('NI'), ('NL'), ('NO'), ('NP'), ('NU'), ('NZ'), ('OM'), ('PA'), ('PE'), ('PF'),
47
			('PG'), ('PH'), ('PK'), ('PL'), ('PS'), ('PT'), ('PW'), ('PY'), ('QA'), ('RE'), ('RO'), ('RS'), ('RU'), ('RW'), ('SA'), ('SB'), ('SC'), ('SD'), ('SE'), ('SG'), ('SI'), ('SJ'),
48
			('SK'), ('SL'), ('SM'), ('SN'), ('SO'), ('SR'), ('SS'), ('ST'), ('SV'), ('SX'), ('SY'), ('SZ'), ('TC'), ('TD'), ('TG'), ('TH'), ('TJ'), ('TL'), ('TM'), ('TN'), ('TO'), ('TR'),
49
			('TT'), ('TV'), ('TW'), ('TZ'), ('UA'), ('UG'), ('US'), ('UY'), ('UZ'), ('VA'), ('VC'), ('VE'), ('VG'), ('VN'), ('WS'), ('XK'), ('YE'), ('ZA'), ('ZM'), ('ZW');
50

    
51
CREATE TABLE user_roles(role text PRIMARY KEY);
52
INSERT INTO user_roles VALUES ('ADMIN'), ('NATIONAL_ADMIN'), ('USER'), ('PENDING'), ('NOT_AUTHORIZED');
53

    
54
CREATE TABLE users (
55
	email     text PRIMARY KEY,
56
	valid     boolean DEFAULT true,
57
	role      text NOT NULL default 'USER' REFERENCES user_roles(role)
58
);
59

    
60
CREATE TABLE user_countries (
61
	email     text REFERENCES users(email),
62
	country   text  REFERENCES countries(val),
63
	PRIMARY KEY(email, country)	
64
);
65

    
66
CREATE SEQUENCE organizations_id_seq;
67

    
68
CREATE TABLE organizations (
69
    id                text PRIMARY KEY DEFAULT 'openorgs____::'||lpad(nextval('organizations_id_seq')::text,10,'0'),
70
    name              text,
71
   	type              text NOT NULL DEFAULT 'UNKNOWN' REFERENCES org_types(val),
72
    lat               double precision,
73
	lng               double precision,
74
	city              text,
75
	country           text REFERENCES countries(val),
76
	created_by        text,
77
	creation_date     timestamp with time zone DEFAULT now(),
78
	modified_by       text,
79
	modification_date timestamp with time zone DEFAULT now()
80
);
81
CREATE INDEX organizations_type_idx ON organizations(type);
82
CREATE INDEX organizations_country_idx ON organizations(country);
83

    
84
CREATE TABLE other_ids (
85
	id      text REFERENCES organizations(id) ON UPDATE CASCADE,
86
	otherid text,
87
	type    text REFERENCES id_types(val),
88
	PRIMARY KEY (id, otherid, type)
89
);
90
CREATE INDEX other_ids_id_idx ON other_ids(id);
91

    
92
CREATE TABLE other_names (
93
	id    text REFERENCES organizations(id) ON UPDATE CASCADE,
94
	name  text,
95
	lang  text REFERENCES languages(val),
96
	PRIMARY KEY (id, name, lang)
97
);
98
CREATE INDEX other_names_id_idx ON other_names(id);
99

    
100
CREATE TABLE acronyms (
101
	id text  REFERENCES organizations(id) ON UPDATE CASCADE,
102
	acronym text,
103
	PRIMARY KEY (id, acronym)
104
);
105
CREATE INDEX acronyms_id_idx ON acronyms(id);
106

    
107
CREATE TABLE relationships (
108
	id1     text REFERENCES organizations(id) ON UPDATE CASCADE,
109
	reltype text,
110
	id2     text REFERENCES organizations(id) ON UPDATE CASCADE,
111
    PRIMARY KEY (id1, reltype, id2)
112
);
113
CREATE INDEX relationships_id1_idx ON relationships(id1);
114
CREATE INDEX relationships_id2_idx ON relationships(id2);
115

    
116
CREATE TABLE urls (
117
	id  text REFERENCES organizations(id) ON UPDATE CASCADE,
118
	url text,
119
	PRIMARY KEY (id, url)
120
);
121
CREATE INDEX urls_id_idx ON urls(id);
122

    
123
CREATE TABLE openaire_simrels (
124
	local_id         text REFERENCES organizations(id) ON UPDATE CASCADE,
125
	oa_original_id   text NOT NULL,
126
	oa_name          text NOT NULL,
127
	oa_acronym       text,
128
	oa_country       text,
129
	oa_url           text,
130
	oa_collectedfrom text,
131
	reltype          text NOT NULL DEFAULT 'suggested',
132
	PRIMARY KEY (local_id, oa_original_id)
133
);
134
CREATE INDEX openaire_simrels_local_id_idx ON openaire_simrels(local_id);
135

    
136
CREATE VIEW organizations_view AS SELECT
137
    org.id,
138
    org.name,
139
    org.type,
140
    org.lat,
141
	org.lng,
142
	org.city,
143
	org.country,
144
	COALESCE(jsonb_agg(DISTINCT jsonb_build_object('id', oid.otherid, 'type', oid.type))                                         FILTER (WHERE oid.otherid IS NOT NULL), '[]') AS other_ids,
145
	COALESCE(jsonb_agg(DISTINCT jsonb_build_object('name', n.name, 'lang', n.lang))                                              FILTER (WHERE n.name      IS NOT NULL), '[]') AS other_names,
146
	COALESCE(jsonb_agg(DISTINCT a.acronym)                                                                                       FILTER (WHERE a.acronym   IS NOT NULL), '[]') AS acronyms,	
147
	COALESCE(jsonb_agg(DISTINCT u.url)                                                                                           FILTER (WHERE u.url       IS NOT NULL), '[]') AS urls,
148
	COALESCE(jsonb_agg(DISTINCT jsonb_build_object('relatedOrgId', relorg.id, 'relatedOrgName', relorg.name, 'type', r.reltype)) FILTER (WHERE relorg.id      IS NOT NULL), '[]') AS relations
149
FROM
150
    organizations org
151
    LEFT OUTER JOIN other_ids oid         ON (org.id = oid.id)
152
    LEFT OUTER JOIN other_names n         ON (org.id = n.id)
153
    LEFT OUTER JOIN acronyms a            ON (org.id = a.id)
154
    LEFT OUTER JOIN urls u                ON (org.id = u.id)
155
    LEFT OUTER JOIN relationships r       ON (org.id = r.id1)
156
    LEFT OUTER JOIN organizations relorg  ON (relorg.id = r.id2)
157
GROUP BY
158
    org.id,
159
    org.name,
160
    org.type,
161
    org.lat,
162
	org.lng,
163
	org.city,
164
	org.country;
165
	
166
CREATE VIEW organizations_info_view AS SELECT
167
	org.id,
168
	org.name,
169
	org.created_by,
170
	org.creation_date,
171
	org.modified_by,
172
	org.modification_date,
173
	count(DISTINCT e.oa_original_id) as n_duplicates,
174
	count(DISTINCT c.oa_original_id) as n_conflicts
175
FROM organizations org
176
	LEFT OUTER JOIN openaire_simrels e ON (org.id = e.local_id AND e.oa_original_id NOT LIKE 'openorgs____::%' AND e.reltype = 'suggested')
177
	LEFT OUTER JOIN openaire_simrels c ON (org.id = c.local_id AND c.oa_original_id     LIKE 'openorgs____::%' AND c.reltype = 'suggested')
178
GROUP BY org.id;
179
	
180
CREATE VIEW organizations_simple_view AS SELECT
181
    org.id,
182
    org.name,
183
    org.type,
184
	org.city,
185
	org.country,
186
	array_agg(DISTINCT a.acronym) FILTER (WHERE a.acronym IS NOT NULL) AS acronyms
187
FROM
188
    organizations org
189
    LEFT OUTER JOIN acronyms a ON (org.id = a.id)
190
GROUP BY
191
    org.id,
192
    org.name,
193
    org.type,
194
	org.city,
195
	org.country;
196
	
197
CREATE VIEW	users_view AS SELECT
198
	u.email,
199
	u.valid,
200
	u.role,
201
	array_agg(uc.country) FILTER (WHERE uc.country IS NOT NULL) AS countries
202
FROM
203
	users u
204
	LEFT OUTER JOIN user_countries uc ON (u.email = uc.email)
205
GROUP BY u.email, u.valid, u.role
206
ORDER BY u.email;
207

    
208
CREATE VIEW conflicts_view AS SELECT
209
    o1.id      AS id_1,
210
    o1.name    AS name_1,
211
    o1.type    AS type_1,
212
	o1.city    AS city_1,
213
	o1.country AS country_1,
214
    o2.id      AS id_2,
215
    o2.name    AS name_2,
216
    o2.type    AS type_2,
217
	o2.city    AS city_2,
218
	o2.country AS country_2
219
FROM
220
	openaire_simrels s
221
	LEFT OUTER JOIN organizations o1 ON (s.local_id = o1.id)
222
    LEFT OUTER JOIN organizations o2 ON (s.oa_original_id = o2.id)
223
WHERE
224
	s.oa_original_id LIKE 'openorgs____::%' AND o1.id IS NOT NULL AND O2.id IS NOT NULL;
225
	
226

    
227

    
228
	
(4-4/4)