Revision 57130
Added by Michele Artini almost 4 years ago
modules/dnet-orgs-database-application/trunk/src/main/java/eu/dnetlib/organizations/repository/RelationshipRepository.java | ||
---|---|---|
1 |
package eu.dnetlib.organizations.repository; |
|
2 |
|
|
3 |
import org.springframework.data.jpa.repository.JpaRepository; |
|
4 |
|
|
5 |
import eu.dnetlib.organizations.model.Relationship; |
|
6 |
import eu.dnetlib.organizations.model.RelationshipPK; |
|
7 |
|
|
8 |
public interface RelationshipRepository extends JpaRepository<Relationship, RelationshipPK> { |
|
9 |
|
|
10 |
void deleteById1(String id1); |
|
11 |
|
|
12 |
void deleteById2(String id2); |
|
13 |
} |
modules/dnet-orgs-database-application/trunk/src/main/java/eu/dnetlib/organizations/model/Relationship.java | ||
---|---|---|
1 |
package eu.dnetlib.organizations.model; |
|
2 |
|
|
3 |
import java.io.Serializable; |
|
4 |
|
|
5 |
import javax.persistence.Column; |
|
6 |
import javax.persistence.Entity; |
|
7 |
import javax.persistence.Id; |
|
8 |
import javax.persistence.IdClass; |
|
9 |
import javax.persistence.Table; |
|
10 |
|
|
11 |
@Entity |
|
12 |
@Table(name = "relationships") |
|
13 |
@IdClass(RelationshipPK.class) |
|
14 |
public class Relationship implements Serializable { |
|
15 |
|
|
16 |
/** |
|
17 |
* |
|
18 |
*/ |
|
19 |
private static final long serialVersionUID = -5700143694178113214L; |
|
20 |
|
|
21 |
@Id |
|
22 |
@Column(name = "id1") |
|
23 |
private String id1; |
|
24 |
|
|
25 |
@Id |
|
26 |
@Column(name = "id2") |
|
27 |
private String id2; |
|
28 |
|
|
29 |
@Id |
|
30 |
@Column(name = "reltype") |
|
31 |
private String relType; |
|
32 |
|
|
33 |
public Relationship() {} |
|
34 |
|
|
35 |
public Relationship(final String id1, final String id2, final String relType) { |
|
36 |
this.id1 = id1; |
|
37 |
this.id2 = id2; |
|
38 |
this.relType = relType; |
|
39 |
} |
|
40 |
|
|
41 |
public String getId1() { |
|
42 |
return id1; |
|
43 |
} |
|
44 |
|
|
45 |
public void setId1(final String id1) { |
|
46 |
this.id1 = id1; |
|
47 |
} |
|
48 |
|
|
49 |
public String getId2() { |
|
50 |
return id2; |
|
51 |
} |
|
52 |
|
|
53 |
public void setId2(final String id2) { |
|
54 |
this.id2 = id2; |
|
55 |
} |
|
56 |
|
|
57 |
public String getRelType() { |
|
58 |
return relType; |
|
59 |
} |
|
60 |
|
|
61 |
public void setRelType(final String relType) { |
|
62 |
this.relType = relType; |
|
63 |
} |
|
64 |
|
|
65 |
} |
modules/dnet-orgs-database-application/trunk/src/main/java/eu/dnetlib/organizations/model/RelationshipPK.java | ||
---|---|---|
1 |
package eu.dnetlib.organizations.model; |
|
2 |
|
|
3 |
import java.io.Serializable; |
|
4 |
import java.util.Objects; |
|
5 |
|
|
6 |
public class RelationshipPK implements Serializable { |
|
7 |
|
|
8 |
/** |
|
9 |
* |
|
10 |
*/ |
|
11 |
private static final long serialVersionUID = 6090016904833186505L; |
|
12 |
|
|
13 |
private String id1; |
|
14 |
|
|
15 |
private String id2; |
|
16 |
|
|
17 |
private String relType; |
|
18 |
|
|
19 |
public String getId1() { |
|
20 |
return id1; |
|
21 |
} |
|
22 |
|
|
23 |
public void setId1(final String id1) { |
|
24 |
this.id1 = id1; |
|
25 |
} |
|
26 |
|
|
27 |
public String getId2() { |
|
28 |
return id2; |
|
29 |
} |
|
30 |
|
|
31 |
public void setId2(final String id2) { |
|
32 |
this.id2 = id2; |
|
33 |
} |
|
34 |
|
|
35 |
public String getRelType() { |
|
36 |
return relType; |
|
37 |
} |
|
38 |
|
|
39 |
public void setRelType(final String relType) { |
|
40 |
this.relType = relType; |
|
41 |
} |
|
42 |
|
|
43 |
@Override |
|
44 |
public int hashCode() { |
|
45 |
return Objects.hash(id1, id2, relType); |
|
46 |
} |
|
47 |
|
|
48 |
@Override |
|
49 |
public boolean equals(final Object obj) { |
|
50 |
if (this == obj) { return true; } |
|
51 |
if (obj == null) { return false; } |
|
52 |
if (!(obj instanceof RelationshipPK)) { return false; } |
|
53 |
final RelationshipPK other = (RelationshipPK) obj; |
|
54 |
return Objects.equals(id1, other.id1) && Objects.equals(id2, other.id2) && Objects.equals(relType, other.relType); |
|
55 |
} |
|
56 |
|
|
57 |
@Override |
|
58 |
public String toString() { |
|
59 |
return String.format("RelationshipPK [id1=%s, id2=%s, relType=%s]", id1, id2, relType); |
|
60 |
} |
|
61 |
} |
modules/dnet-orgs-database-application/trunk/src/main/java/eu/dnetlib/organizations/utils/RelationType.java | ||
---|---|---|
1 |
package eu.dnetlib.organizations.utils; |
|
2 |
|
|
3 |
public enum RelationType { |
|
4 |
Child, Parent, Related, Other; |
|
5 |
|
|
6 |
public RelationType getInverse() { |
|
7 |
switch (this) { |
|
8 |
case Child: |
|
9 |
return Parent; |
|
10 |
case Parent: |
|
11 |
return Child; |
|
12 |
case Related: |
|
13 |
return Related; |
|
14 |
default: |
|
15 |
return Other; |
|
16 |
} |
|
17 |
} |
|
18 |
} |
modules/dnet-orgs-database-application/trunk/src/main/java/eu/dnetlib/organizations/utils/DatabaseUtils.java | ||
---|---|---|
16 | 16 |
import eu.dnetlib.organizations.model.Organization; |
17 | 17 |
import eu.dnetlib.organizations.model.OtherIdentifier; |
18 | 18 |
import eu.dnetlib.organizations.model.OtherName; |
19 |
import eu.dnetlib.organizations.model.Relationship; |
|
19 | 20 |
import eu.dnetlib.organizations.model.Type; |
20 | 21 |
import eu.dnetlib.organizations.model.Url; |
21 | 22 |
import eu.dnetlib.organizations.model.view.OrganizationView; |
... | ... | |
23 | 24 |
import eu.dnetlib.organizations.repository.OrganizationRepository; |
24 | 25 |
import eu.dnetlib.organizations.repository.OtherIdentifierRepository; |
25 | 26 |
import eu.dnetlib.organizations.repository.OtherNameRepository; |
27 |
import eu.dnetlib.organizations.repository.RelationshipRepository; |
|
26 | 28 |
import eu.dnetlib.organizations.repository.TypeRepository; |
27 | 29 |
import eu.dnetlib.organizations.repository.UrlRepository; |
28 | 30 |
|
... | ... | |
41 | 43 |
private TypeRepository typeRepository; |
42 | 44 |
@Autowired |
43 | 45 |
private UrlRepository urlRepository; |
46 |
@Autowired |
|
47 |
private RelationshipRepository relationshipRepository; |
|
44 | 48 |
|
45 | 49 |
@Autowired |
46 | 50 |
private JdbcTemplate jdbcTemplate; |
... | ... | |
85 | 89 |
return jdbcTemplate.queryForList("select val from " + table, String.class); |
86 | 90 |
} |
87 | 91 |
|
92 |
@Transactional |
|
93 |
public void addRelation(final String from, final String to, final RelationType type) { |
|
94 |
final Relationship r1 = new Relationship(from, to, type.toString()); |
|
95 |
final Relationship r2 = new Relationship(to, from, type.getInverse().toString()); |
|
96 |
relationshipRepository.save(r1); |
|
97 |
relationshipRepository.save(r2); |
|
98 |
} |
|
99 |
|
|
100 |
@Transactional |
|
101 |
public void deleteRelation(final String from, final String to, final RelationType type) { |
|
102 |
final Relationship r1 = new Relationship(from, to, type.toString()); |
|
103 |
final Relationship r2 = new Relationship(to, from, type.getInverse().toString()); |
|
104 |
relationshipRepository.delete(r1); |
|
105 |
relationshipRepository.delete(r2); |
|
106 |
} |
|
107 |
|
|
88 | 108 |
} |
modules/dnet-orgs-database-application/trunk/src/main/java/eu/dnetlib/organizations/utils/SimilarityType.java | ||
---|---|---|
1 |
package eu.dnetlib.organizations.utils; |
|
2 |
|
|
3 |
public enum SimilarityType { |
|
4 |
suggested, is_similar, is_different |
|
5 |
} |
modules/dnet-orgs-database-application/trunk/src/main/java/eu/dnetlib/organizations/controller/VocabulariesController.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.organizations.controller; |
2 | 2 |
|
3 |
import java.util.Arrays; |
|
3 | 4 |
import java.util.HashMap; |
4 | 5 |
import java.util.List; |
5 | 6 |
import java.util.Map; |
7 |
import java.util.stream.Collectors; |
|
6 | 8 |
|
7 | 9 |
import org.springframework.beans.factory.annotation.Autowired; |
8 | 10 |
import org.springframework.web.bind.annotation.RequestMapping; |
... | ... | |
11 | 13 |
|
12 | 14 |
import eu.dnetlib.organizations.utils.DatabaseUtils; |
13 | 15 |
import eu.dnetlib.organizations.utils.DatabaseUtils.VocabularyTable; |
16 |
import eu.dnetlib.organizations.utils.RelationType; |
|
17 |
import eu.dnetlib.organizations.utils.SimilarityType; |
|
14 | 18 |
|
15 | 19 |
@RestController |
16 | 20 |
@RequestMapping("/api/vocabularies") |
... | ... | |
24 | 28 |
final Map<String, List<String>> vocs = new HashMap<>(); |
25 | 29 |
vocs.put("orgTypes", databaseUtils.listValuesOfVocabularyTable(VocabularyTable.org_types)); |
26 | 30 |
vocs.put("idTypes", databaseUtils.listValuesOfVocabularyTable(VocabularyTable.id_types)); |
27 |
vocs.put("relTypes", databaseUtils.listValuesOfVocabularyTable(VocabularyTable.rel_types)); |
|
28 |
vocs.put("similaritiesType", databaseUtils.listValuesOfVocabularyTable(VocabularyTable.simrel_types)); |
|
29 | 31 |
vocs.put("languages", databaseUtils.listValuesOfVocabularyTable(VocabularyTable.languages)); |
30 | 32 |
vocs.put("countries", databaseUtils.listValuesOfVocabularyTable(VocabularyTable.countries)); |
33 |
vocs.put("relTypes", Arrays.stream(RelationType.values()).map(Object::toString).collect(Collectors.toList())); |
|
34 |
vocs.put("similaritiesType", Arrays.stream(SimilarityType.values()).map(Object::toString).collect(Collectors.toList())); |
|
31 | 35 |
return vocs; |
32 | 36 |
} |
33 | 37 |
|
modules/dnet-orgs-database-application/trunk/src/main/java/eu/dnetlib/organizations/controller/OrganizationController.java | ||
---|---|---|
24 | 24 |
import eu.dnetlib.organizations.repository.readonly.OrganizationSimpleViewRepository; |
25 | 25 |
import eu.dnetlib.organizations.repository.readonly.OrganizationViewRepository; |
26 | 26 |
import eu.dnetlib.organizations.utils.DatabaseUtils; |
27 |
import eu.dnetlib.organizations.utils.RelationType; |
|
27 | 28 |
|
28 | 29 |
@RestController |
29 | 30 |
@RequestMapping("/api/organizations") |
... | ... | |
55 | 56 |
return organizationViewRepository.findRelations(id); |
56 | 57 |
} |
57 | 58 |
|
59 |
@RequestMapping(value = "/relations", method = RequestMethod.PUT) |
|
60 |
public List<RelationByOrg> addRelation(@RequestParam final String from, @RequestParam final String to, @RequestParam final RelationType type) { |
|
61 |
if (from.equals(to)) { throw new IllegalArgumentException("Invalid relation !!!"); } |
|
62 |
|
|
63 |
databaseUtils.addRelation(from, to, type); |
|
64 |
return organizationViewRepository.findRelations(from); |
|
65 |
} |
|
66 |
|
|
67 |
@RequestMapping(value = "/relations", method = RequestMethod.DELETE) |
|
68 |
public List<RelationByOrg> deleteRelation(@RequestParam final String from, @RequestParam final String to, @RequestParam final RelationType type) { |
|
69 |
if (from.equals(to)) { throw new IllegalArgumentException("Invalid relation !!!"); } |
|
70 |
|
|
71 |
databaseUtils.deleteRelation(from, to, type); |
|
72 |
return organizationViewRepository.findRelations(from); |
|
73 |
} |
|
74 |
|
|
58 | 75 |
@RequestMapping(value = "/similarities", method = RequestMethod.GET) |
59 | 76 |
public List<OpenaireSimRel> findSimilaritiesById(@RequestParam final String id) { |
60 | 77 |
return openaireSimRelRepository.findByLocalId(id); |
modules/dnet-orgs-database-application/trunk/src/main/resources/sql/schema.sql | ||
---|---|---|
11 | 11 |
DROP TABLE IF EXISTS organizations; |
12 | 12 |
DROP TABLE IF EXISTS org_types; |
13 | 13 |
DROP TABLE IF EXISTS id_types; |
14 |
DROP TABLE IF EXISTS rel_types; |
|
15 |
DROP TABLE IF EXISTS simrel_types; |
|
16 | 14 |
DROP TABLE IF EXISTS languages; |
17 | 15 |
DROP TABLE IF EXISTS countries; |
18 | 16 |
|
... | ... | |
22 | 20 |
CREATE TABLE id_types (val text PRIMARY KEY); |
23 | 21 |
INSERT INTO id_types VALUES ('CNRS'), ('FundRef'), ('HESA'), ('ISNI'), ('OrgRef'), ('UCAS'), ('UKPRN'), ('Wikidata'), ('grid.ac'); |
24 | 22 |
|
25 |
CREATE TABLE rel_types (val text PRIMARY KEY); |
|
26 |
INSERT INTO rel_types VALUES ('Child'), ('Parent'), ('Related'), ('Other'); |
|
27 |
|
|
28 |
CREATE TABLE simrel_types (val text PRIMARY KEY); |
|
29 |
INSERT INTO simrel_types VALUES ('suggested'), ('is_similar'), ('is_different'); |
|
30 |
|
|
31 | 23 |
CREATE TABLE languages (val text PRIMARY KEY); |
32 | 24 |
INSERT INTO languages VALUES ('UNKNOWN'), ('aa'), ('af'), ('am'), ('ar'), ('as'), ('az'), ('ba'), ('be'), ('bg'), ('bn'), ('br'), ('bs'), ('ca'), ('ch'), ('co'), ('cs'), ('cy'), ('da'), |
33 | 25 |
('de'), ('dv'), ('dz'), ('el'), ('en'), ('eo'), ('es'), ('et'), ('eu'), ('fa'), ('fi'), ('fr'), ('fy'), ('ga'), ('gd'), ('gl'), ('gu'), ('he'), ('hi'), ('hr'), ('hu'), ('hy'), ('id'), ('is'), |
... | ... | |
83 | 75 |
|
84 | 76 |
CREATE TABLE relationships ( |
85 | 77 |
id1 text REFERENCES organizations(id), |
86 |
reltype text REFERENCES rel_types(val),
|
|
78 |
reltype text, |
|
87 | 79 |
id2 text REFERENCES organizations(id), |
88 | 80 |
PRIMARY KEY (id1, reltype, id2) |
89 | 81 |
); |
... | ... | |
109 | 101 |
oa_country text, |
110 | 102 |
oa_url text, |
111 | 103 |
oa_collectedfrom text, |
112 |
reltype text NOT NULL DEFAULT 'suggested' REFERENCES simrel_types(val),
|
|
104 |
reltype text NOT NULL DEFAULT 'suggested', |
|
113 | 105 |
PRIMARY KEY (local_id, oa_id) |
114 | 106 |
); |
115 | 107 |
|
modules/dnet-orgs-database-application/trunk/src/main/resources/static/html/relations.html | ||
---|---|---|
34 | 34 |
<td class="pl-3"><a href="#!/metadata/{{r.relatedOrgId}}" title="{{r.relatedOrgId}}">{{r.relatedOrgName}}</a></td> |
35 | 35 |
<td>{{r.type}}</td> |
36 | 36 |
<td class="text-right"> |
37 |
<button type="button" class="btn btn-sm btn-outline-danger" ng-click="rels.splice($index, 1)">delete</button>
|
|
37 |
<button type="button" class="btn btn-sm btn-outline-danger" ng-click="deleteRelation(r.relatedOrgId, r.type)">delete</button>
|
|
38 | 38 |
</td> |
39 | 39 |
</tr> |
40 | 40 |
</tbody> |
... | ... | |
49 | 49 |
<option ng-repeat="t in vocabularies.relTypes">{{t}}</option> |
50 | 50 |
</select> |
51 | 51 |
<div class="input-group-append"> |
52 |
<button type="button" class="btn btn-outline-success" ng-click="rels.push({'relatedOrgId': newRelId, 'relatedOrgName': 'New organization (ID: ' + newRelId + ')', 'type': newRelType}); newRelId=newRelType=''" ng-disabled="!newRelId || !newRelType">add</button>
|
|
52 |
<button type="button" class="btn btn-outline-success" ng-click="addRelation(newRelId, newRelType)" ng-disabled="!newRelId || !newRelType">add</button>
|
|
53 | 53 |
</div> |
54 | 54 |
</div> |
55 | 55 |
</div> |
modules/dnet-orgs-database-application/trunk/src/main/resources/static/js/organizations.js | ||
---|---|---|
181 | 181 |
alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')'); |
182 | 182 |
}); |
183 | 183 |
|
184 |
$scope.addRelation = function(to, type) { |
|
185 |
$http.put('/api/organizations/relations', null, { |
|
186 |
'params': { 'from': $scope.orgId, 'to': to, 'type': type } |
|
187 |
}).then(function successCallback(res) { |
|
188 |
$scope.rels = res.data; |
|
189 |
alert("Relation added !!!") |
|
190 |
}, function errorCallback(res) { |
|
191 |
alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')'); |
|
192 |
}); |
|
193 |
} |
|
194 |
|
|
195 |
$scope.deleteRelation = function(to, type) { |
|
196 |
$http.delete('/api/organizations/relations', { |
|
197 |
'params': { 'from': $scope.orgId, 'to': to, 'type': type } |
|
198 |
}).then(function successCallback(res) { |
|
199 |
$scope.rels = res.data; |
|
200 |
alert("Relation deleted !!!") |
|
201 |
}, function errorCallback(res) { |
|
202 |
alert('ERROR: ' + res.data.error + ' (' + res.data.message + ')'); |
|
203 |
}); |
|
204 |
} |
|
205 |
|
|
184 | 206 |
}); |
185 | 207 |
|
186 | 208 |
orgsModule.controller('showSimilaritiesCtrl', function ($scope, $http, $routeParams) { |
Also available in: Unified diff