Cleanup project's name in Sonar
[aai/search-data-service.git] / src / main / java / org / openecomp / sa / searchdbabstraction / elasticsearch / dao / ElasticSearchResultItem.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.openecomp.sa.searchdbabstraction.elasticsearch.dao;
24
25 import org.openecomp.sa.rest.ApiUtils;
26
27 public class ElasticSearchResultItem {
28
29   public static final String REQUEST_URL = "REQUEST_URL";
30
31   private ElasticSearchOperationStatus create;
32   private ElasticSearchOperationStatus index;
33   private ElasticSearchOperationStatus delete;
34
35   public ElasticSearchOperationStatus getCreate() {
36     return create;
37   }
38
39   public void setCreate(ElasticSearchOperationStatus index) {
40     this.create = index;
41   }
42
43   public ElasticSearchOperationStatus getIndex() {
44     return index;
45   }
46
47   public void setIndex(ElasticSearchOperationStatus index) {
48     this.index = index;
49   }
50
51   public ElasticSearchOperationStatus getDelete() {
52     return delete;
53   }
54
55   public void setDelete(ElasticSearchOperationStatus delete) {
56     this.delete = delete;
57   }
58
59   public String operationType() {
60
61     if (create != null) {
62       return "create";
63     }
64     if (index != null) {
65       return "update";
66     }
67     if (delete != null) {
68       return "delete";
69     }
70
71     return "unknown";
72   }
73
74   public ElasticSearchOperationStatus operationStatus() {
75
76     if (create != null) {
77       return create;
78     }
79     if (index != null) {
80       return index;
81     }
82     if (delete != null) {
83       return delete;
84     }
85
86     return null;
87   }
88
89
90   public String toJson() {
91     StringBuilder sb = new StringBuilder();
92
93     sb.append("{");
94
95     sb.append("\"operation\": \"").append(operationType()).append("\", ");
96
97     if (operationStatus().getAdditionalProperties().containsKey(REQUEST_URL)) {
98       sb.append("\"url\": \"").append(operationStatus().getAdditionalProperties()
99           .get(REQUEST_URL)).append("\", ");
100     } else {
101       sb.append("\"url\": \"").append(ApiUtils.buildDocumentUri(operationStatus()
102           .getIndex(), operationStatus().getId())).append("\", ");
103     }
104
105     // We don't want to include an etag field in the response in
106     // the case of a delete, since that would imply that the client
107     // could still access that version of the file in some manner
108     // (which we are not supporting).
109     if (!operationType().equals("delete")) {
110       sb.append("\"etag\": \"").append(operationStatus().getVersion()).append("\", ");
111     }
112     sb.append("\"status-code\": \"").append(operationStatus().getStatus()).append("\", ");
113
114     sb.append("\"status-message\": \"");
115
116     if ((operationStatus().getStatus() >= 200) && (operationStatus().getStatus() < 300)) {
117       sb.append("OK");
118     } else {
119       // Sometimes the error object doesn't get populated, so check
120       // before we try to reference it...
121       if (operationStatus().getError() != null) {
122         sb.append(operationStatus().getError().getReason());
123       } else {
124         sb.append("");
125       }
126     }
127     sb.append("\"");
128     sb.append("}");
129
130     return sb.toString();
131   }
132
133
134   @Override
135   public String toString() {
136     StringBuilder sb = new StringBuilder();
137
138     sb.append("ElasticSearchItemStatus [");
139     if (create != null) {
140       sb.append("create " + create);
141     } else if (index != null) {
142       sb.append("index " + index);
143     } else if (delete != null) {
144       sb.append("delete " + index);
145     }
146     sb.append("]");
147     return sb.toString();
148   }
149
150 }