Project

General

Profile

« Previous | Next » 

Revision 51525

1. Add roles for aai
2. Changes on converter file for new interface object
3. Move emailUtils to avoid bug on bean definitions
4. Add openaire provider authorities mapper class
5. Bug fixes on controllers

View differences:

modules/uoa-repository-manager-service/branches/dev-api/src/main/java/eu/dnetlib/repo/manager/service/utils/OMTDAuthoritiesMapper.java
1
package eu.dnetlib.repo.manager.service.utils;
2

  
3
import com.nimbusds.jwt.JWT;
4
import org.mitre.openid.connect.client.OIDCAuthoritiesMapper;
5
import org.mitre.openid.connect.model.UserInfo;
6
import org.slf4j.Logger;
7
import org.slf4j.LoggerFactory;
8
import org.springframework.security.core.GrantedAuthority;
9
import org.springframework.security.core.authority.SimpleGrantedAuthority;
10

  
11
import java.util.*;
12

  
13
public class OMTDAuthoritiesMapper implements OIDCAuthoritiesMapper {
14

  
15
    private static Logger logger = LoggerFactory.getLogger(OMTDAuthoritiesMapper.class);
16

  
17
    final private static String ROLE_CLAIMS = "edu_person_entitlements";
18

  
19
    private Map<String,SimpleGrantedAuthority> userRolesMap;
20

  
21
    OMTDAuthoritiesMapper(Map<String,String> userRoles) {
22
        userRolesMap = new HashMap<>();
23
        userRoles.forEach((omtdRole, appRole) -> userRolesMap.put(omtdRole, new SimpleGrantedAuthority(appRole)));
24
    }
25

  
26
    @Override
27
    public Collection<? extends GrantedAuthority> mapAuthorities(JWT idToken, UserInfo userInfo) {
28
        Set<GrantedAuthority> out = new HashSet<>();
29
        out.add(new SimpleGrantedAuthority("ROLE_USER"));
30
        if(userInfo.getSource().getAsJsonArray(ROLE_CLAIMS) != null) {
31
            userInfo.getSource().getAsJsonArray(ROLE_CLAIMS).forEach(role -> {
32
                SimpleGrantedAuthority authority = userRolesMap.get(role.getAsString());
33
                if (authority != null) {
34
                    logger.debug("Role mapped " + role);
35
                    out.add(authority);
36
                }
37
            });
38
        }
39
        return out;
40
    }
41
}
modules/uoa-repository-manager-service/branches/dev-api/src/main/java/eu/dnetlib/repo/manager/service/utils/EmailUtils.java
1
package eu.dnetlib.repo.manager.service.utils;
2

  
3
import eu.dnetlib.domain.data.PiwikInfo;
4
import eu.dnetlib.domain.functionality.UserProfile;
5

  
6
public interface EmailUtils {
7

  
8

  
9
    void reportException(Exception exception);
10

  
11
    void sendAdministratorRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception;
12

  
13
    void sendUserRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception;
14

  
15
    void sendAdministratorMetricsEnabled(PiwikInfo piwikInfo) throws Exception;
16

  
17
    void sendUserMetricsEnabled(PiwikInfo piwikInfo) throws Exception;
18
}
modules/uoa-repository-manager-service/branches/dev-api/src/main/java/eu/dnetlib/repo/manager/service/utils/EmailUtilsImpl.java
1
package eu.dnetlib.repo.manager.service.utils;
2

  
3
import eu.dnetlib.domain.data.PiwikInfo;
4
import eu.dnetlib.repo.manager.service.config.CascadingPropertyLoader;
5
import eu.dnetlib.utils.MailLibrary;
6
import org.apache.log4j.Logger;
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.beans.factory.annotation.Value;
9
import org.springframework.stereotype.Component;
10

  
11
import java.io.PrintWriter;
12
import java.io.StringWriter;
13
import java.io.Writer;
14
import java.util.ArrayList;
15
import java.util.List;
16

  
17

  
18
@Component
19
public class EmailUtilsImpl implements EmailUtils {
20

  
21
    private static Logger LOGGER = Logger.getLogger(EmailUtilsImpl.class);
22

  
23
    private List<String> specialRecipients = new ArrayList<String>();
24
    private boolean override = false, logonly = false;
25
    private String overrideEmail = null, from = null;
26

  
27
    @Autowired
28
    private MailLibrary mailLibrary;
29

  
30
    @Autowired
31
    private CascadingPropertyLoader pLoader;
32

  
33
    @Value("${services.repo-manager.baseUrl}")
34
    private String baseUrl;
35

  
36
    @Value("${services.repo-manager.adminEmail}")
37
    private String adminEmail;
38

  
39
    @Value("${services.repomanager.usagestats.adminEmail}")
40
    private String usageStatsAdminEmail;
41

  
42

  
43
    @Override
44
    public void reportException(Exception exception) {
45
        Writer writer = new StringWriter();
46
        PrintWriter printWriter = new PrintWriter(writer);
47
        exception.printStackTrace(printWriter);
48

  
49
        List<String> recipients = new ArrayList<String>();
50

  
51
        try {
52
            recipients.add(this.adminEmail);
53
            String message = "An exception has occurred:\n"+writer.toString();
54
            String subject = "Automatic Bug Report";
55
            this.sendMail(recipients, subject, message, false, null);
56
        } catch (Exception e) {
57
            LOGGER.error("Error sending error report", e);
58
        }
59
    }
60

  
61
    @Override
62
    public void sendAdministratorRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception {
63

  
64
        try {
65
            String subject = "[OpenAIRE-Usage Statistics] New request to enable usage statistics";
66

  
67
            String message = "Dear administrator,\n" +
68
                    "\n" +
69
                    "we have received a request to enable the OpenAIRE usage statistics for the following repository \n" +
70
                    "\n" +
71
                    "Repository - " + piwikInfo.getRepositoryName() + ", " + piwikInfo.getCountry() + " (" + piwikInfo.getRepositoryId() + ")\n" +
72
                    "Requestor - " + piwikInfo.getRequestorName() + ", " + piwikInfo.getRequestorEmail() + "\n" +
73
                    "Piwik ID - " + piwikInfo.getSiteId() + "\n" +
74
                    "Authentication token - " + piwikInfo.getAuthenticationToken() + "\n" +
75
                    "\n" +
76
                    "For more information about this request, go here: \n" +
77
                    this.baseUrl + "/#admin/metrics\n" +
78
                    "\n" +
79
                    "Best,\n" +
80
                    "The OpenAIRE team";
81

  
82
            this.sendMail(this.usageStatsAdminEmail, subject, message, false, null);
83

  
84
        } catch (Exception e) {
85
            LOGGER.error("Error while sending request to enable metrics email to administrator: " + this.usageStatsAdminEmail, e);
86
            throw e;
87
        }
88
    }
89

  
90
    @Override
91
    public void sendUserRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception {
92

  
93
        try {
94
            String subject = "[OpenAIRE-Usage Statistics] Your request to enable usage statistics";
95

  
96
            String message = "Dear " + piwikInfo.getRequestorName() + ",\n" +
97
                    "\n" +
98
                    "we have received your request to enable the OpenAIRE usage statistics for your repository\n" +
99
                    "\n" +
100
                    "Repository - " + piwikInfo.getRepositoryName() + ", " + piwikInfo.getCountry() + " (" + piwikInfo.getRepositoryId() + ")\n" +
101
                    "Piwik ID - " + piwikInfo.getSiteId() + "\n" +
102
                    "Authentication token - " + piwikInfo.getAuthenticationToken() + "\n" +
103
                    "\n" +
104
                    "In order to enable the usage statistics, you must install the OpenAIRE's tracking code in your repository software. " +
105
                    "OpenAIRE's usage statistics service tracking code is maintained on Github as a patch for various versions of DSpace " +
106
                    "(https://github.com/openaire/OpenAIRE-Piwik-DSpace) and as an Eprints plugin for version 3 " +
107
                    "(https://github.com/openaire/EPrints-OAPiwik). In case the platform is different from DSpace or EPrints please contact " +
108
                    "the OpenAIRE team in repositoryusagestats@openaire.eu in order to find a solution.\n" +
109
                    "\n" +
110
                    "For more information about your request and configuration details, go here: \n" +
111
                    this.baseUrl + "/#getImpact/instructions/" + piwikInfo.getRepositoryId() + "\n" +
112
                    "\n" +
113
                    "Once you have finished configuring your repository or if you have any questions, please notify the OpenAIRE team by sending \n" +
114
                    "an email to repositoryusagestats@openaire.eu\n" +
115
                    "\n" +
116
                    "Best,\n" +
117
                    "The OpenAIRE team";
118

  
119
            this.sendMail(piwikInfo.getRequestorEmail(), subject, message, false, null);
120

  
121
        } catch (Exception e) {
122
            LOGGER.error("Error while sending request to enable metrics email to user: " + piwikInfo.getRequestorEmail(), e);
123
            throw e;
124
        }
125
    }
126

  
127
    @Override
128
    public void sendAdministratorMetricsEnabled(PiwikInfo piwikInfo) throws Exception {
129

  
130
        try {
131
            String subject = "[OpenAIRE-Usage Statistics] Usage statistics have been enabled";
132

  
133
            String message = "Dear administrator,\n" +
134
                    "\n" +
135
                    "The installation and configuration of OpenAIRE's tracking code for the following repository " +
136
                    "has been completed and validated and the usage statistics have been enabled in OpenAIRE.\n" +
137
                    "\n" +
138
                    "Repository - " + piwikInfo.getRepositoryName() + ", " + piwikInfo.getCountry() + " (" + piwikInfo.getRepositoryId() + ")\n" +
139
                    "Requestor - " + piwikInfo.getRequestorName() + ", " + piwikInfo.getRequestorEmail() + "\n" +
140
                    "Piwik ID - " + piwikInfo.getSiteId() + "\n" +
141
                    "Authentication token - " + piwikInfo.getAuthenticationToken() + "\n" +
142
                    "\n" +
143
                    "Best,\n" +
144
                    "The OpenAIRE team";
145

  
146
            this.sendMail(piwikInfo.getRequestorEmail(), subject, message, false, null);
147

  
148
        } catch (Exception e) {
149
            LOGGER.error("Error while sending metrics enabled notification email to administator: " + this.usageStatsAdminEmail, e);
150
            throw e;
151
        }
152
    }
153

  
154
    @Override
155
    public void sendUserMetricsEnabled(PiwikInfo piwikInfo) throws Exception {
156

  
157
        try {
158
            String subject = "[OpenAIRE-Usage Statistics] Usage statistics have been enabled";
159

  
160
            String message = "Dear " + piwikInfo.getRequestorName() + ",\n" +
161
                    "\n" +
162
                    "The installation and configuration of OpenAIRE's tracking code for your repository \"" + piwikInfo.getRepositoryName() +
163
                    "\" has been completed and validated and the usage statistics have been enabled in OpenAIRE.\n" +
164
                    "\n" +
165
                    "You can preview the statistics in your repository's dashboard: \n" +
166
                    this.baseUrl + "/#getImpact/" + piwikInfo.getRepositoryId() + "\n" +
167
                    "\n" +
168
                    " For more information and questions, you can contact the openaire support team by sending an email to " +
169
                    "repositoryusagestats@openaire.eu\n" +
170
                    "\n" +
171
                    "Best,\n" +
172
                    "The OpenAIRE team";
173

  
174
            this.sendMail(piwikInfo.getRequestorEmail(), subject, message, false, null);
175

  
176
        } catch (Exception e) {
177
            LOGGER.error("Error while sending metrics enabled notification email to user: " + piwikInfo.getRequestorEmail(), e);
178
            throw e;
179
        }
180
    }
181

  
182
    private void sendMail(String email, String subject, String message, boolean sendToSpecial, List<String> repoAdminMails) throws Exception {
183
        ArrayList<String> to = new ArrayList<String>();
184
        to.add(email);
185
        this.sendMail(to,subject,message,sendToSpecial,repoAdminMails);
186
    }
187

  
188
    private void sendMail(List<String> recipients, String subject, String message, boolean sendToSpecial, List<String> repoAdminMails) throws Exception {
189

  
190
       /* try {
191
            if (sendToSpecial) {
192
                recipients.addAll(this.specialRecipients);
193
            }
194

  
195
            if (repoAdminMails != null)
196
                recipients.addAll(repoAdminMails);
197

  
198
            if (this.override) {
199
                recipients.clear();
200
                recipients.add(overrideEmail);
201
            }
202
            if (!logonly)
203
                mailLibrary.sendEmail(recipients.toArray(new String[]{}), subject, message);
204
            LOGGER.debug("Sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message);
205
        } catch (Exception e) {
206
            LOGGER.error("Error sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message, e);
207
            throw new Exception(e);
208
        }*/
209
    }
210

  
211
    private String getEmailProperty(String key) {
212
        return pLoader.getProperties().getProperty(key);
213
    }
214

  
215
    public void setSpecialRecipients(String specialRecipients) {
216
        String[] recps = specialRecipients.split(",");
217

  
218
        for (String recp : recps) {
219
            recp = recp.trim();
220

  
221
            this.specialRecipients.add(recp);
222
        }
223
    }
224

  
225

  
226
    public void setOverride(boolean override) {
227
        this.override = override;
228
    }
229

  
230
    public void setOverrideEmail(String overrideEmail) {
231
        this.overrideEmail = overrideEmail;
232
    }
233

  
234
    public String getFrom() {
235
        return from;
236
    }
237

  
238
    public void setFrom(String from) {
239
        this.from = from;
240
    }
241

  
242
    public boolean isLogonly() {
243
        return logonly;
244
    }
245

  
246
    public void setLogonly(boolean logonly) {
247
        this.logonly = logonly;
248
    }
249

  
250

  
251
}
modules/uoa-repository-manager-service/branches/dev-api/src/main/java/eu/dnetlib/repo/manager/service/utils/Converter.java
3 3
import com.fasterxml.jackson.databind.ObjectMapper;
4 4
import eu.dnetlib.domain.data.Repository;
5 5
import eu.dnetlib.domain.data.RepositoryInterface;
6
import eu.dnetlib.repo.manager.service.controllers.PiWikApi;
7 6
import eu.dnetlib.repo.manager.shared.AggregationDetails;
8 7
import eu.dnetlib.repo.manager.shared.Timezone;
9 8
import org.apache.commons.codec.digest.DigestUtils;
......
11 10
import org.json.JSONArray;
12 11
import org.json.JSONException;
13 12
import org.json.JSONObject;
14
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.web.bind.annotation.PathVariable;
16 13

  
17 14
import java.io.BufferedReader;
18 15
import java.io.IOException;
......
34 31

  
35 32
        if( datasource.equals(null))
36 33
            return null;
37
        
38
        repository.setActivationId(datasource.get("activationId").toString());
39
        repository.setAggregator(datasource.get("aggregator").toString());
40
//        repository.setCertificates(datasource.get("certificates").toString());
41
//        repository.setCitationGuidelineUrl(datasource.get("citationguidelineurl").toString());
42
        repository.setCollectedFrom( datasource.get("collectedfrom").toString());
43 34

  
44
        repository.setContactEmail(datasource.get("contactemail").toString());
45
        if(repository.getContactEmail().equals("null"))
46
            repository.setContactEmail("");
35
        repository.setId(datasource.get("id").toString());
36
        repository.setOfficialName(datasource.get("officialname").toString());
47 37

  
48
//        repository.setDatabaseAccessRestriction(datasource.get("databaseaccessrestriction").toString());
49
//        repository.setDatabaseAccessType(datasource.get("databaseaccesstype").toString());
50
//        repository.setDataUploadRestriction(datasource.get("datauploadrestriction").toString());
51
//        repository.setDataUploadType(datasource.get("datauploadtype").toString());
52
        repository.setDateOfCollection(convertStringToDate( datasource.get("dateofcollection").toString()));
53
        repository.setDateOfValidation(convertStringToDate( datasource.get("dateofvalidation").toString()));
54

  
55
        repository.setDescription(datasource.get("description").toString());
56
        if(repository.getDescription().equals("null"))
57
            repository.setDescription("");
58

  
59
        repository.setEissn(datasource.get("eissn").toString());
60

  
61 38
        repository.setEnglishName( datasource.get("englishname").toString());
62 39
        if(repository.getEnglishName().equals("null"))
63 40
            repository.setEnglishName("");
64 41

  
65

  
66
        repository.setId(datasource.get("id").toString());
67
        repository.setIssn(datasource.get("issn").toString());
68
        repository.setOdLanguages(datasource.get("languages").toString());
69
        repository.setLatitude( toDouble(datasource.get("latitude").toString()));
70
        repository.setLissn(datasource.get("lissn").toString());
71

  
42
        repository.setWebsiteUrl(datasource.get("websiteurl").toString());
72 43
        repository.setLogoUrl(datasource.get("logourl").toString());
73 44
        if(repository.getLogoUrl().equals("null"))
74 45
            repository.setLogoUrl("");
75 46

  
47
        repository.setContactEmail(datasource.get("contactemail").toString());
48
        repository.setLatitude( toDouble(datasource.get("latitude").toString()));
76 49
        repository.setLongitude(toDouble(datasource.get("longitude").toString()));
77
        //datasource.get("managed");
78
//        repository.setMissionStatementUrl(datasource.get("missionstatementurl").toString());
50
        Double timezone = toDouble(datasource.get("timezone").toString());
51
        repository.setTimezone(timezone!=null?timezone:0.0);
79 52
        repository.setNamespacePrefix(datasource.get("namespaceprefix").toString());
80
//        repository.setOdContentTypes(datasource.get("od_contenttypes").toString());
81
        repository.setOfficialName(datasource.get("officialname").toString());
82
        if(repository.getOfficialName().equals("null"))
83
            repository.setOfficialName("");
53
        repository.setOdLanguages(datasource.get("languages").toString());
54
        repository.setDateOfValidation(convertStringToDate( datasource.get("dateofvalidation").toString()));
84 55

  
85
//        repository.setPidSystems(datasource.get("pidsystems").toString());
86
        //datasource.get("platform");
87
//        repository.setProvenanceActionClass( datasource.get("provenanceaction").toString());
88
//        repository.setQualityManagementKind(datasource.get("qualitymanagementkind").toString());
56
        /*  typology -> platform
57
         *  datasource class -> typology */
58
        repository.setTypology(datasource.get("platform").toString());
59
        if(repository.getTypology().equals("null"))
60
            repository.setTypology("");
61
        repository.setDatasourceClass(datasource.get("typology").toString());
62

  
63
        repository.setDateOfCollection(convertStringToDate( datasource.get("dateofcollection").toString()));
64
        repository.setActivationId(datasource.get("activationId").toString());
65

  
66
        repository.setDescription(datasource.get("description").toString());
67
        if(repository.getDescription().equals("null"))
68
            repository.setDescription("");
69

  
70
        repository.setIssn(datasource.get("issn").toString());
71
        repository.setLissn(datasource.get("lissn").toString());
72
        repository.setEissn(datasource.get("eissn").toString());
89 73
        repository.setRegisteredBy(datasource.get("registeredby").toString());
90 74

  
91
        if(Objects.equals(repository.getRegisteredBy(),"null"))
92
            repository.setRegistered(true);
75
        /* managed field */
76
        repository.setRegistered(Boolean.parseBoolean(datasource.get("managed").toString()));
93 77

  
94
//        repository.setReleaseEndDate(convertStringToDate(datasource.get("releaseenddate").toString()));
95
//        repository.setReleaseStartDate(convertStringToDate(datasource.get("releasestartdate").toString()));
96
//        repository.setServiceProvider(Boolean.valueOf(datasource.get("serviceprovider").toString()));
97
        //datasource.get("subjects");
98
        Double timezone = toDouble(datasource.get("timezone").toString());
99
        repository.setTimezone(timezone!=null?timezone:0.0);
100
        repository.setTypology(datasource.get("platform").toString());
101
//        repository.setVersioning(Boolean.valueOf(datasource.get("versioning").toString()));
102
        repository.setWebsiteUrl(datasource.get("websiteurl").toString());
103
        repository.setDatasourceClass(datasource.get("typology").toString());
78
        //subjects
79
        repository.setAggregator(datasource.get("aggregator").toString());
104 80

  
105
        //TODO change organization to list
106
        JSONArray organizations = ((JSONArray)datasource.get("organizations"));
107
        if(organizations.length() != 0) {
108
            repository.setOrganization(((JSONArray) datasource.get("organizations")).getJSONObject(0).get("legalname").toString());
109
            String countryCode = ((JSONArray) datasource.get("organizations")).getJSONObject(0).get("country").toString();
110
            repository.setCountryCode(countryCode);
111
        }
112

  
113
        
114 81
        String collectedFrom = datasource.get("collectedfrom").toString();
115 82
        //TODO check data consistency
116 83
        String type = "UNKNOWN";
......
121 88
        } else if (collectedFrom.equalsIgnoreCase("infrastruct_::openaire")) {
122 89
            type = "journal";
123 90
        }
91
        /* collected from field  */
124 92
        repository.setDatasourceType(type);
93
        repository.setCollectedFrom(collectedFrom);
94

  
95
        //TODO change organization to list
96
        JSONArray organizations = ((JSONArray)datasource.get("organizations"));
97
        if(organizations.length() != 0) {
98
            repository.setOrganization(((JSONArray) datasource.get("organizations")).getJSONObject(0).get("legalname").toString());
99
            String countryCode = ((JSONArray) datasource.get("organizations")).getJSONObject(0).get("country").toString();
100
            repository.setCountryCode(countryCode);
101
        }
102

  
103
        /* identities field  */
104

  
125 105
        return repository;
126 106
    }
127 107

  
......
177 157

  
178 158
        RepositoryInterface repositoryInterface = new RepositoryInterface();
179 159

  
180
        repositoryInterface.setBaseUrl(repositoryInterfaceObject.get("baseurl").toString());
181
        repositoryInterface.setContentDescription(repositoryInterfaceObject.get("contentdescription").toString());
182 160
        repositoryInterface.setId(repositoryInterfaceObject.get("id").toString());
183
       // repositoryInterface.setMetadataIdentifierPath(repositoryInterfaceObject.get("metadataIdentifierPath").toString());
184 161
        repositoryInterface.setAccessProtocol(repositoryInterfaceObject.get("protocol").toString());
162
        repositoryInterface.setContentDescription(repositoryInterfaceObject.get("contentdescription").toString());
185 163
        repositoryInterface.setTypology(repositoryInterfaceObject.get("typology").toString());
186
        repositoryInterface.setDesiredCompatibilityLevel(repositoryInterfaceObject.get("compatibility").toString());
187
        //repositoryInterface.setActive(Boolean.parseBoolean(repositoryInterfaceObject.get("active").toString()));
188
        repositoryInterface.setRemovable(Boolean.parseBoolean(repositoryInterfaceObject.get("removable").toString()));
189 164
        repositoryInterface.setCompliance(repositoryInterfaceObject.get("compatibility").toString());
190 165
        repositoryInterface.setLastCollectionDate(repositoryInterfaceObject.get("lastCollectionDate").toString());
191 166

  
167
        repositoryInterface.setBaseUrl(repositoryInterfaceObject.get("baseurl").toString());
168
        repositoryInterface.setRemovable(Boolean.parseBoolean(repositoryInterfaceObject.get("removable").toString()));
192 169

  
170

  
171
       // repositoryInterface.setMetadataIdentifierPath(repositoryInterfaceObject.get("metadataIdentifierPath").toString());
172
        repositoryInterface.setDesiredCompatibilityLevel(repositoryInterfaceObject.get("compatibility").toString());
173
        //repositoryInterface.setActive(Boolean.parseBoolean(repositoryInterfaceObject.get("active").toString()));
174

  
175

  
193 176
        Map<String, String> accessParams = new HashMap<>();
194 177
        Map<String, String> extraFields = new HashMap<>();
195 178

  
......
199 182
        for(int i=0;i<apiparams.length();i++)
200 183
            accessParams.put(apiparams.getJSONObject(i).getString("param"),apiparams.getJSONObject(i).getString("value"));
201 184

  
185
        repositoryInterface.setAccessParams(accessParams);
186

  
202 187
        return repositoryInterface;
203 188
    }
204 189

  
......
219 204
        jsonObject.put("namespaceprefix",repository.getNamespacePrefix());
220 205
        jsonObject.put("languages",repository.getOdLanguages());
221 206
        jsonObject.put("dateofvalidation",convertDateToString(repository.getDateOfValidation()));
222
        jsonObject.put("typology",repository.getTypology());
207

  
208
        /*
209
        * typology -> platform
210
        * datasource class -> typology
211
        * */
212
        jsonObject.put("typology",repository.getDatasourceClass());
213
        jsonObject.put("platform",repository.getTypology());
214

  
223 215
        jsonObject.put("dateofcollection",convertDateToString(repository.getDateOfCollection()));
224
//        jsonObject.put("platform",repository.getPlatform());
225 216
        jsonObject.put("activationId",repository.getActivationId());
226 217
        jsonObject.put("description",repository.getDescription());
227 218
        jsonObject.put("eissn",repository.getEissn());
228 219
        jsonObject.put("issn",repository.getIssn());
229 220
        jsonObject.put("lissn",repository.getLissn());
230 221
        jsonObject.put("registeredby",repository.getRegisteredBy());
231
//        jsonObject.put("subjects",repository.getSubjects());
222

  
232 223
        jsonObject.put("aggregator",repository.getAggregator());
233 224
        jsonObject.put("collectedfrom",repository.getCollectedFrom());
234
//        jsonObject.put("managed",repository.getIsManaged());
235 225

  
236
//        jsonObject.put("organizations",repository.getOrganization());
237
//        for(repository.getOrganization())
226
        jsonObject.put("managed",repository.isRegistered());
238 227

  
228
        JSONObject organization = new JSONObject();
229
        organization.put("legalname",repository.getOrganization());
230
        organization.put("country",repository.getCountryCode());
231
        organization.put("legalshortname","");
232
        organization.put("websiteurl","");
233
        organization.put("logourl","");
234

  
235
        JSONArray organizations = new JSONArray();
236
        organizations.put(organization);
237
        jsonObject.put("organizations",organizations);
238

  
239

  
240

  
241

  
239 242
        //TODO check fields
240 243
       /* jsonObject.put("certificates",repository.getCertificates());
241 244
        jsonObject.put("citationguidelineurl",repository.getCitationGuidelineUrl());
......
262 265

  
263 266
        JSONObject jsonObject = new JSONObject();
264 267

  
265
        jsonObject.put("baseurl",repositoryInterface.getBaseUrl());
266
        jsonObject.put("contentdescription",repositoryInterface.getContentDescription());
267 268
        jsonObject.put("id",repositoryInterface.getId());
268
        jsonObject.put("metadataIdentifierPath",repositoryInterface.getMetadataIdentifierPath());
269 269
        jsonObject.put("protocol",repositoryInterface.getAccessProtocol());
270
        jsonObject.put("datasource",repository.getId());
271
        jsonObject.put("contentdescription",repositoryInterface.getContentDescription());
270 272
        jsonObject.put("typology",repositoryInterface.getTypology());
271 273
        jsonObject.put("compatibility",repositoryInterface.getDesiredCompatibilityLevel());
272
        jsonObject.put("datasource",repository.getId());
273
        jsonObject.put("metadataIdentifierPath",repositoryInterface.getMetadataIdentifierPath());
274
        jsonObject.put("protocol",repositoryInterface.getAccessProtocol());
274
        jsonObject.put("compatibilityOverride",repositoryInterface.getDesiredCompatibilityLevel());
275

  
276
        jsonObject.put("lastCollectionTotal","");
277

  
278
        jsonObject.put("lastCollectionDate",repositoryInterface.getLastCollectionDate());
279
        jsonObject.put("lastAggregationTotal","");
280
        jsonObject.put("lastAggregationDate","");
281
        jsonObject.put("lastDownloadTotal","");
282
        jsonObject.put("lastDownloadDate","");
283

  
284
        jsonObject.put("baseurl",repositoryInterface.getBaseUrl());
275 285
        jsonObject.put("removable",repositoryInterface.isRemovable());
276
        jsonObject.put("active",repositoryInterface.isActive());
277 286

  
278

  
287
        
279 288
        JSONArray apiparams = new JSONArray();
280 289
        for(String param: repositoryInterface.getAccessParams().keySet()){
281 290
            JSONObject jo = new JSONObject();
......
283 292
            jo.put("value",repositoryInterface.getAccessParams().get(param));
284 293
            apiparams.put(jo);
285 294
        }
286
        jsonObject.put("apiparam",apiparams);
295
        jsonObject.put("apiParams",apiparams);
287 296

  
288 297

  
289
        jsonObject.put("lastCollectionDate",repositoryInterface.getLastCollectionDate());
290
        //jsonObject.put("lastCollectionMdid",repositoryInterface);
291
        //jsonObject.put("lastCollectionTotal");
292
        //jsonObject.put("lastDownloadDate");
293
//        jsonObject.put("lastDownloadMdid");
294
//        jsonObject.put("lastDownloadTotal");
295
//        jsonObject.put("lastValidationJob");
296
        //jsonObject.put("lastAggregationDate");
297
        //jsonObject.put("lastAggregationMdid");
298
        //jsonObject.put("lastAggregationTotal");
298
//        jsonObject.put("metadataIdentifierPath",repositoryInterface.getMetadataIdentifierPath());
299 299

  
300

  
300 301
        return jsonObject.toString();
301 302
    }
302 303

  
modules/uoa-repository-manager-service/branches/dev-api/src/main/java/eu/dnetlib/repo/manager/service/config/OpenAireProviderAuthoritiesMapper.java
1
package eu.dnetlib.repo.manager.service.config;
2

  
3
import com.nimbusds.jwt.JWT;
4
import org.mitre.openid.connect.client.OIDCAuthoritiesMapper;
5
import org.mitre.openid.connect.model.UserInfo;
6
import org.slf4j.Logger;
7
import org.slf4j.LoggerFactory;
8
import org.springframework.security.core.GrantedAuthority;
9
import org.springframework.security.core.authority.SimpleGrantedAuthority;
10

  
11
import java.util.*;
12

  
13
public class OpenAireProviderAuthoritiesMapper implements OIDCAuthoritiesMapper {
14

  
15
    private static Logger logger = LoggerFactory.getLogger(OpenAireProviderAuthoritiesMapper.class);
16

  
17
    final private static String ROLE_CLAIMS = "edu_person_entitlements";
18

  
19
    private Map<String,SimpleGrantedAuthority> userRolesMap;
20

  
21
    OpenAireProviderAuthoritiesMapper(Map<String,String> userRoles) {
22
        userRolesMap = new HashMap<>();
23
        userRoles.forEach((openaireRole, appRole) -> userRolesMap.put(openaireRole, new SimpleGrantedAuthority(appRole)));
24
    }
25

  
26
    @Override
27
    public Collection<? extends GrantedAuthority> mapAuthorities(JWT idToken, UserInfo userInfo) {
28
        Set<GrantedAuthority> out = new HashSet<>();
29
        out.add(new SimpleGrantedAuthority("ROLE_USER"));
30

  
31
        if(userInfo.getSource().getAsJsonArray(ROLE_CLAIMS) != null) {
32
            userInfo.getSource().getAsJsonArray(ROLE_CLAIMS).forEach(role -> {
33
                SimpleGrantedAuthority authority = userRolesMap.get(role.getAsString());
34
                if (authority != null) {
35
                    logger.debug("Role mapped " + role);
36
                    out.add(authority);
37
                }
38
            });
39
        }
40
        return out;
41
    }
42
}
modules/uoa-repository-manager-service/branches/dev-api/src/main/java/eu/dnetlib/repo/manager/service/config/FrontEndLinkURIAuthenticationSuccessHandler.java
27 27
    @Override
28 28
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
29 29

  
30
        LOGGER.info(request);
31
        LOGGER.info(response);
32

  
33 30
        OIDCAuthenticationToken authOIDC = (OIDCAuthenticationToken) authentication;
34 31
        Cookie sessionCookie = new Cookie("currentUser", authOIDC.getSub());
35 32
        int expireSec = -1;
modules/uoa-repository-manager-service/branches/dev-api/src/main/java/eu/dnetlib/repo/manager/service/controllers/PiWikApiImpl.java
1 1
package eu.dnetlib.repo.manager.service.controllers;
2 2

  
3
import com.fasterxml.jackson.databind.ObjectMapper;
3 4
import eu.dnetlib.domain.data.PiwikInfo;
4
import eu.dnetlib.utils.md5.MD5;
5
import eu.dnetlib.repo.manager.shared.RepositoryServiceException;
5 6
import org.apache.commons.codec.digest.DigestUtils;
6 7
import org.springframework.beans.factory.annotation.Autowired;
7 8
import org.springframework.beans.factory.annotation.Qualifier;
9
import org.springframework.beans.factory.annotation.Value;
8 10
import org.springframework.dao.EmptyResultDataAccessException;
9 11
import org.springframework.jdbc.core.JdbcTemplate;
10 12
import org.springframework.jdbc.core.RowMapper;
......
12 14
import org.springframework.web.bind.annotation.PathVariable;
13 15
import org.springframework.web.bind.annotation.RequestBody;
14 16

  
15
import javax.annotation.PostConstruct;
16 17
import javax.sql.DataSource;
17
import java.security.NoSuchAlgorithmException;
18
import java.sql.ResultSet;
19
import java.sql.SQLException;
18
import java.io.IOException;
19
import java.io.UnsupportedEncodingException;
20
import java.net.URL;
21
import java.net.URLEncoder;
20 22
import java.sql.Types;
21 23
import java.util.List;
24
import java.util.Map;
22 25

  
23 26
@Component
24 27
public class PiWikApiImpl implements PiWikApi{
......
27 30
    @Qualifier("repomanager.dataSource")
28 31
    private DataSource dataSource;
29 32

  
33

  
34
    @Value("${services.repomanager.analyticsURL}")
35
    private String analyticsURL;
36

  
37

  
38
    @Autowired
39
    @Qualifier("emailUtils")
40
    EmailUtils emailUtils;
41

  
30 42
    private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger
31 43
            .getLogger(PiWikApiImpl.class);
32 44

  
......
39 51
    private final static String APPROVE_PIWIK_SITE = "update piwik_site set validated=true, validationdate=now() where repositoryid = ?;";
40 52

  
41 53

  
54

  
42 55
    private RowMapper<PiwikInfo> piwikRowMapper = (rs, i) -> new PiwikInfo(rs.getString("repositoryid"), getOpenaireId(rs.getString("repositoryid")), rs.getString("repositoryname"), rs.getString("country"),
43 56
            rs.getString("siteid"), rs.getString("authenticationtoken"), rs.getTimestamp("creationdate"), rs.getString("requestorname"), rs.getString("requestoremail"),
44 57
            rs.getBoolean("validated"), rs.getTimestamp("validationdate"), rs.getString("comment"));
......
85 98
        return null;
86 99
    }
87 100

  
101
    @Override
102
    public void markPiwikSiteAsValidated(@PathVariable("repositoryId") String repositoryId) throws RepositoryServiceException {
103
        try {
104
            approvePiwikSite(repositoryId);
88 105

  
106
            PiwikInfo piwikInfo = getPiwikSiteForRepo(repositoryId);
107
            emailUtils.sendAdministratorMetricsEnabled(piwikInfo);
108
            emailUtils.sendUserMetricsEnabled(piwikInfo);
109

  
110
        } catch (EmptyResultDataAccessException e) {
111
            LOGGER.error("Error while approving piwik site: ", e);
112
            emailUtils.reportException(e);
113
            throw new RepositoryServiceException("General error", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
114
        } catch (Exception e) {
115
            LOGGER.error("Error while sending email to administrator or user about the enabling of metrics", e);
116
            emailUtils.reportException(e);
117
        }
118
    }
119

  
120
    @Override
121
    public void enableMetricsForRepository(@RequestBody PiwikInfo piwikInfo) throws RepositoryServiceException {
122

  
123
        try {
124
            String URL = analyticsURL + "siteName=" + URLEncoder.encode(piwikInfo.getRepositoryName(), "UTF-8")
125
                    + "&url=" + piwikInfo.getSiteId();
126
            Map<String, Object> map = new ObjectMapper().readValue(new URL(URL), Map.class);
127

  
128
           /* String siteId = null;
129
            if(map.get("value")!=null) {
130
                siteId = map.get("value").toString();
131
            }
132

  
133
            String authenticationToken = "32846584f571be9b57488bf4088f30ea";
134

  
135
            PiwikInfo piwikInfo = new PiwikInfo();
136
            piwikInfo.setRepositoryId(repository.getId());
137
            piwikInfo.setRepositoryName(repository.getOfficialName());
138
            piwikInfo.setCountry(repository.getCountryName());
139
            piwikInfo.setSiteId(siteId);
140
            piwikInfo.setAuthenticationToken(authenticationToken);
141
            piwikInfo.setRequestorEmail(userProfile.getEmail());
142
            piwikInfo.setRequestorName(userProfile.getFirstname() + " " + userProfile.getLastname());
143
            piwikInfo.setValidated(false);
144
*/
145
            savePiwikInfo(piwikInfo);
146

  
147
            emailUtils.sendAdministratorRequestToEnableMetrics(piwikInfo);
148
            emailUtils.sendUserRequestToEnableMetrics(piwikInfo);
149

  
150
        } catch (UnsupportedEncodingException uee) {
151
            LOGGER.error("Error while creating piwikScript URL", uee);
152
            emailUtils.reportException(uee);
153
            throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
154
        } catch (IOException ioe) {
155
            LOGGER.error("Error while creating piwik site", ioe);
156
            emailUtils.reportException(ioe);
157
            throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR);
158
        } catch (Exception e) {
159
            LOGGER.error("Error while sending email to administrator or user about the request to enable metrics", e);
160
            emailUtils.reportException(e);
161
        }
162
    }
163

  
164

  
89 165
}
modules/uoa-repository-manager-service/branches/dev-api/src/main/java/eu/dnetlib/repo/manager/service/controllers/MonitorApiImpl.java
50 50
        LOGGER.debug("Getting jobs of user : " + user);
51 51
        LOGGER.debug(user + "/" + jobType + "/" + offset + "/" + dateFrom + "/" + dateTo + "/" + validationStatus + "/" + includeJobsTotal);
52 52
        JobsOfUser retJobs = new JobsOfUser();
53
        LOGGER.debug("Size of jobs list -> " + getValidationService().getStoredJobsNew(user, jobType, Integer.parseInt(offset),
54
                Integer.parseInt(limit), dateFrom, dateTo, validationStatus).size());
55 53
        retJobs.setJobs(getValidationService().getStoredJobsNew(user, jobType, Integer.parseInt(offset),
56 54
                Integer.parseInt(limit), dateFrom, dateTo, validationStatus));
57 55
        if (Boolean.parseBoolean(includeJobsTotal)) {
......
62 60
        }
63 61

  
64 62
        //TODO fix status with new validator version
65
        for(StoredJob job :retJobs.getJobs()){
66
            if (job.getContentJobStatus().equals("ongoing") || job.getUsageJobStatus().equals("ongoing")) {
67
                job.setValidationStatus("ongoing");
68
            } else if ((job.getValidationType().equals("CU") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("finished") && job.getContentJobScore() > 50 && job.getUsageJobScore() > 50)
69
                    || (job.getValidationType().equals("C") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("none") && job.getContentJobScore() > 50)
70
                    || (job.getValidationType().equals("U") && job.getContentJobStatus().equals("none") && job.getUsageJobStatus().equals("finished") && job.getUsageJobScore() > 50)) {
71
                job.setValidationStatus("successful");
72
            } else if ((job.getValidationType().equals("CU") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("finished") && (job.getContentJobScore() <= 50 || job.getUsageJobScore() <= 50))
73
                    || (job.getValidationType().equals("C") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("none") && job.getContentJobScore() <= 50)
74
                    || (job.getValidationType().equals("U") && job.getContentJobStatus().equals("none") && job.getUsageJobStatus().equals("finished") && job.getUsageJobScore() <= 50) ) {
75
                job.setValidationStatus("failed");
63
        if(retJobs.getJobs() != null){
64
            for(StoredJob job :retJobs.getJobs()){
65
                if (job.getContentJobStatus().equals("ongoing") || job.getUsageJobStatus().equals("ongoing")) {
66
                    job.setValidationStatus("ongoing");
67
                } else if ((job.getValidationType().equals("CU") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("finished") && job.getContentJobScore() > 50 && job.getUsageJobScore() > 50)
68
                        || (job.getValidationType().equals("C") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("none") && job.getContentJobScore() > 50)
69
                        || (job.getValidationType().equals("U") && job.getContentJobStatus().equals("none") && job.getUsageJobStatus().equals("finished") && job.getUsageJobScore() > 50)) {
70
                    job.setValidationStatus("successful");
71
                } else if ((job.getValidationType().equals("CU") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("finished") && (job.getContentJobScore() <= 50 || job.getUsageJobScore() <= 50))
72
                        || (job.getValidationType().equals("C") && job.getContentJobStatus().equals("finished") && job.getUsageJobStatus().equals("none") && job.getContentJobScore() <= 50)
73
                        || (job.getValidationType().equals("U") && job.getContentJobStatus().equals("none") && job.getUsageJobStatus().equals("finished") && job.getUsageJobScore() <= 50) ) {
74
                    job.setValidationStatus("failed");
75
                }
76

  
76 77
            }
77

  
78 78
        }
79 79

  
80

  
80 81
        return retJobs;
81 82

  
82 83
    }
modules/uoa-repository-manager-service/branches/dev-api/src/main/java/eu/dnetlib/repo/manager/service/controllers/EmailUtils.java
1
package eu.dnetlib.repo.manager.service.controllers;
2

  
3
import eu.dnetlib.domain.data.PiwikInfo;
4

  
5
public interface EmailUtils {
6

  
7

  
8
    void reportException(Exception exception);
9

  
10
    void sendAdministratorRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception;
11

  
12
    void sendUserRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception;
13

  
14
    void sendAdministratorMetricsEnabled(PiwikInfo piwikInfo) throws Exception;
15

  
16
    void sendUserMetricsEnabled(PiwikInfo piwikInfo) throws Exception;
17
}
modules/uoa-repository-manager-service/branches/dev-api/src/main/java/eu/dnetlib/repo/manager/service/controllers/RepositoryApi.java
2 2

  
3 3
import eu.dnetlib.domain.data.Repository;
4 4
import eu.dnetlib.domain.data.RepositoryInterface;
5
import eu.dnetlib.domain.functionality.UserProfile;
5 6
import eu.dnetlib.repo.manager.shared.*;
6 7
import io.swagger.annotations.Api;
7 8
import org.json.JSONException;
......
68 69
    Repository addRepository(String datatype, Repository repository) throws Exception;
69 70

  
70 71

  
71
    @RequestMapping(value = "/deleteInterface", method = RequestMethod.DELETE)
72
    @ResponseBody
72
    @RequestMapping(value = "/deleteInterface/", method = RequestMethod.DELETE)
73 73
    void deleteRepositoryInterface(String id);
74 74

  
75 75
    @RequestMapping(value = "/addInterface", method = RequestMethod.POST,
......
134 134
    @ResponseBody
135 135
    Map<String, String> getListLatestUpdate(String mode) throws RepositoryServiceException, JSONException;
136 136

  
137
    @RequestMapping(value = "/updateRepositoryInterface", method = RequestMethod.POST,
138
            consumes = MediaType.APPLICATION_JSON_VALUE)
139
    @ResponseBody
140
    RepositoryInterface updateRepositoryInterface(String repositoryId,RepositoryInterface repositoryInterface) throws JSONException;
137 141

  
138 142

  
143

  
139 144
}
modules/uoa-repository-manager-service/branches/dev-api/src/main/java/eu/dnetlib/repo/manager/service/controllers/UserApiImpl.java
30 30
        LOGGER.debug("User authentication : " + authentication);
31 31
        Map<String,Object> body = new HashMap<>();
32 32
        body.put("sub",authentication.getSub());
33
        if(authentication.getUserInfo().getName() == null || authentication.getUserInfo().getName().equals("")) {
33
        if(authentication.getUserInfo().getName() == null || authentication.getUserInfo().getName().equals(""))
34 34
            body.put("name",authentication.getUserInfo().getGivenName() + " " + authentication.getUserInfo().getFamilyName());
35
        } else {
35
        else
36 36
            body.put("name",authentication.getUserInfo().getName());
37
        }
37

  
38
        for (GrantedAuthority authority : authentication.getAuthorities())
39
            LOGGER.debug("Role: " + authority.getAuthority());
40

  
38 41
        body.put("email",authentication.getUserInfo().getEmail());
39 42
        List<String> roles = authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
40 43
        body.put("role",roles);
modules/uoa-repository-manager-service/branches/dev-api/src/main/java/eu/dnetlib/repo/manager/service/controllers/EmailUtilsImpl.java
1
package eu.dnetlib.repo.manager.service.controllers;
2

  
3
import eu.dnetlib.domain.data.PiwikInfo;
4
import eu.dnetlib.repo.manager.service.config.CascadingPropertyLoader;
5
import eu.dnetlib.utils.MailLibrary;
6
import org.apache.log4j.Logger;
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.beans.factory.annotation.Value;
9
import org.springframework.stereotype.Component;
10

  
11
import java.io.PrintWriter;
12
import java.io.StringWriter;
13
import java.io.Writer;
14
import java.util.ArrayList;
15
import java.util.List;
16

  
17

  
18
@Component("emailUtils")
19
public class EmailUtilsImpl implements EmailUtils {
20

  
21
    private static Logger LOGGER = Logger.getLogger(EmailUtilsImpl.class);
22

  
23
    private List<String> specialRecipients = new ArrayList<String>();
24
    private boolean override = false, logonly = false;
25
    private String overrideEmail = null, from = null;
26

  
27
    @Autowired
28
    private MailLibrary mailLibrary;
29

  
30
    @Autowired
31
    private CascadingPropertyLoader pLoader;
32

  
33
    @Value("${services.repo-manager.baseUrl}")
34
    private String baseUrl;
35

  
36
    @Value("${services.repo-manager.adminEmail}")
37
    private String adminEmail;
38

  
39
    @Value("${services.repomanager.usagestats.adminEmail}")
40
    private String usageStatsAdminEmail;
41

  
42
    @Override
43
    public void reportException(Exception exception) {
44
        Writer writer = new StringWriter();
45
        PrintWriter printWriter = new PrintWriter(writer);
46
        exception.printStackTrace(printWriter);
47

  
48
        List<String> recipients = new ArrayList<String>();
49

  
50
        try {
51
            recipients.add(this.adminEmail);
52
            String message = "An exception has occurred:\n"+writer.toString();
53
            String subject = "Automatic Bug Report";
54
            this.sendMail(recipients, subject, message, false, null);
55
        } catch (Exception e) {
56
            LOGGER.error("Error sending error report", e);
57
        }
58
    }
59

  
60
    @Override
61
    public void sendAdministratorRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception {
62

  
63
        try {
64
            String subject = "[OpenAIRE-Usage Statistics] New request to enable usage statistics";
65

  
66
            String message = "Dear administrator,\n" +
67
                    "\n" +
68
                    "we have received a request to enable the OpenAIRE usage statistics for the following repository \n" +
69
                    "\n" +
70
                    "Repository - " + piwikInfo.getRepositoryName() + ", " + piwikInfo.getCountry() + " (" + piwikInfo.getRepositoryId() + ")\n" +
71
                    "Requestor - " + piwikInfo.getRequestorName() + ", " + piwikInfo.getRequestorEmail() + "\n" +
72
                    "Piwik ID - " + piwikInfo.getSiteId() + "\n" +
73
                    "Authentication token - " + piwikInfo.getAuthenticationToken() + "\n" +
74
                    "\n" +
75
                    "For more information about this request, go here: \n" +
76
                    this.baseUrl + "/#admin/metrics\n" +
77
                    "\n" +
78
                    "Best,\n" +
79
                    "The OpenAIRE team";
80

  
81
            this.sendMail(this.usageStatsAdminEmail, subject, message, false, null);
82

  
83
        } catch (Exception e) {
84
            LOGGER.error("Error while sending request to enable metrics email to administrator: " + this.usageStatsAdminEmail, e);
85
            throw e;
86
        }
87
    }
88

  
89
    @Override
90
    public void sendUserRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception {
91

  
92
        try {
93
            String subject = "[OpenAIRE-Usage Statistics] Your request to enable usage statistics";
94

  
95
            String message = "Dear " + piwikInfo.getRequestorName() + ",\n" +
96
                    "\n" +
97
                    "we have received your request to enable the OpenAIRE usage statistics for your repository\n" +
98
                    "\n" +
99
                    "Repository - " + piwikInfo.getRepositoryName() + ", " + piwikInfo.getCountry() + " (" + piwikInfo.getRepositoryId() + ")\n" +
100
                    "Piwik ID - " + piwikInfo.getSiteId() + "\n" +
101
                    "Authentication token - " + piwikInfo.getAuthenticationToken() + "\n" +
102
                    "\n" +
103
                    "In order to enable the usage statistics, you must install the OpenAIRE's tracking code in your repository software. " +
104
                    "OpenAIRE's usage statistics service tracking code is maintained on Github as a patch for various versions of DSpace " +
105
                    "(https://github.com/openaire/OpenAIRE-Piwik-DSpace) and as an Eprints plugin for version 3 " +
106
                    "(https://github.com/openaire/EPrints-OAPiwik). In case the platform is different from DSpace or EPrints please contact " +
107
                    "the OpenAIRE team in repositoryusagestats@openaire.eu in order to find a solution.\n" +
108
                    "\n" +
109
                    "For more information about your request and configuration details, go here: \n" +
110
                    this.baseUrl + "/#getImpact/instructions/" + piwikInfo.getRepositoryId() + "\n" +
111
                    "\n" +
112
                    "Once you have finished configuring your repository or if you have any questions, please notify the OpenAIRE team by sending \n" +
113
                    "an email to repositoryusagestats@openaire.eu\n" +
114
                    "\n" +
115
                    "Best,\n" +
116
                    "The OpenAIRE team";
117

  
118
            this.sendMail(piwikInfo.getRequestorEmail(), subject, message, false, null);
119

  
120
        } catch (Exception e) {
121
            LOGGER.error("Error while sending request to enable metrics email to user: " + piwikInfo.getRequestorEmail(), e);
122
            throw e;
123
        }
124
    }
125

  
126
    @Override
127
    public void sendAdministratorMetricsEnabled(PiwikInfo piwikInfo) throws Exception {
128

  
129
        try {
130
            String subject = "[OpenAIRE-Usage Statistics] Usage statistics have been enabled";
131

  
132
            String message = "Dear administrator,\n" +
133
                    "\n" +
134
                    "The installation and configuration of OpenAIRE's tracking code for the following repository " +
135
                    "has been completed and validated and the usage statistics have been enabled in OpenAIRE.\n" +
136
                    "\n" +
137
                    "Repository - " + piwikInfo.getRepositoryName() + ", " + piwikInfo.getCountry() + " (" + piwikInfo.getRepositoryId() + ")\n" +
138
                    "Requestor - " + piwikInfo.getRequestorName() + ", " + piwikInfo.getRequestorEmail() + "\n" +
139
                    "Piwik ID - " + piwikInfo.getSiteId() + "\n" +
140
                    "Authentication token - " + piwikInfo.getAuthenticationToken() + "\n" +
141
                    "\n" +
142
                    "Best,\n" +
143
                    "The OpenAIRE team";
144

  
145
            this.sendMail(piwikInfo.getRequestorEmail(), subject, message, false, null);
146

  
147
        } catch (Exception e) {
148
            LOGGER.error("Error while sending metrics enabled notification email to administator: " + this.usageStatsAdminEmail, e);
149
            throw e;
150
        }
151
    }
152

  
153
    @Override
154
    public void sendUserMetricsEnabled(PiwikInfo piwikInfo) throws Exception {
155

  
156
        try {
157
            String subject = "[OpenAIRE-Usage Statistics] Usage statistics have been enabled";
158

  
159
            String message = "Dear " + piwikInfo.getRequestorName() + ",\n" +
160
                    "\n" +
161
                    "The installation and configuration of OpenAIRE's tracking code for your repository \"" + piwikInfo.getRepositoryName() +
162
                    "\" has been completed and validated and the usage statistics have been enabled in OpenAIRE.\n" +
163
                    "\n" +
164
                    "You can preview the statistics in your repository's dashboard: \n" +
165
                    this.baseUrl + "/#getImpact/" + piwikInfo.getRepositoryId() + "\n" +
166
                    "\n" +
167
                    " For more information and questions, you can contact the openaire support team by sending an email to " +
168
                    "repositoryusagestats@openaire.eu\n" +
169
                    "\n" +
170
                    "Best,\n" +
171
                    "The OpenAIRE team";
172

  
173
            this.sendMail(piwikInfo.getRequestorEmail(), subject, message, false, null);
174

  
175
        } catch (Exception e) {
176
            LOGGER.error("Error while sending metrics enabled notification email to user: " + piwikInfo.getRequestorEmail(), e);
177
            throw e;
178
        }
179
    }
180

  
181
    private void sendMail(String email, String subject, String message, boolean sendToSpecial, List<String> repoAdminMails) throws Exception {
182
        ArrayList<String> to = new ArrayList<String>();
183
        to.add(email);
184
        this.sendMail(to,subject,message,sendToSpecial,repoAdminMails);
185
    }
186

  
187
    private void sendMail(List<String> recipients, String subject, String message, boolean sendToSpecial, List<String> repoAdminMails) throws Exception {
188

  
189
        try {
190
            if (sendToSpecial) {
191
                recipients.addAll(this.specialRecipients);
192
            }
193

  
194
            if (repoAdminMails != null)
195
                recipients.addAll(repoAdminMails);
196

  
197
            if (this.override) {
198
                recipients.clear();
199
                recipients.add(overrideEmail);
200
            }
201
            if (!logonly)
202
                mailLibrary.sendEmail(recipients.toArray(new String[]{}), subject, message);
203
            LOGGER.debug("Sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message);
204
        } catch (Exception e) {
205
            LOGGER.error("Error sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message, e);
206
            throw new Exception(e);
207
        }
208
    }
209

  
210
    private String getEmailProperty(String key) {
211
        return pLoader.getProperties().getProperty(key);
212
    }
213

  
214
    public void setSpecialRecipients(String specialRecipients) {
215
        String[] recps = specialRecipients.split(",");
216

  
217
        for (String recp : recps) {
218
            recp = recp.trim();
219

  
220
            this.specialRecipients.add(recp);
221
        }
222
    }
223

  
224

  
225
    public void setOverride(boolean override) {
226
        this.override = override;
227
    }
228

  
229
    public void setOverrideEmail(String overrideEmail) {
230
        this.overrideEmail = overrideEmail;
231
    }
232

  
233
    public String getFrom() {
234
        return from;
235
    }
236

  
237
    public void setFrom(String from) {
238
        this.from = from;
239
    }
240

  
241
    public boolean isLogonly() {
242
        return logonly;
243
    }
244

  
245
    public void setLogonly(boolean logonly) {
246
        this.logonly = logonly;
247
    }
248

  
249

  
250
}
modules/uoa-repository-manager-service/branches/dev-api/src/main/java/eu/dnetlib/repo/manager/service/controllers/RepositoryApiImpl.java
151 151

  
152 152
        String countryCode = countriesMap.get(country);
153 153
        String filterKey = "UNKNOWN";
154
        if (mode.equalsIgnoreCase("opendoar")) {
154
        if (mode.equalsIgnoreCase("opendoar"))
155 155
            filterKey = "openaire____::opendoar";
156
        } else if (mode.equalsIgnoreCase("re3data")) {
156
        else if (mode.equalsIgnoreCase("re3data"))
157 157
            filterKey = "openaire____::re3data";
158
        } else if (mode.equalsIgnoreCase("jour_aggr")) {
159
            filterKey = "infrastruct_::openaire";
160
        }
161 158

  
159

  
162 160
        LOGGER.debug("Country code equals : " + countryCode);
163 161
        LOGGER.debug("Filter mode equals : " + filterKey);
162

  
164 163
        UriComponents uriComponents = searchDatasource(String.valueOf(page),String.valueOf(size));
165 164
        RequestFilter requestFilter = new RequestFilter();
166 165
        requestFilter.setCountry(countryCode);
......
168 167
        String rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class);
169 168
        JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
170 169
        while (jsonArray.length() > 0 ) {
171
            List<Repository> rep = Converter.jsonToRepositoryList(new JSONObject(rs));
172

  
173
            Collection<Repository> repos = this.getRepositoriesByMode(filterKey, rep);
174
            resultSet.addAll(repos);
175

  
170
            resultSet.addAll(this.getRepositoriesByMode(filterKey, Converter.jsonToRepositoryList(new JSONObject(rs))));
176 171
            page += 1;
177 172
            uriComponents = searchDatasource(String.valueOf(page),String.valueOf(size));
178
            
179 173
            rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class);
180 174
            jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
181 175
        }
......
198 192
        for (Repository r : rs) {
199 193
            if (r.getCollectedFrom() != null && r.getCollectedFrom().equals(mode))
200 194
                reps.add(r);
195

  
201 196
        }
202 197
        return reps;
203 198
    }
......
298 293

  
299 294
        repository = this.setRepositoryFeatures(datatype,repository);
300 295
        LOGGER.debug("storing " + datatype + " repository with id: " + repository.getId());
301
        if (!datatype.equalsIgnoreCase("opendoar") && !datatype.equalsIgnoreCase("re3data")) {
302
            if (datatype.equalsIgnoreCase("journal") || datatype.equalsIgnoreCase("aggregator")) {
303
                LOGGER.debug("looking if " + datatype + " " + repository.getOfficialName() + " is already in datasources");
304
                if (getRepositoryById(repository.getId()) != null) {
305
                    String retMessage = datatype + " '" + repository.getOfficialName() + "' is already in datasources.";
306
                    repository.getInterfaces().clear();
307
                    LOGGER.debug(retMessage);
308
                } else {
309
                    LOGGER.debug(datatype + " " + repository.getOfficialName() + " is not in datasources. Inserting..");
310
                    this.storeRepository(repository);
311
                }
312
            }
313
        } else {
314
            this.updateRepository(repository);
315
        }
316

  
317
        LOGGER.debug("Inserting Interfaces");
318
        Iterator var11 = repository.getInterfaces().iterator();
319

  
320
        while (var11.hasNext()) {
321
            RepositoryInterface iFace = (RepositoryInterface) var11.next();
322
            if (!iFace.getBaseUrl().isEmpty() && !iFace.getDesiredCompatibilityLevel().isEmpty()) {
323
                if (iFace.getId() != null && !iFace.getId().isEmpty()) {
324
                    LOGGER.debug("updating iface..");
325
                    this.updateInterface(datatype,iFace);
326
                    LOGGER.debug("updated successfully");
327
                } else {
328
                    LOGGER.debug("adding new iface..");
329
                    this.registerRepositoryInterface(repository.getId(),iFace,datatype);
330
                }
331
            }
332
        }
296
        this.storeRepository(repository);
333 297
        return repository;
334 298
    }
335 299

  
......
345 309
        return repository;
346 310
    }
347 311

  
348
    private void updateRegisteredByValue(String id, String registeredBy) {
349

  
350
        LOGGER.debug("Updating registered by value with  : " + registeredBy );
351
        UriComponents uriComponents = UriComponentsBuilder
352
                .fromHttpUrl(baseAddress + "/ds/registeredby/")
353
                .queryParam("dsId",id)
354
                .queryParam("registeredBy", registeredBy)
355
                .build()
356
                .encode();
357

  
358
        restTemplate.postForObject(uriComponents.toUri(), null,String.class);
359
    }
360

  
361 312
    private Repository setRepositoryFeatures(String datatype, Repository repository) {
362 313

  
363 314
        //TODO update map
......
415 366

  
416 367
    }
417 368

  
418
    private RepositoryInterface createRepositoryInterface(Repository repo, RepositoryInterface iFace, String datatype) {
419

  
420
        iFace.setContentDescription("metadata");
421
        iFace.setCompliance("UNKNOWN");
422
        if (datatype.equals("re3data")) {
423
            iFace.setAccessFormat("oai_datacite");
424
        } else {
425
            iFace.setAccessFormat("oai_dc");
426
        }
427

  
428
        if (repo.getDatasourceClass() != null && !repo.getDatasourceClass().isEmpty()) {
429
            iFace.setTypology(repo.getDatasourceClass());
430
        } else if (datatype.equalsIgnoreCase("journal")) {
431
            iFace.setTypology("pubsrepository::journal");
432
        } else if (datatype.equalsIgnoreCase("aggregator")) {
433
            iFace.setTypology("aggregator::pubsrepository::unknown");
434
        } else if (datatype.equalsIgnoreCase("opendoar")) {
435
            iFace.setTypology("pubsrepository::unknown");
436
        } else if (datatype.equalsIgnoreCase("re3data")) {
437
            iFace.setTypology("datarepository::unknown");
438
        }
439

  
440
        iFace.setRemovable(true);
441
        iFace.setAccessProtocol("oai");
442
        iFace.setMetadataIdentifierPath("//*[local-name()='header']/*[local-name()='identifier']");
443
        iFace.setId("api_________::" + repo.getId() + "::" + UUID.randomUUID().toString().substring(0, 8));
444
        if (iFace.getAccessSet().isEmpty()) {
445
            LOGGER.debug("set is empty: " + iFace.getAccessSet());
446
            iFace.removeAccessSet();
447
        }
448
        return iFace;
449
    }
450

  
451 369
    private void storeRepository(Repository repository) throws JSONException {
452 370

  
453 371
        Date utilDate = new Date();
454 372
        Timestamp date = new Timestamp(utilDate.getTime());
455 373
        repository.setDateOfCollection(date);
456 374
        repository.setAggregator("OPENAIRE");
375
        repository.setCountryCode(countriesMap.get(repository.getCountryName()));
457 376

  
458 377
        UriComponents uriComponents = UriComponentsBuilder
459 378
                .fromHttpUrl(baseAddress + "/ds/add/")
......
465 384
    }
466 385

  
467 386
    @Override
468
    public void deleteRepositoryInterface(@PathVariable("id") String id){
387
    public void deleteRepositoryInterface(@RequestParam("id") String id){
469 388
        UriComponents uriComponents = UriComponentsBuilder
470 389
                .fromHttpUrl(baseAddress + "/ds/api/")
471
                .path("/{id}/")
390
                .path("/{id}")
472 391
                .build().expand(id).encode();
392
        LOGGER.debug(uriComponents.toUri());
473 393
        restTemplate.delete(uriComponents.toUri());
474 394
    }
475 395

  
......
477 397
    public RepositoryInterface addRepositoryInterface(@RequestParam("datatype") String datatype,
478 398
                                                      @RequestParam("repoId") String repoId,
479 399
                                                      @RequestBody RepositoryInterface repositoryInterface) throws JSONException {
480
        return registerRepositoryInterface(repoId,repositoryInterface,datatype);
481
    }
482

  
483
    private RepositoryInterface registerRepositoryInterface(String repoId, RepositoryInterface iFace, String datatype) {
484
        Repository e = null;
485 400
        try {
486
            e = this.getRepositoryById(repoId);
487
            iFace = createRepositoryInterface(e,iFace,datatype);
401
            Repository e = this.getRepositoryById(repoId);
402
            repositoryInterface = createRepositoryInterface(e,repositoryInterface,datatype);
403
            String json_interface = Converter.repositoryInterfaceObjectToJson(e,repositoryInterface);
488 404

  
489
            String json_interface = Converter.repositoryInterfaceObjectToJson(e,iFace);
490
            LOGGER.debug("iFace equals -> "  + json_interface);
491 405
            UriComponents uriComponents = UriComponentsBuilder
492 406
                    .fromHttpUrl(baseAddress + "/ds/api/add/")
493 407
                    .build()
494 408
                    .encode();
495 409

  
496

  
497 410
            HttpEntity<String> httpEntity = new HttpEntity <String> (json_interface,httpHeaders);
498 411
            restTemplate.postForObject(uriComponents.toUri(),httpEntity,String.class);
499
            return iFace;
412
            return repositoryInterface;
500 413

  
501 414
        } catch (JSONException e1) {
502 415
            LOGGER.debug("Error parsing json ",e1);
......
504 417
        return null;
505 418
    }
506 419

  
420
    private RepositoryInterface createRepositoryInterface(Repository repo, RepositoryInterface iFace, String datatype) {
421

  
422
        iFace.setContentDescription("metadata");
423
        iFace.setCompliance("UNKNOWN");
424

  
425
        if (datatype.equals("re3data"))
426
            iFace.setAccessFormat("oai_datacite");
427
        else
428
            iFace.setAccessFormat("oai_dc");
429

  
430

  
431
        if (repo.getDatasourceClass() != null && !repo.getDatasourceClass().isEmpty())
432
            iFace.setTypology(repo.getDatasourceClass());
433
        else if (datatype.equalsIgnoreCase("journal"))
434
            iFace.setTypology("pubsrepository::journal");
435
        else if (datatype.equalsIgnoreCase("aggregator"))
436
            iFace.setTypology("aggregator::pubsrepository::unknown");
437
        else if (datatype.equalsIgnoreCase("opendoar"))
438
            iFace.setTypology("pubsrepository::unknown");
439
        else if (datatype.equalsIgnoreCase("re3data"))
440
            iFace.setTypology("datarepository::unknown");
441

  
442
        iFace.setRemovable(true);
443
        iFace.setAccessProtocol("oai");
444
        iFace.setMetadataIdentifierPath("//*[local-name()='header']/*[local-name()='identifier']");
445
        iFace.setId("api_________::" + repo.getId() + "::" + UUID.randomUUID().toString().substring(0, 8));
446
        if (iFace.getAccessSet().isEmpty()) {
447
            LOGGER.debug("set is empty: " + iFace.getAccessSet());
448
            iFace.removeAccessSet();
449
        }
450
        return iFace;
451
    }
452

  
507 453
    @Override
508 454
    public List<String> getDnetCountries() {
509 455
        LOGGER.debug("Getting dnet-countries!");
......
656 602
            return Collections.singletonMap("lastCollectionDate", getRepositoryInterface("openaire____::"+mode).get(1).getLastCollectionDate());
657 603
    }
658 604

  
605
    @Override
606
    public RepositoryInterface updateRepositoryInterface(@RequestParam("repoId") String repoId,
607
                                                         @RequestBody RepositoryInterface repositoryInterface) throws JSONException {
608

  
609
        this.updateBaseUrl(repoId,repositoryInterface.getId(),repositoryInterface.getBaseUrl());
610
        this.updateCompliance(repoId,repositoryInterface.getId(),repositoryInterface.getCompliance());
611
        return repositoryInterface;
612
    }
613

  
614

  
615
    private void updateBaseUrl(String repositoryId, String repositoryInterfaceId, String baseUrl) {
616
        UriComponents uriComponents = UriComponentsBuilder
617
                .fromHttpUrl(baseAddress + "/ds/api/baseurl")
618
                .queryParam("dsId",repositoryId)
619
                .queryParam("apiId",repositoryInterfaceId)
620
                .queryParam("baseUrl",baseUrl)
621
                .build().encode();
622
        restTemplate.postForObject(uriComponents.toUri(),null,String.class);
623
    }
624

  
625
    private void updateCompliance(String repositoryId, String repositoryInterfaceId,String compliance) {
626
        UriComponents uriComponents = UriComponentsBuilder
627
                .fromHttpUrl(baseAddress + "/ds/api/compliance")
628
                .queryParam("dsId",repositoryId)
629
                .queryParam("apiId",repositoryInterfaceId)
630
                .queryParam("compliance",compliance)
631
                .build().encode();
632
        restTemplate.postForObject(uriComponents.toUri(),null,String.class);
633
    }
634

  
659 635
    private MetricsNumbers getMetricsNumbers(String openAIREID) throws BrokerException {
660 636

  
661 637
        //build the uri params
modules/uoa-repository-manager-service/branches/dev-api/src/main/java/eu/dnetlib/repo/manager/service/controllers/PiWikApi.java
2 2

  
3 3

  
4 4
import eu.dnetlib.domain.data.PiwikInfo;
5
import eu.dnetlib.domain.data.Repository;
6
import eu.dnetlib.domain.functionality.UserProfile;
7
import eu.dnetlib.repo.manager.shared.RepositoryServiceException;
5 8
import io.swagger.annotations.Api;
6 9
import org.springframework.http.MediaType;
7 10
import org.springframework.web.bind.annotation.*;
......
31 34
    @RequestMapping(value = "/getOpenaireId/{repositoryId}" , method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
32 35
    @ResponseBody
33 36
    String getOpenaireId(String repositoryid);
37

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff