1
|
package eu.dnetlib.uoaadmintoolslibrary.controllers;
|
2
|
|
3
|
import eu.dnetlib.uoaadmintoolslibrary.dao.CommunityDAO;
|
4
|
import eu.dnetlib.uoaadmintoolslibrary.dao.EntityDAO;
|
5
|
import eu.dnetlib.uoaadmintoolslibrary.dao.PageDAO;
|
6
|
import eu.dnetlib.uoaadmintoolslibrary.entities.Community;
|
7
|
import eu.dnetlib.uoaadmintoolslibrary.entities.Entity;
|
8
|
import eu.dnetlib.uoaadmintoolslibrary.entities.Page;
|
9
|
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.CommunityEntity;
|
10
|
import org.apache.log4j.Logger;
|
11
|
import org.springframework.beans.factory.annotation.Autowired;
|
12
|
import org.springframework.web.bind.annotation.*;
|
13
|
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
|
17
|
@RestController
|
18
|
@CrossOrigin(origins = "*")
|
19
|
public class EntityController {
|
20
|
private final Logger log = Logger.getLogger(this.getClass());
|
21
|
|
22
|
@Autowired
|
23
|
private EntityDAO entityDAO;
|
24
|
|
25
|
@Autowired
|
26
|
private PageDAO pageDAO;
|
27
|
|
28
|
@Autowired
|
29
|
private CommunityDAO communityDAO;
|
30
|
|
31
|
@RequestMapping(value = "/entity", method = RequestMethod.GET)
|
32
|
public List<Entity> getAllEntities() {
|
33
|
return entityDAO.findAll();
|
34
|
}
|
35
|
|
36
|
@RequestMapping(value = "/entity", method = RequestMethod.DELETE)
|
37
|
public void deleteAllEntities() {
|
38
|
entityDAO.deleteAll();
|
39
|
}
|
40
|
|
41
|
@RequestMapping(value = "/entity", method = RequestMethod.POST)
|
42
|
public Entity insertOrUpdateEntity(@RequestBody Entity entity) {
|
43
|
return entityDAO.save(entity);
|
44
|
}
|
45
|
|
46
|
@RequestMapping(value = "/entity/{id}", method = RequestMethod.GET)
|
47
|
public Entity getEntity(@PathVariable(value = "id") String id) {
|
48
|
return entityDAO.findById(id);
|
49
|
}
|
50
|
|
51
|
@RequestMapping(value = "/entity/{id}", method = RequestMethod.DELETE)
|
52
|
public void deleteEntity(@PathVariable(value = "id") String id) {
|
53
|
entityDAO.delete(id);
|
54
|
}
|
55
|
|
56
|
@RequestMapping(value = "/entity/update", method = RequestMethod.POST)
|
57
|
public CommunityEntity updateEntity(@RequestBody CommunityEntity communityEntity) {
|
58
|
Entity entity = this.getEntityByCommunityEntity(communityEntity);
|
59
|
entityDAO.save(entity);
|
60
|
|
61
|
return communityEntity;
|
62
|
}
|
63
|
|
64
|
@RequestMapping(value = "/entity/save", method = RequestMethod.POST)
|
65
|
public CommunityEntity insertEntity(@RequestBody Entity entity) {
|
66
|
//Entity entity = this.getEntityByCommunityEntity(communityEntity);
|
67
|
Entity savedEntity = entityDAO.save(entity);
|
68
|
CommunityEntity communityEntity = new CommunityEntity(savedEntity);
|
69
|
|
70
|
// add entity in communities
|
71
|
List<Community> communities = communityDAO.findAll();
|
72
|
for( Community community : communities ) {
|
73
|
Map<String, Boolean> entities = community.getEntities();
|
74
|
entities.put(entity.getId(), true);
|
75
|
community.setEntities(entities);
|
76
|
communityDAO.save(community);
|
77
|
}
|
78
|
|
79
|
return communityEntity;
|
80
|
}
|
81
|
|
82
|
private Entity getEntityByCommunityEntity(CommunityEntity communityEntity) {
|
83
|
Entity entity = new Entity();
|
84
|
entity.setId(communityEntity.getId());
|
85
|
entity.setPid(communityEntity.getPid());
|
86
|
entity.setName(communityEntity.getName());
|
87
|
|
88
|
return entity;
|
89
|
}
|
90
|
|
91
|
@RequestMapping(value = "/entity/delete", method = RequestMethod.POST)
|
92
|
public Boolean deleteEntities(@RequestBody List<String> entities) throws Exception {
|
93
|
for (String id: entities) {
|
94
|
entityDAO.delete(id);
|
95
|
|
96
|
// delete entity from communities
|
97
|
List<Community> communities = communityDAO.findAll();
|
98
|
for( Community community : communities ) {
|
99
|
Map<String, Boolean> communityEntities = community.getEntities();
|
100
|
communityEntities.remove(id);
|
101
|
community.setEntities(communityEntities);
|
102
|
communityDAO.save(community);
|
103
|
}
|
104
|
|
105
|
// delete entity from pages
|
106
|
List<Page> pages = pageDAO.findAll();
|
107
|
for( Page page : pages ) {
|
108
|
List<String> pageEntities = page.getEntities();
|
109
|
int index = 0;
|
110
|
for(String pageEntity : pageEntities) {
|
111
|
if(pageEntity == id) {
|
112
|
pageEntities.remove(index);
|
113
|
break;
|
114
|
}
|
115
|
index++;
|
116
|
}
|
117
|
page.setEntities(pageEntities);
|
118
|
pageDAO.save(page);
|
119
|
}
|
120
|
}
|
121
|
|
122
|
return true;
|
123
|
}
|
124
|
}
|