31f08396bef49dcddfe7cfb3113984960cf5c480
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / hub / service / CriteriaBuilderServiceOrder.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.hub.service;
17
18 import com.fasterxml.jackson.databind.JsonNode;
19 import org.onap.nbi.apis.hub.model.Event;
20 import org.springframework.data.mongodb.core.query.Criteria;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 public class CriteriaBuilderServiceOrder implements CriteriaBuilder {
26     @Override
27     public Criteria adjust(Criteria base, Event event) {
28         switch (event.getEventType()) {
29             case "ServiceOrderCreationNotification":
30                 return base;
31
32             case "ServiceOrderStateChangeNotification":
33                 JsonNode stateNode = event.getEvent().path("state");
34                 if (stateNode.isValueNode())
35                     return base.orOperator(
36                             Criteria.where("query.serviceOrder__state").exists(false),
37                             Criteria.where("query.serviceOrder__state").in(event.getEvent().path("state").textValue())
38                     );
39                 else
40                     return base.and("query.serviceOrder__state").exists(false);
41
42             case "ServiceOrderItemStateChangeNotification":
43                 Object[] states = getStates(event);
44                 if (states.length > 0)
45                     return base.orOperator(
46                             Criteria.where("query.serviceOrder__serviceOrderItem__state").exists(false),
47                             Criteria.where("query.serviceOrder__serviceOrderItem__state").in(states)
48                     );
49                 else
50                     return base.and("query.serviceOrder__serviceOrderItem__state").exists(false);
51         }
52
53         return base;
54     }
55
56     private String[] getStates(Event event) {
57         List<String> states = new ArrayList<>();
58
59         JsonNode orderItems = event.getEvent().path("orderItem");
60         if (orderItems.isArray()) {
61             for (JsonNode node : orderItems) {
62                 JsonNode stateNode = node.path("state");
63                 if (stateNode.isValueNode()) {
64                     states.add(stateNode.textValue());
65                 }
66             }
67         }
68
69         return states.toArray(new String[0]);
70     }
71 }