Reduced code complexity
[cli.git] / framework / src / main / java / org / onap / cli / fw / output / OnapCommandResult.java
1 /*
2  * Copyright 2017 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.cli.fw.output;
18
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import org.onap.cli.fw.conf.OnapCommandConstants;
24 import org.onap.cli.fw.error.OnapCommandException;
25 import org.onap.cli.fw.error.OnapCommandOutputFormatNotsupported;
26 import org.onap.cli.fw.error.OnapCommandOutputPrintingFailed;
27 import org.onap.cli.fw.output.print.OnapCommandPrint;
28
29 /**
30  * Oclip Command result holds the final output of the command.
31  *
32  */
33 public class OnapCommandResult {
34
35     /*
36      * if type=JSON, then JSON response of the command from back-end Oclip service, by default all the command would
37      * set this value once the back-end call returns, which would be useful to print the output in JSON format, returned
38      * from the back-end service.
39      *
40      * if type=TEXT, then it holds the result in text format such as help message
41      */
42     private Object output = "";
43
44     /*
45      * Type requested by user
46      */
47     private OnapCommandResultType type = OnapCommandResultType.TABLE;
48
49     /*
50      * Scope requested by user
51      */
52     private OnapCommandResultAttributeScope scope = OnapCommandResultAttributeScope.SHORT;
53
54     /*
55      * if type=TABLE, then List of result records, which could be printed on the CLI console, loaded from schema file
56      */
57     private List<OnapCommandResultAttribute> records = new ArrayList<>();
58
59     /*
60      * Print horizontally or vertically, Mostly for show command, horizontal table while for list commands , it will be
61      * vertically printed. Respective command should set appropriately.
62      *
63      * loaded from schema file
64      */
65     private OnapCommandPrintDirection printDirection = OnapCommandPrintDirection.LANDSCAPE;
66
67     private String debugInfo = "";
68
69     /**
70      * Requested by user.
71      */
72     private boolean includeTitle = true;
73
74     /**
75      * Requested by user.
76      */
77     private boolean includeSeparator = true;
78
79     /**
80      * Requested by user.
81      */
82     private boolean isDebug = false;
83
84     /**
85      * Command passed/failed
86      * @return
87      */
88
89     private boolean passed = true;
90
91     public OnapCommandPrintDirection getPrintDirection() {
92         return printDirection;
93     }
94
95     public void setPrintDirection(OnapCommandPrintDirection printDirection) {
96         this.printDirection = printDirection;
97     }
98
99     public Object getOutput() {
100         return output;
101     }
102
103     public void setOutput(Object output) {
104         this.output = output;
105     }
106
107     public List<OnapCommandResultAttribute> getRecords() {
108         return records;
109     }
110
111     public void setRecords(List<OnapCommandResultAttribute> records) {
112         this.records = records;
113     }
114
115     /**
116      * Record mapping.
117      *
118      * @return attributes
119      */
120     public Map<String, OnapCommandResultAttribute> getRecordsMap() {
121         Map<String, OnapCommandResultAttribute> recordMap = new HashMap<>();
122
123         for (OnapCommandResultAttribute record : this.getRecords()) {
124             recordMap.put(record.getName(), record);
125         }
126
127         return recordMap;
128     }
129
130     public OnapCommandResultType getType() {
131         return type;
132     }
133
134     public void setType(OnapCommandResultType type) {
135         this.type = type;
136     }
137
138     public OnapCommandResultAttributeScope getScope() {
139         return scope;
140     }
141
142     public void setScope(OnapCommandResultAttributeScope scope) {
143         this.scope = scope;
144     }
145
146     public boolean isIncludeTitle() {
147         return includeTitle;
148     }
149
150     public void setIncludeTitle(boolean includeTitle) {
151         this.includeTitle = includeTitle;
152     }
153
154     public boolean isIncludeSeparator() {
155         return includeSeparator;
156     }
157
158     public void setIncludeSeparator(boolean includeSeparator) {
159         this.includeSeparator = includeSeparator;
160     }
161
162     public String getDebugInfo() {
163         return debugInfo;
164     }
165
166     public void setDebugInfo(String debugInfo) {
167         this.debugInfo = debugInfo;
168     }
169
170     public boolean isDebug() {
171         return isDebug;
172     }
173
174     public void setDebug(boolean isDebug) {
175         this.isDebug = isDebug;
176     }
177
178     public int getNumberOfRows() {
179         int noOfRecords = 0;
180
181         for (OnapCommandResultAttribute cols : this.records) {
182             if (cols != null && noOfRecords < cols.getValues().size()) {
183                 noOfRecords = cols.getValues().size();
184             }
185         }
186
187         return noOfRecords;
188     }
189     /**
190      * Helps to print the result based on the type.
191      *
192      * @return string
193      * @throws OnapCommandOutputFormatNotsupported
194      *             excpetion
195      * @throws OnapCommandOutputPrintingFailed
196      *             exception
197      */
198     public String print() throws OnapCommandException {
199         if (this.getType().equals(OnapCommandResultType.TEXT)) {
200              return this.getOutput().toString();
201         }
202
203         OnapCommandPrint print = new OnapCommandPrint();
204         print.setPrintTitle(this.isIncludeTitle());
205         print.setDirection(this.printDirection);
206
207         if (!this.getRecords().isEmpty()) {
208             if (this.getPrintDirection().equals(OnapCommandPrintDirection.LANDSCAPE)) {
209                 for (OnapCommandResultAttribute record : this.getScopedRecords()) {
210                     print.addColumn(record.getName(), record.getValues());
211                 }
212             } else {
213                 // Add property column
214                 OnapCommandResultAttribute prp = new OnapCommandResultAttribute();
215                 prp.setName(OnapCommandConstants.PORTRAINT_COLUMN_NAME_PROPERTY);
216                 prp.setScope(OnapCommandResultAttributeScope.SHORT);
217                 // Add value column
218                 OnapCommandResultAttribute val = new OnapCommandResultAttribute();
219                 val.setName(OnapCommandConstants.PORTRAINT_COLUMN_NAME_VALUE);
220                 val.setScope(OnapCommandResultAttributeScope.SHORT);
221
222                 for (OnapCommandResultAttribute record : this.getScopedRecords()) {
223                     prp.getValues().add(record.getName());
224                     if (record.getValues().size() == 1) {
225                         val.getValues().add(record.getValues().get(0));
226                     } else {
227                         val.getValues().add(record.getValues().toString());
228                     }
229                 }
230
231                 print.addColumn(prp.getName(), prp.getValues());
232                 print.addColumn(val.getName(), val.getValues());
233             }
234         }
235
236         if (this.getType().equals(OnapCommandResultType.JSON)) {
237             return print.printJson();
238         } else if (this.getType().equals(OnapCommandResultType.TABLE)) {
239             return print.printTable(this.isIncludeSeparator());
240         } else if (this.getType().equals(OnapCommandResultType.CSV)) {
241             return print.printCsv();
242         } else if (this.getType().equals(OnapCommandResultType.YAML)) {
243             return print.printYaml();
244         }
245
246         throw new OnapCommandOutputFormatNotsupported(this.getType().name());
247     }
248
249     private List<OnapCommandResultAttribute> getScopedRecords() {
250         List<OnapCommandResultAttribute> recordList = new ArrayList<>();
251         for (OnapCommandResultAttribute record : this.getRecords()) {
252             if (record.getScope().ordinal() > this.getScope().ordinal()) {
253                 continue;
254             }
255             recordList.add(record);
256         }
257
258         return recordList;
259     }
260
261     public boolean isPassed() {
262         return passed;
263     }
264
265     public void setPassed(boolean passed) {
266         this.passed = passed;
267     }
268 }