2 * Copyright (c) 2018 Orange
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.nbi.apis.serviceorder;
18 import java.text.ParseException;
19 import java.text.SimpleDateFormat;
20 import java.util.Date;
21 import java.util.List;
22 import org.onap.nbi.apis.serviceorder.model.StateType;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.data.domain.PageRequest;
26 import org.springframework.data.domain.Pageable;
27 import org.springframework.data.mongodb.core.query.Criteria;
28 import org.springframework.data.mongodb.core.query.Query;
29 import org.springframework.stereotype.Service;
30 import org.springframework.util.CollectionUtils;
31 import org.springframework.util.MultiValueMap;
34 public class MultiCriteriaRequestBuilder {
36 private static final Logger LOGGER = LoggerFactory.getLogger(ServiceOrderResource.class);
39 public Query buildRequest(MultiValueMap<String, String> params) {
40 Query query = new Query();
42 List<String> externalIds = params.get("externalId");
43 if (!CollectionUtils.isEmpty(externalIds)) {
44 String externalId = externalIds.get(0);
45 LOGGER.debug("add criterion externalId {}", externalId);
46 query.addCriteria(Criteria.where("externalId").is(externalId));
49 List<String> states = params.get("state");
50 if (!CollectionUtils.isEmpty(states)) {
51 String state = states.get(0);
52 LOGGER.debug("add criterion state {}", state);
53 query.addCriteria(Criteria.where("state").is(StateType.fromValue(state)));
56 List<String> descriptions = params.get("description");
57 if (!CollectionUtils.isEmpty(descriptions)) {
58 String description = descriptions.get(0);
59 LOGGER.debug("add criterion description {}", description);
60 query.addCriteria(Criteria.where("description").is(description));
64 handleDate(params, query);
66 handleOffsetAndLimit(params, query);
71 private void handleDate(MultiValueMap<String, String> params, Query query) {
72 List<String> orderDateLts = params.get("orderDate.lt");
73 List<String> orderDateGts = params.get("orderDate.gt");
74 if (!CollectionUtils.isEmpty(orderDateLts) || !CollectionUtils.isEmpty(orderDateGts)) {
75 Criteria orderDateCriteria = Criteria.where("orderDate");
77 if (!CollectionUtils.isEmpty(orderDateLts)) {
78 String orderDateLt = orderDateLts.get(0);
79 LOGGER.debug("add criterion orderDate.lt {}", orderDateLt);
80 orderDateCriteria.lt(convertDate(orderDateLt));
82 if (!CollectionUtils.isEmpty(orderDateGts)) {
83 String orderDateGt = orderDateGts.get(0);
84 LOGGER.debug("add criterion orderDate.gt {}", orderDateGt);
85 orderDateCriteria.gt(convertDate(orderDateGt));
87 query.addCriteria(orderDateCriteria);
91 private void handleOffsetAndLimit(MultiValueMap<String, String> params, Query query) {
92 List<String> offsets = params.get("offset");
93 List<String> limits = params.get("limit");
94 if (!CollectionUtils.isEmpty(offsets) && !CollectionUtils.isEmpty(limits)) {
95 String offsetString = offsets.get(0);
96 String limitString = limits.get(0);
97 int offset = Integer.parseInt(offsetString);
98 int limit = Integer.parseInt(limitString);
99 final Pageable pageableRequest = new PageRequest(offset, limit);
100 query.with(pageableRequest);
101 } else if (!CollectionUtils.isEmpty(limits)) {
102 String limitString = limits.get(0);
103 int limit = Integer.parseInt(limitString);
104 final Pageable pageableRequest = new PageRequest(0, limit);
105 query.with(pageableRequest);
109 private Date convertDate(String dateString) {
110 String dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
111 SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
113 return formatter.parse(dateString);
114 } catch (ParseException e) {
115 LOGGER.error("unable to convert date " + dateString + ", the pattern is " + dateFormat + " ; " + e);