3507d63a4a10c9359ff621742d1f9b396c02a5c7
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / hub / service / SubscriptionService.java
1 /**
2  * Copyright (c) 2018 Orange
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11  * specific language governing permissions and limitations under the License.
12  */
13 package org.onap.nbi.apis.hub.service;
14
15 import com.google.common.collect.Lists;
16 import java.text.MessageFormat;
17 import java.util.List;
18 import java.util.Optional;
19 import org.onap.nbi.apis.hub.model.Subscriber;
20 import org.onap.nbi.apis.hub.model.Subscription;
21 import org.onap.nbi.apis.hub.repository.SubscriberRepository;
22 import org.onap.nbi.exceptions.ValidationException;
23 import org.springframework.beans.factory.annotation.Autowired;
24 import org.springframework.data.domain.Example;
25 import org.springframework.stereotype.Service;
26 import org.springframework.validation.ObjectError;
27
28 @Service
29 public class SubscriptionService {
30
31     @Autowired
32     SubscriberRepository subscriberRepository;
33
34     public Optional<Subscriber> findSubscriptionById(String subscriptionId) {
35         return subscriberRepository.findById(subscriptionId);
36     }
37
38     public Subscriber createSubscription(Subscription subscription) {
39         subscription.setId(null);
40         Subscriber subscriber = Subscriber.createFromSubscription(subscription);
41         if (isSubscriberAlreadyExisting(subscriber)) {
42             String message = MessageFormat
43                 .format("subscription with callback {0} and query {1} already exists", subscription.getCallback(),
44                     subscription.getQuery());
45             ObjectError error = new ObjectError("subscription", message);
46             List<ObjectError> errors = Lists.newArrayList(error);
47             throw new ValidationException(errors);
48         } else {
49             return subscriberRepository.save(subscriber);
50         }
51     }
52
53     private boolean isSubscriberAlreadyExisting(Subscriber subscriber) {
54         Example<Subscriber> subscriberExample = Example.of(subscriber);
55         Optional<Subscriber> subscriberAlreadyExisting = subscriberRepository.findOne(subscriberExample);
56         return subscriberAlreadyExisting.isPresent();
57     }
58
59     public void deleteSubscription(String subscriptionId) {
60         subscriberRepository.deleteById(subscriptionId);
61     }
62
63     public void deleteAll() {
64         subscriberRepository.deleteAll();
65     }
66
67     public long countSubscription() {
68         return subscriberRepository.count();
69     }
70
71 }