Project

General

Profile

« Previous | Next » 

Revision 57097

View differences:

modules/dnet-orgs-database-application/trunk/src/main/java/eu/dnetlib/organizations/repository/OrganizationRepository.java
1 1
package eu.dnetlib.organizations.repository;
2 2

  
3
import java.time.OffsetDateTime;
4

  
3 5
import org.springframework.data.jpa.repository.JpaRepository;
6
import org.springframework.data.jpa.repository.Modifying;
7
import org.springframework.data.jpa.repository.Query;
4 8

  
5 9
import eu.dnetlib.organizations.model.Organization;
6 10

  
7 11
public interface OrganizationRepository extends JpaRepository<Organization, String> {
8 12

  
13
	@Modifying
14
	@Query("update Organization set created_by = ?2, creation_date = ?3 where id = ?1")
15
	void updateCreationDate(String id, String user, OffsetDateTime now);
16

  
17
	@Modifying
18
	@Query("update Organization set modified_by = ?2, modification_date = ?3 where id = ?1")
19
	void updateModificationDate(String id, String user, OffsetDateTime now);
20

  
9 21
}
modules/dnet-orgs-database-application/trunk/src/main/java/eu/dnetlib/organizations/utils/DatabaseUtils.java
1
package eu.dnetlib.organizations.utils;
2

  
3
import java.time.OffsetDateTime;
4
import java.util.UUID;
5

  
6
import javax.transaction.Transactional;
7

  
8
import org.apache.commons.codec.digest.DigestUtils;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.stereotype.Component;
11

  
12
import eu.dnetlib.organizations.model.Acronym;
13
import eu.dnetlib.organizations.model.Organization;
14
import eu.dnetlib.organizations.model.OtherIdentifier;
15
import eu.dnetlib.organizations.model.OtherName;
16
import eu.dnetlib.organizations.model.Type;
17
import eu.dnetlib.organizations.model.Url;
18
import eu.dnetlib.organizations.model.view.OrganizationView;
19
import eu.dnetlib.organizations.repository.AcronymRepository;
20
import eu.dnetlib.organizations.repository.OrganizationRepository;
21
import eu.dnetlib.organizations.repository.OtherIdentifierRepository;
22
import eu.dnetlib.organizations.repository.OtherNameRepository;
23
import eu.dnetlib.organizations.repository.TypeRepository;
24
import eu.dnetlib.organizations.repository.UrlRepository;
25

  
26
@Component
27
public class DatabaseUtils {
28

  
29
	@Autowired
30
	private AcronymRepository acronymRepository;
31
	@Autowired
32
	private OrganizationRepository organizationRepository;
33
	@Autowired
34
	private OtherIdentifierRepository otherIdentifierRepository;
35
	@Autowired
36
	private OtherNameRepository otherNameRepository;
37
	@Autowired
38
	private TypeRepository typeRepository;
39
	@Autowired
40
	private UrlRepository urlRepository;
41

  
42
	@Transactional
43
	public String insertOrUpdateOrganization(final OrganizationView org, final String user, final boolean update) {
44

  
45
		final String orgId = update ? org.getId() : "openorgs____::" + DigestUtils.md5Hex(UUID.randomUUID().toString());
46
		final OffsetDateTime now = OffsetDateTime.now();
47

  
48
		if (update) {
49
			acronymRepository.deleteByOrgId(orgId);
50
			otherNameRepository.deleteByOrgId(orgId);
51
			otherIdentifierRepository.deleteByOrgId(orgId);
52
			typeRepository.deleteByOrgId(orgId);
53
			urlRepository.deleteByOrgId(orgId);
54
		}
55

  
56
		organizationRepository.save(new Organization(orgId, org.getName(), org.getLat(), org.getLng(), org.getCity(), org.getCountry()));
57
		org.getAcronyms().forEach(s -> acronymRepository.save(new Acronym(orgId, s)));
58
		org.getOtherNames().forEach(n -> otherNameRepository.save(new OtherName(orgId, n.getName(), n.getLang())));
59
		org.getOtherIdentifiers().forEach(id -> otherIdentifierRepository.save(new OtherIdentifier(orgId, id.getId(), id.getType())));
60
		org.getTypes().forEach(t -> typeRepository.save(new Type(orgId, t)));
61
		org.getUrls().forEach(u -> urlRepository.save(new Url(orgId, u)));
62

  
63
		if (update) {
64
			organizationRepository.updateModificationDate(orgId, user, now);
65
		} else {
66
			organizationRepository.updateCreationDate(orgId, user, now);
67
			organizationRepository.updateModificationDate(orgId, user, now);
68
		}
69

  
70
		return orgId;
71
	}
72

  
73
}
modules/dnet-orgs-database-application/trunk/src/main/java/eu/dnetlib/organizations/controller/OrganizationController.java
1 1
package eu.dnetlib.organizations.controller;
2 2

  
3
import java.security.Principal;
3 4
import java.util.Arrays;
4 5
import java.util.List;
5
import java.util.UUID;
6 6

  
7
import org.apache.commons.codec.digest.DigestUtils;
8 7
import org.apache.commons.lang3.StringUtils;
9 8
import org.springframework.beans.factory.annotation.Autowired;
10 9
import org.springframework.data.domain.Page;
......
16 15
import org.springframework.web.bind.annotation.RequestParam;
17 16
import org.springframework.web.bind.annotation.RestController;
18 17

  
19
import eu.dnetlib.organizations.model.Acronym;
20 18
import eu.dnetlib.organizations.model.OpenaireSimRel;
21
import eu.dnetlib.organizations.model.Organization;
22
import eu.dnetlib.organizations.model.OtherIdentifier;
23
import eu.dnetlib.organizations.model.OtherName;
24
import eu.dnetlib.organizations.model.Type;
25
import eu.dnetlib.organizations.model.Url;
26 19
import eu.dnetlib.organizations.model.utils.BrowseEntry;
27 20
import eu.dnetlib.organizations.model.utils.RelationByOrg;
28 21
import eu.dnetlib.organizations.model.view.OrganizationSimpleView;
29 22
import eu.dnetlib.organizations.model.view.OrganizationView;
30
import eu.dnetlib.organizations.repository.AcronymRepository;
31 23
import eu.dnetlib.organizations.repository.OpenaireSimRelRepository;
32
import eu.dnetlib.organizations.repository.OrganizationRepository;
33
import eu.dnetlib.organizations.repository.OtherIdentifierRepository;
34
import eu.dnetlib.organizations.repository.OtherNameRepository;
35
import eu.dnetlib.organizations.repository.TypeRepository;
36
import eu.dnetlib.organizations.repository.UrlRepository;
37 24
import eu.dnetlib.organizations.repository.readonly.OrganizationSimpleViewRepository;
38 25
import eu.dnetlib.organizations.repository.readonly.OrganizationViewRepository;
26
import eu.dnetlib.organizations.utils.DatabaseUtils;
39 27

  
40 28
@RestController
41 29
@RequestMapping("/api/organizations")
......
47 35
	private OrganizationSimpleViewRepository organizationSimpleViewRepository;
48 36
	@Autowired
49 37
	private OpenaireSimRelRepository openaireSimRelRepository;
50

  
51 38
	@Autowired
52
	private AcronymRepository acronymRepository;
53
	@Autowired
54
	private OrganizationRepository organizationRepository;
55
	@Autowired
56
	private OtherIdentifierRepository otherIdentifierRepository;
57
	@Autowired
58
	private OtherNameRepository otherNameRepository;
59
	@Autowired
60
	private TypeRepository typeRepository;
61
	@Autowired
62
	private UrlRepository urlRepository;
39
	private DatabaseUtils databaseUtils;
63 40

  
64 41
	@RequestMapping(value = "/save", method = RequestMethod.POST)
65
	public List<String> save(@RequestBody final OrganizationView org) {
66
		final String orgId = insertOrUpdateOrganization(org, StringUtils.isNotBlank(org.getId()));
42
	public List<String> save(@RequestBody final OrganizationView org, final Principal principal) {
43
		final String user = principal != null ? principal.getName() : "anonymous";
44
		final String orgId = databaseUtils.insertOrUpdateOrganization(org, user, StringUtils.isNotBlank(org.getId()));
67 45
		return Arrays.asList(orgId);
68 46
	}
69 47

  
70
	private String insertOrUpdateOrganization(final OrganizationView org, final boolean update) {
71

  
72
		final String orgId = update ? org.getId() : "openorgs____::" + DigestUtils.md5Hex(UUID.randomUUID().toString());
73

  
74
		if (update) {
75
			acronymRepository.deleteByOrgId(orgId);
76
			otherNameRepository.deleteByOrgId(orgId);
77
			otherIdentifierRepository.deleteByOrgId(orgId);
78
			typeRepository.deleteByOrgId(orgId);
79
			urlRepository.deleteByOrgId(orgId);
80
		}
81

  
82
		organizationRepository.save(new Organization(orgId, org.getName(), org.getLat(), org.getLng(), org.getCity(), org.getCountry()));
83
		org.getAcronyms().forEach(s -> acronymRepository.save(new Acronym(orgId, s)));
84
		org.getOtherNames().forEach(n -> otherNameRepository.save(new OtherName(orgId, n.getName(), n.getLang())));
85
		org.getOtherIdentifiers().forEach(id -> otherIdentifierRepository.save(new OtherIdentifier(orgId, id.getId(), id.getType())));
86
		org.getTypes().forEach(t -> typeRepository.save(new Type(orgId, t)));
87
		org.getUrls().forEach(u -> urlRepository.save(new Url(orgId, u)));
88

  
89
		if (update) {
90
			// SET LAST MODIFICATION DATE AND USER
91
		} else {
92
			// SET LAST CREATION AND MODIFICATION 5DATE AND USER
93
		}
94

  
95
		return orgId;
96
	}
97

  
98 48
	@RequestMapping(value = "/get", method = RequestMethod.GET)
99 49
	public OrganizationView findById(@RequestParam final String id) {
100 50
		return organizationViewRepository.findById(id).get();
modules/dnet-orgs-database-application/trunk/src/main/resources/sql/import_grid_ac.sql
92 92

  
93 93
INSERT INTO organizations(id, name, lat, lng, city, country, created_by, modified_by) (SELECT 'openorgs____::'||md5(o.grid_id), o.name, a.lat, a.lng, a.city, a.country_code, 'import:grid.ac', 'import:grid.ac'  FROM grid_institutes o LEFT OUTER JOIN grid_addresses a ON (o.grid_id=a.grid_id)) ON CONFLICT DO NOTHING;
94 94
INSERT INTO other_ids    (id, otherid, type)                         (SELECT 'openorgs____::'||md5(grid_id), grid_id,     'grid.ac'                                              FROM grid_institutes   ) ON CONFLICT DO NOTHING;
95
INSERT INTO other_ids    (id, otherid, type)                         (SELECT 'openorgs____::'||md5(grid_id), external_id, external_id_type::id_type                              FROM grid_external_ids ) ON CONFLICT DO NOTHING;
95
INSERT INTO other_ids    (id, otherid, type)                         (SELECT 'openorgs____::'||md5(grid_id), external_id, external_id_type                                       FROM grid_external_ids ) ON CONFLICT DO NOTHING;
96 96
INSERT INTO other_names  (id, lang, name)                            (SELECT 'openorgs____::'||md5(grid_id), 'en',   name                                                        FROM grid_institutes   ) ON CONFLICT DO NOTHING;
97 97
INSERT INTO other_names  (id, lang, name)                            (SELECT 'openorgs____::'||md5(grid_id), iso639, label                                                       FROM grid_labels       ) ON CONFLICT DO NOTHING;
98 98
INSERT INTO other_names  (id, lang, name)                            (SELECT 'openorgs____::'||md5(grid_id), '',     alias                                                       FROM grid_aliases      ) ON CONFLICT DO NOTHING;
99 99
INSERT INTO acronyms     (id, acronym)                               (SELECT 'openorgs____::'||md5(grid_id), acronym                                                             FROM grid_acronyms     ) ON CONFLICT DO NOTHING;
100
INSERT INTO relationships(id1, reltype, id2)                         (SELECT 'openorgs____::'||md5(grid_id), relationship_type::rel_type, 'openorgs____::'||md5(related_grid_id) FROM grid_relationships) ON CONFLICT DO NOTHING;
101
INSERT INTO types        (id, type)                                  (SELECT 'openorgs____::'||md5(grid_id), type::org_type                                                      FROM grid_types        ) ON CONFLICT DO NOTHING;
100
INSERT INTO relationships(id1, reltype, id2)                         (SELECT 'openorgs____::'||md5(grid_id), relationship_type, 'openorgs____::'||md5(related_grid_id)           FROM grid_relationships) ON CONFLICT DO NOTHING;
101
INSERT INTO types        (id, type)                                  (SELECT 'openorgs____::'||md5(grid_id), type                                                                FROM grid_types        ) ON CONFLICT DO NOTHING;
102 102
INSERT INTO urls         (id, url)                                   (SELECT 'openorgs____::'||md5(grid_id), link                                                                FROM grid_links        ) ON CONFLICT DO NOTHING;
103 103

  
modules/dnet-orgs-database-application/trunk/src/main/resources/sql/schema.sql
9 9
DROP TABLE IF EXISTS urls;
10 10
DROP TABLE IF EXISTS openaire_simrels;
11 11
DROP TABLE IF EXISTS organizations;
12
DROP TABLE IF EXISTS org_types;
13
DROP TABLE IF EXISTS id_types;
14
DROP TABLE IF EXISTS rel_types;
15
DROP TABLE IF EXISTS simrel_types;
12 16

  
13
DROP TYPE IF EXISTS org_type;
14
DROP TYPE IF EXISTS id_type;
15
DROP TYPE IF EXISTS rel_type;
16
DROP TYPE IF EXISTS simrel_type;
17
CREATE TABLE org_types (val text PRIMARY KEY);
18
INSERT INTO org_types VALUES ('Archive'), ('Company'), ('Education'), ('Facility'), ('Government'), ('Healthcare'), ('Nonprofit'), ('Other');
17 19

  
18
CREATE TYPE org_type AS ENUM ('Archive', 'Company', 'Education', 'Facility', 'Government', 'Healthcare', 'Nonprofit', 'Other');
19
CREATE TYPE id_type  AS ENUM ('CNRS', 'FundRef', 'HESA', 'ISNI', 'OrgRef', 'UCAS', 'UKPRN', 'Wikidata', 'grid.ac');
20
CREATE TYPE rel_type AS ENUM ('Child', 'Parent', 'Related', 'Other');
21
CREATE TYPE simrel_type AS ENUM ('suggested', 'is_similar', 'is_different');
20
CREATE TABLE id_types (val text PRIMARY KEY);
21
INSERT INTO id_types VALUES ('CNRS'), ('FundRef'), ('HESA'), ('ISNI'), ('OrgRef'), ('UCAS'), ('UKPRN'), ('Wikidata'), ('grid.ac');
22 22

  
23
CREATE TABLE rel_types (val text PRIMARY KEY);
24
INSERT INTO rel_types VALUES ('Child'), ('Parent'), ('Related'), ('Other');
23 25

  
26
CREATE TABLE simrel_types (val text PRIMARY KEY);
27
INSERT INTO simrel_types VALUES ('suggested'), ('is_similar'), ('is_different');
28

  
29

  
24 30
CREATE TABLE organizations (
25 31
    id                text PRIMARY KEY,
26 32
    name              text,
......
37 43
CREATE TABLE other_ids (
38 44
	id      text REFERENCES organizations(id),
39 45
	otherid text,
40
	type    id_type,
46
	type    text REFERENCES id_types(val),
41 47
	PRIMARY KEY (id, otherid, type)
42 48
);
43 49

  
......
56 62

  
57 63
CREATE TABLE relationships (
58 64
	id1     text REFERENCES organizations(id),
59
	reltype rel_type,
65
	reltype text REFERENCES rel_types(val),
60 66
	id2     text REFERENCES organizations(id),
61 67
    PRIMARY KEY (id1, reltype, id2)
62 68
);
63 69

  
64 70
CREATE TABLE types (
65 71
	id   text REFERENCES organizations(id),
66
	type org_type,
72
	type text REFERENCES org_types(val),
67 73
	PRIMARY KEY (id, type)
68 74
);
69 75

  
......
82 88
	oa_country       text,
83 89
	oa_url           text,
84 90
	oa_collectedfrom text,
85
	reltype          simrel_type NOT NULL DEFAULT 'suggested',
91
	reltype          text  NOT NULL DEFAULT 'suggested' REFERENCES simrel_types(val),
86 92
	PRIMARY KEY (local_id, oa_id)
87 93
);
88 94

  
modules/dnet-orgs-database-application/trunk/src/main/resources/static/html/edit.html
219 219
							</div>
220 220
						</div>
221 221
					</fieldset>
222
					
223
					<button type="submit" class="btn btn-primary" ng-click="save()">save</button>
222 224
				</form>
223 225
			</div>
224 226
		</div>
modules/dnet-orgs-database-application/trunk/src/main/resources/static/js/organizations.js
48 48
	$http.get('/api/vocabularies').then(function successCallback(res) {
49 49
		$scope.vocabularies = res.data;
50 50
	}, function errorCallback(res) {
51
		alert("Error");
51
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
52 52
	});
53 53
	
54 54
});
......
65 65
	$http.get('/api/organizations/search/' + $routeParams.page + '/' + $routeParams.size + '?q=' + $scope.fieldValue).then(function successCallback(res) {
66 66
		$scope.orgs = res.data;
67 67
	}, function errorCallback(res) {
68
		alert("Error");
68
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
69 69
	});
70 70
});
71 71

  
......
77 77
	$http.get('/api/organizations/browse/countries').then(function successCallback(res) {
78 78
		$scope.entries = res.data;
79 79
	}, function errorCallback(res) {
80
		alert("Error");
80
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
81 81
	});
82 82
	
83 83
});
......
91 91
	$http.get('/api/organizations/byCountry/' + $routeParams.code + '/' + $routeParams.page + '/' + $routeParams.size).then(function successCallback(res) {
92 92
		$scope.orgs = res.data;
93 93
	}, function errorCallback(res) {
94
		alert("Error");
94
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
95 95
	});
96 96
});
97 97

  
......
103 103
	$http.get('/api/organizations/browse/types').then(function successCallback(res) {
104 104
		$scope.entries = res.data;
105 105
	}, function errorCallback(res) {
106
		alert("Error");
106
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
107 107
	});
108 108
	
109 109
});
......
117 117
	$http.get('/api/organizations/byType/' + $routeParams.type + '/' + $routeParams.page + '/' + $routeParams.size).then(function successCallback(res) {
118 118
		$scope.orgs = res.data;
119 119
	}, function errorCallback(res) {
120
		alert("Error");
120
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
121 121
	});
122 122
});
123 123

  
124 124

  
125
orgsModule.controller('showMetadataCtrl', function ($scope, $http, $routeParams) {
125
orgsModule.controller('showMetadataCtrl', function ($scope, $http, $routeParams, $route) {
126 126
	$scope.orgId = $routeParams.id;
127 127
	$scope.org = {};
128 128
	$scope.vocabularies = {};
......
133 133
		$http.get('/api/organizations/get?id=' + $routeParams.id).then(function successCallback(res) {
134 134
			$scope.org = res.data;
135 135
		}, function errorCallback(res) {
136
			alert("Error");
136
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
137 137
		});
138 138
	}, function errorCallback(res) {
139
		alert("Error");
139
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
140 140
	});
141 141
	
142
	$scope.save = function() {
143
		$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
144
		$http.post('/api/organizations/save?update=true', $scope.org).then(function successCallback(res) {
145
			alert('Organization updated !!!');
146
			$route.reload();
147
		}, function errorCallback(res) {
148
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
149
		});
150
	}
151
	
142 152
});
143 153

  
144 154

  
......
153 163
		$http.get('/api/organizations/relations?id=' + $routeParams.id).then(function successCallback(res) {
154 164
			$scope.rels = res.data;
155 165
		}, function errorCallback(res) {
156
			alert("Error");
166
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
157 167
		});
158 168
	}, function errorCallback(res) {
159
		alert("Error");
169
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
160 170
	});
161 171
	
162 172
});
......
172 182
		$http.get('/api/organizations/similarities?id=' + $routeParams.id).then(function successCallback(res) {
173 183
			$scope.similarities = res.data;
174 184
		}, function errorCallback(res) {
175
			alert("Error");
185
			alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
176 186
		});
177 187
	}, function errorCallback(res) {
178
		alert("Error");
188
		alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')');
179 189
	});
180 190
	
181 191
});

Also available in: Unified diff