Project

General

Profile

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
}
(4-4/7)