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;
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;
36 * The Class ApexEditorAPIResult return the result of and messages from all model API method calls on the
37 * {@link ApexModel} API.
40 @XmlAccessorType(XmlAccessType.NONE)
41 public class ApexApiResult {
44 * This enumeration is used to represent the result status of a call on the {@link ApexModel} API.
46 @XmlEnum(value = String.class)
48 /** The method call succeeded. */
50 /** The method call succeeded and all operations are now completed. */
52 /** The method call for a create operation failed because the concept already exists. */
55 * The method call for a create operation failed because multiple concepts already exists.
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. */
63 * The method call failed because of a structural error, a missing reference, or other error on the model.
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.
73 * Check if a result is OK.
75 * @param result the result
76 * @return true if the result is not OK
78 public static boolean isOk(final Result result) {
79 return result == Result.SUCCESS || result == Result.FINISHED;
83 * Check if a result is not OK.
85 * @param result the result
86 * @return true if the result is not OK
88 public static boolean isNok(final Result result) {
93 private Result result;
94 private List<String> messages = new ArrayList<>();
97 * The Default Constructor creates a result for a successful operation with no messages.
99 public ApexApiResult() {
100 result = Result.SUCCESS;
104 * This Constructor creates a result with the given result status with no messages.
106 * @param result the result status to use on this result
108 public ApexApiResult(final Result result) {
109 this.result = result;
113 * This Constructor creates a result with the given result status and message.
115 * @param result the result status to use on this result
116 * @param message the message to return with the result
118 public ApexApiResult(final Result result, final String message) {
119 this.result = result;
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.
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
130 public ApexApiResult(final Result result, final Throwable throwable) {
131 this.result = result;
132 addThrowable(throwable);
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
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
144 public ApexApiResult(final Result result, final String message, final Throwable throwable) {
145 this.result = result;
147 addThrowable(throwable);
151 * This message is a utility message that checks if the result of an operation on the API was OK.
153 * @return true, if the result indicates the API operation succeeded
155 @XmlAttribute(required = true)
156 public boolean isOk() {
157 return Result.isOk(result);
161 * This message is a utility message that checks if the result of an operation on the API was not OK.
163 * @return true, if the result indicates the API operation did not succeed
165 public boolean isNok() {
166 return Result.isNok(result);
170 * Gets the result status of an API operation.
172 * @return the result status
174 @XmlAttribute(required = true)
175 public Result getResult() {
180 * Sets the result status of an API operation.
182 * @param result the result status
184 public void setResult(final Result result) {
185 this.result = result;
189 * Gets the list of messages returned by an API operation.
191 * @return the list of messages returned by an API operation
193 @XmlElementWrapper(required = false, nillable = true)
194 @XmlElement(nillable = true, name = "message")
195 public List<String> getMessages() {
200 * Sets the list of messages to return as a result of an API operation.
202 * @param messages the list of messages to return as a result of an API operation
204 public void setMessages(final List<String> messages) {
205 this.messages = messages;
209 * Gets all the messages returned by an API operation as a single string.
211 * @return the messages returned by an API operation as a single string
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');
221 return builder.toString();
225 * Adds a message from an API operation to the bottom of the list of messages to be returned.
227 * @param message the message from an API operation to add to the bottom of the list of messages to be returned
229 public void addMessage(final String message) {
230 if (message != null && message.trim().length() > 0) {
231 messages.add(message);
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.
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
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());
251 * Gets a representation of the {@link ApexApiResult} instance as a JSON string.
253 * @return the result instance JSON string
255 public String toJson() {
256 final StringBuilder builder = new StringBuilder();
257 builder.append("{\n");
259 builder.append("\"result\": \"");
260 builder.append(result.toString());
261 builder.append("\",\n");
263 builder.append("\"messages\": [");
264 boolean first = true;
265 for (final String message : messages) {
267 builder.append("\n\"");
270 builder.append(",\n\"");
272 builder.append(message.replaceAll("\"", "\\\\\""));
273 builder.append("\"");
275 builder.append("]\n");
277 builder.append("}\n");
279 return builder.toString();
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();