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

IQSS / dataverse / #22693

03 Jul 2024 01:09PM CUT coverage: 20.626% (-0.09%) from 20.716%
#22693

push

github

web-flow
Merge pull request #10664 from IQSS/develop

merge develop into master for 6.3

195 of 1852 new or added lines in 82 files covered. (10.53%)

72 existing lines in 33 files now uncovered.

17335 of 84043 relevant lines covered (20.63%)

0.21 hits per line

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

45.16
/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/UpdateDatasetThumbnailCommand.java
1
package edu.harvard.iq.dataverse.engine.command.impl;
2

3
import edu.harvard.iq.dataverse.DataFile;
4
import edu.harvard.iq.dataverse.Dataset;
5
import edu.harvard.iq.dataverse.authorization.Permission;
6
import edu.harvard.iq.dataverse.dataaccess.ImageThumbConverter;
7
import edu.harvard.iq.dataverse.dataset.DatasetThumbnail;
8
import edu.harvard.iq.dataverse.engine.command.AbstractCommand;
9
import edu.harvard.iq.dataverse.engine.command.CommandContext;
10
import edu.harvard.iq.dataverse.engine.command.DataverseRequest;
11
import edu.harvard.iq.dataverse.engine.command.RequiredPermissions;
12
import edu.harvard.iq.dataverse.engine.command.exception.CommandException;
13
import edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException;
14
import edu.harvard.iq.dataverse.util.BundleUtil;
15
import edu.harvard.iq.dataverse.util.FileUtil;
16
import java.io.File;
17
import java.io.FileInputStream;
18
import java.io.FileNotFoundException;
19
import java.io.IOException;
20
import java.io.InputStream;
21
import java.util.List;
22
import java.util.logging.Level;
23
import java.util.logging.Logger;
24
import org.apache.commons.io.IOUtils;
25

26
@RequiredPermissions(Permission.EditDataset)
27
public class UpdateDatasetThumbnailCommand extends AbstractCommand<DatasetThumbnail> {
28

29
    private static final Logger logger = Logger.getLogger(UpdateDatasetThumbnailCommand.class.getCanonicalName());
1✔
30

31
    private final Dataset dataset;
32
    private final UserIntent userIntent;
33
    /**
34
     * @todo make this a long rather than a Long.
35
     */
36
    private final Long dataFileIdSupplied;
37
    private final InputStream inputStream;
38

39
    public enum UserIntent {
1✔
40
        setDatasetFileAsThumbnail,
1✔
41
        setNonDatasetFileAsThumbnail,
1✔
42
        removeThumbnail
1✔
43
    };
44

45
    public UpdateDatasetThumbnailCommand(DataverseRequest aRequest, Dataset theDataset, UserIntent theUserIntent, Long theDataFileIdSupplied, InputStream theInputStream) {
46
        super(aRequest, theDataset);
1✔
47
        dataset = theDataset;
1✔
48
        userIntent = theUserIntent;
1✔
49
        inputStream = theInputStream;
1✔
50
        this.dataFileIdSupplied = theDataFileIdSupplied;
1✔
51
    }
1✔
52

53
    @Override
54
    public DatasetThumbnail execute(CommandContext ctxt) throws CommandException {
55
        if (dataset == null) {
1✔
56
            String message = "Can't update dataset thumbnail. Dataset is null.";
1✔
57
            logger.info(message);
1✔
58
            throw new IllegalCommandException(message, this);
1✔
59
        }
60
//        if (true) {
61
//            throw new CommandException("Just testing what an error would look like in the GUI.", this);
62
//        }
63
        if (userIntent == null) {
1✔
64
            throw new IllegalCommandException(BundleUtil.getStringFromBundle("datasets.api.thumbnail.noChange"), this);
1✔
65
        }
66
        switch (userIntent) {
1✔
67

68
            case setDatasetFileAsThumbnail:
69
                if (dataFileIdSupplied == null) {
1✔
70
                    throw new CommandException(BundleUtil.getStringFromBundle("datasets.api.thumbnail.fileNotSupplied"), this);
1✔
71
                }
72
                DataFile datasetFileThumbnailToSwitchTo = ctxt.files().find(dataFileIdSupplied);
1✔
73
                if (datasetFileThumbnailToSwitchTo == null) {
1✔
74
                    throw new CommandException(BundleUtil.getStringFromBundle("datasets.api.thumbnail.fileNotFound",
1✔
75
                            List.of(dataFileIdSupplied.toString())), this);
1✔
76
                }
77
                Dataset ds1 = ctxt.datasets().setDatasetFileAsThumbnail(dataset, datasetFileThumbnailToSwitchTo);
1✔
78
                DatasetThumbnail datasetThumbnail = ds1.getDatasetThumbnail(ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE);
1✔
79
                if (datasetThumbnail != null) {
1✔
80
                    DataFile dataFile = datasetThumbnail.getDataFile();
×
81
                    if (dataFile != null) {
×
82
                        if (dataFile.getId().equals(dataFileIdSupplied)) {
×
83
                            return datasetThumbnail;
×
84
                        } else {
NEW
85
                            throw new CommandException(BundleUtil.getStringFromBundle("datasets.api.thumbnail.basedOnWrongFileId",
×
NEW
86
                                    List.of(String.valueOf(dataFile.getId()),String.valueOf(dataFileIdSupplied))), this);
×
87
                        }
88
                    }
89
                } else {
×
90
                    throw new CommandException(BundleUtil.getStringFromBundle("datasets.api.thumbnail.missing"), this);
1✔
91
                }
92

93
            case setNonDatasetFileAsThumbnail:
94
                File uploadedFile;
95
                try {
96
                    uploadedFile = FileUtil.inputStreamToFile(inputStream);
×
97
                } catch (IOException ex) {
×
NEW
98
                    throw new CommandException(BundleUtil.getStringFromBundle("datasets.api.thumbnail.inputStreamToFile.exception", List.of(ex.getMessage())), this);
×
99
                }
×
100
                if (uploadedFile == null) {
×
NEW
101
                    throw new CommandException(BundleUtil.getStringFromBundle("datasets.api.thumbnail.nonDatasetsFileIsNull"), this);
×
102
                }
103
                long uploadLogoSizeLimit = ctxt.systemConfig().getUploadLogoSizeLimit();
×
104
                if (uploadedFile.length() > uploadLogoSizeLimit) {
×
NEW
105
                    throw new IllegalCommandException(BundleUtil.getStringFromBundle("datasets.api.thumbnail.fileToLarge", List.of(String.valueOf(uploadLogoSizeLimit))), this);
×
106
                }
107
                FileInputStream fileAsStream = null;
×
108
                try {
109
                    fileAsStream = new FileInputStream(uploadedFile);
×
110
                } catch (FileNotFoundException ex) {
×
111
                    Logger.getLogger(UpdateDatasetThumbnailCommand.class.getName()).log(Level.SEVERE, null, ex);
×
112
                }
×
113
                Dataset datasetWithNewThumbnail = ctxt.datasets().setNonDatasetFileAsThumbnail(dataset, fileAsStream);
×
NEW
114
                IOUtils.closeQuietly(fileAsStream);
×
115
                if (datasetWithNewThumbnail != null) {
×
NEW
116
                    DatasetThumbnail thumbnail = datasetWithNewThumbnail.getDatasetThumbnail(ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE);
×
NEW
117
                    if (thumbnail != null) {
×
NEW
118
                        return thumbnail;
×
119
                    }
120
                }
NEW
121
                throw new IllegalCommandException(BundleUtil.getStringFromBundle("datasets.api.thumbnail.nonDatasetFailed"), this);
×
122

123
            case removeThumbnail:
NEW
124
                Dataset ds2 = ctxt.datasets().clearDatasetLevelThumbnail(dataset);
×
125
                DatasetThumbnail datasetThumbnail2 = ds2.getDatasetThumbnail(ImageThumbConverter.DEFAULT_CARDIMAGE_SIZE);
×
126
                if (datasetThumbnail2 == null) {
×
127
                    return null;
×
128
                } else {
NEW
129
                    throw new CommandException(BundleUtil.getStringFromBundle("datasets.api.thumbnail.notDeleted"), this);
×
130
                }
131
            default:
NEW
132
                throw new IllegalCommandException(BundleUtil.getStringFromBundle("datasets.api.thumbnail.actionNotSupported"), this);
×
133
        }
134
    }
135

136
}
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

© 2025 Coveralls, Inc