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