Enable spotbugs and fix spotbugs warns
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / impl / CpsAdminPersistenceServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation. All rights reserved.
4  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.spi.impl;
23
24 import java.util.Collection;
25 import java.util.stream.Collectors;
26 import org.onap.cps.spi.CpsAdminPersistenceService;
27 import org.onap.cps.spi.entities.Dataspace;
28 import org.onap.cps.spi.entities.Fragment;
29 import org.onap.cps.spi.entities.SchemaSet;
30 import org.onap.cps.spi.exceptions.AnchorAlreadyDefinedException;
31 import org.onap.cps.spi.exceptions.DataspaceAlreadyDefinedException;
32 import org.onap.cps.spi.model.Anchor;
33 import org.onap.cps.spi.repository.DataspaceRepository;
34 import org.onap.cps.spi.repository.FragmentRepository;
35 import org.onap.cps.spi.repository.SchemaSetRepository;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.dao.DataIntegrityViolationException;
38 import org.springframework.stereotype.Component;
39
40 @Component
41 public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceService {
42
43     @Autowired
44     private DataspaceRepository dataspaceRepository;
45
46     @Autowired
47     private FragmentRepository fragmentRepository;
48
49     @Autowired
50     private SchemaSetRepository schemaSetRepository;
51
52     @Override
53     public void createDataspace(final String dataspaceName) {
54         try {
55             dataspaceRepository.save(new Dataspace(dataspaceName));
56         } catch (final DataIntegrityViolationException e) {
57             throw new DataspaceAlreadyDefinedException(dataspaceName, e);
58         }
59     }
60
61     @Override
62     public void createAnchor(final String dataspaceName, final String schemaSetName, final String anchorName) {
63         final Dataspace dataspace = dataspaceRepository.getByName(dataspaceName);
64         final SchemaSet schemaSet = schemaSetRepository.getByDataspaceAndName(dataspace, schemaSetName);
65         final Fragment anchor = Fragment.builder()
66             .xpath(anchorName)
67             .anchorName(anchorName)
68             .dataspace(dataspace)
69             .schemaSet(schemaSet)
70             .build();
71         try {
72             fragmentRepository.save(anchor);
73         } catch (final DataIntegrityViolationException e) {
74             throw new AnchorAlreadyDefinedException(dataspaceName, anchorName, e);
75         }
76     }
77
78     @Override
79     public Collection<Anchor> getAnchors(final String dataspaceName) {
80         final Dataspace dataspace = dataspaceRepository.getByName(dataspaceName);
81         final Collection<Fragment> fragments = fragmentRepository.findFragmentsThatAreAnchorsByDataspace(dataspace);
82         return fragments.stream().map(
83             entity -> Anchor.builder()
84                 .name(entity.getAnchorName())
85                 .dataspaceName(dataspaceName)
86                 .schemaSetName(entity.getSchemaSet().getName())
87                 .build()
88         ).collect(Collectors.toList());
89     }
90 }