From: Kanagaraj Manickam Date: Thu, 31 Mar 2022 04:09:27 +0000 (+0000) Subject: Merge "Reduced code complexity" X-Git-Url: https://gerrit.onap.org/r/gitweb?a=commitdiff_plain;h=b2aeb4fae1c300d0073b8291e7a5f11bc53d3043;hp=98799b7c586dee5236d885078f6b0a9990a80713;p=cli.git Merge "Reduced code complexity" --- diff --git a/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResult.java b/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResult.java index b88477c0..fa9bcf02 100644 --- a/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResult.java +++ b/framework/src/main/java/org/onap/cli/fw/output/OnapCommandResult.java @@ -186,20 +186,8 @@ public class OnapCommandResult { return noOfRecords; } - /** - * Helps to print the result based on the type. - * - * @return string - * @throws OnapCommandOutputFormatNotsupported - * excpetion - * @throws OnapCommandOutputPrintingFailed - * exception - */ - public String print() throws OnapCommandException { - if (this.getType().equals(OnapCommandResultType.TEXT)) { - return this.getOutput().toString(); - } + public OnapCommandPrint createAndLoadPrint() { OnapCommandPrint print = new OnapCommandPrint(); print.setPrintTitle(this.isIncludeTitle()); print.setDirection(this.printDirection); @@ -232,6 +220,24 @@ public class OnapCommandResult { print.addColumn(val.getName(), val.getValues()); } } + return print; + } + + /** + * Helps to print the result based on the type. + * + * @return string + * @throws OnapCommandOutputFormatNotsupported + * excpetion + * @throws OnapCommandOutputPrintingFailed + * exception + */ + public String print() throws OnapCommandException { + if (this.getType().equals(OnapCommandResultType.TEXT)) { + return this.getOutput().toString(); + } + + OnapCommandPrint print = createAndLoadPrint(); if (this.getType().equals(OnapCommandResultType.JSON)) { return print.printJson(); diff --git a/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java b/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java index d2bc98be..14e37c3c 100644 --- a/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java +++ b/framework/src/main/java/org/onap/cli/fw/output/print/OnapCommandPrint.java @@ -98,6 +98,21 @@ public class OnapCommandPrint { return max; } + public List> addTitle(List> rows, boolean isNormalize){ + if (this.isPrintTitle()) { + List list = new ArrayList<>(); + for (String key : this.data.keySet()) { + if (isNormalize && key != null && key.length() > MAX_COLUMN_LENGTH) { + list.add(splitIntoList(key, MAX_COLUMN_LENGTH)); + } else { + list.add(key); + } + } + rows.add(list); + } + return rows; + } + /** * Helps to form the rows from columns. * @@ -112,17 +127,7 @@ public class OnapCommandPrint { List> rows = new ArrayList<>(); // add title - if (this.isPrintTitle()) { - List list = new ArrayList<>(); - for (String key : this.data.keySet()) { - if (isNormalize && key != null && key.length() > MAX_COLUMN_LENGTH) { - list.add(splitIntoList(key, MAX_COLUMN_LENGTH)); - } else { - list.add(key); - } - } - rows.add(list); - } + rows = addTitle(rows, isNormalize); // form row for (int i = 0; i < this.findMaxRows(); i++) { @@ -166,7 +171,7 @@ public class OnapCommandPrint { } // new line is converted to space char if (inp.contains("\n")) { - inp = inp.replaceAll("\n", ""); + inp = inp.replace("\n", ""); } StringTokenizer tok = new StringTokenizer(inp, " "); @@ -243,15 +248,20 @@ public class OnapCommandPrint { } } + public JSONObject printPortrait(List> rows){ + JSONObject result = new JSONObject(); + for (int i=1; i> rows = this.formRows(false); if (this.direction.equals(OnapCommandPrintDirection.PORTRAIT)) { - JSONObject result = new JSONObject(); - for (int i=1; i longOptions = new ArrayList<>(); if (validate) { - OnapCommandUtils.validateTags(exceptionList, (Map) values, OnapCommandConfig.getCommaSeparatedList(TOP_LEVEL_PARAMS_LIST), + OnapCommandUtils.validateTags(exceptionList, values, OnapCommandConfig.getCommaSeparatedList(TOP_LEVEL_PARAMS_LIST), OnapCommandConfig.getCommaSeparatedList(TOP_LEVEL_MANDATORY_LIST), "root level"); } diff --git a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandArtifactStore.java b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandArtifactStore.java index 32ececfa..0d68db1c 100644 --- a/framework/src/main/java/org/onap/cli/fw/store/OnapCommandArtifactStore.java +++ b/framework/src/main/java/org/onap/cli/fw/store/OnapCommandArtifactStore.java @@ -263,6 +263,37 @@ public class OnapCommandArtifactStore { } } + public Artifact setArtifact(Artifact artifact, Artifact existing) throws OnapCommandArtifactNotFound, OnapCommandArtifactContentNotExist, OnapCommandArtifactAlreadyExist, IOException, NoSuchAlgorithmException { + if (artifact.getName() == null) { + artifact.setName(existing.getName()); + } + + if (artifact.getDescription() == null) { + artifact.setDescription(existing.getDescription()); + } + + if (artifact.getCategoty() == null) { + artifact.setCategoty(existing.getCategoty()); + } + + if (artifact.getPath()!= null) { + if (!new File(artifact.getPath()).exists()) { + throw new OnapCommandArtifactContentNotExist(artifact.getPath()); + } + String actual = this.getChecksum(artifact.getPath()); + if (!existing.getChecksum().equals(actual)) { + artifact.setChecksum(actual); + artifact.setSize(new File(artifact.getPath()).length() / 1024); + } + } else { + artifact.setPath(existing.getPath()); + } + + artifact.setCreateAt(existing.getCreateAt()); + artifact.setLastUpdatedAt(dateFormatter.format(new Date())); + return artifact; + } + public Artifact updateArtifact(String name, String category, Artifact artifact) throws OnapCommandArtifactNotFound, OnapCommandArtifactContentNotExist, OnapCommandArtifactAlreadyExist { Artifact existing = this.getArtifact(name, category); String existingStorePath = getArtifactPath(name, category); @@ -273,33 +304,7 @@ public class OnapCommandArtifactStore { } try { - if (artifact.getName() == null) { - artifact.setName(existing.getName()); - } - - if (artifact.getDescription() == null) { - artifact.setDescription(existing.getDescription()); - } - - if (artifact.getCategoty() == null) { - artifact.setCategoty(existing.getCategoty()); - } - - if (artifact.getPath()!= null) { - if (!new File(artifact.getPath()).exists()) { - throw new OnapCommandArtifactContentNotExist(artifact.getPath()); - } - String actual = this.getChecksum(artifact.getPath()); - if (!existing.getChecksum().equals(actual)) { - artifact.setChecksum(actual); - artifact.setSize(new File(artifact.getPath()).length() / 1024); - } - } else { - artifact.setPath(existing.getPath()); - } - - artifact.setCreateAt(existing.getCreateAt()); - artifact.setLastUpdatedAt(dateFormatter.format(new Date())); + artifact = setArtifact(artifact, existing); if (artifact.getMetadata().size() > 0) { //update to existing one for (Map.Entry entry: artifact.getMetadata().entrySet()) { diff --git a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java index dab6669c..acf44a9b 100644 --- a/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java +++ b/framework/src/main/java/org/onap/cli/fw/utils/OnapCommandUtils.java @@ -22,6 +22,7 @@ import static org.onap.cli.fw.conf.OnapCommandConstants.IS_INCLUDE; import java.io.File; import java.io.IOException; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -270,7 +271,7 @@ public class OnapCommandUtils { //start to read after file:filepath String fileName = splEntry.substring(5); try { - value = FileUtils.readFileToString(new File(fileName)); + value = FileUtils.readFileToString((new File(fileName)), (Charset) null); } catch (IOException e) { //when file is not found, assign the same file:FILE_PATH //so that it will given hit to user that FILE_PATH to be diff --git a/main/src/main/java/org/onap/cli/main/OnapCli.java b/main/src/main/java/org/onap/cli/main/OnapCli.java index ee9c7b33..08ef640f 100644 --- a/main/src/main/java/org/onap/cli/main/OnapCli.java +++ b/main/src/main/java/org/onap/cli/main/OnapCli.java @@ -17,6 +17,7 @@ package org.onap.cli.main; import java.io.IOException; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -189,7 +190,7 @@ public class OnapCli { public void handleHelp() { try { if (this.printHelp) { - this.print(IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("oclip-readme.txt"))); + this.print(IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream("oclip-readme.txt") , (Charset) null)); String help = OnapCommandRegistrar.getRegistrar().getHelp(); this.print(help); this.exitSuccessfully(); diff --git a/main/src/main/java/org/onap/cli/main/utils/OnapCliArgsParser.java b/main/src/main/java/org/onap/cli/main/utils/OnapCliArgsParser.java index d0885def..53e90661 100644 --- a/main/src/main/java/org/onap/cli/main/utils/OnapCliArgsParser.java +++ b/main/src/main/java/org/onap/cli/main/utils/OnapCliArgsParser.java @@ -19,6 +19,7 @@ package org.onap.cli.main.utils; import java.io.File; import java.io.IOException; import java.net.URL; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -224,7 +225,7 @@ public class OnapCliArgsParser { try { File file = new File(input); if (file.isFile()) { - return FileUtils.readFileToString(file); + return FileUtils.readFileToString(file, (Charset) null); } else { return input; } @@ -238,7 +239,7 @@ public class OnapCliArgsParser { try { File file = new File(input); if (file.isFile()) { - String value = FileUtils.readFileToString(file); + String value = FileUtils.readFileToString(file, (Charset) null); YamlReader reader = new YamlReader(value); value = (String) reader.read(); return value;