Project

General

Profile

1
package eu.dnetlib.uoaadmintoolslibrary.services;
2

    
3
import eu.dnetlib.uoaadmintoolslibrary.dao.EntityDAO;
4
import eu.dnetlib.uoaadmintoolslibrary.entities.Entity;
5
import eu.dnetlib.uoaadmintoolslibrary.entities.Page;
6
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal;
7
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.PortalEntity;
8

    
9
import org.apache.log4j.Logger;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.stereotype.Service;
12

    
13
import java.util.Iterator;
14
import java.util.List;
15
import java.util.Map;
16

    
17
@Service
18
public class EntityService {
19
    private final Logger log = Logger.getLogger(this.getClass());
20

    
21
    @Autowired
22
    private EntityDAO entityDAO;
23

    
24
    @Autowired
25
    private PageService pageService;
26

    
27
    @Autowired
28
    private PortalService portalService;
29

    
30
    public List<Entity> getAllEntities() {
31
        return entityDAO.findAll();
32
    }
33

    
34
    public void deleteAllEntities() {
35
        entityDAO.deleteAll();
36
    }
37

    
38
    public Entity insertOrUpdateEntity(Entity entity) {
39
        return entityDAO.save(entity);
40
    }
41

    
42
    public Entity getEntity(String id) {
43
        return entityDAO.findById(id);
44
    }
45

    
46
    public Entity getEntityByPid(String pid) {
47
        return entityDAO.findByPid(pid);
48
    }
49

    
50
    public void deleteEntity(String id) {
51
        entityDAO.delete(id);
52
    }
53

    
54
    public PortalEntity updateEntity(PortalEntity portalEntity) {
55
        Entity entity = this.getEntityByPortalEntity(portalEntity);
56
        entityDAO.save(entity);
57

    
58
        return portalEntity;
59
    }
60

    
61
    public PortalEntity insertEntity(Entity entity) {
62
        Entity savedEntity = entityDAO.save(entity);
63
        PortalEntity portalEntity = new PortalEntity(savedEntity);
64

    
65
        // add entity in portals
66
        List<Portal> portals = portalService.getAllPortals();
67
        for( Portal portal : portals ) {
68
            Map<String, Boolean> entities = portal.getEntities();
69
            entities.put(entity.getId(), true);
70
            portal.setEntities(entities);
71
            portalService.insertOrUpdatePortal(portal);
72
        }
73

    
74
        return portalEntity;
75
    }
76

    
77
    private Entity getEntityByPortalEntity(PortalEntity portalEntity) {
78
        Entity entity = new Entity();
79
        entity.setId(portalEntity.getId());
80
        entity.setPid(portalEntity.getPid());
81
        entity.setName(portalEntity.getName());
82

    
83
        return entity;
84
    }
85

    
86
    public Boolean deleteEntities(List<String> entities) throws Exception {
87
        for (String id: entities) {
88
            // delete entity from portals
89
            List<Portal> portals = portalService.getAllPortals();
90
            for( Portal portal : portals ) {
91
                Map<String, Boolean> portalEntities = portal.getEntities();
92
                portalEntities.remove(id);
93
                portal.setEntities(portalEntities);
94
                portalService.insertOrUpdatePortal(portal);
95
            }
96

    
97
            // delete entity from pages
98
            List<Page> pages = pageService.getAllPages(null, null, null);
99
            for( Page page : pages ) {
100
                List<String> pageEntities = page.getEntities();
101
                Iterator<String> pageEntityIterator = pageEntities.iterator();
102
                while(pageEntityIterator.hasNext()) {
103
                    String pageEntity = pageEntityIterator.next();
104
                    if(pageEntity.equals(id)) {
105
                        pageEntityIterator.remove();
106
                        break;
107
                    }
108
                }
109
                page.setEntities(pageEntities);
110
                pageService.insertOrUpdatePage(page);
111
            }
112
            entityDAO.delete(id);
113
        }
114

    
115
        return true;
116
    }
117
}
(3-3/6)