Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DD-1605 Implement several dd-dataverse-cli dataset commands #4

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<dependency>
<groupId>nl.knaw.dans</groupId>
<artifactId>dans-dataverse-client-lib</artifactId>
<version>0.33.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/nl/knaw/dans/dvcli/DdDataverseCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import lombok.extern.slf4j.Slf4j;
import nl.knaw.dans.dvcli.action.Database;
import nl.knaw.dans.dvcli.command.CollectionAssignRole;
import nl.knaw.dans.dvcli.command.CollectionCmd;
import nl.knaw.dans.dvcli.command.CollectionCreateDataset;
import nl.knaw.dans.dvcli.command.CollectionDelete;
Expand All @@ -27,14 +26,19 @@
import nl.knaw.dans.dvcli.command.CollectionImportDataset;
import nl.knaw.dans.dvcli.command.CollectionIsMetadataBlocksRoot;
import nl.knaw.dans.dvcli.command.CollectionListMetadataBlocks;
import nl.knaw.dans.dvcli.command.CollectionListRoleAssignments;
import nl.knaw.dans.dvcli.command.CollectionListRoles;
import nl.knaw.dans.dvcli.command.CollectionPublish;
import nl.knaw.dans.dvcli.command.CollectionRoleAssignment;
import nl.knaw.dans.dvcli.command.CollectionSetMetadataBlocksRoot;
import nl.knaw.dans.dvcli.command.CollectionView;
import nl.knaw.dans.dvcli.command.DatasetCmd;
import nl.knaw.dans.dvcli.command.DatasetDeleteDraft;
import nl.knaw.dans.dvcli.command.DatasetGetFiles;
import nl.knaw.dans.dvcli.command.DatasetGetLatestVersion;
import nl.knaw.dans.dvcli.command.DatasetGetVersion;
import nl.knaw.dans.dvcli.command.DatasetPublish;
import nl.knaw.dans.dvcli.command.DatasetRoleAssignment;
import nl.knaw.dans.dvcli.command.DatasetValidateFiles;
import nl.knaw.dans.dvcli.command.DeleteDraft;
import nl.knaw.dans.dvcli.command.NotificationTruncate;
import nl.knaw.dans.dvcli.config.DdDataverseCliConfig;
import nl.knaw.dans.lib.dataverse.DataverseClient;
Expand Down Expand Up @@ -63,24 +67,28 @@
var dataverseClient = config.getApi().build();
var databaseConfig = config.getDb();
var database = new Database(databaseConfig);

commandLine.addSubcommand(new CommandLine(new CollectionCmd(dataverseClient))
.addSubcommand(new CollectionAssignRole())
.addSubcommand(new CollectionCreateDataset())
.addSubcommand(new CollectionDelete())
.addSubcommand(new CollectionGetContents())
.addSubcommand(new CollectionGetStorageSize())
.addSubcommand(new CollectionImportDataset())
.addSubcommand(new CollectionIsMetadataBlocksRoot())
.addSubcommand(new CollectionListMetadataBlocks())
.addSubcommand(new CollectionListRoleAssignments())
.addSubcommand(new CollectionListRoles())
.addSubcommand(new CollectionPublish())
.addSubcommand(new CollectionRoleAssignment())

Check warning on line 81 in src/main/java/nl/knaw/dans/dvcli/DdDataverseCli.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/DdDataverseCli.java#L81

Added line #L81 was not covered by tests
.addSubcommand(new CollectionSetMetadataBlocksRoot())
.addSubcommand(new CollectionView()))
.addSubcommand(new CommandLine(new DatasetCmd(dataverseClient))
.addSubcommand(new DatasetDeleteDraft())
.addSubcommand(new DatasetGetFiles())
.addSubcommand(new DatasetGetLatestVersion())
.addSubcommand(new DatasetGetVersion())
.addSubcommand(new DatasetPublish())
.addSubcommand(new DatasetRoleAssignment())

Check warning on line 90 in src/main/java/nl/knaw/dans/dvcli/DdDataverseCli.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/DdDataverseCli.java#L85-L90

Added lines #L85 - L90 were not covered by tests
.addSubcommand(new DatasetValidateFiles())
.addSubcommand(new DeleteDraft())
)
.addSubcommand(new CommandLine(new NotificationTruncate(database)));
log.debug("Configuring command line");
Expand Down
107 changes: 107 additions & 0 deletions src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright (C) 2024 DANS - Data Archiving and Networked Services ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.knaw.dans.dvcli.command;

import nl.knaw.dans.dvcli.action.Pair;
import nl.knaw.dans.lib.dataverse.model.RoleAssignment;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import picocli.CommandLine.ArgGroup;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Command
public abstract class AbstractAssignmentRole<CMD extends AbstractSubcommandContainer<?>, API> extends AbstractCmd {

Check warning on line 37 in src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java#L37

Added line #L37 was not covered by tests

static class CommandParameter {
@Parameters(description = "Alias and role assignee (example: @dataverseAdmin=contributor)")

Check warning on line 40 in src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java#L39-L40

Added lines #L39 - L40 were not covered by tests
String assignment = "";

@Option(names = { "-f",
"--parameter-file" }, description = "CSV file to read parameters from. The file should have a header row with columns 'PID', 'ASSIGNEE' and 'ROLE'.")
Path parameterFile;
}

@ArgGroup(multiplicity = "1")
CommandParameter commandParameter;

private Optional<RoleAssignment> readFromCommandLine() {
if (!this.commandParameter.assignment.isEmpty() && this.commandParameter.assignment.contains("=")) {
String[] assigneeRole = this.commandParameter.assignment.split("=");

Check warning on line 53 in src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java#L53

Added line #L53 was not covered by tests

RoleAssignment roleAssignment = new RoleAssignment();
roleAssignment.setAssignee(assigneeRole[0]);
roleAssignment.setRole(assigneeRole[1]);
return Optional.of(roleAssignment);

Check warning on line 58 in src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java#L55-L58

Added lines #L55 - L58 were not covered by tests
}
return Optional.empty();

Check warning on line 60 in src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java#L60

Added line #L60 was not covered by tests
}

private List<Pair<String, RoleAssignmentParams<API>>> readFromFile(CMD cmd) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(commandParameter.parameterFile);
CSVParser csvParser = new CSVParser(reader, CSVFormat.Builder.create(CSVFormat.DEFAULT)
.setHeader("PID", "ASSIGNEE", "ROLE")
.setSkipHeaderRecord(true)
.build())) {

Check warning on line 68 in src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java#L64-L68

Added lines #L64 - L68 were not covered by tests

List<Pair<String, RoleAssignmentParams<API>>> result = new ArrayList<>();

Check warning on line 70 in src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java#L70

Added line #L70 was not covered by tests

for (CSVRecord csvRecord : csvParser) {
var pid = csvRecord.get("PID");
API api = getItem(pid);
RoleAssignment roleAssignment = new RoleAssignment();
roleAssignment.setAssignee(csvRecord.get("ASSIGNEE"));
roleAssignment.setRole(csvRecord.get("ROLE"));

Check warning on line 77 in src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java#L73-L77

Added lines #L73 - L77 were not covered by tests

RoleAssignmentParams<API> params = new RoleAssignmentParams<>(api, Optional.of(roleAssignment));
result.add(new Pair<>(pid, params));
}

Check warning on line 81 in src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java#L79-L81

Added lines #L79 - L81 were not covered by tests

return result;

Check warning on line 83 in src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java#L83

Added line #L83 was not covered by tests
}
}

protected abstract API getItem(String pid);

protected List<Pair<String, RoleAssignmentParams<API>>> getRoleAssignmentParams(CMD cmd) throws IOException {
if (commandParameter.parameterFile != null) {
return readFromFile(cmd);

Check warning on line 91 in src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java#L91

Added line #L91 was not covered by tests
}
else if (commandParameter.assignment != null) {
var items = cmd.getItems();
return items.stream()
.map(p -> {
var second = new RoleAssignmentParams<API>((API) p.getSecond(), readFromCommandLine());
return new Pair<>(p.getFirst(), second);

Check warning on line 98 in src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java#L94-L98

Added lines #L94 - L98 were not covered by tests
})
.toList();

Check warning on line 100 in src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java#L100

Added line #L100 was not covered by tests
}
return List.of();

Check warning on line 102 in src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java#L102

Added line #L102 was not covered by tests
}

protected record RoleAssignmentParams<A>(A pid, Optional<RoleAssignment> roleAssignment) {

Check warning on line 105 in src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/AbstractAssignmentRole.java#L105

Added line #L105 was not covered by tests
}
}
123 changes: 0 additions & 123 deletions src/main/java/nl/knaw/dans/dvcli/command/CollectionAssignRole.java

This file was deleted.

7 changes: 4 additions & 3 deletions src/main/java/nl/knaw/dans/dvcli/command/CollectionCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
mixinStandardHelpOptions = true,
description = "Manage Dataverse collections (i.e. 'dataverses')")
public class CollectionCmd extends AbstractSubcommandContainer<DataverseApi> {
public CollectionCmd(@NonNull DataverseClient dataverseClient) {
PaulBoon marked this conversation as resolved.
Show resolved Hide resolved
super(dataverseClient);
}

@Override
protected List<Pair<String, DataverseApi>> getItems() throws IOException {
return new SingleCollectionOrCollectionsFile(targets, dataverseClient).getCollections().toList();
}

public CollectionCmd(@NonNull DataverseClient dataverseClient) {
super(dataverseClient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package nl.knaw.dans.dvcli.command;

import nl.knaw.dans.dvcli.action.ConsoleReport;
import nl.knaw.dans.lib.dataverse.DataverseException;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
Expand Down Expand Up @@ -43,14 +42,7 @@ public class CollectionCreateDataset extends AbstractCmd {

@Override
public void doCall() throws IOException, DataverseException {
collectionCmd.batchProcessorBuilder()
.action(d -> {
var json = Files.readString(Path.of(dataset));
var r = d.createDataset(json, metadataKeys);
return r.getEnvelopeAsString();
})
.report(new ConsoleReport<>())
.build()
.process();
collectionCmd.batchProcessor(c ->
c.createDataset(Files.readString(Path.of(dataset)), metadataKeys).getEnvelopeAsString()).process();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package nl.knaw.dans.dvcli.command;

import nl.knaw.dans.dvcli.action.ConsoleReport;
import nl.knaw.dans.lib.dataverse.DataverseException;
import picocli.CommandLine.Command;
import picocli.CommandLine.ParentCommand;
Expand All @@ -31,13 +30,6 @@

@Override
public void doCall() throws IOException, DataverseException {
collectionCmd.batchProcessorBuilder()
.action(d -> {
var r = d.delete();
return r.getEnvelopeAsString();
})
.report(new ConsoleReport<>())
.build()
.process();
collectionCmd.batchProcessor(c -> c.delete().getEnvelopeAsString()).process();

Check warning on line 33 in src/main/java/nl/knaw/dans/dvcli/command/CollectionDelete.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/CollectionDelete.java#L33

Added line #L33 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package nl.knaw.dans.dvcli.command;

import nl.knaw.dans.dvcli.action.ConsoleReport;
import nl.knaw.dans.lib.dataverse.DataverseException;
import picocli.CommandLine.Command;
import picocli.CommandLine.ParentCommand;
Expand All @@ -31,13 +30,6 @@

@Override
public void doCall() throws IOException, DataverseException {
collectionCmd.batchProcessorBuilder()
.action(d -> {
var r = d.getContents();
return r.getEnvelopeAsString();
})
.report(new ConsoleReport<>())
.build()
.process();
collectionCmd.batchProcessor(c -> c.getContents().getEnvelopeAsString()).process();

Check warning on line 33 in src/main/java/nl/knaw/dans/dvcli/command/CollectionGetContents.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/nl/knaw/dans/dvcli/command/CollectionGetContents.java#L33

Added line #L33 was not covered by tests
}
}
Loading