8d899b0ee3997b6451a8480b9bcfd1c86ba8388e
[aai/search-data-service.git] / src / main / java / org / onap / aai / sa / searchdbabstraction / searchapi / DateRange.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.searchapi;
22
23 import com.fasterxml.jackson.annotation.JsonProperty;
24
25 /**
26  * This class represents the ranges specification in an date_range statement.
27  * <p>
28  * The expected JSON structure for a ranges is as follows:
29  * <p>
30  * <pre>
31  * {
32  *  "from": <from-date>
33  * }
34  * </pre>
35  * <p>
36  * or
37  * <p>
38  * <pre>
39  * {
40  *  "to": <to-date>
41  * }
42  * </pre>
43  * <p>
44  * or
45  * <p>
46  * <pre>
47  * {
48  *  "from": <from-date>,
49  *  "to": <to-date>
50  * }
51  * </pre>
52  *
53  * @author sye
54  */
55 public class DateRange {
56
57   @JsonProperty("from")
58   private String fromDate;
59
60   @JsonProperty("to")
61   private String toDate;
62
63   public String getFromDate() {
64     return fromDate;
65   }
66
67   public void setFromDate(String fromDate) {
68     this.fromDate = fromDate;
69   }
70
71   public String getToDate() {
72     return toDate;
73   }
74
75   public void setToDate(String toDate) {
76     this.toDate = toDate;
77   }
78
79   public String toElasticSearch() {
80     StringBuilder sb = new StringBuilder();
81
82     sb.append("{");
83
84     if (fromDate != null) {
85       sb.append("\"from\": \"");
86       sb.append(fromDate.toString());
87       sb.append("\"");
88     }
89
90     if (toDate != null) {
91       if (fromDate != null) {
92         sb.append(", \"to\": \"");
93         sb.append(toDate.toString());
94         sb.append("\"");
95       } else {
96         sb.append("\"to\": \"");
97         sb.append(toDate.toString());
98         sb.append("\"");
99       }
100     }
101
102     sb.append("}");
103
104     return sb.toString();
105   }
106
107   public String toString() {
108     return "{from: " + fromDate + ", to: " + toDate + "}";
109   }
110
111 }