From 3778c02d1b80a2aca8b57e2bdf1b2a861b31749a Mon Sep 17 00:00:00 2001 From: thomas loubrieu Date: Tue, 6 Feb 2024 18:48:13 -0500 Subject: [PATCH] add list of status authorized when one was not authorized. --- .../mgr/cmd/data/SetArchiveStatusCmd.java | 201 +++++++++--------- 1 file changed, 96 insertions(+), 105 deletions(-) diff --git a/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/SetArchiveStatusCmd.java b/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/SetArchiveStatusCmd.java index 7e0f2e2..c8a2b24 100644 --- a/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/SetArchiveStatusCmd.java +++ b/src/main/java/gov/nasa/pds/registry/mgr/cmd/data/SetArchiveStatusCmd.java @@ -17,118 +17,109 @@ /** - * A CLI command to set PDS label archive status in Elasticsearch registry index. - * Status can be updated by LidVid or PackageId. + * A CLI command to set PDS label archive status in Elasticsearch registry index. Status can be + * updated by LidVid or PackageId. * * @author karpenko */ -public class SetArchiveStatusCmd implements CliCommand -{ - private Set statusNames; - - /** - * Constructor - */ - public SetArchiveStatusCmd() - { - statusNames = new TreeSet<>(); - statusNames.add("staged"); - statusNames.add("archived"); - statusNames.add("certified"); - statusNames.add("restricted"); +public class SetArchiveStatusCmd implements CliCommand { + private Set statusNames; + + /** + * Constructor + */ + public SetArchiveStatusCmd() { + statusNames = new TreeSet<>(); + statusNames.add("staged"); + statusNames.add("archived"); + statusNames.add("certified"); + statusNames.add("restricted"); + } + + + @Override + public void run(CommandLine cmdLine) throws Exception { + if (cmdLine.hasOption("help")) { + printHelp(); + return; + } + + String esUrl = cmdLine.getOptionValue("es", "http://localhost:9200"); + String indexName = cmdLine.getOptionValue("index", Constants.DEFAULT_REGISTRY_INDEX); + String authPath = cmdLine.getOptionValue("auth"); + + String status = getStatus(cmdLine); + + String lidvid = cmdLine.getOptionValue("lidvid"); + if (lidvid == null) + throw new Exception("Missing required parameter '-lidvid'"); + + RestClient client = null; + + try { + // Call Elasticsearch + client = EsClientFactory.createRestClient(esUrl, authPath); + ProductDao dao = new ProductDao(client, indexName); + ProductService srv = new ProductService(dao); + + srv.updateArchveStatus(lidvid, status); + } catch (ResponseException ex) { + throw new Exception(EsUtils.extractErrorMessage(ex)); + } finally { + CloseUtils.close(client); } - - - @Override - public void run(CommandLine cmdLine) throws Exception - { - if(cmdLine.hasOption("help")) - { - printHelp(); - return; - } - - String esUrl = cmdLine.getOptionValue("es", "http://localhost:9200"); - String indexName = cmdLine.getOptionValue("index", Constants.DEFAULT_REGISTRY_INDEX); - String authPath = cmdLine.getOptionValue("auth"); - - String status = getStatus(cmdLine); - - String lidvid = cmdLine.getOptionValue("lidvid"); - if(lidvid == null) throw new Exception("Missing required parameter '-lidvid'"); - - RestClient client = null; - - try - { - // Call Elasticsearch - client = EsClientFactory.createRestClient(esUrl, authPath); - ProductDao dao = new ProductDao(client, indexName); - ProductService srv = new ProductService(dao); - - srv.updateArchveStatus(lidvid, status); - } - catch(ResponseException ex) - { - throw new Exception(EsUtils.extractErrorMessage(ex)); - } - finally - { - CloseUtils.close(client); - } + } + + + /** + * Get value of "-status" command-line parameter. Throw exception if invalid status is passed. + * + * @param cmdLine + * @return valid status value + * @throws Exception Throw exception if invalid status is passed. + */ + private String getStatus(CommandLine cmdLine) throws Exception { + String tmp = cmdLine.getOptionValue("status"); + if (tmp == null) { + throw new Exception("Missing required parameter '-status'"); } - - /** - * Get value of "-status" command-line parameter. - * Throw exception if invalid status is passed. - * @param cmdLine - * @return valid status value - * @throws Exception Throw exception if invalid status is passed. - */ - private String getStatus(CommandLine cmdLine) throws Exception - { - String tmp = cmdLine.getOptionValue("status"); - if(tmp == null) - { - throw new Exception("Missing required parameter '-status'"); - } - - String status = tmp.toLowerCase(); - if(!statusNames.contains(status)) - { - throw new Exception("Invalid '-status' parameter value: '" + tmp + "'"); - } - - return status; + String status = tmp.toLowerCase(); + if (!statusNames.contains(status)) { + String authorized_status = String.join(", ", this.statusNames); + throw new Exception("Invalid '-status' parameter value: '" + tmp + "'. Authorized values are " + + authorized_status + "."); } - - - /** - * Print help screen - */ - public void printHelp() - { - System.out.println("Usage: registry-manager set-archive-status "); - - System.out.println(); - System.out.println("Set product archive status"); - System.out.println(); - System.out.println("Required parameters:"); - System.out.println(" -status One of the following values:"); - - for(String name: statusNames) - { - System.out.println(" " + name); - } - - System.out.println(" -lidvid Update archive status of a document with given LIDVID."); - System.out.println(" For a collection also update primary references from collection inventory."); - System.out.println("Optional parameters:"); - System.out.println(" -auth Authentication config file"); - System.out.println(" -es Elasticsearch URL. Default is http://localhost:9200"); - System.out.println(" -index Elasticsearch index name. Default is 'registry'"); - System.out.println(); + // Authorized values are " + String.join(", ", this.statusNames) + + return status; + } + + + /** + * Print help screen + */ + public void printHelp() { + System.out.println("Usage: registry-manager set-archive-status "); + + System.out.println(); + System.out.println("Set product archive status"); + System.out.println(); + System.out.println("Required parameters:"); + System.out.println(" -status One of the following values:"); + + for (String name : statusNames) { + System.out.println(" " + name); } + System.out.println(" -lidvid Update archive status of a document with given LIDVID."); + System.out.println( + " For a collection also update primary references from collection inventory."); + System.out.println("Optional parameters:"); + System.out.println(" -auth Authentication config file"); + System.out.println(" -es Elasticsearch URL. Default is http://localhost:9200"); + System.out.println(" -index Elasticsearch index name. Default is 'registry'"); + System.out.println(); + } + }