65bd84addf9062e0bc79c0441fc13a5b7ca1b4d1
[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
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;
35
36 /**
37  * The Class ApexEditorAPIResult return the result of and messages from all model API method calls on the
38  * {@link ApexModel} API.
39  */
40 @XmlRootElement
41 @XmlAccessorType(XmlAccessType.NONE)
42 public class ApexApiResult {
43
44     /**
45      * This enumeration is used to represent the result status of a call on the {@link ApexModel} API.
46      */
47     @XmlEnum(value = String.class)
48     public enum Result {
49         /** The method call succeeded. */
50         SUCCESS,
51         /** The method call succeeded and all operations are now completed. */
52         FINISHED,
53         /** The method call for a create operation failed because the concept already exists. */
54         CONCEPT_EXISTS,
55         /**
56          * The method call for a create operation failed because multiple concepts already exists.
57          */
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. */
62         NO_ACTION_SPECIFIED,
63         /**
64          * The method call failed because of a structural error, a missing reference, or other error on the model.
65          */
66         FAILED,
67         /**
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.
70          */
71         OTHER_ERROR;
72
73         /**
74          * Check if a result is OK.
75          *
76          * @param result the result
77          * @return true if the result is not OK
78          */
79         public static boolean isOk(final Result result) {
80             return result == Result.SUCCESS || result == Result.FINISHED;
81         }
82
83         /**
84          * Check if a result is not OK.
85          *
86          * @param result the result
87          * @return true if the result is not OK
88          */
89         public static boolean isNok(final Result result) {
90             return !isOk(result);
91         }
92     }
93
94     private Result result;
95     private List<String> messages = new ArrayList<>();
96
97     /**
98      * The Default Constructor creates a result for a successful operation with no messages.
99      */
100     public ApexApiResult() {
101         result = Result.SUCCESS;
102     }
103
104     /**
105      * This Constructor creates a result with the given result status with no messages.
106      *
107      * @param result the result status to use on this result
108      */
109     public ApexApiResult(final Result result) {
110         this.result = result;
111     }
112
113     /**
114      * This Constructor creates a result with the given result status and message.
115      *
116      * @param result the result status to use on this result
117      * @param message the message to return with the result
118      */
119     public ApexApiResult(final Result result, final String message) {
120         this.result = result;
121         addMessage(message);
122     }
123
124     /**
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.
127      *
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
130      */
131     public ApexApiResult(final Result result, final Throwable throwable) {
132         this.result = result;
133         addThrowable(throwable);
134     }
135
136     /**
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
139      * message.
140      *
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
144      */
145     public ApexApiResult(final Result result, final String message, final Throwable throwable) {
146         this.result = result;
147         addMessage(message);
148         addThrowable(throwable);
149     }
150
151     /**
152      * This message is a utility message that checks if the result of an operation on the API was OK.
153      *
154      * @return true, if the result indicates the API operation succeeded
155      */
156     @XmlAttribute(required = true)
157     public boolean isOk() {
158         return Result.isOk(result);
159     }
160
161     /**
162      * This message is a utility message that checks if the result of an operation on the API was not OK.
163      *
164      * @return true, if the result indicates the API operation did not succeed
165      */
166     public boolean isNok() {
167         return Result.isNok(result);
168     }
169
170     /**
171      * Gets the result status of an API operation.
172      *
173      * @return the result status
174      */
175     @XmlAttribute(required = true)
176     public Result getResult() {
177         return result;
178     }
179
180     /**
181      * Sets the result status of an API operation.
182      *
183      * @param result the result status
184      */
185     public void setResult(final Result result) {
186         this.result = result;
187     }
188
189     /**
190      * Gets the list of messages returned by an API operation.
191      *
192      * @return the list of messages returned by an API operation
193      */
194     @XmlElementWrapper(required = false, nillable = true)
195     @XmlElement(nillable = true, name = "message")
196     public List<String> getMessages() {
197         return messages;
198     }
199
200     /**
201      * Sets the list of messages to return as a result of an API operation.
202      *
203      * @param messages the list of messages to return as a result of an API operation
204      */
205     public void setMessages(final List<String> messages) {
206         this.messages = messages;
207     }
208
209     /**
210      * Gets all the messages returned by an API operation as a single string.
211      *
212      * @return the messages returned by an API operation as a single string
213      */
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');
220         }
221
222         return builder.toString();
223     }
224
225     /**
226      * Adds a message from an API operation to the bottom of the list of messages to be returned.
227      *
228      * @param message the message from an API operation to add to the bottom of the list of messages to be returned
229      */
230     public void addMessage(final String message) {
231         if (message != null && message.trim().length() > 0) {
232             messages.add(message);
233         }
234     }
235
236     /**
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.
239      *
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
242      */
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());
249     }
250
251     /**
252      * Gets a representation of the {@link ApexApiResult} instance as a JSON string.
253      *
254      * @return the result instance JSON string
255      */
256     public String toJson() {
257         final StringBuilder builder = new StringBuilder();
258         builder.append("{\n");
259
260         builder.append("\"result\": \"");
261         builder.append(result.toString());
262         builder.append("\",\n");
263
264         builder.append("\"messages\": [");
265         boolean first = true;
266         for (final String message : messages) {
267             if (first) {
268                 builder.append("\n\"");
269                 first = false;
270             } else {
271                 builder.append(",\n\"");
272             }
273             builder.append(message.replaceAll("\"", "\\\\\""));
274             builder.append("\"");
275         }
276         builder.append("]\n");
277
278         builder.append("}\n");
279
280         return builder.toString();
281     }
282
283     /**
284      * {@inheritDoc}.
285      */
286     @Override
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();
294     }
295 }