Changes for checkstyle 8.32
[policy/apex-pdp.git] / model / model-api / src / main / java / org / onap / policy / apex / model / modelapi / ApexApiResult.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.modelapi;
22
23 import java.io.PrintWriter;
24 import java.io.StringWriter;
25 import java.util.ArrayList;
26 import java.util.List;
27 import javax.xml.bind.annotation.XmlAccessType;
28 import javax.xml.bind.annotation.XmlAccessorType;
29 import javax.xml.bind.annotation.XmlAttribute;
30 import javax.xml.bind.annotation.XmlElement;
31 import javax.xml.bind.annotation.XmlElementWrapper;
32 import javax.xml.bind.annotation.XmlEnum;
33 import javax.xml.bind.annotation.XmlRootElement;
34
35 /**
36  * The Class ApexEditorAPIResult return the result of and messages from all model API method calls on the
37  * {@link ApexModel} API.
38  */
39 @XmlRootElement
40 @XmlAccessorType(XmlAccessType.NONE)
41 public class ApexApiResult {
42
43     /**
44      * This enumeration is used to represent the result status of a call on the {@link ApexModel} API.
45      */
46     @XmlEnum(value = String.class)
47     public enum Result {
48         /** The method call succeeded. */
49         SUCCESS,
50         /** The method call succeeded and all operations are now completed. */
51         FINISHED,
52         /** The method call for a create operation failed because the concept already exists. */
53         CONCEPT_EXISTS,
54         /**
55          * The method call for a create operation failed because multiple concepts already exists.
56          */
57         MULTIPLE_CONCEPTS_EXIST,
58         /** The method call on a concept failed because the referenced concept does not exist. */
59         CONCEPT_DOES_NOT_EXIST,
60         /** The method call failed because no action was specified on the method call. */
61         NO_ACTION_SPECIFIED,
62         /**
63          * The method call failed because of a structural error, a missing reference, or other error on the model.
64          */
65         FAILED,
66         /**
67          * The method call failed for another reason such as the method call is not implemented yet on the concept on
68          * which it was called.
69          */
70         OTHER_ERROR;
71
72         /**
73          * Check if a result is OK.
74          *
75          * @param result the result
76          * @return true if the result is not OK
77          */
78         public static boolean isOk(final Result result) {
79             return result == Result.SUCCESS || result == Result.FINISHED;
80         }
81
82         /**
83          * Check if a result is not OK.
84          *
85          * @param result the result
86          * @return true if the result is not OK
87          */
88         public static boolean isNok(final Result result) {
89             return !isOk(result);
90         }
91     }
92
93     private Result result;
94     private List<String> messages = new ArrayList<>();
95
96     /**
97      * The Default Constructor creates a result for a successful operation with no messages.
98      */
99     public ApexApiResult() {
100         result = Result.SUCCESS;
101     }
102
103     /**
104      * This Constructor creates a result with the given result status with no messages.
105      *
106      * @param result the result status to use on this result
107      */
108     public ApexApiResult(final Result result) {
109         this.result = result;
110     }
111
112     /**
113      * This Constructor creates a result with the given result status and message.
114      *
115      * @param result the result status to use on this result
116      * @param message the message to return with the result
117      */
118     public ApexApiResult(final Result result, final String message) {
119         this.result = result;
120         addMessage(message);
121     }
122
123     /**
124      * This Constructor creates a result with the given result status and {@link Throwable} object such as an exception.
125      * The message and stack trace from the {@link Throwable} object are added to the message list of this message.
126      *
127      * @param result the result status to use on this result
128      * @param throwable the throwable object from which to add the message and stack trace
129      */
130     public ApexApiResult(final Result result, final Throwable throwable) {
131         this.result = result;
132         addThrowable(throwable);
133     }
134
135     /**
136      * This Constructor creates a result with the given result status, message, and {@link Throwable} object such as an
137      * exception. The message and stack trace from the {@link Throwable} object are added to the message list of this
138      * message.
139      *
140      * @param result the result status to use on this result
141      * @param message the message to return with the result
142      * @param throwable the throwable object from which to add the message and stack trace
143      */
144     public ApexApiResult(final Result result, final String message, final Throwable throwable) {
145         this.result = result;
146         addMessage(message);
147         addThrowable(throwable);
148     }
149
150     /**
151      * This message is a utility message that checks if the result of an operation on the API was OK.
152      *
153      * @return true, if the result indicates the API operation succeeded
154      */
155     @XmlAttribute(required = true)
156     public boolean isOk() {
157         return Result.isOk(result);
158     }
159
160     /**
161      * This message is a utility message that checks if the result of an operation on the API was not OK.
162      *
163      * @return true, if the result indicates the API operation did not succeed
164      */
165     public boolean isNok() {
166         return Result.isNok(result);
167     }
168
169     /**
170      * Gets the result status of an API operation.
171      *
172      * @return the result status
173      */
174     @XmlAttribute(required = true)
175     public Result getResult() {
176         return result;
177     }
178
179     /**
180      * Sets the result status of an API operation.
181      *
182      * @param result the result status
183      */
184     public void setResult(final Result result) {
185         this.result = result;
186     }
187
188     /**
189      * Gets the list of messages returned by an API operation.
190      *
191      * @return the list of messages returned by an API operation
192      */
193     @XmlElementWrapper(required = false, nillable = true)
194     @XmlElement(nillable = true, name = "message")
195     public List<String> getMessages() {
196         return messages;
197     }
198
199     /**
200      * Sets the list of messages to return as a result of an API operation.
201      *
202      * @param messages the list of messages to return as a result of an API operation
203      */
204     public void setMessages(final List<String> messages) {
205         this.messages = messages;
206     }
207
208     /**
209      * Gets all the messages returned by an API operation as a single string.
210      *
211      * @return the messages returned by an API operation as a single string
212      */
213     @XmlElement(required = true, name = "content")
214     public String getMessage() {
215         final StringBuilder builder = new StringBuilder();
216         for (final String message : messages) {
217             builder.append(message);
218             builder.append('\n');
219         }
220
221         return builder.toString();
222     }
223
224     /**
225      * Adds a message from an API operation to the bottom of the list of messages to be returned.
226      *
227      * @param message the message from an API operation to add to the bottom of the list of messages to be returned
228      */
229     public void addMessage(final String message) {
230         if (message != null && message.trim().length() > 0) {
231             messages.add(message);
232         }
233     }
234
235     /**
236      * Adds the message and stack trace from a {@link Throwable} object such as an exception from an API operation to
237      * the bottom of the list of messages to be returned.
238      *
239      * @param throwable the {@link Throwable} object such as an exception from an API operation from which the message
240      *        and stack trace are to be extracted and placed at the bottom of the list of messages to be returned
241      */
242     public void addThrowable(final Throwable throwable) {
243         final StringWriter throwableStringWriter = new StringWriter();
244         final PrintWriter throwablePrintWriter = new PrintWriter(throwableStringWriter);
245         throwable.printStackTrace(throwablePrintWriter);
246         messages.add(throwable.getMessage());
247         messages.add(throwableStringWriter.toString());
248     }
249
250     /**
251      * Gets a representation of the {@link ApexApiResult} instance as a JSON string.
252      *
253      * @return the result instance JSON string
254      */
255     public String toJson() {
256         final StringBuilder builder = new StringBuilder();
257         builder.append("{\n");
258
259         builder.append("\"result\": \"");
260         builder.append(result.toString());
261         builder.append("\",\n");
262
263         builder.append("\"messages\": [");
264         boolean first = true;
265         for (final String message : messages) {
266             if (first) {
267                 builder.append("\n\"");
268                 first = false;
269             } else {
270                 builder.append(",\n\"");
271             }
272             builder.append(message.replaceAll("\"", "\\\\\""));
273             builder.append("\"");
274         }
275         builder.append("]\n");
276
277         builder.append("}\n");
278
279         return builder.toString();
280     }
281
282     /**
283      * {@inheritDoc}.
284      */
285     @Override
286     public String toString() {
287         final StringBuilder builder = new StringBuilder();
288         builder.append("result: ");
289         builder.append(result);
290         builder.append('\n');
291         builder.append(getMessage());
292         return builder.toString();
293     }
294 }