132 lines
4.6 KiB
Java
132 lines
4.6 KiB
Java
package eu.dnetlib.uoaadmintools.controllers;
|
|
|
|
import eu.dnetlib.uoaadmintools.dao.CommunityDAO;
|
|
import eu.dnetlib.uoaadmintools.dao.PageDAO;
|
|
import eu.dnetlib.uoaadmintools.entities.Community;
|
|
import eu.dnetlib.uoaadmintools.entities.CommunityEntity;
|
|
import eu.dnetlib.uoaadmintools.entities.Entity;
|
|
import eu.dnetlib.uoaadmintools.dao.EntityDAO;
|
|
|
|
import eu.dnetlib.uoaadmintools.entities.Page;
|
|
import org.apache.log4j.Logger;
|
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
@RestController
|
|
@CrossOrigin(origins = "*")
|
|
public class EntityController {
|
|
private final Logger log = Logger.getLogger(this.getClass());
|
|
|
|
@Autowired
|
|
private EntityDAO entityDAO;
|
|
|
|
@Autowired
|
|
private PageDAO pageDAO;
|
|
|
|
@Autowired
|
|
private CommunityDAO communityDAO;
|
|
|
|
@RequestMapping(value = "/entity", method = RequestMethod.GET)
|
|
public List<Entity> getAllEntities() {
|
|
return entityDAO.findAll();
|
|
}
|
|
|
|
@RequestMapping(value = "/entity", method = RequestMethod.DELETE)
|
|
public void deleteAllEntities() {
|
|
entityDAO.deleteAll();
|
|
}
|
|
|
|
@RequestMapping(value = "/entity", method = RequestMethod.POST)
|
|
public Entity insertOrUpdateEntity(@RequestBody Entity entity) {
|
|
return entityDAO.save(entity);
|
|
}
|
|
|
|
@RequestMapping(value = "/entity/{id}", method = RequestMethod.GET)
|
|
public Entity getEntity(@PathVariable(value = "id") String id) {
|
|
return entityDAO.findById(id);
|
|
}
|
|
|
|
@RequestMapping(value = "/entity/{id}", method = RequestMethod.DELETE)
|
|
public void deleteEntity(@PathVariable(value = "id") String id) {
|
|
entityDAO.delete(id);
|
|
}
|
|
|
|
@RequestMapping(value = "/entity/update", method = RequestMethod.POST)
|
|
public CommunityEntity updateEntity(@RequestBody CommunityEntity communityEntity) {
|
|
Entity entity = this.getEntityByCommunityEntity(communityEntity);
|
|
entityDAO.save(entity);
|
|
|
|
return communityEntity;
|
|
}
|
|
|
|
@RequestMapping(value = "/entity/save", method = RequestMethod.POST)
|
|
public CommunityEntity insertEntity(@RequestBody Entity entity) {
|
|
//Entity entity = this.getEntityByCommunityEntity(communityEntity);
|
|
Entity savedEntity = entityDAO.save(entity);
|
|
CommunityEntity communityEntity = new CommunityEntity(savedEntity);
|
|
|
|
// add entity in communities
|
|
List<Community> communities = communityDAO.findAll();
|
|
for( Community community : communities ) {
|
|
Map<String, Boolean> entities = community.getEntities();
|
|
entities.put(entity.getId(), true);
|
|
community.setEntities(entities);
|
|
communityDAO.save(community);
|
|
}
|
|
|
|
return communityEntity;
|
|
}
|
|
|
|
private Entity getEntityByCommunityEntity(CommunityEntity communityEntity) {
|
|
Entity entity = new Entity();
|
|
entity.setId(communityEntity.getId());
|
|
entity.setPid(communityEntity.getPid());
|
|
entity.setName(communityEntity.getName());
|
|
|
|
return entity;
|
|
}
|
|
|
|
@RequestMapping(value = "/entity/delete", method = RequestMethod.POST)
|
|
public Boolean deleteEntities(@RequestBody List<String> entities) throws Exception {
|
|
for (String id: entities) {
|
|
entityDAO.delete(id);
|
|
|
|
// delete entity from communities
|
|
List<Community> communities = communityDAO.findAll();
|
|
for( Community community : communities ) {
|
|
Map<String, Boolean> communityEntities = community.getEntities();
|
|
communityEntities.remove(id);
|
|
community.setEntities(communityEntities);
|
|
communityDAO.save(community);
|
|
}
|
|
|
|
// delete entity from pages
|
|
List<Page> pages = pageDAO.findAll();
|
|
for( Page page : pages ) {
|
|
List<String> pageEntities = page.getEntities();
|
|
int index = 0;
|
|
for(String pageEntity : pageEntities) {
|
|
if(pageEntity == id) {
|
|
pageEntities.remove(index);
|
|
break;
|
|
}
|
|
index++;
|
|
}
|
|
page.setEntities(pageEntities);
|
|
pageDAO.save(page);
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|