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

IQSS / dataverse / #23533

23 Oct 2024 01:18PM CUT coverage: 20.865%. First build
#23533

Pull #10958

github

GPortas
Changed: using new UpdateDataverseAttributeCommand from updateAttribute endpoint
Pull Request #10958: Fixes updateAttributes endpoint

0 of 42 new or added lines in 2 files covered. (0.0%)

17977 of 86159 relevant lines covered (20.86%)

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/UpdateDataverseAttributeCommand.java
1
package edu.harvard.iq.dataverse.engine.command.impl;
2

3
import edu.harvard.iq.dataverse.Dataverse;
4
import edu.harvard.iq.dataverse.authorization.Permission;
5
import edu.harvard.iq.dataverse.engine.command.AbstractCommand;
6
import edu.harvard.iq.dataverse.engine.command.CommandContext;
7
import edu.harvard.iq.dataverse.engine.command.DataverseRequest;
8
import edu.harvard.iq.dataverse.engine.command.RequiredPermissions;
9
import edu.harvard.iq.dataverse.engine.command.exception.CommandException;
10
import edu.harvard.iq.dataverse.engine.command.exception.IllegalCommandException;
11
import edu.harvard.iq.dataverse.engine.command.exception.PermissionException;
12
import edu.harvard.iq.dataverse.settings.SettingsServiceBean;
13

14
import java.util.Collections;
15

16
/**
17
 * Command to update an existing Dataverse attribute.
18
 */
19
@RequiredPermissions(Permission.EditDataverse)
20
public class UpdateDataverseAttributeCommand extends AbstractCommand<Dataverse> {
21

22
    private static final String ATTRIBUTE_ALIAS = "alias";
23
    private static final String ATTRIBUTE_NAME = "name";
24
    private static final String ATTRIBUTE_DESCRIPTION = "description";
25
    private static final String ATTRIBUTE_AFFILIATION = "affiliation";
26
    private static final String ATTRIBUTE_FILE_PIDS_ENABLED = "filePIDsEnabled";
27

28
    private final Dataverse dataverse;
29
    private final String attributeName;
30
    private final Object attributeValue;
31

32
    public UpdateDataverseAttributeCommand(DataverseRequest request, Dataverse dataverse, String attributeName, Object attributeValue) {
NEW
33
        super(request, dataverse);
×
NEW
34
        this.dataverse = dataverse;
×
NEW
35
        this.attributeName = attributeName;
×
NEW
36
        this.attributeValue = attributeValue;
×
NEW
37
    }
×
38

39
    @Override
40
    public Dataverse execute(CommandContext ctxt) throws CommandException {
NEW
41
        switch (attributeName) {
×
42
            case ATTRIBUTE_ALIAS:
43
            case ATTRIBUTE_NAME:
44
            case ATTRIBUTE_DESCRIPTION:
45
            case ATTRIBUTE_AFFILIATION:
NEW
46
                setStringAttribute(attributeName, attributeValue);
×
NEW
47
                break;
×
48
            case ATTRIBUTE_FILE_PIDS_ENABLED:
NEW
49
                setBooleanAttributeForFilePIDs(ctxt);
×
NEW
50
                break;
×
51
            default:
NEW
52
                throw new IllegalCommandException("'" + attributeName + "' is not a supported attribute", this);
×
53
        }
54

NEW
55
        return ctxt.engine().submit(new UpdateDataverseCommand(dataverse, null, null, getRequest(), null));
×
56
    }
57

58
    /**
59
     * Helper method to set a string attribute.
60
     *
61
     * @param attributeName  The name of the attribute.
62
     * @param attributeValue The value of the attribute (must be a String).
63
     * @throws IllegalCommandException if the provided attribute value is not of String type.
64
     */
65
    private void setStringAttribute(String attributeName, Object attributeValue) throws IllegalCommandException {
NEW
66
        if (!(attributeValue instanceof String stringValue)) {
×
NEW
67
            throw new IllegalCommandException("'" + attributeName + "' requires a string value", this);
×
68
        }
69

NEW
70
        switch (attributeName) {
×
71
            case ATTRIBUTE_ALIAS:
NEW
72
                dataverse.setAlias(stringValue);
×
NEW
73
                break;
×
74
            case ATTRIBUTE_NAME:
NEW
75
                dataverse.setName(stringValue);
×
NEW
76
                break;
×
77
            case ATTRIBUTE_DESCRIPTION:
NEW
78
                dataverse.setDescription(stringValue);
×
NEW
79
                break;
×
80
            case ATTRIBUTE_AFFILIATION:
NEW
81
                dataverse.setAffiliation(stringValue);
×
NEW
82
                break;
×
83
            default:
NEW
84
                throw new IllegalCommandException("Unsupported string attribute: " + attributeName, this);
×
85
        }
NEW
86
    }
×
87

88
    /**
89
     * Helper method to handle the "filePIDsEnabled" boolean attribute.
90
     *
91
     * @param ctxt The command context.
92
     * @throws PermissionException if the user doesn't have permission to modify this attribute.
93
     */
94
    private void setBooleanAttributeForFilePIDs(CommandContext ctxt) throws CommandException {
NEW
95
        if (!getRequest().getUser().isSuperuser()) {
×
NEW
96
            throw new PermissionException("You must be a superuser to change this setting",
×
NEW
97
                    this, Collections.singleton(Permission.EditDataset), dataverse);
×
98
        }
NEW
99
        if (!ctxt.settings().isTrueForKey(SettingsServiceBean.Key.AllowEnablingFilePIDsPerCollection, false)) {
×
NEW
100
            throw new PermissionException("Changing File PID policy per collection is not enabled on this server",
×
NEW
101
                    this, Collections.singleton(Permission.EditDataset), dataverse);
×
102
        }
103

NEW
104
        if (!(attributeValue instanceof Boolean)) {
×
NEW
105
            throw new IllegalCommandException("'" + ATTRIBUTE_FILE_PIDS_ENABLED + "' requires a boolean value", this);
×
106
        }
107

NEW
108
        dataverse.setFilePIDsEnabled((Boolean) attributeValue);
×
NEW
109
    }
×
110
}
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