7ea2fd4578ed261826999fe25ea0013cfbaeec32
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / serviceorder / MultiCriteriaRequestBuilder.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 package org.onap.nbi.apis.serviceorder;
17
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;
32
33 @Service
34 public class MultiCriteriaRequestBuilder {
35
36     private static final Logger LOGGER = LoggerFactory.getLogger(ServiceOrderResource.class);
37
38
39     public Query buildRequest(MultiValueMap<String, String> params) {
40         Query query = new Query();
41
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));
47
48         }
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)));
54
55         }
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));
61
62         }
63
64         handleDate(params, query);
65
66         handleOffsetAndLimit(params, query);
67
68         return query;
69     }
70
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");
76
77             if (!CollectionUtils.isEmpty(orderDateLts)) {
78                 String orderDateLt = orderDateLts.get(0);
79                 LOGGER.debug("add criterion orderDate.lt {}", orderDateLt);
80                 orderDateCriteria.lt(convertDate(orderDateLt));
81             }
82             if (!CollectionUtils.isEmpty(orderDateGts)) {
83                 String orderDateGt = orderDateGts.get(0);
84                 LOGGER.debug("add criterion orderDate.gt {}", orderDateGt);
85                 orderDateCriteria.gt(convertDate(orderDateGt));
86             }
87             query.addCriteria(orderDateCriteria);
88         }
89     }
90
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);
106         }
107     }
108
109     private Date convertDate(String dateString) {
110         String dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
111         SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
112         try {
113             return formatter.parse(dateString);
114         } catch (ParseException e) {
115             LOGGER.error("unable to convert date " + dateString + ", the pattern is " + dateFormat + " ; " + e);
116         }
117         return null;
118
119     }
120
121 }