• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CeON / dataverse / 1371

28 Jun 2024 05:54AM UTC coverage: 25.453% (-0.05%) from 25.503%
1371

push

jenkins

web-flow
Closes #2474: Store the entity id in saml session once user logs in with the identity provider (#2486)

49 of 54 new or added lines in 4 files covered. (90.74%)

860 existing lines in 14 files now uncovered.

17805 of 69953 relevant lines covered (25.45%)

0.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/SendFeedbackDialog.java
1
package edu.harvard.iq.dataverse;
2

3
import com.google.common.collect.Lists;
4
import edu.harvard.iq.dataverse.common.BrandingUtil;
5
import edu.harvard.iq.dataverse.common.BundleUtil;
6
import edu.harvard.iq.dataverse.feedback.Feedback;
7
import edu.harvard.iq.dataverse.feedback.FeedbackInfo;
8
import edu.harvard.iq.dataverse.feedback.FeedbackRecipient;
9
import edu.harvard.iq.dataverse.feedback.FeedbackUtil;
10
import edu.harvard.iq.dataverse.mail.MailService;
11
import edu.harvard.iq.dataverse.persistence.DvObject;
12
import edu.harvard.iq.dataverse.persistence.dataset.Dataset;
13
import edu.harvard.iq.dataverse.persistence.dataverse.Dataverse;
14
import edu.harvard.iq.dataverse.persistence.user.AuthenticatedUser;
15
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
16
import edu.harvard.iq.dataverse.util.JsfHelper;
17
import edu.harvard.iq.dataverse.util.MailUtil;
18
import edu.harvard.iq.dataverse.util.SystemConfig;
19
import org.apache.commons.validator.routines.EmailValidator;
20
import org.omnifaces.cdi.ViewScoped;
21

22
import javax.faces.application.FacesMessage;
23
import javax.faces.component.UIComponent;
24
import javax.faces.context.FacesContext;
25
import javax.faces.event.ActionEvent;
26
import javax.faces.validator.ValidatorException;
27
import javax.inject.Inject;
28
import javax.inject.Named;
29
import javax.mail.internet.InternetAddress;
30
import java.util.List;
31
import java.util.Locale;
32
import java.util.Random;
33
import java.util.logging.Logger;
34

35
@ViewScoped
36
@Named
37
public class SendFeedbackDialog implements java.io.Serializable {
38

UNCOV
39
    private static final Logger logger = Logger.getLogger(SendFeedbackDialog.class.getCanonicalName());
×
40

41
    private MailService mailService;
42
    private SettingsServiceBean settingsService;
43
    private DataverseDao dataverseDao;
44
    private SystemConfig systemConfig;
45
    private DataverseSession dataverseSession;
46

47
    /** The email address supplied by the person filling out the contact form. */
48
    private String userEmail = "";
×
49

50
    /** Body of the message. */
51
    private String userMessage = "";
×
52

53
    /** Becomes the subject of the email. */
UNCOV
54
    private String messageSubject = "";
×
55

56
    /** First operand in addition problem. */
57
    private Long op1;
58

59
    /** Second operand in addition problem. */
60
    private Long op2;
61

62
    /** The guess the user makes in addition problem. */
63
    private Long userSum;
64

65
    /**
66
     * Either the dataverse or the dataset that the message is pertaining to.
67
     * If there is no target, the feedback message is about the repo as a whole.
68
     */
69
    private DvObject feedbackTarget;
70

71
    /** Whether a copy of the message should be sent to user's mail */
72
    private boolean sendCopy;
73

74
    /** :SystemEmail (the main support address for an installation). */
75
    private InternetAddress systemAddress;
76

77
    private FeedbackRecipient recipientOption;
78

79
    // -------------------- CONSTRUCTORS --------------------
80

81
    public SendFeedbackDialog() { }
×
82

83
    @Inject
84
    public SendFeedbackDialog(MailService mailService, SettingsServiceBean settingsService,
85
                              DataverseDao dataverseDao, SystemConfig systemConfig,
86
                              DataverseSession dataverseSession) {
×
87
        this.mailService = mailService;
×
UNCOV
88
        this.settingsService = settingsService;
×
UNCOV
89
        this.dataverseDao = dataverseDao;
×
UNCOV
90
        this.systemConfig = systemConfig;
×
UNCOV
91
        this.dataverseSession = dataverseSession;
×
92
    }
×
93

94
    // -------------------- GETTERS --------------------
95

96
    public String getUserEmail() {
UNCOV
97
        return userEmail;
×
98
    }
99

100
    public Long getOp1() {
UNCOV
101
        return op1;
×
102
    }
103

104
    public Long getOp2() {
UNCOV
105
        return op2;
×
106
    }
107

108
    public Long getUserSum() {
UNCOV
109
        return userSum;
×
110
    }
111

112
    public String getUserMessage() {
UNCOV
113
        return userMessage;
×
114
    }
115

116
    public String getMessageSubject() {
UNCOV
117
        return messageSubject;
×
118
    }
119

120
    public boolean getSendCopy() {
UNCOV
121
        return sendCopy;
×
122
    }
123

124
    public DvObject getFeedbackTarget() {
UNCOV
125
        return feedbackTarget;
×
126
    }
127

128
    public FeedbackRecipient getRecipientOption() {
129
        return recipientOption;
×
130
    }
131

132
    // -------------------- LOGIC --------------------
133

134
    public void initUserInput() {
135
        userEmail = "";
×
136
        userMessage = "";
×
UNCOV
137
        messageSubject = "";
×
UNCOV
138
        Random random = new Random();
×
139
        op1 = (long) random.nextInt(10);
×
140
        op2 = (long) random.nextInt(10);
×
UNCOV
141
        userSum = null;
×
UNCOV
142
        String systemEmail = settingsService.getValueForKey(SettingsServiceBean.Key.SystemEmail);
×
143
        systemAddress = MailUtil.parseSystemAddress(systemEmail);
×
144
        sendCopy = false;
×
145
    }
×
146

147
    public void initUserInput(ActionEvent ae) {
148
        initUserInput();
×
UNCOV
149
    }
×
150

151

152
    public List<FeedbackRecipient> getRecipientOptions() {
153
        if (feedbackTarget == null) {
×
154
            return Lists.newArrayList(FeedbackRecipient.SYSTEM_SUPPORT);
×
155
        } else if (feedbackTarget.isInstanceofDataverse()) {
×
156
            return Lists.newArrayList(FeedbackRecipient.SYSTEM_SUPPORT, FeedbackRecipient.DATAVERSE_CONTACT);
×
157
        } else {
158
            return Lists.newArrayList(FeedbackRecipient.SYSTEM_SUPPORT, FeedbackRecipient.DATAVERSE_CONTACT,
×
159
                    FeedbackRecipient.DATASET_CONTACT);
160
        }
161
    }
162

163
    public String getRecipientOptionLabel(FeedbackRecipient option) {
164
        if (option == FeedbackRecipient.SYSTEM_SUPPORT) {
×
165
            return BundleUtil.getStringFromBundle("contact.to.option.repo", dataverseDao.findRootDataverse().getName());
×
166
        } else if (option == FeedbackRecipient.DATAVERSE_CONTACT) {
×
UNCOV
167
            return BundleUtil.getStringFromBundle("contact.to.option.dataverse");
×
168
        } else {
UNCOV
169
            return BundleUtil.getStringFromBundle("contact.to.option.dataset");
×
170
        }
171
    }
172

173
    public String getFormHeader() {
174
        if (feedbackTarget == null) {
×
UNCOV
175
            return BrandingUtil.getContactHeader(systemAddress, dataverseDao.findRootDataverse().getName());
×
176
        } else if (feedbackTarget.isInstanceofDataverse()) {
×
UNCOV
177
            return BundleUtil.getStringFromBundle("contact.dataverse.header");
×
178
        } else {
UNCOV
179
            return BundleUtil.getStringFromBundle("contact.dataset.header");
×
180
        }
181
    }
182

183
    public void validateUserSum(FacesContext context, UIComponent component, Object value) throws ValidatorException {
184
        if (op1 + op2 != (Long) value) {
×
185
            FacesMessage msg = new FacesMessage(BundleUtil.getStringFromBundle("contact.sum.invalid"));
×
186
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
×
187
            throw new ValidatorException(msg);
×
188
        }
189
    }
×
190

191
    public void validateUserEmail(FacesContext context, UIComponent component, Object value) throws ValidatorException {
192
        if (!EmailValidator.getInstance().isValid((String) value)) {
×
193
            FacesMessage msg = new FacesMessage(BundleUtil.getStringFromBundle("external.newAccount.emailInvalid"));
×
194
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
×
UNCOV
195
            throw new ValidatorException(msg);
×
196
        }
UNCOV
197
    }
×
198

199
    public String sendMessage() {
200
        // FIXME: move dataverseDao.findRootDataverse() to init
UNCOV
201
        String rootDataverseName = dataverseDao.findRootDataverse().getName();
×
202
        String installationBrandName = BrandingUtil.getInstallationBrandName(rootDataverseName);
×
UNCOV
203
        String supportTeamName = BrandingUtil.getSupportTeamName(systemAddress, rootDataverseName);
×
UNCOV
204
        List<Feedback> feedbacks = FeedbackUtil.gatherFeedback(new FeedbackInfo<>()
×
UNCOV
205
                .withFeedbackTarget(feedbackTarget)
×
206
                .withRecipient(recipientOption)
×
UNCOV
207
                .withUserEmail(dataverseSession, userEmail)
×
UNCOV
208
                .withSystemEmail(systemAddress)
×
UNCOV
209
                .withMessageSubject(messageSubject)
×
UNCOV
210
                .withUserMessage(userMessage)
×
UNCOV
211
                .withDataverseSiteUrl(systemConfig.getDataverseSiteUrl())
×
212
                .withInstallationBrandName(installationBrandName)
×
213
                .withSupportTeamName(supportTeamName));
×
UNCOV
214
        if (feedbacks.isEmpty()) {
×
UNCOV
215
            logger.warning("No feedback has been sent!");
×
216
            JsfHelper.addErrorMessage(BundleUtil.getStringFromBundle("contact.send.failure"));
×
217
            return null;
×
218
        }
219
        for (Feedback feedback : feedbacks) {
×
220
            logger.fine("sending feedback: " + feedback);
×
221
            mailService.sendMailAsync(feedback.getFromEmail(), feedback.getToEmail(), feedback.getSubject(), feedback.getBody());
×
222
        }
×
223
        if (sendCopy) {
×
224
            sendCopy(rootDataverseName, feedbacks.get(0));
×
225
        }
226
        JsfHelper.addSuccessMessage(BundleUtil.getStringFromBundle("contact.send.success"));
×
227

228
        return null;
×
229
    }
230

231
    public boolean isLoggedIn() {
232
        return dataverseSession.getUser().isAuthenticated();
×
233
    }
234

235
    public String loggedInUserEmail() {
UNCOV
236
        return dataverseSession.getUser().getDisplayInfo().getEmailAddress();
×
237
    }
238

239
    public void setFeedbackTarget(DvObject feedbackTarget) {
UNCOV
240
        this.feedbackTarget = feedbackTarget;
×
241

UNCOV
242
        if (feedbackTarget == null) {
×
243
            recipientOption = FeedbackRecipient.SYSTEM_SUPPORT;
×
244
        } else if (feedbackTarget.isInstanceofDataverse()) {
×
UNCOV
245
            recipientOption = FeedbackRecipient.DATAVERSE_CONTACT;
×
246
        } else {
247
            recipientOption = FeedbackRecipient.DATASET_CONTACT;
×
248
        }
UNCOV
249
    }
×
250

251
    // -------------------- PRIVATE --------------------
252

253
    private void sendCopy(String rootDataverseName, Feedback feedback) {
UNCOV
254
        String mail = isLoggedIn() ? loggedInUserEmail() : userEmail;
×
255
        Locale locale = isLoggedIn() ? loggedInUserLanguage() : BundleUtil.getCurrentLocale();
×
256
        
257
        String header;
UNCOV
258
        String siteUrl = systemConfig.getDataverseSiteUrl();
×
259
        if (feedbackTarget != null && feedbackTarget.isInstanceofDataverse()) {
×
260
            Dataverse dataverse = (Dataverse) feedbackTarget;
×
UNCOV
261
            header = BundleUtil.getStringFromBundleWithLocale("contact.copy.message.header.dataverse", locale,
×
UNCOV
262
                    rootDataverseName, dataverse.getName(),
×
263
                    siteUrl + "/dataverse/" + dataverse.getAlias());
×
264
        } else if (feedbackTarget != null && feedbackTarget.isInstanceofDataset()) {
×
UNCOV
265
            Dataset dataset = (Dataset) feedbackTarget;
×
UNCOV
266
            header = BundleUtil.getStringFromBundleWithLocale("contact.copy.message.header.dataset", locale,
×
267
                    rootDataverseName, dataset.getDisplayName(),
×
268
                    siteUrl + "/dataset.xhtml?persistentId=" + dataset.getGlobalId().asString());
×
UNCOV
269
        } else {
×
UNCOV
270
            header = BundleUtil.getStringFromBundleWithLocale("contact.copy.message.header.general", locale, rootDataverseName);
×
271
        }
272
        String content = header + BundleUtil.getStringFromBundleWithLocale("contact.copy.message.template", locale, userMessage)
×
UNCOV
273
                + mailService.getFooterMailMessage(null, locale);
×
UNCOV
274
        mailService.sendMailAsync(null, mail,
×
UNCOV
275
                BundleUtil.getStringFromBundleWithLocale("contact.copy.message.subject", locale, feedback.getSubject()), content);
×
UNCOV
276
    }
×
277

278
    private Locale loggedInUserLanguage() {
UNCOV
279
        return ((AuthenticatedUser)dataverseSession.getUser()).getNotificationsLanguage();
×
280
    }
281

282
    // -------------------- SETTERS --------------------
283

284
    public void setUserEmail(String uEmail) {
UNCOV
285
        userEmail = uEmail;
×
UNCOV
286
    }
×
287

288
    public void setOp1(Long op1) {
UNCOV
289
        this.op1 = op1;
×
UNCOV
290
    }
×
291

292
    public void setOp2(Long op2) {
UNCOV
293
        this.op2 = op2;
×
UNCOV
294
    }
×
295

296
    public void setUserSum(Long userSum) {
UNCOV
297
        this.userSum = userSum;
×
UNCOV
298
    }
×
299

300
    public void setUserMessage(String mess) {
UNCOV
301
        userMessage = mess;
×
UNCOV
302
    }
×
303

304
    public void setMessageSubject(String messageSubject) {
UNCOV
305
        this.messageSubject = messageSubject;
×
UNCOV
306
    }
×
307

308
    public void setSendCopy(boolean sendCopy) {
UNCOV
309
        this.sendCopy = sendCopy;
×
UNCOV
310
    }
×
311

312
    public void setRecipientOption(FeedbackRecipient recipientOption) {
UNCOV
313
        this.recipientOption = recipientOption;
×
UNCOV
314
    }
×
315

316
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc