Format Java code with respect to ONAP Code Style
[externalapi/nbi.git] / src / main / java / org / onap / nbi / commons / QueryParserUtils.java
1 /**
2  *     Copyright (c) 2018 Orange
3  *
4  *     Licensed under the Apache License, Version 2.0 (the "License");
5  *     you may not use this file except in compliance with the License.
6  *     You may obtain a copy of the License at
7  *
8  *         http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *     Unless required by applicable law or agreed to in writing, software
11  *     distributed under the License is distributed on an "AS IS" BASIS,
12  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *     See the License for the specific language governing permissions and
14  *     limitations under the License.
15  */
16
17 package org.onap.nbi.commons;
18
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Map.Entry;
24 import java.util.Set;
25 import org.springframework.util.LinkedMultiValueMap;
26 import org.springframework.util.MultiValueMap;
27
28 /**
29  *
30  */
31 public class QueryParserUtils {
32
33     private QueryParserUtils() {
34     }
35
36     /**
37      *
38      * @param queryParameters
39      * @return
40      */
41     public static Set<String> getFields(MultiValueMap<String, String> queryParameters) {
42
43         Set<String> fieldSet = new HashSet<>();
44         if (queryParameters != null) {
45             // search for "all" parameter
46             if (queryParameters.containsKey(ReservedKeys.ALL_FIELDS)) {
47                 queryParameters.remove(ReservedKeys.ALL_FIELDS);
48                 fieldSet.add(ReservedKeys.ALL_FIELDS);
49             }
50             // search for "fields" parameters
51             List<String> queryParameterField =
52                     queryParameters.remove(ReservedKeys.QUERY_KEY_FIELD_ESCAPE + ReservedKeys.QUERY_KEY_FIELD);
53             if (queryParameterField == null) {
54                 queryParameterField = queryParameters.remove(ReservedKeys.QUERY_KEY_FIELD);
55             }
56             if (queryParameterField != null && !queryParameterField.isEmpty()) {
57                 String queryParameterValue = queryParameterField.get(0);
58                 if (queryParameterValue != null) {
59                     String[] tokenArray = queryParameterValue.split(",");
60                     fieldSet.addAll(Arrays.asList(tokenArray));
61                 }
62             }
63         }
64         return fieldSet;
65     }
66
67     public static MultiValueMap<String, String> popCriteria(MultiValueMap<String, String> queryParameters) {
68
69         Set<Entry<String, List<String>>> entrySet = queryParameters.entrySet();
70
71         MultiValueMap<String, String> criterias = new LinkedMultiValueMap<>();
72
73         entrySet.stream().forEach(entry -> {
74             final List<String> tempValues = new ArrayList<>();
75             entry.getValue().stream().forEach(value -> tempValues.addAll(Arrays.asList(value.split(","))));
76             criterias.put(entry.getKey(), tempValues);
77         });
78
79         return criterias;
80     }
81
82 }