Project

General

Profile

1
package eu.dnetlib.uoaadmintools.controllers;
2

    
3
import eu.dnetlib.uoaadmintools.dao.CommunityDAO;
4
import eu.dnetlib.uoaadmintools.dao.HtmlPageContentDAO;
5
import eu.dnetlib.uoaadmintools.dao.PageDAO;
6
import eu.dnetlib.uoaadmintools.entities.Community;
7
import eu.dnetlib.uoaadmintools.entities.HtmlPageContent;
8
import eu.dnetlib.uoaadmintools.entities.HtmlPageContentResponse;
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.ArrayList;
15
import java.util.Iterator;
16
import java.util.List;
17

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

    
23
    @Autowired
24
    private HtmlPageContentDAO htmlPageContentDAO;
25

    
26
    @Autowired
27
    private PageDAO pageDAO;
28

    
29
    @Autowired
30
    private CommunityDAO communityDAO;
31

    
32
    @RequestMapping(value = "/htmlpagecontent", method = RequestMethod.GET)
33
    public List<HtmlPageContent> getHtmlPageContents(@RequestParam(required = false) String community,
34
                                                     @RequestParam(required = false) String page) {
35
        List<HtmlPageContent> htmlPageContents = null;
36

    
37
        Community _community = null;
38
        String communityId = null;
39
        if(community != null) {
40
            _community = communityDAO.findByPid(community);
41
            if(_community != null) {
42
                communityId = _community.getId();
43
            }
44
        }
45

    
46
        if(community != null) {
47
            htmlPageContents = htmlPageContentDAO.findByCommunity(communityId);
48
        } else {
49
            htmlPageContents = htmlPageContentDAO.findAll();
50
        }
51

    
52
        Iterator<HtmlPageContent> iterator = htmlPageContents.iterator();
53
        while(iterator.hasNext()) {
54
            HtmlPageContent htmlPageContent = iterator.next();
55

    
56
            Page _page = pageDAO.findById(htmlPageContent.getPage());
57
            if (page != null && !page.equals(_page.getRoute())) {
58
                iterator.remove();
59
            }
60
        }
61
        return htmlPageContents;
62
    }
63

    
64
    /*
65
    @RequestMapping(value = "/htmlpagecontent", method = RequestMethod.DELETE)
66
    public void deleteAllHtmlPageContents() {
67
        htmlPageContentDAO.deleteAll();
68
    }
69

    
70
    @RequestMapping(value = "/htmlpagecontent/save", method = RequestMethod.POST)
71
    public HtmlPageContent insertHtmlPageContent(@RequestBody HtmlPageContent htmlPageContent) {
72
        String communityId = communityDAO.findByPid(htmlPageContent.getCommunity()).getId();
73
        htmlPageContent.setCommunity(communityId);
74
        return htmlPageContentDAO.save(htmlPageContent);
75
    }*/
76

    
77
    @RequestMapping(value = "/htmlpagecontent/update", method = RequestMethod.POST)
78
    public HtmlPageContent updateHtmlPageContent(@RequestBody HtmlPageContent htmlPageContent) {
79
        return htmlPageContentDAO.save(htmlPageContent);
80
    }
81

    
82
    @RequestMapping(value = "/htmlpagecontent/{id}", method = RequestMethod.DELETE)
83
    public void deleteHtmlPageContent(@PathVariable(value = "id") String id) {
84
        htmlPageContentDAO.delete(id);
85
    }
86
    /*
87
    @RequestMapping(value = "/htmlpagecontent/{id}", method = RequestMethod.GET)
88
    public HtmlPageContent getHtmlPageContent(@PathVariable(value = "id") String id) {
89
        return htmlPageContentDAO.findById(id);
90
    }
91

    
92

    
93
    @RequestMapping(value = "/htmlpagecontent/delete", method = RequestMethod.POST)
94
    public Boolean deleteHtmlPageContents(@RequestBody List<String> htmlPageContents) throws Exception {
95
        for (String id: htmlPageContents) {
96
            htmlPageContentDAO.delete(id);
97
        }
98
        return true;
99
    }
100
    */
101
}
(7-7/15)