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.commons;
18 import org.onap.nbi.apis.serviceorder.model.StateType;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.springframework.data.domain.PageRequest;
22 import org.springframework.data.domain.Pageable;
23 import org.springframework.data.mongodb.core.query.Criteria;
24 import org.springframework.data.mongodb.core.query.Query;
25 import org.springframework.stereotype.Service;
26 import org.springframework.util.CollectionUtils;
27 import org.springframework.util.MultiValueMap;
29 import java.text.ParseException;
30 import java.text.SimpleDateFormat;
31 import java.util.Date;
32 import java.util.List;
35 public class MultiCriteriaRequestBuilder {
37 private static final Logger LOGGER = LoggerFactory.getLogger(MultiCriteriaRequestBuilder.class);
40 public Query buildRequest(MultiValueMap<String, String> params) {
41 Query query = new Query();
43 List<String> externalIds = params.get("externalId");
44 if (!CollectionUtils.isEmpty(externalIds)) {
45 String externalId = externalIds.get(0);
46 LOGGER.debug("add criterion externalId {}", externalId);
47 query.addCriteria(Criteria.where("externalId").is(externalId));
50 List<String> states = params.get("state");
51 if (!CollectionUtils.isEmpty(states)) {
52 String state = states.get(0);
53 LOGGER.debug("add criterion state {}", state);
54 query.addCriteria(Criteria.where("state").is(StateType.fromValue(state)));
57 List<String> descriptions = params.get("description");
58 if (!CollectionUtils.isEmpty(descriptions)) {
59 String description = descriptions.get(0);
60 LOGGER.debug("add criterion description {}", description);
61 query.addCriteria(Criteria.where("description").is(description));
64 List<String> eventTypes = params.get("query.eventType");
65 if (!CollectionUtils.isEmpty(eventTypes)) {
66 Object[] eventType = new String[]{eventTypes.get(0)};
67 LOGGER.debug("add criterion query.eventType {}", eventType);
68 query.addCriteria(Criteria.where("query.eventType").in(eventType));
71 List<String> callbacks = params.get("callback");
72 if (!CollectionUtils.isEmpty(callbacks)) {
73 String callback = callbacks.get(0);
74 LOGGER.debug("add criterion callback {}", callback);
75 query.addCriteria(Criteria.where("callback").is(callback));
79 handleDate(params, query);
81 handleOffsetAndLimit(params, query);
86 private void handleDate(MultiValueMap<String, String> params, Query query) {
87 List<String> orderDateLts = params.get("orderDate.lt");
88 List<String> orderDateGts = params.get("orderDate.gt");
89 if (!CollectionUtils.isEmpty(orderDateLts) || !CollectionUtils.isEmpty(orderDateGts)) {
90 Criteria orderDateCriteria = Criteria.where("orderDate");
92 if (!CollectionUtils.isEmpty(orderDateLts)) {
93 String orderDateLt = orderDateLts.get(0);
94 LOGGER.debug("add criterion orderDate.lt {}", orderDateLt);
95 orderDateCriteria.lt(convertDate(orderDateLt));
97 if (!CollectionUtils.isEmpty(orderDateGts)) {
98 String orderDateGt = orderDateGts.get(0);
99 LOGGER.debug("add criterion orderDate.gt {}", orderDateGt);
100 orderDateCriteria.gt(convertDate(orderDateGt));
102 query.addCriteria(orderDateCriteria);
106 private void handleOffsetAndLimit(MultiValueMap<String, String> params, Query query) {
107 List<String> offsets = params.get("offset");
108 List<String> limits = params.get("limit");
109 if (!CollectionUtils.isEmpty(offsets) && !CollectionUtils.isEmpty(limits)) {
110 String offsetString = offsets.get(0);
111 String limitString = limits.get(0);
112 int offset = Integer.parseInt(offsetString);
113 int limit = Integer.parseInt(limitString);
114 final Pageable pageableRequest = new PageRequest(offset, limit);
115 query.with(pageableRequest);
116 } else if (!CollectionUtils.isEmpty(limits)) {
117 String limitString = limits.get(0);
118 int limit = Integer.parseInt(limitString);
119 final Pageable pageableRequest = new PageRequest(0, limit);
120 query.with(pageableRequest);
124 private Date convertDate(String dateString) {
125 String dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
126 SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
128 return formatter.parse(dateString);
129 } catch (ParseException e) {
130 LOGGER.error("unable to convert date " + dateString + ", the pattern is " + dateFormat + " ; " + e);