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

IQSS / dataverse / #22987

23 Aug 2024 06:44PM CUT coverage: 20.61% (-0.2%) from 20.791%
#22987

Pull #10781

github

landreev
added an upfront locks check to the /addGlobusFiles api #10623
Pull Request #10781: Improved handling of Globus uploads

4 of 417 new or added lines in 15 files covered. (0.96%)

4194 existing lines in 35 files now uncovered.

17388 of 84365 relevant lines covered (20.61%)

0.21 hits per line

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

68.42
/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/MoveDatasetCommand.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.engine.command.impl;
7

8
import edu.harvard.iq.dataverse.Dataset;
9
import edu.harvard.iq.dataverse.DatasetLinkingDataverse;
10
import edu.harvard.iq.dataverse.Dataverse;
11
import edu.harvard.iq.dataverse.Guestbook;
12
import edu.harvard.iq.dataverse.authorization.Permission;
13
import edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser;
14
import edu.harvard.iq.dataverse.engine.command.AbstractVoidCommand;
15
import edu.harvard.iq.dataverse.engine.command.CommandContext;
16
import edu.harvard.iq.dataverse.engine.command.DataverseRequest;
17
import edu.harvard.iq.dataverse.engine.command.RequiredPermissions;
18
import edu.harvard.iq.dataverse.engine.command.RequiredPermissionsMap;
19
import edu.harvard.iq.dataverse.engine.command.exception.CommandException;
20
import edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException;
21
import edu.harvard.iq.dataverse.engine.command.exception.PermissionException;
22
import edu.harvard.iq.dataverse.engine.command.exception.UnforcedCommandException;
23
import edu.harvard.iq.dataverse.util.BundleUtil;
24
import java.util.ArrayList;
25
import java.util.Arrays;
26
import java.util.Collections;
27
import java.util.List;
28
import java.util.logging.Level;
29
import java.util.logging.Logger;
30

31
/**
32
 * Moves Dataset from one dataverse to another
33
 *
34
 * @author skraffmi
35
 */
36
@RequiredPermissionsMap({
37
    @RequiredPermissions(dataverseName = "moved", value = {Permission.PublishDataset})
38
    ,        @RequiredPermissions(dataverseName = "destination", value = {Permission.AddDataset, Permission.PublishDataset})
39
})
40
public class MoveDatasetCommand extends AbstractVoidCommand {
41

42
    private static final Logger logger = Logger.getLogger(MoveDatasetCommand.class.getCanonicalName());
1✔
43
    // FIXME: "toMove" would be a better name than "moved".
44
    final Dataset moved;
45
    final Dataverse destination;
46
    final Boolean force;
47

48
    public MoveDatasetCommand(DataverseRequest aRequest, Dataset moved, Dataverse destination, Boolean force) {
49
        super(
1✔
50
                aRequest,
51
                dv("moved", moved),
1✔
52
                dv("destination", destination)
1✔
53
        );
54
        this.moved = moved;
1✔
55
        this.destination = destination;
1✔
56
        this.force= force;
1✔
57
    }
1✔
58

59
    @Override
60
    public void executeImpl(CommandContext ctxt) throws CommandException {
61
        boolean removeGuestbook = false, removeLinkDs = false;
1✔
62
        if (!(getUser() instanceof AuthenticatedUser)) {
1✔
63
            /**
64
             * This English wasn't moved to the bundle because it is impossible
65
             * to exercise it via both API and UI. See also the note in in the
66
             * PermissionException catch in AbstractApiBean.
67
             */
68
            throw new PermissionException("Move Dataset can only be called by authenticated users.", this, Collections.singleton(Permission.DeleteDatasetDraft), moved);
1✔
69
        }
70

71
        // validate the move makes sense
72
        if (moved.getOwner().equals(destination)) {
1✔
73
            throw new IllegalCommandException(BundleUtil.getStringFromBundle("dashboard.card.datamove.dataset.command.error.targetDataverseSameAsOriginalDataverse"), this);
1✔
74
        }
75
        
76
        // if dataset is published make sure that its target is published
77
        
78
        if (moved.isReleased() && !destination.isReleased()){
1✔
79
            throw new IllegalCommandException(BundleUtil.getStringFromBundle("dashboard.card.datamove.dataset.command.error.targetDataverseUnpublishedDatasetPublished", Arrays.asList(destination.getDisplayName())), this);
1✔
80
        }
81
                
82
        //if the datasets guestbook is not contained in the new dataverse then remove it
83
        if (moved.getGuestbook() != null) {
1✔
84
            Guestbook gb = moved.getGuestbook();
1✔
85
            List<Guestbook> gbs = destination.getGuestbooks();
1✔
86
            boolean inheritGuestbooksValue = !destination.isGuestbookRoot();
1✔
87
            if (inheritGuestbooksValue && destination.getOwner() != null) {
1✔
88
                for (Guestbook pg : destination.getParentGuestbooks()) {
1✔
89
                    gbs.add(pg);
1✔
90
                }
1✔
91
            }
92
            if (gbs == null || !gbs.contains(gb)) {
1✔
93
                if (force == null  || !force){
1✔
UNCOV
94
                    removeGuestbook = true;
×
95
                } else {
96
                    moved.setGuestbook(null);
1✔
97
                }
98
            }
99
        }
100
        
101
        // generate list of all possible parent dataverses to check against
102
        List<Dataverse> ownersToCheck = new ArrayList<>();
1✔
103
        ownersToCheck.add(destination);
1✔
104
        if (destination.getOwners() != null) {
1✔
105
            ownersToCheck.addAll(destination.getOwners());
1✔
106
        }
107
        
108
        // if the dataset is linked to the new dataverse or any of 
109
        // its parent dataverses then remove the link
110
        List<DatasetLinkingDataverse> linkingDatasets = new ArrayList<>();
1✔
111
        if (moved.getDatasetLinkingDataverses() != null) {
1✔
UNCOV
112
            linkingDatasets.addAll(moved.getDatasetLinkingDataverses());
×
113
        }
114
        for (DatasetLinkingDataverse dsld : linkingDatasets) {
1✔
UNCOV
115
            for (Dataverse owner : ownersToCheck){
×
UNCOV
116
                if ((dsld.getLinkingDataverse()).equals(owner)){
×
117
                    if (force == null || !force) {
×
118
                        removeLinkDs = true;
×
119
                        break;
×
120
                    }
121
                    boolean index = false;
×
UNCOV
122
                    ctxt.engine().submit(new DeleteDatasetLinkingDataverseCommand(getRequest(), dsld.getDataset(), dsld, index));
×
123
                    moved.getDatasetLinkingDataverses().remove(dsld);
×
124
                }
125
            }
×
UNCOV
126
        }
×
127
        
128
        if (removeGuestbook || removeLinkDs) {
1✔
UNCOV
129
            StringBuilder errorString = new StringBuilder();
×
UNCOV
130
            if (removeGuestbook) {
×
131
                errorString.append(BundleUtil.getStringFromBundle("dashboard.card.datamove.dataset.command.error.unforced.datasetGuestbookNotInTargetDataverse"));
×
132
            }
133
            if (removeLinkDs) {
×
UNCOV
134
                errorString.append(BundleUtil.getStringFromBundle("dashboard.card.datamove.dataset.command.error.unforced.linkedToTargetDataverseOrOneOfItsParents"));
×
135
            }
136
            throw new UnforcedCommandException(errorString.toString(), this);
×
137
        }
138

139

140
        // OK, move
141
        moved.setOwner(destination);
1✔
142
        ctxt.em().merge(moved);
1✔
143

144
        boolean doNormalSolrDocCleanUp = true;
1✔
145
        ctxt.index().asyncIndexDataset(moved, doNormalSolrDocCleanUp);
1✔
146

147
    }
1✔
148

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