Skip to content

Commit

Permalink
DD-1605 Implement several dd-dataverse-cli dataset commands
Browse files Browse the repository at this point in the history
  • Loading branch information
aliassheikh committed Aug 5, 2024
1 parent 2704328 commit 0504900
Show file tree
Hide file tree
Showing 10 changed files with 341 additions and 9 deletions.
14 changes: 12 additions & 2 deletions src/main/java/nl/knaw/dans/dvcli/DdDataverseCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@
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.DeleteDraft;
import nl.knaw.dans.dvcli.command.DatasetGetFiles;
import nl.knaw.dans.dvcli.command.DatasetDeleteDraft;
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.DatasetAssignRole;
import nl.knaw.dans.dvcli.config.DdDataverseCliConfig;
import nl.knaw.dans.lib.util.AbstractCommandLineApp;
import nl.knaw.dans.lib.util.PicocliVersionProvider;
Expand Down Expand Up @@ -72,7 +77,12 @@ public void configureCommandLine(CommandLine commandLine, DdDataverseCliConfig c
.addSubcommand(new CollectionSetMetadataBlocksRoot())
.addSubcommand(new CollectionView()))
.addSubcommand(new CommandLine(new DatasetCmd(dataverseClient))
.addSubcommand(new DeleteDraft())
.addSubcommand(new DatasetDeleteDraft())
.addSubcommand(new DatasetGetFiles())
.addSubcommand(new DatasetGetLatestVersion())
.addSubcommand(new DatasetGetVersion())
.addSubcommand(new DatasetPublish())
.addSubcommand(new DatasetAssignRole())
);
log.debug("Configuring command line");
}
Expand Down
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) {
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 @@ -17,8 +17,8 @@

import nl.knaw.dans.dvcli.action.ConsoleReport;
import nl.knaw.dans.lib.dataverse.DataverseException;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Parameters;
import picocli.CommandLine.ParentCommand;

import java.io.IOException;
Expand All @@ -30,7 +30,7 @@ public class CollectionSetMetadataBlocksRoot extends AbstractCmd {
@ParentCommand
private CollectionCmd collectionCmd;

@CommandLine.Parameters(index = "0", paramLabel = "isRoot", type = Boolean.class, description = "Whether to make it a metadata blocks root.")
@Parameters(index = "0", paramLabel = "isRoot", type = Boolean.class, description = "Whether to make it a metadata blocks root.")
private Boolean isRoot;

@Override
Expand Down
123 changes: 123 additions & 0 deletions src/main/java/nl/knaw/dans/dvcli/command/DatasetAssignRole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* 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.ConsoleReport;
import nl.knaw.dans.dvcli.action.Pair;
import nl.knaw.dans.dvcli.action.ThrowingFunction;
import nl.knaw.dans.lib.dataverse.DatasetApi;
import nl.knaw.dans.lib.dataverse.DataverseException;
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 picocli.CommandLine.ParentCommand;

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

@Command(name = "role-assignment",
mixinStandardHelpOptions = true,
description = "Manage role assignments on one or more datasets.")
public class DatasetAssignRole extends AbstractCmd {
@ParentCommand
private DatasetCmd datasetCmd;

static class SingleAssignment {
@Parameters(index = "0", paramLabel = "role", description = "The role to assign")
String role;

@Parameters(index = "1", paramLabel = "assigsment", description = "The identifier of the user to assign the role to")
String assigsment;
}

static class AllArgs {
@ArgGroup(exclusive = false)
SingleAssignment singleAssignment;

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

@ArgGroup(exclusive = true, multiplicity = "1")
private AllArgs allArgs;

private record RoleAssignmentParams(DatasetApi collection, String role, String assignee) {
}

private static class RoleAssignmentAction implements ThrowingFunction<RoleAssignmentParams, String, Exception> {
@Override
public String apply(RoleAssignmentParams roleAssignmentParams) throws IOException, DataverseException {
var assignment = new RoleAssignment();
assignment.setAssignee(roleAssignmentParams.assignee());
assignment.setRole(roleAssignmentParams.role());
var r = roleAssignmentParams.collection.assignRole(assignment);
return r.getEnvelopeAsString();
}
}

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

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

for (CSVRecord csvRecord : csvParser) {
var pid = csvRecord.get("PID");
DatasetApi collection = datasetCmd.dataverseClient.dataset(pid);
String role = csvRecord.get("ROLE");
String assignee = csvRecord.get("ASSIGNEE");

RoleAssignmentParams params = new RoleAssignmentParams(collection, role, assignee);
result.add(new Pair<>(pid, params));
}

return result;
}
}

private List<Pair<String, RoleAssignmentParams>> getRoleAssignmentParams() throws IOException {
if (allArgs.paramsFile != null) {
return readFromFile(allArgs.paramsFile);
}
else {
return datasetCmd.getItems().stream()
.map(p -> new Pair<>(p.getFirst(), new RoleAssignmentParams(p.getSecond(), allArgs.singleAssignment.role, allArgs.singleAssignment.assigsment)))
.toList();
}
}

@Override
public void doCall() throws IOException, DataverseException {
datasetCmd.<RoleAssignmentParams> paramsBatchProcessorBuilder()
.labeledItems(getRoleAssignmentParams())
.action(new RoleAssignmentAction())
.report(new ConsoleReport<>())
.build()
.process();
}
}
3 changes: 2 additions & 1 deletion src/main/java/nl/knaw/dans/dvcli/command/DatasetCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package nl.knaw.dans.dvcli.command;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import nl.knaw.dans.dvcli.action.Pair;
import nl.knaw.dans.dvcli.action.SingleDatasetOrDatasetsFile;
Expand All @@ -31,7 +32,7 @@
description = "Manage Dataverse datasets")
@Slf4j
public class DatasetCmd extends AbstractSubcommandContainer<DatasetApi> {
public DatasetCmd(DataverseClient dataverseClient) {
public DatasetCmd(@NonNull DataverseClient dataverseClient) {
super(dataverseClient);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@Command(name = "delete-draft",
mixinStandardHelpOptions = true,
description = "Delete the draft version of a dataset.")
public class DeleteDraft extends AbstractCmd {
public class DatasetDeleteDraft extends AbstractCmd {
@ParentCommand
private DatasetCmd datasetCmd;

Expand Down
47 changes: 47 additions & 0 deletions src/main/java/nl/knaw/dans/dvcli/command/DatasetGetFiles.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.ConsoleReport;
import nl.knaw.dans.lib.dataverse.DataverseException;
import picocli.CommandLine.Command;
import picocli.CommandLine.Parameters;
import picocli.CommandLine.ParentCommand;

import java.io.IOException;

@Command(name = "get-files",
mixinStandardHelpOptions = true,
description = "Get a list of file metadata.")
public class DatasetGetFiles extends AbstractCmd {
@ParentCommand
private DatasetCmd datasetCmd;

@Parameters(index = "0", paramLabel = "version", description = "version to get file metadata from.")
private String version;

@Override
public void doCall() throws IOException, DataverseException {
datasetCmd.batchProcessorBuilder()
.action(d -> {
var r = d.getFiles(version);
return r.getEnvelopeAsString();
})
.report(new ConsoleReport<>())
.build()
.process();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.ConsoleReport;
import nl.knaw.dans.lib.dataverse.DataverseException;
import picocli.CommandLine.Command;
import picocli.CommandLine.ParentCommand;

import java.io.IOException;

@Command(name = "get-latest-version",
mixinStandardHelpOptions = true,
description = "A JSON object that starts at the dataset level, most fields are replicated at the dataset version level.")
public class DatasetGetLatestVersion extends AbstractCmd {
@ParentCommand
private DatasetCmd datasetCmd;

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

}
46 changes: 46 additions & 0 deletions src/main/java/nl/knaw/dans/dvcli/command/DatasetGetVersion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.ConsoleReport;
import nl.knaw.dans.lib.dataverse.DataverseException;
import picocli.CommandLine.Command;
import picocli.CommandLine.Parameters;
import picocli.CommandLine.ParentCommand;

import java.io.IOException;

@Command(name = "get-version",
mixinStandardHelpOptions = true,
description = "Returns an object containing the dataset version metadata.")
public class DatasetGetVersion extends AbstractCmd {
@ParentCommand
private DatasetCmd datasetCmd;
@Parameters(index = "0", paramLabel = "version", description = "version to retrieve.")
String version;

@Override
public void doCall() throws IOException, DataverseException {
datasetCmd.batchProcessorBuilder()
.action(d -> {
var r = d.getVersion(version);
return r.getEnvelopeAsString();
})
.report(new ConsoleReport<>())
.build()
.process();
}
}
Loading

0 comments on commit 0504900

Please sign in to comment.