151e02b7cc69e1addcd83c71d2fb7ad62162e7c9
[aai/search-data-service.git] / search-data-service / src / main / java / org / onap / aai / sa / searchdbabstraction / elasticsearch / dao / ElasticSearchResultItem.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.sa.searchdbabstraction.elasticsearch.dao;
22
23 import org.onap.aai.sa.rest.ApiUtils;
24
25 public class ElasticSearchResultItem {
26
27     public static final String REQUEST_URL = "REQUEST_URL";
28
29     private ElasticSearchOperationStatus create;
30     private ElasticSearchOperationStatus index;
31     private ElasticSearchOperationStatus delete;
32
33     public ElasticSearchOperationStatus getCreate() {
34         return create;
35     }
36
37     public void setCreate(ElasticSearchOperationStatus index) {
38         this.create = index;
39     }
40
41     public ElasticSearchOperationStatus getIndex() {
42         return index;
43     }
44
45     public void setIndex(ElasticSearchOperationStatus index) {
46         this.index = index;
47     }
48
49     public ElasticSearchOperationStatus getDelete() {
50         return delete;
51     }
52
53     public void setDelete(ElasticSearchOperationStatus delete) {
54         this.delete = delete;
55     }
56
57     public String operationType() {
58
59         if (create != null) {
60             return "create";
61         }
62         if (index != null) {
63             return "update";
64         }
65         if (delete != null) {
66             return "delete";
67         }
68
69         return "unknown";
70     }
71
72     public ElasticSearchOperationStatus operationStatus() {
73
74         if (create != null) {
75             return create;
76         }
77         if (index != null) {
78             return index;
79         }
80         if (delete != null) {
81             return delete;
82         }
83
84         return null;
85     }
86
87
88     public String toJson() {
89         StringBuilder sb = new StringBuilder();
90
91         sb.append("{");
92
93         sb.append("\"operation\": \"").append(operationType()).append("\", ");
94
95         if (operationStatus().getAdditionalProperties().containsKey(REQUEST_URL)) {
96             sb.append("\"url\": \"").append(operationStatus().getAdditionalProperties().get(REQUEST_URL))
97                     .append("\", ");
98         } else {
99             sb.append("\"url\": \"")
100                     .append(ApiUtils.buildDocumentUri(operationStatus().getIndex(), operationStatus().getId()))
101                     .append("\", ");
102         }
103
104         // We don't want to include an etag field in the response in
105         // the case of a delete, since that would imply that the client
106         // could still access that version of the file in some manner
107         // (which we are not supporting).
108         if (!operationType().equals("delete")) {
109             sb.append("\"etag\": \"").append(operationStatus().getVersion()).append("\", ");
110         }
111         sb.append("\"status-code\": \"").append(operationStatus().getStatus()).append("\", ");
112
113         sb.append("\"status-message\": \"");
114
115         if (ApiUtils.isSuccessStatusCode(operationStatus().getStatus())) {
116             sb.append("OK");
117         } else {
118             // Sometimes the error object doesn't get populated, so check
119             // before we try to reference it...
120             if (operationStatus().getError() != null) {
121                 sb.append(operationStatus().getError().getReason());
122             } else {
123                 sb.append("");
124             }
125         }
126         sb.append("\"");
127         sb.append("}");
128
129         return sb.toString();
130     }
131
132
133     @Override
134     public String toString() {
135         StringBuilder sb = new StringBuilder();
136
137         sb.append("ElasticSearchItemStatus [");
138         if (create != null) {
139             sb.append("create " + create);
140         } else if (index != null) {
141             sb.append("index " + index);
142         } else if (delete != null) {
143             sb.append("delete " + index);
144         }
145         sb.append("]");
146         return sb.toString();
147     }
148
149 }