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

CeON / dataverse / 1370

26 Jun 2024 06:59AM UTC coverage: 25.503% (-0.004%) from 25.507%
1370

push

jenkins

web-flow
Closes #2496: Make the banner link optional (#2503)

17782 of 69725 relevant lines covered (25.5%)

0.26 hits per line

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

1.11
/dataverse-webapp/src/main/java/edu/harvard/iq/dataverse/datafile/page/FileDownloadHelper.java
1
/*
2
 * To change this license header, choose License Headers in Project Properties.
3
 * To change this template file, choose Tools | Templates
4
 * and open the template in the editor.
5
 */
6
package edu.harvard.iq.dataverse.datafile.page;
7

8
import edu.harvard.iq.dataverse.DataverseSession;
9
import edu.harvard.iq.dataverse.PermissionsWrapper;
10
import edu.harvard.iq.dataverse.datafile.FileDownloadServiceBean;
11
import edu.harvard.iq.dataverse.guestbook.GuestbookResponseServiceBean;
12
import edu.harvard.iq.dataverse.persistence.datafile.DataFile;
13
import edu.harvard.iq.dataverse.persistence.datafile.ExternalTool;
14
import edu.harvard.iq.dataverse.persistence.datafile.FileMetadata;
15
import edu.harvard.iq.dataverse.persistence.datafile.license.FileTermsOfUse.TermsOfUseType;
16
import edu.harvard.iq.dataverse.persistence.dataset.DatasetVersion;
17
import edu.harvard.iq.dataverse.persistence.guestbook.GuestbookResponse;
18
import edu.harvard.iq.dataverse.util.FileUtil;
19
import edu.harvard.iq.dataverse.util.PrimefacesUtil;
20
import org.apache.commons.lang3.StringUtils;
21
import org.omnifaces.cdi.ViewScoped;
22
import org.primefaces.PrimeFaces;
23

24
import javax.inject.Inject;
25
import javax.inject.Named;
26
import java.util.List;
27
import java.util.logging.Logger;
28

29
import static java.util.stream.Collectors.toList;
30

31
/**
32
 * Class joining downloads and guestbook response writing on UI
33
 * <p>
34
 * Note that following dialogs must be defined on page for proper functioning:
35
 * <ul>
36
 * <li>downloadPopup</li>
37
 * <li>downloadPackagePopup</li>
38
 * </ul>
39
 *
40
 * @author skraffmi
41
 */
42
@ViewScoped
43
@Named
44
public class FileDownloadHelper implements java.io.Serializable {
45

46
    private static final Logger logger = Logger.getLogger(FileDownloadHelper.class.getCanonicalName());
1✔
47

48
    private DataverseSession session;
49

50
    private PermissionsWrapper permissionsWrapper;
51

52
    private FileDownloadServiceBean fileDownloadService;
53

54
    private GuestbookResponseServiceBean guestbookResponseService;
55

56
    private RequestedDownloadType requestedDownloadType;
57

58
    // -------------------- CONSTRUCTORS --------------------
59

60
    @Deprecated
61
    public FileDownloadHelper() {
×
62

63
    }
×
64

65
    @Inject
66
    public FileDownloadHelper(DataverseSession session, PermissionsWrapper permissionsWrapper,
67
                              FileDownloadServiceBean fileDownloadService, GuestbookResponseServiceBean guestbookResponseService,
68
                              RequestedDownloadType requestedDownloadType) {
×
69
        this.session = session;
×
70
        this.permissionsWrapper = permissionsWrapper;
×
71
        this.fileDownloadService = fileDownloadService;
×
72
        this.guestbookResponseService = guestbookResponseService;
×
73
        this.requestedDownloadType = requestedDownloadType;
×
74
    }
×
75

76
    // -------------------- LOGIC --------------------
77

78
    /**
79
     * Writes guestbook response and start download
80
     *
81
     * @param userProvidedGuestbookResponse - guestbook response with values
82
     * that should be provided by user (already validated) (that is name, email, custom question responses, etc.)
83
     */
84
    public void writeGuestbookAndStartDownloadAccordingToType(GuestbookResponse userProvidedGuestbookResponse) {
85

86
        DownloadType fileFormat = requestedDownloadType.getFileFormat();
×
87
        List<FileMetadata> fileMetadatas = requestedDownloadType.getFileMetadatas();
×
88

89
        userProvidedGuestbookResponse.setDownloadtype(buildGuestbookResponseDownloadType(fileFormat, requestedDownloadType.getTool()));
×
90

91
        for (FileMetadata fileMetadata : fileMetadatas) {
×
92
            DataFile dataFile = fileMetadata.getDataFile();
×
93

94
            if (dataFile.isReleased()) {
×
95
                userProvidedGuestbookResponse.setDataFile(fileMetadata.getDataFile());
×
96
                fileDownloadService.writeGuestbookResponseRecord(userProvidedGuestbookResponse);
×
97
            }
98
        }
×
99

100
        startDownloadAccordingToType(fileMetadatas, fileFormat, requestedDownloadType.getTool(), true);
×
101
    }
×
102

103
    public void writeGuestbookResponseForPreview(GuestbookResponse guestbookResponse, FileMetadata fileMetadata, ExternalTool previewer) {
104
        guestbookResponse.setDownloadtype(buildGuestbookResponseDownloadType(DownloadType.PREVIEW, previewer));
×
105
        DataFile dataFile = fileMetadata.getDataFile();
×
106
        if (dataFile.isReleased()) {
×
107
            guestbookResponse.setDataFile(dataFile);
×
108
            fileDownloadService.writeGuestbookResponseRecord(guestbookResponse);
×
109
        }
110
    }
×
111

112
    /**
113
     * Initializes {@link RequestedDownloadType} object and start
114
     * downloading process using {@link #requestDownload()}.
115
     *
116
     * @param fileMetadatas - files to download
117
     * @param requestedOriginalDownload - true if should download original format of files
118
     * or false for default download
119
     */
120
    public void requestDownloadWithFiles(List<FileMetadata> fileMetadatas, boolean requestedOriginalDownload) {
121

122
        if (requestedOriginalDownload) {
×
123
            requestedDownloadType.initMultiOriginalDownloadType(fileMetadatas);
×
124
        } else {
125
            requestedDownloadType.initMultiDownloadType(fileMetadatas);
×
126
        }
127

128
        requestDownload();
×
129
    }
×
130

131
    public String requestDownloadOfWholeDataset(DatasetVersion dsv, boolean requestedOriginalDownload) {
132

133
        DownloadType downloadType = requestedOriginalDownload ? DownloadType.ORIGINAL : DownloadType.DOWNLOAD;
×
134

135
        String filesDownloadUrl = FileUtil.getDownloadWholeDatasetUrlPath(dsv, false, downloadType.getApiBatchDownloadEquivalent());
×
136
        PrimeFaces.current().ajax().addCallbackParam("apiDownloadLink", filesDownloadUrl);
×
137

138
        return StringUtils.EMPTY;
×
139
    }
140

141
    public void requestDownloadOfWholeDatasetAsCSV(DatasetVersion dsv) {
142
        PrimeFaces.current().ajax().addCallbackParam("apiDownloadLink", FileUtil.getDownloadWholeDatasetAsCSVUrlPath(dsv));
×
143
    }
×
144

145
    /**
146
     * Starts downloading process according on values stored in {@link RequestedDownloadType}.
147
     * <p>
148
     * If guestbook fill is required then it shows proper dialog or handles download
149
     * directly otherwise.
150
     */
151
    public String requestDownload() {
152

153
        List<FileMetadata> fileMetadatas = requestedDownloadType.getFileMetadatas();
×
154
        DownloadType fileFormat = requestedDownloadType.getFileFormat();
×
155

156
        if (FileUtil.isDownloadPopupRequired(fileMetadatas.get(0).getDatasetVersion())) {
×
157
            PrimefacesUtil.showDialog("downloadPopup");
×
158
            return StringUtils.EMPTY;
×
159
        }
160

161
        startDownloadAccordingToType(fileMetadatas, fileFormat, requestedDownloadType.getTool(), false);
×
162
        return StringUtils.EMPTY;
×
163
    }
164

165
    public boolean canUserDownloadFile(FileMetadata fileMetadata) {
166
        if (fileMetadata == null) {
×
167
            return false;
×
168
        }
169

170
        if ((fileMetadata.getId() == null) || (fileMetadata.getDataFile().getId() == null)) {
×
171
            return false;
×
172
        }
173

174
        if (fileMetadata.getDatasetVersion().isDeaccessioned()) {
×
175
            return permissionsWrapper.canCurrentUserUpdateDataset(fileMetadata.getDatasetVersion().getDataset());
×
176
        }
177

178

179
        boolean isRestrictedFile = fileMetadata.getTermsOfUse().getTermsOfUseType() == TermsOfUseType.RESTRICTED;
×
180
        if (!isRestrictedFile) {
×
181
            return true;
×
182
        }
183

184
        // See if the DataverseRequest, which contains IP Groups, has permission to download the file.
185
        if (permissionsWrapper.hasDownloadFilePermission(fileMetadata.getDataFile())) {
×
186
            logger.fine("The DataverseRequest (User plus IP address) has access to download the file.");
×
187
            return true;
×
188
        }
189

190
        return false;
×
191
    }
192

193

194
    // -------------------- PRIVATE --------------------
195

196
    private void startDownloadAccordingToType(List<FileMetadata> fileMetadatas, DownloadType fileFormat, ExternalTool tool, boolean guestbookRecordsAlreadyWritten) {
197
        if (fileMetadatas.size() > 1) {
×
198
            String filesDownloadUrl = FileUtil.getBatchFilesDownloadUrlPath(
×
199
                    fileMetadatas.stream().map(x -> x.getDataFile().getId()).collect(toList()), guestbookRecordsAlreadyWritten,
×
200
                    fileFormat.getApiBatchDownloadEquivalent());
×
201
            PrimeFaces.current().ajax().addCallbackParam("apiDownloadLink", filesDownloadUrl);
×
202

203
            return;
×
204
        }
205

206
        DataFile dataFile = fileMetadatas.get(0).getDataFile();
×
207

208
        if (fileFormat.isCompatibleWithApiDownload()) {
×
209
            String fileDownloadUrl = FileUtil.getFileDownloadUrlPath(
×
210
                        fileFormat.getApiDownloadEquivalent(),
×
211
                        dataFile.getId(),
×
212
                        guestbookRecordsAlreadyWritten);
213
            PrimeFaces.current().ajax().addCallbackParam("apiDownloadLink", fileDownloadUrl);
×
214

215
        } else if (fileFormat == DownloadType.WORLDMAP) {
×
216
            writeGuestbookResponseIfReleased(dataFile, DownloadType.WORLDMAP, null, guestbookRecordsAlreadyWritten);
×
217
            fileDownloadService.startWorldMapDownloadLink(dataFile);
×
218

219
        } else if (fileFormat == DownloadType.EXTERNALTOOL) {
×
220
            writeGuestbookResponseIfReleased(dataFile, DownloadType.EXTERNALTOOL, tool, guestbookRecordsAlreadyWritten);
×
221
            fileDownloadService.explore(dataFile, tool);
×
222

223
        } else if (fileFormat == DownloadType.PACKAGE) {
×
224
            PrimefacesUtil.showDialogAndResize("downloadPackagePopup");
×
225
        }
226
    }
×
227

228
    private void writeGuestbookResponseIfReleased(DataFile dataFile, DownloadType fileFormat, ExternalTool tool, boolean guestbookRecordsAlreadyWritten) {
229
        if (!guestbookRecordsAlreadyWritten && dataFile.isReleased()) {
×
230

231
            GuestbookResponse downloadOnlyGuestbook = guestbookResponseService.initUIGuestbookResponseWithoutFile(
×
232
                    dataFile.getOwner(), session);
×
233
            downloadOnlyGuestbook.setDownloadtype(buildGuestbookResponseDownloadType(fileFormat, tool));
×
234
            downloadOnlyGuestbook.setDataFile(dataFile);
×
235
            fileDownloadService.writeGuestbookResponseRecord(downloadOnlyGuestbook);
×
236
        }
237
    }
×
238

239
    private String buildGuestbookResponseDownloadType(DownloadType fileFormat, ExternalTool tool) {
240
        switch (fileFormat) {
×
241
            case WORLDMAP:
242
                return "WorldMap";
×
243
            case EXTERNALTOOL:
244
            case PREVIEW:
245
                return tool.getDisplayName();
×
246
            default:
247
                return "Download";
×
248
        }
249
    }
250
}
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