Project

General

Profile

1
package eu.dnetlib.uoaadmintools.controllers;
2

    
3
import com.sun.org.apache.xpath.internal.operations.Div;
4
import eu.dnetlib.uoaadmintools.dao.CommunityDAO;
5
import eu.dnetlib.uoaadmintools.dao.DivIdDAO;
6
import eu.dnetlib.uoaadmintools.dao.PageDAO;
7
import eu.dnetlib.uoaadmintools.entities.DivId;
8
import eu.dnetlib.uoaadmintools.entities.DivIdResponse;
9
import eu.dnetlib.uoaadmintools.entities.Page;
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.*;
15

    
16
@RestController
17
@CrossOrigin(origins = "*")
18
public class DivIdController {
19
    private final Logger log = Logger.getLogger(this.getClass());
20

    
21
    @Autowired
22
    private DivIdDAO divIdDAO;
23

    
24
    @Autowired
25
    private CommunityDAO communityDAO;
26

    
27
    @Autowired
28
    private PageDAO pageDAO;
29

    
30
    DivIdController() {}
31

    
32
    @RequestMapping(value = "/div", method = RequestMethod.GET)
33
    public List<DivId> getDivIds(@RequestParam(required = false) String community,
34
                                 @RequestParam(required = false) String page,
35
                                 @RequestParam(required = false) String name) {
36
        List<DivId> divIds = null;
37
        String communityId = null;
38
        if(community != null) {
39
            communityId = communityDAO.findByPid(community).getId();
40
        }
41

    
42
        if(community != null && page != null && name != null) {
43
            divIds = divIdDAO.findByCommunityAndPageAndName(communityId, page, name);
44
        } else if(community != null && page != null) {
45
            divIds = divIdDAO.findByCommunityAndPage(communityId, page);
46
        } else if(community != null && name != null) {
47
            divIds = divIdDAO.findByCommunityAndName(communityId, name);
48
        } else if(page != null && name != null) {
49
            divIds = divIdDAO.findByPageAndName(page, name);
50
        } else if(community != null) {
51
            divIds = divIdDAO.findByCommunity(communityId);
52
        } else if(page != null) {
53
            divIds = divIdDAO.findByPage(page);
54
        } else if(name != null) {
55
            divIds = divIdDAO.findByName(name);
56
        } else {
57
            divIds = divIdDAO.findAll();
58
        }
59
        return divIds;
60
    }
61

    
62
    @RequestMapping(value = "/divFull", method = RequestMethod.GET)
63
    public List<DivIdResponse> getDivIdsFull(@RequestParam(required = false) String community,
64
                                             @RequestParam(required = false) String page,
65
                                             @RequestParam(required = false) String name) {
66

    
67
        List<DivId> divIds = this.getDivIds(community, page, name);
68

    
69
        List<DivIdResponse> divIdResponses = new ArrayList<>();
70
        for(DivId divId : divIds) {
71
            DivIdResponse divIdResponse = new DivIdResponse(divId);
72
            divIdResponse.setCommunity(communityDAO.findById(divId.getCommunity()));
73
            divIdResponse.setPage(pageDAO.findById(divId.getPage()));
74

    
75
            divIdResponses.add(divIdResponse);
76
        }
77

    
78
        return divIdResponses;
79
    }
80

    
81
    @RequestMapping(value = "/div/{id}", method = RequestMethod.GET)
82
    public DivId getDivId(@PathVariable(value = "id") String id) {
83
        return divIdDAO.findById(id);
84
    }
85

    
86
    @RequestMapping(value = "/divFull/{id}", method = RequestMethod.GET)
87
    public DivIdResponse getDivIdFull(@PathVariable(value = "id") String id) {
88
        DivId divId = divIdDAO.findById(id);
89
        DivIdResponse divIdResponse = new DivIdResponse(divId);
90
        divIdResponse.setCommunity(communityDAO.findById(divId.getCommunity()));
91
        divIdResponse.setPage(pageDAO.findById(divId.getPage()));
92
        return divIdResponse;
93
    }
94

    
95
    @RequestMapping(value = "/div", method = RequestMethod.DELETE)
96
    public void deleteAllDivIds() {
97
        divIdDAO.deleteAll();
98
    }
99

    
100
    @RequestMapping(value = "/div/save", method = RequestMethod.POST)
101
    public DivIdResponse insertDivId(@RequestBody DivId divId) {
102
        String community_id = communityDAO.findByPid(divId.getCommunity()).getId();
103
        divId.setCommunity(community_id);
104
        divIdDAO.save(divId);
105
        return this.getDivIdFull(divId.getId());
106
    }
107

    
108
    @RequestMapping(value = "/div/update", method = RequestMethod.POST)
109
    public DivIdResponse updateDivId(@RequestBody DivIdResponse divIdResponse) {
110
        DivId divId = divIdDAO.findById(divIdResponse.getId());
111
        divId.setName(divIdResponse.getName());
112
        divId.setPage(divIdResponse.getPage().getId());
113
        divIdDAO.save(divId);
114
        return divIdResponse;
115
    }
116

    
117
    @RequestMapping(value = "/div/delete", method = RequestMethod.POST)
118
    public Boolean deleteDivIds(@RequestBody List<String> divIds) throws Exception {
119
        for (String id: divIds) {
120
            divIdDAO.delete(id);
121
        }
122
        return true;
123
    }
124

    
125
    @RequestMapping(value = "/div/{id}", method = RequestMethod.DELETE)
126
    public void deleteDivId(@PathVariable(value = "id") String id) {
127
        divIdDAO.delete(id);
128
    }
129

    
130
    @RequestMapping(value = "/div/pages", method = RequestMethod.GET)
131
    public Map<String, Set<String>> getDivIdsPages(@RequestParam(required = false) String community) {
132
        List<DivId> divIds = null;
133
        Map<String, Set<String>> hasCommunityPageDivIds = new HashMap<>();
134
        if(community != null) {
135
            String communityId = communityDAO.findByPid(community).getId();
136
            divIds = divIdDAO.findByCommunity(communityId);
137
        } else {
138
            divIds = divIdDAO.findAll();
139
        }
140

    
141
        for(DivId divId : divIds) {
142
            String communityPid;
143
            if(community == null) {
144
                communityPid = communityDAO.findById(divId.getCommunity()).getPid();
145
            } else {
146
                communityPid = community;
147
            }
148

    
149
            if(hasCommunityPageDivIds.containsKey(communityPid)) {
150
                hasCommunityPageDivIds.get(communityPid).add(divId.getPage());
151
            } else {
152
                hasCommunityPageDivIds.put(communityPid, new HashSet<>());
153
                hasCommunityPageDivIds.get(communityPid).add(divId.getPage());
154
            }
155
        }
156
        return hasCommunityPageDivIds;
157
    }
158
}
(3-3/9)