32b1d0c9ed78da2c9761d4a4dfcf3e9f24e3f119
[aai/search-data-service.git] / search-data-service / src / main / java / org / onap / aai / sa / searchdbabstraction / entity / OperationResultBuilder.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
22 package org.onap.aai.sa.searchdbabstraction.entity;
23
24 import javax.ws.rs.core.Response;
25 import javax.ws.rs.core.Response.Status;
26
27 public class OperationResultBuilder {
28
29     public enum Type {
30         DOCUMENT,
31         SEARCH
32     }
33
34     private static final String INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT =
35             "Internal Error: ElasticSearch operation fault occurred";
36
37     private OperationResult opResult;
38
39     public OperationResultBuilder() {
40         opResult = new OperationResult();
41     }
42
43     public OperationResultBuilder(Type type) {
44         switch (type) {
45             case DOCUMENT:
46                 opResult = new DocumentOperationResult();
47                 break;
48             case SEARCH:
49                 opResult = new SearchOperationResult();
50                 break;
51             default:
52                 opResult = new OperationResult();
53         }
54     }
55
56     public OperationResult build() {
57         return opResult;
58     }
59
60     public OperationResultBuilder useDefaults() {
61         opResult.setResultCode(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
62         opResult.setResult(INTERNAL_SERVER_ERROR_ELASTIC_SEARCH_OPERATION_FAULT);
63         return this;
64     }
65
66     public OperationResultBuilder resultCode(int resultCode) {
67         opResult.setResultCode(resultCode);
68         return this;
69     }
70
71     public OperationResultBuilder status(Status status) {
72         return resultCode(status.getStatusCode());
73     }
74
75     public OperationResultBuilder failureCause(String failureCause) {
76         opResult.setFailureCause(failureCause);
77         return this;
78     }
79
80     public OperationResultBuilder result(String resultMsg) {
81         opResult.setResult(resultMsg);
82         return this;
83     }
84
85     public OperationResultBuilder resultVersion(String resultVersion) {
86         opResult.setResultVersion(resultVersion);
87         return this;
88     }
89
90 }