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

IQSS / dataverse / #22002

01 Apr 2024 07:56PM CUT coverage: 20.716% (+0.5%) from 20.173%
#22002

push

github

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

Merge 6.2 into master

704 of 2679 new or added lines in 152 files covered. (26.28%)

81 existing lines in 49 files now uncovered.

17160 of 82836 relevant lines covered (20.72%)

0.21 hits per line

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

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

3
import edu.harvard.iq.dataverse.Dataset;
4
import edu.harvard.iq.dataverse.engine.command.CommandContext;
5
import edu.harvard.iq.dataverse.engine.command.DataverseRequest;
6
import edu.harvard.iq.dataverse.engine.command.exception.CommandException;
7
import edu.harvard.iq.dataverse.engine.command.exception.CommandExecutionException;
8
import edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException;
9
import edu.harvard.iq.dataverse.engine.command.exception.PermissionException;
10
import edu.harvard.iq.dataverse.pidproviders.PidProvider;
11
import edu.harvard.iq.dataverse.pidproviders.PidUtil;
12

13
import java.io.IOException;
14
import java.util.Collections;
15
import java.util.logging.Level;
16
import java.util.logging.Logger;
17
import org.apache.commons.httpclient.HttpClient;
18
import org.apache.commons.httpclient.methods.GetMethod;
19
import static org.apache.commons.lang3.StringUtils.isEmpty;
20

21
/**
22
 * Imports a dataset from a different system. This command validates that the PID
23
 * of the new dataset exists, and then inserts the new dataset into the database.
24
 * 
25
 * <b>NOTE:</b> At the moment, this command only supports a single version in the 
26
 * dataset, and was tested with package files only.
27
 * 
28
 * @author michael
29
 */
30
public class ImportDatasetCommand extends AbstractCreateDatasetCommand {
31
    
32
    private static final Logger logger = Logger.getLogger(ImportDatasetCommand.class.getName());
×
33
    
34
    /**
35
     * Creates a new instance of the command.
36
     * @param theDataset The dataset we want to import.
37
     * @param aRequest Request context for the command.
38
     */
39
    public ImportDatasetCommand(Dataset theDataset, DataverseRequest aRequest) {
40
        super(theDataset, aRequest);
×
41
    }
×
42

43
    /**
44
     * Validate that the PID of the dataset, if any, exists.
45
     * @param ctxt
46
     * @throws CommandException 
47
     */
48
    @Override
49
    protected void additionalParameterTests(CommandContext ctxt) throws CommandException {
50
        
51
        if ( ! getUser().isSuperuser() ) {
×
52
            throw new PermissionException("ImportDatasetCommand can only be issued by a super-user.", this, Collections.emptySet(), getDataset());
×
53
        }
54
        
55
        Dataset ds = getDataset();
×
56
        
57
        if ( isEmpty(ds.getIdentifier()) ) {
×
58
            throw new IllegalCommandException("Imported datasets must have a persistent global identifier.", this);
×
59
        }
60
        
61
        if ( ! ctxt.dvObjects().isGlobalIdLocallyUnique(ds.getGlobalId()) ) {
×
62
            throw new IllegalCommandException("Persistent identifier " + ds.getGlobalId().asString() + " already exists in this Dataverse installation.", this);
×
63
        }
64
        
65
        String pid = ds.getPersistentURL();
×
66
        GetMethod httpGet = new GetMethod(pid); 
×
67
        httpGet.setFollowRedirects(false);
×
68

69
        HttpClient client = new HttpClient();
×
70

71
        try {
72
            int responseStatus = client.executeMethod(httpGet);
×
73

74
            if (responseStatus == 404) {
×
75
                /*
76
                 * Using test DOIs from DataCite, we'll get a 404 when trying to resolve the DOI
77
                 * to a landing page, but the DOI may already exist. An extra check here allows
78
                 * use of DataCite test DOIs. It also changes import slightly in allowing PIDs
79
                 * that exist (and accessible in the PID provider account configured in
80
                 * Dataverse) but aren't findable to be used. That could be the case if, for
81
                 * example, someone was importing a draft dataset from elsewhere.
82
                 */
NEW
83
                PidProvider pidProvider = PidUtil.getPidProvider(ds.getGlobalId().getProviderId());
×
NEW
84
                if (pidProvider != null) {
×
NEW
85
                    if (pidProvider.alreadyRegistered(ds.getGlobalId(), true)) {
×
UNCOV
86
                        return;
×
87
                    }
88
                }
89
                throw new CommandExecutionException(
×
90
                        "Provided PID does not exist. Status code for GET '" + pid + "' is 404.", this);
91
            }
92

93
        } catch (Exception ex) {
×
94
            logger.log(Level.WARNING,
×
95
                    "Error while validating PID at '" + pid + "' for an imported dataset: " + ex.getMessage(), ex);
×
96
            throw new CommandExecutionException("Cannot validate PID due to an error: " + ex.getMessage(), this);
×
97
        }
×
98

99
    }
×
100
    
101
    @Override
102
    protected void handlePid(Dataset theDataset, CommandContext ctxt) {
103
        theDataset.setGlobalIdCreateTime( getTimestamp() );
×
104
    }
×
105

106
    @Override
107
    protected void postPersist(Dataset theDataset, CommandContext ctxt) throws CommandException {
108
    }
×
109
    
110
    
111
    
112
}
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