Internal Server Error when creating the same data node twice
[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.AnchorEntity;
28 import org.onap.cps.spi.entities.DataspaceEntity;
29 import org.onap.cps.spi.entities.SchemaSetEntity;
30 import org.onap.cps.spi.exceptions.AlreadyDefinedException;
31 import org.onap.cps.spi.model.Anchor;
32 import org.onap.cps.spi.repository.AnchorRepository;
33 import org.onap.cps.spi.repository.DataspaceRepository;
34 import org.onap.cps.spi.repository.SchemaSetRepository;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.dao.DataIntegrityViolationException;
37 import org.springframework.stereotype.Component;
38
39 @Component
40 public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceService {
41
42     @Autowired
43     private DataspaceRepository dataspaceRepository;
44
45     @Autowired
46     private AnchorRepository anchorRepository;
47
48     @Autowired
49     private SchemaSetRepository schemaSetRepository;
50
51     @Override
52     public void createDataspace(final String dataspaceName) {
53         try {
54             dataspaceRepository.save(new DataspaceEntity(dataspaceName));
55         } catch (final DataIntegrityViolationException e) {
56             throw AlreadyDefinedException.forDataspace(dataspaceName, e);
57         }
58     }
59
60     @Override
61     public void createAnchor(final String dataspaceName, final String schemaSetName, final String anchorName) {
62         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
63         final SchemaSetEntity schemaSetEntity =
64             schemaSetRepository.getByDataspaceAndName(dataspaceEntity, schemaSetName);
65         final AnchorEntity anchorEntity = AnchorEntity.builder()
66             .name(anchorName)
67             .dataspace(dataspaceEntity)
68             .schemaSet(schemaSetEntity)
69             .build();
70         try {
71             anchorRepository.save(anchorEntity);
72         } catch (final DataIntegrityViolationException e) {
73             throw AlreadyDefinedException.forAnchor(anchorName, dataspaceName, e);
74         }
75     }
76
77     @Override
78     public Collection<Anchor> getAnchors(final String dataspaceName) {
79         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
80         final Collection<AnchorEntity> anchorEntities = anchorRepository.findAllByDataspace(dataspaceEntity);
81         return anchorEntities.stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toList());
82     }
83
84     @Override
85     public Anchor getAnchor(final String dataspaceName, final String anchorName) {
86         final DataspaceEntity dataspaceEntity = dataspaceRepository.getByName(dataspaceName);
87         final AnchorEntity anchorEntity =
88             anchorRepository.getByDataspaceAndName(dataspaceEntity, anchorName);
89         return toAnchor(anchorEntity);
90     }
91
92     private static Anchor toAnchor(final AnchorEntity anchorEntity) {
93         return Anchor.builder()
94             .name(anchorEntity.getName())
95             .dataspaceName(anchorEntity.getDataspace().getName())
96             .schemaSetName(anchorEntity.getSchemaSet().getName())
97             .build();
98     }
99 }