package org.onap.policy.models.base;
+import com.google.re2j.Pattern;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.NavigableMap;
import java.util.Set;
import java.util.TreeMap;
-
+import java.util.TreeSet;
+import java.util.function.Function;
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;
-
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
-
+import org.apache.commons.lang3.StringUtils;
import org.onap.policy.models.base.PfValidationResult.ValidationResult;
// @formatter:off
*/
//@formatter:on
@MappedSuperclass
-@Entity
@Table(name = "PfConceptContainer")
@Data
@EqualsAndHashCode(callSuper = false)
implements PfConceptGetter<C>, PfAuthorative<List<Map<String, A>>> {
private static final long serialVersionUID = -324211738823208318L;
+ private static final Pattern KEY_ID_PATTERN = Pattern.compile(PfKey.KEY_ID_REGEXP);
+
@EmbeddedId
private PfConceptKey key;
* @param key the concept key
*/
public PfConceptContainer(@NonNull final PfConceptKey key) {
- this(key, new TreeMap<PfConceptKey, C>());
+ this(key, new TreeMap<>());
}
/**
public List<Map<String, A>> toAuthorative() {
// The returned list is a list of map singletons with one map for each map
// entry in the concept container
- List<Map<String, A>> toscaPolicyMapList = new ArrayList<>();
+ List<Map<String, A>> toscaConceptMapList = new ArrayList<>();
for (Entry<PfConceptKey, C> conceptEntry : getConceptMap().entrySet()) {
// Create a map to hold this entry
toscaPolicyMap.put(conceptEntry.getKey().getName(), authoritiveImpl.toAuthorative());
// Add the map to the returned list
- toscaPolicyMapList.add(toscaPolicyMap);
+ toscaConceptMapList.add(toscaPolicyMap);
}
- return toscaPolicyMapList;
+ return toscaConceptMapList;
}
@Override
for (Map<String, A> incomingConceptMap : authorativeList) {
// Add the map entries one by one
for (Entry<String, A> incomingConceptEntry : incomingConceptMap.entrySet()) {
- C jpaConcept = getConceptNewInstance();
+ PfConceptKey conceptKey = new PfConceptKey();
+ if (KEY_ID_PATTERN.matches(incomingConceptEntry.getKey())) {
+ conceptKey = new PfConceptKey(incomingConceptEntry.getKey());
+ } else {
+ conceptKey.setName(incomingConceptEntry.getKey());
+ if (incomingConceptEntry.getValue().getVersion() != null) {
+ conceptKey.setVersion(incomingConceptEntry.getValue().getVersion());
+ } else {
+ conceptKey.setVersion(PfKey.NULL_KEY_VERSION);
+ }
+ }
+
+ incomingConceptEntry.getValue().setName(findConceptField(conceptKey, conceptKey.getName(),
+ incomingConceptEntry.getValue(), PfNameVersion::getName));
+ incomingConceptEntry.getValue().setVersion(findConceptField(conceptKey, conceptKey.getVersion(),
+ incomingConceptEntry.getValue(), PfNameVersion::getVersion));
+
+ C jpaConcept = getConceptNewInstance();
// This cast allows us to call the fromAuthorative method
@SuppressWarnings("unchecked")
PfAuthorative<A> authoritiveImpl = (PfAuthorative<A>) jpaConcept;
- if (incomingConceptEntry.getValue().getName() == null) {
- incomingConceptEntry.getValue().setName(incomingConceptEntry.getKey());
- }
-
// Set the key name and the rest of the values on the concept
authoritiveImpl.fromAuthorative(incomingConceptEntry.getValue());
- // This cast gets the key of the concept
- PfConceptKey conceptKey = (PfConceptKey) jpaConcept.getKey();
-
- // Set the concept key of the concept
- conceptKey.setName(incomingConceptEntry.getValue().getName());
-
- if (incomingConceptEntry.getValue().getVersion() != null) {
- conceptKey.setVersion(incomingConceptEntry.getValue().getVersion());
- } else {
- conceptKey.setVersion(PfKey.NULL_KEY_VERSION);
- }
-
// After all that, save the map entry
conceptMap.put(conceptKey, jpaConcept);
}
}
}
+ /**
+ * Get an authorative list of the concepts in this container.
+ *
+ * @return the authorative list of concepts
+ */
+ public List<A> toAuthorativeList() {
+ List<A> toscaConceptList = new ArrayList<>();
+
+ for (Map<String, A> toscaConceptMap : toAuthorative()) {
+ toscaConceptList.addAll(toscaConceptMap.values());
+ }
+
+ return toscaConceptList;
+ }
+
@Override
public void clean() {
key.clean();
return retVal;
}
- if (!conceptMap.equals(other.conceptMap)) {
- return (conceptMap.hashCode() - other.conceptMap.hashCode());
- }
+ return PfUtils.compareMaps(conceptMap, other.conceptMap);
+ }
- return 0;
+ /**
+ * Get all the concepts that match the given name and version.
+ *
+ * @param conceptKeyName the name of the concept, if null, return all names
+ * @param conceptKeyVersion the version of the concept, if null, return all versions
+ * @return conceptKeyVersion
+ */
+ public Set<C> getAllNamesAndVersions(final String conceptKeyName, final String conceptKeyVersion) {
+ if (conceptKeyName == null || conceptKeyVersion == null || PfKey.NULL_KEY_VERSION.equals(conceptKeyVersion)) {
+ return getAll(conceptKeyName, conceptKeyVersion);
+ } else {
+ final Set<C> returnSet = new TreeSet<>();
+ C foundConcept = get(conceptKeyName, conceptKeyVersion);
+ if (foundConcept != null) {
+ returnSet.add(foundConcept);
+ }
+ return returnSet;
+ }
}
@Override
public C get(final PfConceptKey conceptKey) {
- return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).get(conceptKey);
+ if (conceptKey.isNullVersion()) {
+ return get(conceptKey.getName());
+ } else {
+ return new PfConceptGetterImpl<>((NavigableMap<PfConceptKey, C>) conceptMap).get(conceptKey);
+ }
}
@Override
"failed to instantiate instance of container concept class", ex);
}
}
+
+ private String findConceptField(final PfConceptKey conceptKey, final String keyFieldValue,
+ final PfNameVersion concept, final Function<PfNameVersion, String> fieldGetterFunction) {
+
+ String conceptField = fieldGetterFunction.apply(concept);
+
+ if (StringUtils.isBlank(conceptField) || keyFieldValue.equals(conceptField)) {
+ return keyFieldValue;
+ } else {
+ throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, "Key " + conceptKey.getId() + " field "
+ + keyFieldValue + " does not match the value " + conceptField + " in the concept field");
+ }
+ }
}