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.List;
16

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

    
21
    @Autowired
22
    private DivHelpContentDAO divHelpContentDAO;
23

    
24
    @Autowired
25
    DivIdService divIdService;
26

    
27
    @Autowired
28
    private PortalService portalService;
29

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

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

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

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

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

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

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

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

    
90
        return divHelpContentResponses;
91
    }
92

    
93
    public DivHelpContent getDivHelpContent(String id) {
94
        return divHelpContentDAO.findById(id);
95
    }
96

    
97
    public DivHelpContent insertOrUpdateDivHelpContent(DivHelpContent divHelpContent) {
98
        return divHelpContentDAO.save(divHelpContent);
99
    }
100

    
101
    public void deleteDivHelpContent(String id) {
102
        divHelpContentDAO.delete(id);
103
    }
104

    
105
    public Boolean deleteDivHelpContents(List<String> divHelpContents, String pid, PortalType portalType) throws Exception {
106
        Portal portal = portalService.getPortal(pid);
107
        portalService.checkPortalInfo(pid, portalType.name(), portal, pid, "pid");
108

    
109
        for (String id: divHelpContents) {
110
            DivHelpContent divHelpContent = getDivHelpContent(id);
111
            if(divHelpContent == null) {
112
                throw new ContentNotFoundException("Div help content with id: " + id + " not found");
113
            }
114
            if(!divHelpContent.getPortal().equals(portal.getId())) {
115
                throw new MismatchingContentException("["+portalType+ " - "+ pid+"] Conflicting div help content: portal id: "+divHelpContent.getPortal());
116
            }
117

    
118
            divHelpContentDAO.delete(id);
119
        }
120
        return true;
121
    }
122

    
123
    public List<String> toggleDivHelpContent(List<String> divHelpContents, String status, String pid, PortalType portalType) throws Exception {
124
        Portal portal = portalService.getPortal(pid);
125
        portalService.checkPortalInfo(pid, portalType.name(), portal, pid, "pid");
126

    
127
        for (String id: divHelpContents) {
128
            log.debug("Id of divHelpContent: "+id);
129
            DivHelpContent divHelpContent = getDivHelpContent(id);
130
            if(divHelpContent == null) {
131
                throw new ContentNotFoundException("Div help content with id: " + id + " not found");
132
            }
133
            if(!divHelpContent.getPortal().equals(portal.getId())) {
134
                throw new MismatchingContentException("["+portalType+ " - "+ pid+"] Conflicting div help content: portal id: "+divHelpContent.getPortal());
135
            }
136

    
137
            divHelpContent.setIsActive(Boolean.parseBoolean(status));
138
            divHelpContentDAO.save(divHelpContent);
139
        }
140
        return divHelpContents;
141
    }
142

    
143
    public void addDivHelpContentsInPortal(String portalId, String portalType) {
144
        //String organizations_class_content = "<div> <p>Here you can write more details about the organizations related to your portal.</p> </div>";
145
        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>";
146
        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>";
147
        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>";
148
        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>";
149
        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>";
150

    
151
        List<DivId> divIds = divIdService.getDivIdsByPortalType(portalType);
152

    
153
        for( DivId div : divIds ) {
154
            if (div != null) {
155
//                    (div.getOpenaire() && pid.equals("openaire")) ||
156
//                            (div.getConnect() && pid.equals("connect")) ||
157
//                            (div.getCommunities() && !pid.equals("openaire") && !pid.equals("connect"))
158
//            )) {
159
                /*
160
                if (div.getName().equals("organizations")) {
161
                    this.insertOrUpdateDivHelpContent(new DivHelpContent(div.getId(), portalId, organizations_class_content, false));
162
                } else
163
                */
164
                if (div.getName().equals("link-context-form")) {
165
                    this.insertOrUpdateDivHelpContent(new DivHelpContent(div.getId(), portalId, link_context_form_content, false));
166
                } else if (div.getName().equals("link-project-form")) {
167
                    this.insertOrUpdateDivHelpContent(new DivHelpContent(div.getId(), portalId, link_project_form_content, false));
168
                } else if (div.getName().equals("link-result-form")) {
169
                    this.insertOrUpdateDivHelpContent(new DivHelpContent(div.getId(), portalId, link_result_form_content, false));
170
                } else if (div.getName().equals("link-result-bulk")) {
171
                    this.insertOrUpdateDivHelpContent(new DivHelpContent(div.getId(), portalId, link_result_bulk_content, false));
172
                } else if (div.getName().equals("link-metadata")) {
173
                    this.insertOrUpdateDivHelpContent(new DivHelpContent(div.getId(), portalId, link_metadata_content, false));
174
                }
175
            }
176
        }
177
    }
178
}
(1-1/6)