Add Apache license header per file
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / serviceorder / MultiCriteriaRequestBuilder.java
1 /**
2  *
3  *     Copyright (c) 2017 Orange.  All rights reserved.
4  *
5  *     Licensed under the Apache License, Version 2.0 (the "License");
6  *     you may not use this file except in compliance with the License.
7  *     You may obtain a copy of the License at
8  *
9  *         http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *     Unless required by applicable law or agreed to in writing, software
12  *     distributed under the License is distributed on an "AS IS" BASIS,
13  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *     See the License for the specific language governing permissions and
15  *     limitations under the License.
16  */
17 package org.onap.nbi.apis.serviceorder;
18
19 import org.onap.nbi.apis.serviceorder.model.StateType;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.springframework.data.domain.PageRequest;
23 import org.springframework.data.domain.Pageable;
24 import org.springframework.data.mongodb.core.query.Criteria;
25 import org.springframework.data.mongodb.core.query.Query;
26 import org.springframework.stereotype.Service;
27 import org.springframework.util.CollectionUtils;
28 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;
33
34 @Service
35 public class MultiCriteriaRequestBuilder {
36
37     private static final Logger LOGGER = LoggerFactory.getLogger(ServiceOrderResource.class);
38
39
40     public Query buildRequest(MultiValueMap<String, String> params) {
41         Query query = new Query();
42
43         List<String> externalIds = params.get("externalId");
44         if (!CollectionUtils.isEmpty(externalIds)) {
45             String externalId = externalIds.get(0);
46             LOGGER.debug("add criterion externalId {0}", externalId);
47             query.addCriteria(Criteria.where("externalId").is(externalId));
48
49         }
50         List<String> states = params.get("state");
51         if (!CollectionUtils.isEmpty(states)) {
52             String state = states.get(0);
53             LOGGER.debug("add criterion state {0}", state);
54             query.addCriteria(Criteria.where("state").is(StateType.fromValue(state)));
55
56         }
57         List<String> descriptions = params.get("description");
58         if (!CollectionUtils.isEmpty(descriptions)) {
59             String description = descriptions.get(0);
60             LOGGER.debug("add criterion description {0}", description);
61             query.addCriteria(Criteria.where("description").is(description));
62
63         }
64
65         handleDate(params, query);
66
67         handleOffsetAndLimit(params, query);
68
69         return query;
70     }
71
72     private void handleDate(MultiValueMap<String, String> params, Query query) {
73         List<String> orderDateLts = params.get("orderDate.lt");
74         List<String> orderDateGts = params.get("orderDate.gt");
75         if (!CollectionUtils.isEmpty(orderDateLts) || !CollectionUtils.isEmpty(orderDateGts)) {
76             Criteria orderDateCriteria = Criteria.where("orderDate");
77
78             if (!CollectionUtils.isEmpty(orderDateLts)) {
79                 String orderDateLt = orderDateLts.get(0);
80                 LOGGER.debug("add criterion orderDate.lt {0}", orderDateLt);
81                 orderDateCriteria.lt(convertDate(orderDateLt));
82             }
83             if (!CollectionUtils.isEmpty(orderDateGts)) {
84                 String orderDateGt = orderDateGts.get(0);
85                 LOGGER.debug("add criterion orderDate.gt {0}", orderDateGt);
86                 orderDateCriteria.gt(convertDate(orderDateGt));
87             }
88             query.addCriteria(orderDateCriteria);
89         }
90     }
91
92     private void handleOffsetAndLimit(MultiValueMap<String, String> params, Query query) {
93         List<String> offsets = params.get("offset");
94         List<String> limits = params.get("limit");
95         if (!CollectionUtils.isEmpty(offsets) && !CollectionUtils.isEmpty(limits)) {
96             String offsetString = offsets.get(0);
97             String limitString = limits.get(0);
98             int offset = Integer.parseInt(offsetString);
99             int limit = Integer.parseInt(limitString);
100             final Pageable pageableRequest = new PageRequest(offset, limit);
101             query.with(pageableRequest);
102         } else if (!CollectionUtils.isEmpty(limits)) {
103             String limitString = limits.get(0);
104             int limit = Integer.parseInt(limitString);
105             final Pageable pageableRequest = new PageRequest(0, limit);
106             query.with(pageableRequest);
107         }
108     }
109
110     private Date convertDate(String dateString) {
111         String dateFormat = "yyyy-MM-dd HH:mm:ss.SSS";
112         SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
113         try {
114             return formatter.parse(dateString);
115         } catch (ParseException e) {
116             LOGGER.error("unable to convert date " + dateString + ", the pattern is " + dateFormat + " ; " + e);
117         }
118         return null;
119
120     }
121
122 }