Project

General

Profile

1
package eu.dnetlib.uoaadmintoolslibrary.services;
2

    
3
import eu.dnetlib.uoaadmintoolslibrary.dao.PageHelpContentDAO;
4
import eu.dnetlib.uoaadmintoolslibrary.entities.Page;
5
import eu.dnetlib.uoaadmintoolslibrary.entities.PageHelpContent;
6
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal;
7
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.PageHelpContentResponse;
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.ArrayList;
14
import java.util.List;
15

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

    
20
    @Autowired
21
    private PageHelpContentDAO pageHelpContentDAO;
22

    
23
    @Autowired
24
    private PageService pageService;
25

    
26
    @Autowired
27
    private PortalService portalService;
28

    
29
    public List<PageHelpContentResponse> getPageHelpContents(String pid, String portalType, String page, String position, String active, String before) {
30
        List<PageHelpContent> pageHelpContents = null;
31

    
32
        Portal _portal = null;
33
        String portalId = null;
34
        if(pid != null) {
35
            _portal = portalService.getPortal(pid);
36
            if(_portal != null) {
37
                portalId = _portal.getId();
38
            }
39
        }
40

    
41
        if(pid != null && position != null && active != null && before != null) {
42
            pageHelpContents = pageHelpContentDAO.findByPortalAndPlacementAndIsActiveAndIsPriorToOrderByOrderAsc(portalId, position, Boolean.parseBoolean(active), Boolean.parseBoolean(before));
43
        } else if(pid != null && position != null && active != null) {
44
            pageHelpContents = pageHelpContentDAO.findByPortalAndPlacementAndIsActiveOrderByOrderAsc(portalId, position, Boolean.parseBoolean(active));
45
        } else if(pid != null && position != null && before != null) {
46
            pageHelpContents = pageHelpContentDAO.findByPortalAndPlacementAndIsPriorToOrderByOrderAsc(portalId, position, Boolean.parseBoolean(before));
47
        } else if(pid != null && active != null && before != null) {
48
            pageHelpContents = pageHelpContentDAO.findByPortalAndIsActiveAndIsPriorToOrderByPlacementAscOrderAsc(portalId, Boolean.parseBoolean(active), Boolean.parseBoolean(before));
49
        } else if(position != null && active != null && before != null) {
50
            pageHelpContents = pageHelpContentDAO.findByPlacementAndIsActiveAndIsPriorToOrderByOrderAsc(position, Boolean.parseBoolean(active), Boolean.parseBoolean(before));
51
        } else if(pid != null && position != null ) {
52
            pageHelpContents = pageHelpContentDAO.findByPortalAndPlacementOrderByOrderAsc(portalId, position);
53
        } else if(pid != null && active != null ) {
54
            pageHelpContents = pageHelpContentDAO.findByPortalAndIsActiveOrderByPlacementAscOrderAsc(portalId, Boolean.parseBoolean(active));
55
        } else if(pid != null && before != null) {
56
            pageHelpContents = pageHelpContentDAO.findByPortalAndIsPriorToOrderByPlacementAscOrderAsc(portalId, Boolean.parseBoolean(before));
57
        } else if(position != null && active != null) {
58
            pageHelpContents = pageHelpContentDAO.findByPlacementAndIsActiveOrderByOrderAsc(position, Boolean.parseBoolean(active));
59
        } else if(position != null && before != null) {
60
            pageHelpContents = pageHelpContentDAO.findByPlacementAndIsPriorToOrderByOrderAsc(position,  Boolean.parseBoolean(before));
61
        } else if(active != null && before != null) {
62
            pageHelpContents = pageHelpContentDAO.findByIsActiveAndIsPriorToOrderByPlacementAscOrderAsc(Boolean.parseBoolean(active), Boolean.parseBoolean(before));
63
        } else if(pid != null) {
64
            pageHelpContents = pageHelpContentDAO.findByPortalOrderByPlacementAscOrderAsc(portalId);
65
        } else if(position != null) {
66
            pageHelpContents = pageHelpContentDAO.findByPlacementOrderByOrderAsc(position);
67
        } else if(active != null) {
68
            pageHelpContents = pageHelpContentDAO.findByIsActiveOrderByPlacementAscOrderAsc(Boolean.parseBoolean(active));
69
        } else if(before != null) {
70
            pageHelpContents = pageHelpContentDAO.findByIsPriorToOrderByPlacementAscOrderAsc(Boolean.parseBoolean(before));
71
        } else {
72
            pageHelpContents = pageHelpContentDAO.findAllByOrderByPlacementAscOrderAsc();
73
        }
74

    
75
        List<PageHelpContentResponse> pageHelpContentResponses = new ArrayList<>();
76
        for (PageHelpContent pageHelpContent : pageHelpContents) {
77
            Page _page = pageService.getPage(pageHelpContent.getPage());
78
            if((page == null || page.equals(_page.getRoute())) && (portalType == null || portalType.equals(_page.getPortalType()))) {
79
                PageHelpContentResponse pageHelpContentResponse = new PageHelpContentResponse(pageHelpContent);
80

    
81
                pageHelpContentResponse.setPage(_page);
82
                pageHelpContentResponse.setPortal(portalService.getPortalById(pageHelpContent.getPortal()));
83
                pageHelpContentResponses.add(pageHelpContentResponse);
84
            }
85
        }
86
        return pageHelpContentResponses;
87
    }
88

    
89
    public void deleteAllPageHelpContents() {
90
        pageHelpContentDAO.deleteAll();
91
    }
92

    
93
//    public PageHelpContent insertPageHelpContent(PageHelpContent pageHelpContent) {
94
//        String portalId = portalService.getPortal(pageHelpContent.getPortal()).getId();
95
//        pageHelpContent.setPortal(portalId);
96
//        return pageHelpContentDAO.save(pageHelpContent);
97
//    }
98
//
99
//    public PageHelpContent updatePageHelpContent(PageHelpContent pageHelpContent) {
100
//        return pageHelpContentDAO.save(pageHelpContent);
101
//    }
102

    
103
    public PageHelpContent insertOrUpdatePageHelpContent(PageHelpContent pageHelpContent) {
104
        return pageHelpContentDAO.save(pageHelpContent);
105
    }
106

    
107
    public PageHelpContent getPageHelpContent(String id) {
108
        return pageHelpContentDAO.findById(id);
109
    }
110

    
111
    public List<String> togglePageHelpContent(List<String> pageHelpContents, String status) throws Exception {
112
        for (String id: pageHelpContents) {
113
            log.debug("Id of pageHelpContent: "+id);
114
            PageHelpContent pageHelpContent = pageHelpContentDAO.findById(id);
115
            pageHelpContent.setIsActive(Boolean.parseBoolean(status));
116
            pageHelpContentDAO.save(pageHelpContent);
117
        }
118
        return pageHelpContents;
119
    }
120

    
121
    public void deletePageHelpContent(String id) {
122
        pageHelpContentDAO.delete(id);
123
    }
124

    
125
    public Boolean deletePageHelpContents(List<String> pageHelpContents) throws Exception {
126
        for (String id: pageHelpContents) {
127
            pageHelpContentDAO.delete(id);
128
        }
129
        return true;
130
    }
131

    
132
    public void addPageHelpContentsInPortal(String portalId, String portalType) {
133
        Page organizationsPage = pageService.getPageByPortalTypeAndRoute(portalType, "/organizations");
134
        if(organizationsPage != null) {
135
            String organizations_page_content = "<div> <p>Here you can write more details about the organizations related to your community.</p> </div>";
136

    
137
            PageHelpContent organizations_pageHelpContent = new PageHelpContent(organizationsPage.getId(), portalId, "top", 1, organizations_page_content, false, false);
138
            //this.insertPageHelpContent(organizations_pageHelpContent);
139
            pageHelpContentDAO.save(organizations_pageHelpContent);
140
        }
141

    
142
        Page depositLearnHowPage = pageService.getPageByPortalTypeAndRoute(portalType, "/participate/deposit/learn-how");
143
        if(depositLearnHowPage != null) {
144
            String depositLearnHow_page_content = "" +
145
                    "<div class=\"uk-width-3-5 uk-align-center\"> " +
146
                    " <div class=\"uk-text-bold\">How to comply with funder Open Access policies</div> " +
147
                    " <ul class=\"uk-list uk-list-bullet\"> " +
148
                    "   <li>Read the <a href=\"https://www.openaire.eu/how-to-comply-to-h2020-mandates-for-publications\" target=\"_blank\"> <span>OpenAIRE guide to learn how to comply with EC H2020 Open Access policy on publications>/span> <span class=\"custom-external custom-icon space\"> </span> </a></li> " +
149
                    "   <li>Read the <a href=\"https://www.openaire.eu/how-to-comply-to-h2020-mandates-for-data\" target=\"_blank\"> <span>OpenAIRE guide to learn how to comply with EC H2020 Open Access policy on research data</span> <span class=\"custom-external custom-icon space\"> </span></a></li> " +
150
                    "   <li>If you want to know about National Open Access policies, please check them out <a href=\"https://www.openaire.eu/frontpage/country-pages\" target=\"_blank\"><span>here</span> <span class=\"custom-external custom-icon space\"> </span></a></li> " +
151
                    "   <li>All OpenAIRE guides can be found <a href=\"https://www.openaire.eu/guides\" target=\"_blank\"><span>here</span> <span class=\"custom-external custom-icon space\"> </span></a></li> " +
152
                    " </ul> " +
153
                    "</div>";
154

    
155
            PageHelpContent depositLearnHow_pageHelpContent = new PageHelpContent(depositLearnHowPage.getId(), portalId, "bottom", 1, depositLearnHow_page_content, true, false);
156
            //this.insertPageHelpContent(depositLearnHow_pageHelpContent);
157
            pageHelpContentDAO.save(depositLearnHow_pageHelpContent);
158
        }
159
    }
160
}
(4-4/6)