-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DD-1605 Implement several dd-dataverse-cli dataset commands
- Loading branch information
1 parent
2704328
commit 0504900
Showing
10 changed files
with
341 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
src/main/java/nl/knaw/dans/dvcli/command/DatasetAssignRole.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/nl/knaw/dans/dvcli/command/DatasetGetFiles.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/nl/knaw/dans/dvcli/command/DatasetGetLatestVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
46
src/main/java/nl/knaw/dans/dvcli/command/DatasetGetVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.