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.*;
8
import org.apache.log4j.Logger;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.web.bind.annotation.*;
11

    
12
import java.util.*;
13

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

    
19
    @Autowired
20
    private DivIdDAO divIdDAO;
21

    
22
    @Autowired
23
    private CommunityDAO communityDAO;
24

    
25
    @Autowired
26
    private PageDAO pageDAO;
27

    
28
    @Autowired
29
    private DivHelpContentController divHelpContentController;
30

    
31
    DivIdController() {}
32

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

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

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

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

    
70
        List<DivIdResponse> divIdResponses = new ArrayList<>();
71
        for(DivId divId : divIds) {
72
            DivIdResponse divIdResponse = new DivIdResponse(divId);
73
            divIdResponse.setCommunity(communityDAO.findById(divId.getCommunity()));
74
            List<Page> pages = new ArrayList<>();
75
            for(String pageId : divId.getPages()) {
76
                pages.add(pageDAO.findById(pageId));
77
            }
78
            divIdResponse.setPages(pages);
79

    
80
            divIdResponses.add(divIdResponse);
81
        }
82

    
83
        return divIdResponses;
84
    }
85

    
86
    @RequestMapping(value = "/div/{id}", method = RequestMethod.GET)
87
    public DivId getDivId(@PathVariable(value = "id") String id) {
88
        return divIdDAO.findById(id);
89
    }
90

    
91
    @RequestMapping(value = "/divFull/{id}", method = RequestMethod.GET)
92
    public DivIdResponse getDivIdFull(@PathVariable(value = "id") String id) {
93
        DivId divId = divIdDAO.findById(id);
94
        DivIdResponse divIdResponse = new DivIdResponse(divId);
95
        divIdResponse.setCommunity(communityDAO.findById(divId.getCommunity()));
96
        List<Page> pages = new ArrayList<>();
97
        for(String pageId : divId.getPages()) {
98
            pages.add(pageDAO.findById(pageId));
99
        }
100
        divIdResponse.setPages(pages);
101
        return divIdResponse;
102
    }
103

    
104
    @RequestMapping(value = "/div", method = RequestMethod.DELETE)
105
    public void deleteAllDivIds() {
106
        divIdDAO.deleteAll();
107
    }
108

    
109
    @RequestMapping(value = "/div/save", method = RequestMethod.POST)
110
    public DivIdResponse insertDivId(@RequestBody DivId divId) {
111
        String community_id = communityDAO.findByPid(divId.getCommunity()).getId();
112
        divId.setCommunity(community_id);
113
        divIdDAO.save(divId);
114
        return this.getDivIdFull(divId.getId());
115
    }
116

    
117
    @RequestMapping(value = "/div/update", method = RequestMethod.POST)
118
    public DivIdResponse updateDivId(@RequestBody DivId _divId) {
119
        DivId divId = divIdDAO.findById(_divId.getId());
120
        divId.setName(_divId.getName());
121
        /*List<String> pageIds = new ArrayList<>();
122
        for(Page page : divIdResponse.getPages()) {
123
            pageIds.add(page.getId());
124
        }*/
125
        divId.setPages(_divId.getPages());
126
        divIdDAO.save(divId);
127
        return this.getDivIdFull(divId.getId());
128
    }
129

    
130
    @RequestMapping(value = "/div/delete", method = RequestMethod.POST)
131
    public Boolean deleteDivIds(@RequestBody List<String> divIds) throws Exception {
132
        for (String id: divIds) {
133
            DivId divId = divIdDAO.findById(id);
134

    
135
            Community community = communityDAO.findById(divId.getCommunity());
136

    
137
            // delete div contents related to this divId
138
            List<DivHelpContentResponse> divHelpContentResponses = divHelpContentController.getDivHelpContents(community.getPid(), null, divId.getName(), null);
139
            for(DivHelpContentResponse divHelpContentResponse : divHelpContentResponses) {
140
                divHelpContentController.deleteDivHelpContent(divHelpContentResponse.getId());
141
            }
142

    
143
            divIdDAO.delete(id);
144
        }
145
        return true;
146
    }
147

    
148
    @RequestMapping(value = "/div/{id}", method = RequestMethod.DELETE)
149
    public void deleteDivId(@PathVariable(value = "id") String id) {
150
        divIdDAO.delete(id);
151
    }
152

    
153
    @RequestMapping(value = "/div/pages", method = RequestMethod.GET)
154
    public Map<String, Set<String>> getDivIdsPages(@RequestParam(required = false) String community) {
155
        List<DivId> divIds = null;
156
        Map<String, Set<String>> hasCommunityPageDivIds = new HashMap<>();
157
        if(community != null) {
158
            String communityId = communityDAO.findByPid(community).getId();
159
            divIds = divIdDAO.findByCommunity(communityId);
160
        } else {
161
            divIds = divIdDAO.findAll();
162
        }
163

    
164
        for(DivId divId : divIds) {
165
            String communityPid;
166
            if(community == null) {
167
                communityPid = communityDAO.findById(divId.getCommunity()).getPid();
168
            } else {
169
                communityPid = community;
170
            }
171

    
172
            if(hasCommunityPageDivIds.containsKey(communityPid)) {
173
                for(String pageId : divId.getPages()) {
174
                    hasCommunityPageDivIds.get(communityPid).add(pageId);
175
                }
176
            } else {
177
                hasCommunityPageDivIds.put(communityPid, new HashSet<>());
178
                for(String pageId : divId.getPages()) {
179
                    hasCommunityPageDivIds.get(communityPid).add(pageId);
180
                }
181
            }
182
        }
183
        return hasCommunityPageDivIds;
184
    }
185

    
186
}
(3-3/10)