aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/.gitignore
AgeCommit message (Collapse)AuthorFilesLines
2017-02-19push addional codeMichael Lando1-0/+13
Change-Id: Ia427bb3460cda3a896f8faced2de69eaf3807b74 Signed-off-by: Michael Lando <ml636r@att.com>
/features/tree/sdnr/wt/common/src/main/java/org/onap/ccsdk/features?h=0.7.4&id=50b7033a87ea120dc2f317ab69b0b972013bb3a1'>features/sdnr/wt/common/database/data/DbFilter.java
blob: 82c036406b2c56e745ddc2e2d566defe29bbe347 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * ============LICENSE_START=======================================================
 * ONAP : ccsdk features
 * ================================================================================
 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
 * All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 */
package org.onap.ccsdk.features.sdnr.wt.common.database.data;

import java.util.Arrays;
import java.util.List;
import org.onap.ccsdk.features.sdnr.wt.common.database.queries.RangeQueryBuilder;

public class DbFilter {

    private static final List<String> timestampValueNames = Arrays.asList("timestamp", "start", "end", "date");

    public static String createDatabaseRegex(String restFilterValue) {
        return restFilterValue == null ? null : restFilterValue.replace("?", ".{1,1}").replace("*", ".*");
    }

    public static boolean hasSearchParams(String restFilterValue) {
        return restFilterValue == null ? false : restFilterValue.contains("*") || restFilterValue.contains("?");
    }

    public static boolean isComparisonValid(String restFilterValue) {
        return restFilterValue == null ? false : restFilterValue.contains(">") || restFilterValue.contains("<");
    }
    public static boolean isDatetimeKeyValue(String key, String value) {
        return timestampValueNames.contains(key.toLowerCase());
    }

    public static RangeQueryBuilder getRangeQuery(String key, String restFilterValue) {
        RangeQueryBuilder query = new RangeQueryBuilder(key);
        restFilterValue = restFilterValue.trim();
        if (restFilterValue.startsWith(">=")) {
            query.gte(getObjectFromString(key,restFilterValue.substring(2).trim()));
        } else if (restFilterValue.startsWith(">")) {
            query.gt(getObjectFromString(key,restFilterValue.substring(1).trim()));
        } else if (restFilterValue.startsWith("<=")) {
            query.lte(getObjectFromString(key,restFilterValue.substring(2).trim()));
        } else if (restFilterValue.startsWith("<")) {
            query.lt(getObjectFromString(key,restFilterValue.substring(1).trim()));
        } else {
            return null;
        }

        return query;
    }

    private static Object getObjectFromString(String key, String str) {
        if(isDatetimeKeyValue(key, str)) {
            return str;
        }
        try {
            return Double.parseDouble(str);
        } catch (NumberFormatException | NullPointerException nfe) {
            return str;
        }

    }
}