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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.modelapi;
23 import java.io.PrintWriter;
24 import java.io.StringWriter;
25 import java.util.ArrayList;
26 import java.util.List;
28 import javax.xml.bind.annotation.XmlAccessType;
29 import javax.xml.bind.annotation.XmlAccessorType;
30 import javax.xml.bind.annotation.XmlAttribute;
31 import javax.xml.bind.annotation.XmlElement;
32 import javax.xml.bind.annotation.XmlElementWrapper;
33 import javax.xml.bind.annotation.XmlEnum;
34 import javax.xml.bind.annotation.XmlRootElement;
37 * The Class ApexEditorAPIResult return the result of and messages from all model API method calls on the
38 * {@link ApexModel} API.
41 @XmlAccessorType(XmlAccessType.NONE)
42 public class ApexApiResult {
45 * This enumeration is used to represent the result status of a call on the {@link ApexModel} API.
47 @XmlEnum(value = String.class)
49 /** The method call succeeded. */
51 /** The method call succeeded and all operations are now completed. */
53 /** The method call for a create operation failed because the concept already exists. */
56 * The method call for a create operation failed because multiple concepts already exists.
58 MULTIPLE_CONCEPTS_EXIST,
59 /** The method call on a concept failed because the referenced concept does not exist. */
60 CONCEPT_DOES_NOT_EXIST,
61 /** The method call failed because no action was specified on the method call. */
64 * The method call failed because of a structural error, a missing reference, or other error on the model.
68 * The method call failed for another reason such as the method call is not implemented yet on the concept on
69 * which it was called.
74 * Check if a result is OK.
76 * @param result the result
77 * @return true if the result is not OK
79 public static boolean isOk(final Result result) {
80 return result == Result.SUCCESS || result == Result.FINISHED;
84 * Check if a result is not OK.
86 * @param result the result
87 * @return true if the result is not OK
89 public static boolean isNok(final Result result) {
94 private Result result;
95 private List<String> messages = new ArrayList<>();
98 * The Default Constructor creates a result for a successful operation with no messages.
100 public ApexApiResult() {
101 result = Result.SUCCESS;
105 * This Constructor creates a result with the given result status with no messages.
107 * @param result the result status to use on this result
109 public ApexApiResult(final Result result) {
110 this.result = result;
114 * This Constructor creates a result with the given result status and message.
116 * @param result the result status to use on this result
117 * @param message the message to return with the result
119 public ApexApiResult(final Result result, final String message) {
120 this.result = result;
125 * This Constructor creates a result with the given result status and {@link Throwable} object such as an exception.
126 * The message and stack trace from the {@link Throwable} object are added to the message list of this message.
128 * @param result the result status to use on this result
129 * @param throwable the throwable object from which to add the message and stack trace
131 public ApexApiResult(final Result result, final Throwable throwable) {
132 this.result = result;
133 addThrowable(throwable);
137 * This Constructor creates a result with the given result status, message, and {@link Throwable} object such as an
138 * exception. The message and stack trace from the {@link Throwable} object are added to the message list of this
141 * @param result the result status to use on this result
142 * @param message the message to return with the result
143 * @param throwable the throwable object from which to add the message and stack trace
145 public ApexApiResult(final Result result, final String message, final Throwable throwable) {
146 this.result = result;
148 addThrowable(throwable);
152 * This message is a utility message that checks if the result of an operation on the API was OK.
154 * @return true, if the result indicates the API operation succeeded
156 @XmlAttribute(required = true)
157 public boolean isOk() {
158 return Result.isOk(result);
162 * This message is a utility message that checks if the result of an operation on the API was not OK.
164 * @return true, if the result indicates the API operation did not succeed
166 public boolean isNok() {
167 return Result.isNok(result);
171 * Gets the result status of an API operation.
173 * @return the result status
175 @XmlAttribute(required = true)
176 public Result getResult() {
181 * Sets the result status of an API operation.
183 * @param result the result status
185 public void setResult(final Result result) {
186 this.result = result;
190 * Gets the list of messages returned by an API operation.
192 * @return the list of messages returned by an API operation
194 @XmlElementWrapper(required = false, nillable = true)
195 @XmlElement(nillable = true, name = "message")
196 public List<String> getMessages() {
201 * Sets the list of messages to return as a result of an API operation.
203 * @param messages the list of messages to return as a result of an API operation
205 public void setMessages(final List<String> messages) {
206 this.messages = messages;
210 * Gets all the messages returned by an API operation as a single string.
212 * @return the messages returned by an API operation as a single string
214 @XmlElement(required = true, name = "content")
215 public String getMessage() {
216 final StringBuilder builder = new StringBuilder();
217 for (final String message : messages) {
218 builder.append(message);
219 builder.append('\n');
222 return builder.toString();
226 * Adds a message from an API operation to the bottom of the list of messages to be returned.
228 * @param message the message from an API operation to add to the bottom of the list of messages to be returned
230 public void addMessage(final String message) {
231 if (message != null && message.trim().length() > 0) {
232 messages.add(message);
237 * Adds the message and stack trace from a {@link Throwable} object such as an exception from an API operation to
238 * the bottom of the list of messages to be returned.
240 * @param throwable the {@link Throwable} object such as an exception from an API operation from which the message
241 * and stack trace are to be extracted and placed at the bottom of the list of messages to be returned
243 public void addThrowable(final Throwable throwable) {
244 final StringWriter throwableStringWriter = new StringWriter();
245 final PrintWriter throwablePrintWriter = new PrintWriter(throwableStringWriter);
246 throwable.printStackTrace(throwablePrintWriter);
247 messages.add(throwable.getMessage());
248 messages.add(throwableStringWriter.toString());
252 * Gets a representation of the {@link ApexApiResult} instance as a JSON string.
254 * @return the result instance JSON string
256 public String toJson() {
257 final StringBuilder builder = new StringBuilder();
258 builder.append("{\n");
260 builder.append("\"result\": \"");
261 builder.append(result.toString());
262 builder.append("\",\n");
264 builder.append("\"messages\": [");
265 boolean first = true;
266 for (final String message : messages) {
268 builder.append("\n\"");
271 builder.append(",\n\"");
273 builder.append(message.replaceAll("\"", "\\\\\""));
274 builder.append("\"");
276 builder.append("]\n");
278 builder.append("}\n");
280 return builder.toString();
287 public String toString() {
288 final StringBuilder builder = new StringBuilder();
289 builder.append("result: ");
290 builder.append(result);
291 builder.append('\n');
292 builder.append(getMessage());
293 return builder.toString();