3e245e123d317e7fdbd10f96eea07073799cdf3a
[integration.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Simulator
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.pnfsimulator.template;
22
23 import java.util.List;
24 import java.util.Optional;
25
26 import com.google.gson.JsonObject;
27 import org.onap.pnfsimulator.db.Storage;
28 import org.onap.pnfsimulator.template.search.TemplateSearchHelper;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.context.annotation.Primary;
31 import org.springframework.stereotype.Service;
32
33 @Primary
34 @Service
35 public class TemplateService implements Storage<Template> {
36
37     private final TemplateRepository templateRepository;
38     private TemplateSearchHelper searchHelper;
39
40
41     @Autowired
42     public TemplateService(TemplateRepository templateRepository, TemplateSearchHelper searchHelper) {
43         this.templateRepository = templateRepository;
44         this.searchHelper = searchHelper;
45     }
46
47     @Override
48     public List<Template> getAll() {
49         return templateRepository.findAll();
50     }
51
52     @Override
53     public Optional<Template> get(String name) {
54         return templateRepository.findById(name);
55     }
56
57     @Override
58     public void persist(Template template) {
59         templateRepository.save(template);
60     }
61
62     @Override
63     public boolean tryPersistOrOverwrite(Template template, boolean overwrite) {
64         if (templateRepository.existsById(template.getId()) && !overwrite) {
65             return false;
66         }
67         templateRepository.save(template);
68         return true;
69     }
70
71     @Override
72     public void delete(String templateName) {
73         templateRepository.deleteById(templateName);
74     }
75
76     @Override
77     public List<String> getIdsByContentCriteria(JsonObject stringQueryJson) {
78         return searchHelper.getIdsOfDocumentMatchingCriteria(stringQueryJson);
79     }
80
81 }