diff options
author | Ruslan Kashapov <ruslan.kashapov@pantheon.tech> | 2021-01-11 16:44:32 +0200 |
---|---|---|
committer | Rishi Chail <rishi.chail@est.tech> | 2021-01-13 10:18:31 +0000 |
commit | 49fe075361e4f0f3b2a1a561bc01f831983a3efb (patch) | |
tree | a4cd3e36428f0d3d922e01c22700344088dd4bd2 /cps-ri/src/main/java | |
parent | a6c59f024092ac339d851a372aa6deae36de622d (diff) |
Decouple anchor from fragment in persistence module
Issue-ID: CPS-161
Change-Id: Ia446b26ee4eca9281e86bd2be3dd6836aa201597
Signed-off-by: Ruslan Kashapov <ruslan.kashapov@pantheon.tech>
Diffstat (limited to 'cps-ri/src/main/java')
5 files changed, 122 insertions, 36 deletions
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/AnchorEntity.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/AnchorEntity.java new file mode 100644 index 0000000000..e7e9c971dd --- /dev/null +++ b/cps-ri/src/main/java/org/onap/cps/spi/entities/AnchorEntity.java @@ -0,0 +1,70 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Pantheon.tech + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.spi.entities; + +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; +import javax.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +/** + * Entity to store an anchor. + */ +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@Builder +@Entity +@Table(name = "anchor") +public class AnchorEntity implements Serializable { + + private static final long serialVersionUID = -8049987915308262518L; + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + @NotNull + @Column + private String name; + + @NotNull + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "schema_set_id") + private SchemaSet schemaSet; + + @NotNull + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "dataspace_id") + private Dataspace dataspace; +} diff --git a/cps-ri/src/main/java/org/onap/cps/spi/entities/Fragment.java b/cps-ri/src/main/java/org/onap/cps/spi/entities/Fragment.java index 2dbd6e98fe..053a223219 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/entities/Fragment.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/entities/Fragment.java @@ -46,10 +46,10 @@ import org.hibernate.annotations.TypeDefs; */
@Getter
@Setter
-@Entity
@AllArgsConstructor
@NoArgsConstructor
@Builder
+@Entity
@TypeDefs({@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)})
public class Fragment implements Serializable {
@@ -67,9 +67,6 @@ public class Fragment implements Serializable { @Column(columnDefinition = "jsonb")
private String attributes;
- @Column(columnDefinition = "text")
- private String anchorName;
-
@NotNull
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "dataspace_id")
@@ -77,13 +74,9 @@ public class Fragment implements Serializable { @OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "anchor_id")
- private Fragment anchorFragment;
+ private AnchorEntity anchor;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
private Fragment parentFragment;
-
- @OneToOne(fetch = FetchType.LAZY)
- @JoinColumn(name = "schema_set_id")
- private SchemaSet schemaSet;
}
diff --git a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java index d6579bdbb5..fdb446c849 100644 --- a/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/impl/CpsAdminPersistenceServiceImpl.java @@ -24,14 +24,14 @@ package org.onap.cps.spi.impl; import java.util.Collection; import java.util.stream.Collectors; import org.onap.cps.spi.CpsAdminPersistenceService; +import org.onap.cps.spi.entities.AnchorEntity; import org.onap.cps.spi.entities.Dataspace; -import org.onap.cps.spi.entities.Fragment; import org.onap.cps.spi.entities.SchemaSet; import org.onap.cps.spi.exceptions.AnchorAlreadyDefinedException; import org.onap.cps.spi.exceptions.DataspaceAlreadyDefinedException; import org.onap.cps.spi.model.Anchor; +import org.onap.cps.spi.repository.AnchorRepository; import org.onap.cps.spi.repository.DataspaceRepository; -import org.onap.cps.spi.repository.FragmentRepository; import org.onap.cps.spi.repository.SchemaSetRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; @@ -44,7 +44,7 @@ public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceServic private DataspaceRepository dataspaceRepository; @Autowired - private FragmentRepository fragmentRepository; + private AnchorRepository anchorRepository; @Autowired private SchemaSetRepository schemaSetRepository; @@ -62,14 +62,13 @@ public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceServic public void createAnchor(final String dataspaceName, final String schemaSetName, final String anchorName) { final Dataspace dataspace = dataspaceRepository.getByName(dataspaceName); final SchemaSet schemaSet = schemaSetRepository.getByDataspaceAndName(dataspace, schemaSetName); - final Fragment anchor = Fragment.builder() - .xpath(anchorName) - .anchorName(anchorName) + final AnchorEntity anchorEntity = AnchorEntity.builder() + .name(anchorName) .dataspace(dataspace) .schemaSet(schemaSet) .build(); try { - fragmentRepository.save(anchor); + anchorRepository.save(anchorEntity); } catch (final DataIntegrityViolationException e) { throw new AnchorAlreadyDefinedException(dataspaceName, anchorName, e); } @@ -78,13 +77,15 @@ public class CpsAdminPersistenceServiceImpl implements CpsAdminPersistenceServic @Override public Collection<Anchor> getAnchors(final String dataspaceName) { final Dataspace dataspace = dataspaceRepository.getByName(dataspaceName); - final Collection<Fragment> fragments = fragmentRepository.findFragmentsThatAreAnchorsByDataspace(dataspace); - return fragments.stream().map( - entity -> Anchor.builder() - .name(entity.getAnchorName()) - .dataspaceName(dataspaceName) - .schemaSetName(entity.getSchemaSet().getName()) - .build() - ).collect(Collectors.toList()); + final Collection<AnchorEntity> anchorEntities = anchorRepository.findAllByDataspace(dataspace); + return anchorEntities.stream().map(CpsAdminPersistenceServiceImpl::toAnchor).collect(Collectors.toList()); + } + + private static Anchor toAnchor(final AnchorEntity anchorEntity) { + return Anchor.builder() + .name(anchorEntity.getName()) + .dataspaceName(anchorEntity.getDataspace().getName()) + .schemaSetName(anchorEntity.getSchemaSet().getName()) + .build(); } } diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java new file mode 100644 index 0000000000..df665f931c --- /dev/null +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/AnchorRepository.java @@ -0,0 +1,33 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Pantheon.tech + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.spi.repository; + +import java.util.Collection; +import java.util.Optional; +import javax.validation.constraints.NotNull; +import org.onap.cps.spi.entities.AnchorEntity; +import org.onap.cps.spi.entities.Dataspace; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface AnchorRepository extends JpaRepository<AnchorEntity, Integer> { + Optional<AnchorEntity> findByDataspaceAndName(@NotNull Dataspace dataspace, @NotNull String name); + + Collection<AnchorEntity> findAllByDataspace(@NotNull Dataspace dataspace); +} diff --git a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java index 2ee76cc126..4521d091df 100755 --- a/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java +++ b/cps-ri/src/main/java/org/onap/cps/spi/repository/FragmentRepository.java @@ -21,22 +21,11 @@ package org.onap.cps.spi.repository;
-import java.util.Collection;
-import java.util.Optional;
-import javax.validation.constraints.NotNull;
-import org.onap.cps.spi.entities.Dataspace;
import org.onap.cps.spi.entities.Fragment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
-public interface FragmentRepository extends JpaRepository<Fragment, Integer> {
+public interface FragmentRepository extends JpaRepository<Fragment, Long> {
- Optional<Fragment> findByDataspaceAndAnchorName(@NotNull Dataspace dataspace, @NotNull String anchorName);
-
- default Collection<Fragment> findFragmentsThatAreAnchorsByDataspace(Dataspace dataspace) {
- return findFragmentsByDataspaceAndParentFragmentIsNull(dataspace);
- }
-
- Collection<Fragment> findFragmentsByDataspaceAndParentFragmentIsNull(Dataspace dataspace);
}
\ No newline at end of file |