Project

General

Profile

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 javax.annotation.PostConstruct;
12
import java.io.PrintWriter;
13
import java.io.StringWriter;
14
import java.io.Writer;
15
import java.util.ArrayList;
16
import java.util.List;
17

    
18

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

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

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

    
28
    @Autowired
29
    private MailLibrary mailLibrary;
30

    
31
    @Autowired
32
    private CascadingPropertyLoader pLoader;
33

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

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

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

    
43

    
44
    @PostConstruct
45
    public void init(){
46
        System.out.println("base url -> " + this.baseUrl);
47
    }
48

    
49

    
50
    @Override
51
    public void reportException(Exception exception) {
52
        Writer writer = new StringWriter();
53
        PrintWriter printWriter = new PrintWriter(writer);
54
        exception.printStackTrace(printWriter);
55

    
56
        List<String> recipients = new ArrayList<String>();
57

    
58
        try {
59
            recipients.add(this.adminEmail);
60
            String message = "An exception has occurred:\n"+writer.toString();
61
            String subject = "Automatic Bug Report";
62
            this.sendMail(recipients, subject, message, false, null);
63
        } catch (Exception e) {
64
            LOGGER.error("Error sending error report", e);
65
        }
66
    }
67

    
68
    @Override
69
    public void sendAdministratorRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception {
70

    
71
        try {
72
            String subject = "[OpenAIRE-Usage Statistics] New request to enable usage statistics";
73

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

    
89
            this.sendMail(this.usageStatsAdminEmail, subject, message, false, null);
90

    
91
        } catch (Exception e) {
92
            LOGGER.error("Error while sending request to enable metrics email to administrator: " + this.usageStatsAdminEmail, e);
93
            throw e;
94
        }
95
    }
96

    
97
    @Override
98
    public void sendUserRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception {
99

    
100
        try {
101
            String subject = "[OpenAIRE-Usage Statistics] Your request to enable usage statistics";
102

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

    
126
            this.sendMail(piwikInfo.getRequestorEmail(), subject, message, false, null);
127

    
128
        } catch (Exception e) {
129
            LOGGER.error("Error while sending request to enable metrics email to user: " + piwikInfo.getRequestorEmail(), e);
130
            throw e;
131
        }
132
    }
133

    
134
    @Override
135
    public void sendAdministratorMetricsEnabled(PiwikInfo piwikInfo) throws Exception {
136

    
137
        try {
138
            String subject = "[OpenAIRE-Usage Statistics] Usage statistics have been enabled";
139

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

    
153
            this.sendMail(this.usageStatsAdminEmail, subject, message, false, null);
154

    
155
        } catch (Exception e) {
156
            LOGGER.error("Error while sending metrics enabled notification email to administator: " + this.usageStatsAdminEmail, e);
157
            throw e;
158
        }
159
    }
160

    
161
    @Override
162
    public void sendUserMetricsEnabled(PiwikInfo piwikInfo) throws Exception {
163

    
164
        try {
165
            String subject = "[OpenAIRE-Usage Statistics] Usage statistics have been enabled";
166

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

    
181
            this.sendMail(piwikInfo.getRequestorEmail(), subject, message, false, null);
182

    
183
        } catch (Exception e) {
184
            LOGGER.error("Error while sending metrics enabled notification email to user: " + piwikInfo.getRequestorEmail(), e);
185
            throw e;
186
        }
187
    }
188

    
189
    private void sendMail(String email, String subject, String message, boolean sendToSpecial, List<String> repoAdminMails) throws Exception {
190
        ArrayList<String> to = new ArrayList<String>();
191
        to.add(email);
192
        this.sendMail(to,subject,message,sendToSpecial,repoAdminMails);
193
    }
194

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

    
197
        try {
198
            if (sendToSpecial) {
199
                recipients.addAll(this.specialRecipients);
200
            }
201

    
202
            if (repoAdminMails != null)
203
                recipients.addAll(repoAdminMails);
204

    
205
            if (this.override) {
206
                recipients.clear();
207
                recipients.add(overrideEmail);
208
            }
209
            if (!logonly)
210
                mailLibrary.sendEmail(recipients.toArray(new String[]{}), subject, message);
211
            LOGGER.debug("Sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message);
212
        } catch (Exception e) {
213
            LOGGER.error("Error sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message, e);
214
            throw new Exception(e);
215
        }
216
    }
217

    
218
    private String getEmailProperty(String key) {
219
        return pLoader.getProperties().getProperty(key);
220
    }
221

    
222
    public void setSpecialRecipients(String specialRecipients) {
223
        String[] recps = specialRecipients.split(",");
224

    
225
        for (String recp : recps) {
226
            recp = recp.trim();
227

    
228
            this.specialRecipients.add(recp);
229
        }
230
    }
231

    
232

    
233
    public void setOverride(boolean override) {
234
        this.override = override;
235
    }
236

    
237
    public void setOverrideEmail(String overrideEmail) {
238
        this.overrideEmail = overrideEmail;
239
    }
240

    
241
    public String getFrom() {
242
        return from;
243
    }
244

    
245
    public void setFrom(String from) {
246
        this.from = from;
247
    }
248

    
249
    public boolean isLogonly() {
250
        return logonly;
251
    }
252

    
253
    public void setLogonly(boolean logonly) {
254
        this.logonly = logonly;
255
    }
256

    
257

    
258
}
(4-4/19)