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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
/**
*
* Copyright (c) 2017 Orange. 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.
*/
package org.onap.nbi.apis.serviceorder;
import org.onap.nbi.apis.serviceorder.model.StateType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
public class MultiCriteriaRequestBuilder {
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceOrderResource.class);
public Query buildRequest(MultiValueMap<String, String> params) {
Query query = new Query();
List<String> externalIds = params.get("externalId");
if (!CollectionUtils.isEmpty(externalIds)) {
String externalId = externalIds.get(0);
LOGGER.debug("add criterion externalId {0}", externalId);
query.addCriteria(Criteria.where("externalId").is(externalId));
}
List<String> states = params.get("state");
if (!CollectionUtils.isEmpty(states)) {
String state = states.get(0);
LOGGER.debug("add criterion state {0}", state);
query.addCriteria(Criteria.where("state").is(StateType.fromValue(state)));
}
List<String> descriptions = params.get("description");
if (!CollectionUtils.isEmpty(descriptions)) {
String description = descriptions.get(0);
LOGGER.debug("add criterion description {0}", description);
query.addCriteria(Criteria.where("description").is(description));
}
handleDate(params, query);
handleOffsetAndLimit(params, query);
return query;
}
private void handleDate(MultiValueMap<String, String> params, Query query) {
List<String> orderDateLts = params.get("orderDate.lt");
List<String> orderDateGts = params.get("orderDate.gt");
if (!CollectionUtils.isEmpty(orderDateLts) || !CollectionUtils.isEmpty(orderDateGts)) {
Criteria orderDateCriteria = Criteria.where("orderDate");
if (!CollectionUtils.isEmpty(orderDateLts)) {
String orderDateLt = orderDateLts.get(0);
LOGGER.debug("add criterion orderDate.lt {0}", orderDateLt);
orderDateCriteria.lt(convertDate(orderDateLt));
}
if (!CollectionUtils.isEmpty(orderDateGts)) {
String orderDateGt = orderDateGts.get(0);
LOGGER.debug("add criterion orderDate.gt {0}", orderDateGt);
orderDateCriteria.gt(convertDate(orderDateGt));
}
query.addCriteria(orderDateCriteria);
}
}
private void handleOffsetAndLimit(MultiValueMap<String, String> params, Query query) {
List<String> offsets = params.get("offset");
List<String> limits = params.get("limit");
if (!CollectionUtils.isEmpty(offsets) && !CollectionUtils.isEmpty(limits)) {
String offsetString = offsets.get(0);
String limitString = limits.get(0);
int offset = Integer.parseInt(offsetString);
int limit = Integer.parseInt(limitString);
final Pageable pageableRequest = new PageRequest(offset, limit);
query.with(pageableRequest);
} else if (!CollectionUtils.isEmpty(limits)) {
String limitString = limits.get(0);
int limit = Integer.parseInt(limitString);
final Pageable pageableRequest = new PageRequest(0, limit);
query.with(pageableRequest);
}
}
private Date convertDate(String dateString) {
String dateFormat = "yyyy-MM-dd HH:mm:ss.SSS";
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
try {
return formatter.parse(dateString);
} catch (ParseException e) {
LOGGER.error("unable to convert date " + dateString + ", the pattern is " + dateFormat + " ; " + e);
}
return null;
}
}
|