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