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