f59805e7448295c9765b112cdf42a276f268955b
[aai/search-data-service.git] / 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()
97           .get(REQUEST_URL)).append("\", ");
98     } else {
99       sb.append("\"url\": \"").append(ApiUtils.buildDocumentUri(operationStatus()
100           .getIndex(), operationStatus().getId())).append("\", ");
101     }
102
103     // We don't want to include an etag field in the response in
104     // the case of a delete, since that would imply that the client
105     // could still access that version of the file in some manner
106     // (which we are not supporting).
107     if (!operationType().equals("delete")) {
108       sb.append("\"etag\": \"").append(operationStatus().getVersion()).append("\", ");
109     }
110     sb.append("\"status-code\": \"").append(operationStatus().getStatus()).append("\", ");
111
112     sb.append("\"status-message\": \"");
113
114     if ((operationStatus().getStatus() >= 200) && (operationStatus().getStatus() < 300)) {
115       sb.append("OK");
116     } else {
117       // Sometimes the error object doesn't get populated, so check
118       // before we try to reference it...
119       if (operationStatus().getError() != null) {
120         sb.append(operationStatus().getError().getReason());
121       } else {
122         sb.append("");
123       }
124     }
125     sb.append("\"");
126     sb.append("}");
127
128     return sb.toString();
129   }
130
131
132   @Override
133   public String toString() {
134     StringBuilder sb = new StringBuilder();
135
136     sb.append("ElasticSearchItemStatus [");
137     if (create != null) {
138       sb.append("create " + create);
139     } else if (index != null) {
140       sb.append("index " + index);
141     } else if (delete != null) {
142       sb.append("delete " + index);
143     }
144     sb.append("]");
145     return sb.toString();
146   }
147
148 }