Project

General

Profile

1
package eu.dnetlib.uoaadmintoolslibrary.services;
2

    
3
import eu.dnetlib.uoaadmintoolslibrary.dao.DivHelpContentDAO;
4
import eu.dnetlib.uoaadmintoolslibrary.entities.*;
5
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.DivHelpContentResponse;
6
import eu.dnetlib.uoaadmintoolslibrary.entities.fullEntities.DivIdResponse;
7

    
8
import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException;
9
import eu.dnetlib.uoaadmintoolslibrary.handlers.MismatchingContentException;
10
import org.apache.log4j.Logger;
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.stereotype.Service;
13

    
14
import java.util.ArrayList;
15
import java.util.Iterator;
16
import java.util.List;
17

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

    
22
    @Autowired
23
    private DivHelpContentDAO divHelpContentDAO;
24

    
25
    @Autowired
26
    DivIdService divIdService;
27

    
28
    @Autowired
29
    private PortalService portalService;
30

    
31
    public List<DivHelpContentResponse> getDivHelpContents(String pid, String page_route, String divIdId, String active) {
32

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

    
39
//        DivId divId = divIdService.getDivIdByName(divId);
40
//        String divIdId = null;
41
//        if(divId != null) {
42
//            divIdId = divId.getId();
43
//        }
44

    
45
        List<DivHelpContentResponse> divHelpContentResponses = new ArrayList<>();
46

    
47
        List<DivHelpContent> divHelpContents = null;
48
        if(pid != null && divIdId != null && active != null) {
49
            divHelpContents = divHelpContentDAO.findByPortalAndDivIdAndIsActive(portalId, divIdId, Boolean.parseBoolean(active));
50
        } else if(pid != null && divIdId != null) {
51
            divHelpContents = divHelpContentDAO.findByPortalAndDivId(portalId, divIdId);
52
        } else if(pid != null && active != null) {
53
            divHelpContents = divHelpContentDAO.findByPortalAndIsActive(portalId, Boolean.parseBoolean(active));
54
        } else if(divIdId != null && active != null) {
55
            divHelpContents = divHelpContentDAO.findByDivIdAndIsActive(divIdId, Boolean.parseBoolean(active));
56
        } else if(pid != null) {
57
            divHelpContents = divHelpContentDAO.findByPortal(portalId);
58
        } else if(divIdId != null) {
59
            divHelpContents = divHelpContentDAO.findByDivId(divIdId);
60
        } else if(active != null){
61
            divHelpContents = divHelpContentDAO.findByIsActive(Boolean.parseBoolean(active));
62
        } else {
63
            divHelpContents = divHelpContentDAO.findAll();
64
        }
65

    
66
        for (DivHelpContent divHelpContent : divHelpContents) {
67
            DivIdResponse divIdResponse = null;
68
            DivId divId;
69
            if(divIdId == null) {
70
                divId = divIdService.getDivId(divHelpContent.getDivId());
71
            } else {
72
                divId = divIdService.getDivId(divIdId);
73
            }
74
            divIdResponse = divIdService.divIdResponseFromDivId(divId);
75

    
76
            if(pid == null) {
77
                _portal = portalService.getPortalById(divHelpContent.getPortal());
78
            }
79

    
80
            for(Page p : divIdResponse.getPages()) {
81
                if (page_route == null || p.getRoute().equals(page_route)) {
82
                    DivHelpContentResponse divHelpContentResponse = new DivHelpContentResponse(divHelpContent);
83
                    divHelpContentResponse.setDivId(divIdResponse);
84
                    divHelpContentResponse.setPortal(_portal);
85
                    divHelpContentResponses.add(divHelpContentResponse);
86
                    break;
87
                }
88
            }
89
        }
90

    
91
        return divHelpContentResponses;
92
    }
93

    
94

    
95
    public List<DivHelpContent> getDivHelpContentsBasic(String pid, String portalType, String pageId) {
96
        List<DivHelpContent> divHelpContents = null;
97

    
98
        Portal _portal = null;
99
        String portalId = null;
100
        if(pid != null) {
101
            _portal = portalService.getPortal(pid);
102
            portalService.checkPortalInfo(pid, portalType, _portal, pid, "pid");
103
            if(_portal != null) {
104
                portalId = _portal.getId();
105
            }
106
        }
107

    
108
        if(pid != null) {
109
            divHelpContents = divHelpContentDAO.findByPortal(portalId);
110
        } else {
111
            divHelpContents = divHelpContentDAO.findAll();
112
        }
113

    
114
        if(pageId != null) {
115
            Iterator<DivHelpContent> divHelpContentsIterator = divHelpContents.iterator();
116
            while (divHelpContentsIterator.hasNext()) {
117
                DivHelpContent divHelpContent = divHelpContentsIterator.next();
118

    
119
                String divIdId = divHelpContent.getDivId();
120
                DivId divId = divIdService.getDivId(divIdId);
121

    
122
                Boolean remove = true;
123
                for (String pageIdInDivId : divId.getPages()) {
124
                    if (pageId.equals(pageIdInDivId)) {
125
                        remove = false;
126
                        break;
127
                    }
128
                }
129
                if(remove) {
130
                    divHelpContentsIterator.remove();
131
                }
132
            }
133
        }
134

    
135
        return divHelpContents;
136
    }
137

    
138
    public DivHelpContent getDivHelpContent(String id) {
139
        return divHelpContentDAO.findById(id);
140
    }
141

    
142
    public DivHelpContent insertOrUpdateDivHelpContent(DivHelpContent divHelpContent) {
143
        return divHelpContentDAO.save(divHelpContent);
144
    }
145

    
146
    public void deleteDivHelpContent(String id) {
147
        divHelpContentDAO.delete(id);
148
    }
149

    
150
    public Boolean deleteDivHelpContents(List<String> divHelpContents, String pid, PortalType portalType) throws Exception {
151
        Portal portal = portalService.getPortal(pid);
152
        portalService.checkPortalInfo(pid, portalType.name(), portal, pid, "pid");
153

    
154
        for (String id: divHelpContents) {
155
            DivHelpContent divHelpContent = getDivHelpContent(id);
156
            if(divHelpContent == null) {
157
                throw new ContentNotFoundException("Div help content with id: " + id + " not found");
158
            }
159
            if(!divHelpContent.getPortal().equals(portal.getId())) {
160
                throw new MismatchingContentException("["+portalType+ " - "+ pid+"] Conflicting div help content: portal id: "+divHelpContent.getPortal());
161
            }
162

    
163
            divHelpContentDAO.delete(id);
164
        }
165
        return true;
166
    }
167

    
168
    public List<String> toggleDivHelpContent(List<String> divHelpContents, String status, String pid, PortalType portalType) throws Exception {
169
        Portal portal = portalService.getPortal(pid);
170
        portalService.checkPortalInfo(pid, portalType.name(), portal, pid, "pid");
171

    
172
        for (String id: divHelpContents) {
173
            log.debug("Id of divHelpContent: "+id);
174
            DivHelpContent divHelpContent = getDivHelpContent(id);
175
            if(divHelpContent == null) {
176
                throw new ContentNotFoundException("Div help content with id: " + id + " not found");
177
            }
178
            if(!divHelpContent.getPortal().equals(portal.getId())) {
179
                throw new MismatchingContentException("["+portalType+ " - "+ pid+"] Conflicting div help content: portal id: "+divHelpContent.getPortal());
180
            }
181

    
182
            divHelpContent.setIsActive(Boolean.parseBoolean(status));
183
            divHelpContentDAO.save(divHelpContent);
184
        }
185
        return divHelpContents;
186
    }
187

    
188
    public void addDivHelpContentsInPortal(String portalId, String portalType) {
189
        //String organizations_class_content = "<div> <p>Here you can write more details about the organizations related to your portal.</p> </div>";
190
        String link_context_form_content = "<div> <div><span class=\"uk-text-bold\"><span uk-icon=\"icon: info\">&nbsp;</span> Information:</span> Select a research portal and/or a category and search for a portal concept, or browse to the portal tree through the categories</div> </div>";
191
        String link_project_form_content = "<div> <div><span class=\"uk-text-bold\"><span uk-icon=\"icon: info\">&nbsp;</span> Information:</span> Search for projects using project name or grant id. Limit results filtering by funder.</div> </div>";
192
        String link_result_form_content = "<div> <div><span class=\"uk-text-bold\"><span uk-icon=\"icon: info\">&nbsp;</span> Information:</span></div> Search for research results in OpenAIRE information space, Datacite, CrossRef or ORCID. <div class=\"uk-text-small\">Use keywords, DOI (more than one - space separated), author&#39;s ORCID</div> </div>";
193
        String link_result_bulk_content = "<div> <div><span class=\"uk-text-bold\"><span uk-icon=\"icon: info\">&nbsp;</span> Information:</span> Upload a csv file containing a list of DOIs. For each DOI found in the file, metadata will be fetched from CrossRef or Datacite and will be added to your selected research results.</div> <div class=\"uk-margin-top uk-text-small\"><span class=\"uk-text-bold\">CSV format:</span> <ul class=\"uk-list\"> <li>The format of CSV file should be &quot;DOI&quot;,&quot;ACCESS_MODE&quot;,&quot;DATE&quot;.</li> <li>The value &quot;DOI&quot; is required</li> <li>Access mode column should have values: &quot;OPEN&quot;,&quot;CLOSED&quot; or &quot;EMBARGO&quot;.</li> <li>Date column valid format is YYYY-MM-DD and is required when access mode has value EMBARGO.</li> <li>In case access mode is not available default value is &quot;OPEN&quot;.</li> </ul> </div> </div>";
194
        String link_metadata_content = "<div> <div><span class=\"uk-text-bold\"><span uk-icon=\"icon: info\">&nbsp;</span> Information:</span> Manage access mode &amp; type of selected research results. For OpenAIRE this functionality isn&#39;t available.</div> </div>";
195

    
196
        String footer_content = "<p class=\"uk-margin-remove-bottom\"><span style=\"font-size:8pt\">OpenAIRE has received funding from the European Union's Horizon 2020 research and innovation programme under grant agreements No. 777541, 731011 and 101017452</span></p>";
197

    
198
        List<DivId> divIds = divIdService.getDivIdsByPortalType(portalType);
199

    
200
        for( DivId div : divIds ) {
201
            if (div != null) {
202
//                    (div.getOpenaire() && pid.equals("openaire")) ||
203
//                            (div.getConnect() && pid.equals("connect")) ||
204
//                            (div.getCommunities() && !pid.equals("openaire") && !pid.equals("connect"))
205
//            )) {
206
                /*
207
                if (div.getName().equals("organizations")) {
208
                    this.insertOrUpdateDivHelpContent(new DivHelpContent(div.getId(), portalId, organizations_class_content, false));
209
                } else
210
                */
211
                if (div.getName().equals("link-context-form")) {
212
                    this.insertOrUpdateDivHelpContent(new DivHelpContent(div.getId(), portalId, link_context_form_content, false));
213
                } else if (div.getName().equals("link-project-form")) {
214
                    this.insertOrUpdateDivHelpContent(new DivHelpContent(div.getId(), portalId, link_project_form_content, false));
215
                } else if (div.getName().equals("link-result-form")) {
216
                    this.insertOrUpdateDivHelpContent(new DivHelpContent(div.getId(), portalId, link_result_form_content, false));
217
                } else if (div.getName().equals("link-result-bulk")) {
218
                    this.insertOrUpdateDivHelpContent(new DivHelpContent(div.getId(), portalId, link_result_bulk_content, false));
219
                } else if (div.getName().equals("link-metadata")) {
220
                    this.insertOrUpdateDivHelpContent(new DivHelpContent(div.getId(), portalId, link_metadata_content, false));
221
                } else if (div.getName().equals("footer")) {
222
                    this.insertOrUpdateDivHelpContent(new DivHelpContent(div.getId(), portalId, footer_content, true));
223
                }
224
            }
225
        }
226
    }
227
}
(1-1/6)