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").in(StateType.fromValueSearch(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));
63 List<String> eventTypes = params.get("query.eventType");
64 if (!CollectionUtils.isEmpty(eventTypes)) {
65 Object[] eventType = new String[]{eventTypes.get(0)};
66 LOGGER.debug("add criterion query.eventType {}", eventType);
67 query.addCriteria(Criteria.where("query.eventType").in(eventType));
70 List<String> callbacks = params.get("callback");
71 if (!CollectionUtils.isEmpty(callbacks)) {
72 String callback = callbacks.get(0);
73 LOGGER.debug("add criterion callback {}", callback);
74 query.addCriteria(Criteria.where("callback").is(callback));
78 handleDate(params, query);
80 handleOffsetAndLimit(params, query);
85 private void handleDate(MultiValueMap<String, String> params, Query query) {
86 List<String> orderDateLts = params.get("orderDate.lt");
87 List<String> orderDateGts = params.get("orderDate.gt");
88 if (!CollectionUtils.isEmpty(orderDateLts) || !CollectionUtils.isEmpty(orderDateGts)) {
89 Criteria orderDateCriteria = Criteria.where("orderDate");
91 if (!CollectionUtils.isEmpty(orderDateLts)) {
92 String orderDateLt = orderDateLts.get(0);
93 LOGGER.debug("add criterion orderDate.lt {}", orderDateLt);
94 orderDateCriteria.lt(convertDate(orderDateLt));
96 if (!CollectionUtils.isEmpty(orderDateGts)) {
97 String orderDateGt = orderDateGts.get(0);
98 LOGGER.debug("add criterion orderDate.gt {}", orderDateGt);
99 orderDateCriteria.gt(convertDate(orderDateGt));
101 query.addCriteria(orderDateCriteria);
105 private void handleOffsetAndLimit(MultiValueMap<String, String> params, Query query) {
106 List<String> offsets = params.get("offset");
107 List<String> limits = params.get("limit");
108 if (!CollectionUtils.isEmpty(offsets) && !CollectionUtils.isEmpty(limits)) {
109 String offsetString = offsets.get(0);
110 String limitString = limits.get(0);
111 int offset = Integer.parseInt(offsetString);
112 int limit = Integer.parseInt(limitString);
113 final Pageable pageableRequest = new PageRequest(offset, limit);
114 query.with(pageableRequest);
115 } else if (!CollectionUtils.isEmpty(limits)) {
116 String limitString = limits.get(0);
117 int limit = Integer.parseInt(limitString);
118 final Pageable pageableRequest = new PageRequest(0, limit);
119 query.with(pageableRequest);
123 private Date convertDate(String dateString) {
124 String dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
125 SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
127 return formatter.parse(dateString);
128 } catch (ParseException e) {
129 LOGGER.error("unable to convert date " + dateString + ", the pattern is " + dateFormat + " ; " + e);