X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-base%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fmodels%2Fbase%2FPfConceptContainer.java;h=949cb96dc8bf27c1811e05faf9644dba51a75790;hb=70dbf7a387b0e5d00f50a954751c4d4b4e54d7e2;hp=65a28cffe9f0d57baa4421aa3501cf0c1dc45dcd;hpb=52229882d7ee3e934641de0bd2df74ed1268130e;p=policy%2Fmodels.git diff --git a/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java b/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java index 65a28cffe..949cb96dc 100644 --- a/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java +++ b/models-base/src/main/java/org/onap/policy/models/base/PfConceptContainer.java @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. + * Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,7 +34,10 @@ import java.util.TreeMap; import javax.persistence.CascadeType; import javax.persistence.EmbeddedId; import javax.persistence.Entity; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; import javax.persistence.ManyToMany; +import javax.persistence.MappedSuperclass; import javax.persistence.Table; import javax.ws.rs.core.Response; @@ -41,7 +45,6 @@ import lombok.Data; import lombok.EqualsAndHashCode; import lombok.NonNull; -import org.onap.policy.common.utils.validation.Assertions; import org.onap.policy.models.base.PfValidationResult.ValidationResult; // @formatter:off @@ -57,6 +60,7 @@ import org.onap.policy.models.base.PfValidationResult.ValidationResult; * @param C the concept being contained */ //@formatter:on +@MappedSuperclass @Entity @Table(name = "PfConceptContainer") @Data @@ -70,6 +74,18 @@ public class PfConceptContainer ex private PfConceptKey key; @ManyToMany(cascade = CascadeType.ALL) + // @formatter:off + @JoinTable( + joinColumns = { + @JoinColumn(name = "conceptContainerMapName", referencedColumnName = "name"), + @JoinColumn(name = "concpetContainerMapVersion", referencedColumnName = "version") + }, + inverseJoinColumns = { + @JoinColumn(name = "conceptContainerName", referencedColumnName = "name"), + @JoinColumn(name = "conceptContainerVersion", referencedColumnName = "version") + } + ) + // @formatter:on private Map conceptMap; /** @@ -110,6 +126,14 @@ public class PfConceptContainer ex */ public PfConceptContainer(@NonNull final PfConceptContainer copyConcept) { super(copyConcept); + this.key = new PfConceptKey(copyConcept.key); + + this.conceptMap = new TreeMap<>(); + for (final Entry conceptMapEntry : copyConcept.conceptMap.entrySet()) { + PfConceptKey newK = new PfConceptKey(conceptMapEntry.getKey()); + C newC = PfUtils.makeCopy(conceptMapEntry.getValue()); + this.conceptMap.put(newK, newC); + } } @Override @@ -125,16 +149,22 @@ public class PfConceptContainer ex @Override public List> toAuthorative() { - Map toscaPolicyMap = new LinkedHashMap<>(); + // The returned list is a list of map singletons with one map for each map + // entry in the concept container + List> toscaPolicyMapList = new ArrayList<>(); for (Entry conceptEntry : getConceptMap().entrySet()) { + // Create a map to hold this entry + Map toscaPolicyMap = new LinkedHashMap<>(1); + + // Add the concept container entry to the singleton map @SuppressWarnings("unchecked") PfAuthorative authoritiveImpl = (PfAuthorative) conceptEntry.getValue(); toscaPolicyMap.put(conceptEntry.getKey().getName(), authoritiveImpl.toAuthorative()); - } - List> toscaPolicyMapList = new ArrayList<>(); - toscaPolicyMapList.add(toscaPolicyMap); + // Add the map to the returned list + toscaPolicyMapList.add(toscaPolicyMap); + } return toscaPolicyMapList; } @@ -169,8 +199,7 @@ public class PfConceptContainer ex if (incomingConceptEntry.getValue().getVersion() != null) { conceptKey.setVersion(incomingConceptEntry.getValue().getVersion()); - } - else { + } else { conceptKey.setVersion(PfKey.NULL_KEY_VERSION); } @@ -205,10 +234,7 @@ public class PfConceptContainer ex result = key.validate(result); - if (conceptMap.isEmpty()) { - result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "conceptMap may not be empty")); - } else { + if (!conceptMap.isEmpty()) { result = validateConceptMap(result); } @@ -228,19 +254,17 @@ public class PfConceptContainer ex if (conceptEntry.getKey().equals(PfConceptKey.getNullKey())) { result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, "key on concept entry " + conceptEntry.getKey() + " may not be the null key")); - } else - if (conceptEntry.getValue() == null) { - result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, - "value on concept entry " + conceptEntry.getKey() + " may not be null")); - } else - if (!conceptEntry.getKey().equals(conceptEntry.getValue().getKey())) { - result.addValidationMessage(new PfValidationMessage(key, this.getClass(), - ValidationResult.INVALID, "key on concept entry key " + conceptEntry.getKey() - + " does not equal concept value key " + conceptEntry.getValue().getKey())); - result = conceptEntry.getValue().validate(result); - } else { - result = conceptEntry.getValue().validate(result); - } + } else if (conceptEntry.getValue() == null) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "value on concept entry " + conceptEntry.getKey() + " may not be null")); + } else if (!conceptEntry.getKey().equals(conceptEntry.getValue().getKey())) { + result.addValidationMessage(new PfValidationMessage(key, this.getClass(), ValidationResult.INVALID, + "key on concept entry key " + conceptEntry.getKey() + " does not equal concept value key " + + conceptEntry.getValue().getKey())); + result = conceptEntry.getValue().validate(result); + } else { + result = conceptEntry.getValue().validate(result); + } } return result; } @@ -254,7 +278,7 @@ public class PfConceptContainer ex return 0; } if (getClass() != otherConcept.getClass()) { - return this.hashCode() - otherConcept.hashCode(); + return getClass().getName().compareTo(otherConcept.getClass().getName()); } @SuppressWarnings("unchecked") @@ -271,24 +295,6 @@ public class PfConceptContainer ex return 0; } - @Override - public PfConcept copyTo(@NonNull final PfConcept target) { - Assertions.instanceOf(target, PfConceptContainer.class); - - @SuppressWarnings("unchecked") - final PfConceptContainer copy = (PfConceptContainer) target; - copy.setKey(new PfConceptKey(key)); - final Map newConceptMap = new TreeMap<>(); - for (final Entry conceptMapEntry : conceptMap.entrySet()) { - C newC = getConceptNewInstance(); - conceptMapEntry.getValue().copyTo(newC); - newConceptMap.put(new PfConceptKey(conceptMapEntry.getKey()), newC); - } - copy.setConceptMap(newConceptMap); - - return copy; - } - @Override public C get(final PfConceptKey conceptKey) { return new PfConceptGetterImpl<>((NavigableMap) conceptMap).get(conceptKey); @@ -326,7 +332,7 @@ public class PfConceptContainer ex try { String conceptClassName = ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0].getTypeName(); - return (C) Class.forName(conceptClassName).newInstance(); + return (C) Class.forName(conceptClassName).getDeclaredConstructor().newInstance(); } catch (Exception ex) { throw new PfModelRuntimeException(Response.Status.INTERNAL_SERVER_ERROR, "failed to instantiate instance of container concept class", ex);