diff options
Diffstat (limited to 'model/basic-model/src/main')
11 files changed, 3 insertions, 1174 deletions
diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyInfo.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyInfo.java index 7e92faddf..2e788a3d5 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyInfo.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyInfo.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2021 Nordix Foundation. + * Modifications Copyright (C) 2019-2022 Nordix Foundation. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -26,7 +26,6 @@ import java.util.List; import java.util.Random; import java.util.UUID; import javax.persistence.Column; -import javax.persistence.Convert; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.Table; @@ -35,11 +34,8 @@ import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; -import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import org.apache.commons.lang3.StringUtils; import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult; -import org.onap.policy.apex.model.basicmodel.dao.converters.CDataConditioner; -import org.onap.policy.apex.model.basicmodel.dao.converters.Uuid2String; import org.onap.policy.common.utils.validation.Assertions; /** @@ -74,14 +70,10 @@ public class AxKeyInfo extends AxConcept { private AxArtifactKey key; @Column(name = "uuid") - @Convert(converter = Uuid2String.class) - @XmlJavaTypeAdapter(value = Uuid2String.class) @XmlElement(name = "UUID", required = true) private UUID uuid; @Column(name = "description", length = MAX_DESCRIPTION_LENGTH_8192) - @Convert(converter = CDataConditioner.class) - @XmlJavaTypeAdapter(value = CDataConditioner.class) @XmlElement(required = true) private String description; @@ -299,9 +291,7 @@ public class AxKeyInfo extends AxConcept { if (!uuid.equals(other.uuid)) { return false; } - final String thisdesc = CDataConditioner.clean(description); - final String otherdesc = CDataConditioner.clean(other.description); - return thisdesc.equals(otherdesc); + return description.equals(description); } /** diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/ApexDao.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/ApexDao.java deleted file mode 100644 index b84646038..000000000 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/ApexDao.java +++ /dev/null @@ -1,206 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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. - * 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.policy.apex.model.basicmodel.dao; - -import java.util.Collection; -import java.util.List; -import org.onap.policy.apex.model.basicmodel.concepts.ApexException; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxConcept; -import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; - -/** - * The Interface ApexDao describes the DAO interface for reading and writing Apex {@link AxConcept} concepts to and from - * databases using JDBC. - * - * @author Sergey Sachkov - * @author liam.fallon@ericsson.com - */ -public interface ApexDao { - - /** - * Initialize the Apex DAO with the given parameters. - * - * @param daoParameters parameters to use to access the database - * @throws ApexException on initialization errors - */ - void init(DaoParameters daoParameters) throws ApexException; - - /** - * Close the Apex DAO. - */ - void close(); - - /** - * Creates an Apex concept on the database. - * - * @param <T> the type of the object to create, a subclass of {@link AxConcept} - * @param obj the object to create - */ - <T extends AxConcept> void create(T obj); - - /** - * Delete an Apex concept on the database. - * - * @param <T> the type of the object to delete, a subclass of {@link AxConcept} - * @param obj the object to delete - */ - <T extends AxConcept> void delete(T obj); - - /** - * Delete an Apex concept on the database. - * - * @param <T> the type of the object to delete, a subclass of {@link AxConcept} - * @param someClass the class of the object to delete, a subclass of {@link AxConcept} - * @param key the key of the object to delete - */ - <T extends AxConcept> void delete(Class<T> someClass, AxArtifactKey key); - - /** - * Delete an Apex concept on the database. - * - * @param <T> the type of the object to delete, a subclass of {@link AxConcept} - * @param someClass the class of the object to delete, a subclass of {@link AxConcept} - * @param key the key of the object to delete - */ - <T extends AxConcept> void delete(Class<T> someClass, AxReferenceKey key); - - /** - * Create a collection of objects in the database. - * - * @param <T> the type of the object to create, a subclass of {@link AxConcept} - * @param objs the objects to create - */ - <T extends AxConcept> void createCollection(Collection<T> objs); - - /** - * Delete a collection of objects in the database. - * - * @param <T> the type of the objects to delete, a subclass of {@link AxConcept} - * @param objs the objects to delete - */ - <T extends AxConcept> void deleteCollection(Collection<T> objs); - - /** - * Delete a collection of objects in the database referred to by artifact key. - * - * @param <T> the type of the objects to delete, a subclass of {@link AxConcept} - * @param someClass the class of the objects to delete, a subclass of {@link AxConcept} - * @param keys the keys of the objects to delete - * @return the number of objects deleted - */ - <T extends AxConcept> int deleteByArtifactKey(Class<T> someClass, Collection<AxArtifactKey> keys); - - /** - * Delete a collection of objects in the database referred to by reference key. - * - * @param <T> the type of the objects to delete, a subclass of {@link AxConcept} - * @param someClass the class of the objects to delete, a subclass of {@link AxConcept} - * @param keys the keys of the objects to delete - * @return the number of objects deleted - */ - <T extends AxConcept> int deleteByReferenceKey(Class<T> someClass, Collection<AxReferenceKey> keys); - - /** - * Delete all objects of a given class in the database. - * - * @param <T> the type of the objects to delete, a subclass of {@link AxConcept} - * @param someClass the class of the objects to delete, a subclass of {@link AxConcept} - */ - <T extends AxConcept> void deleteAll(Class<T> someClass); - - /** - * Get an object from the database, referred to by artifact key. - * - * @param <T> the type of the object to get, a subclass of {@link AxConcept} - * @param someClass the class of the object to get, a subclass of {@link AxConcept} - * @param key the key of the object to get - * @return the object that was retrieved from the database - */ - <T extends AxConcept> T get(Class<T> someClass, AxArtifactKey key); - - /** - * Get an object from the database, referred to by reference key. - * - * @param <T> the type of the object to get, a subclass of {@link AxConcept} - * @param someClass the class of the object to get, a subclass of {@link AxConcept} - * @param key the key of the object to get - * @return the object that was retrieved from the database or null if the object was not retrieved - */ - <T extends AxConcept> T get(Class<T> someClass, AxReferenceKey key); - - /** - * Get all the objects in the database of a given type. - * - * @param <T> the type of the objects to get, a subclass of {@link AxConcept} - * @param someClass the class of the objects to get, a subclass of {@link AxConcept} - * @return the objects or null if no objects were retrieved - */ - <T extends AxConcept> List<T> getAll(Class<T> someClass); - - /** - * Get all the objects in the database of the given type with the given parent artifact key. - * - * @param <T> the type of the objects to get, a subclass of {@link AxConcept} - * @param someClass the class of the objects to get, a subclass of {@link AxConcept} - * @param parentKey the parent key of the concepts to get - * @return the all - */ - <T extends AxConcept> List<T> getAll(Class<T> someClass, AxArtifactKey parentKey); - - /** - * Get a concept from the database with the given artifact key. - * - * @param <T> the type of the object to get, a subclass of {@link AxConcept} - * @param someClass the class of the object to get, a subclass of {@link AxConcept} - * @param artifactId the artifact key of the concept to get - * @return the concept that matches the key or null if the concept is not retrieved - */ - <T extends AxConcept> T getArtifact(Class<T> someClass, AxArtifactKey artifactId); - - /** - * Get a concept from the database with the given reference key. - * - * @param <T> the type of the object to get, a subclass of {@link AxConcept} - * @param someClass the class of the object to get, a subclass of {@link AxConcept} - * @param artifactId the artifact key of the concept to get - * @return the concept that matches the key or null if the concept is not retrieved - */ - <T extends AxConcept> T getArtifact(Class<T> someClass, AxReferenceKey artifactId); - - /** - * Get the number of instances of a concept that exist in the database. - * - * @param <T> the type of the object to get, a subclass of {@link AxConcept} - * @param someClass the class of the object to get, a subclass of {@link AxConcept} - * @return the number of instances of the concept in the database - */ - <T extends AxConcept> long size(Class<T> someClass); - - /** - * Update a concept in the database. - * - * @param <T> the type of the object to get, a subclass of {@link AxConcept} - * @param obj the object to update - * @return the updated object - */ - <T extends AxConcept> T update(T obj); -} diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/ApexDaoFactory.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/ApexDaoFactory.java deleted file mode 100644 index fad3b08b9..000000000 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/ApexDaoFactory.java +++ /dev/null @@ -1,70 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2020 Nordix Foundation. - * ================================================================================ - * 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.policy.apex.model.basicmodel.dao; - -import org.onap.policy.apex.model.basicmodel.concepts.ApexException; -import org.onap.policy.common.utils.validation.Assertions; -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; - -/** - * This factory class returns an Apex DAO for the configured persistence mechanism. The factory uses the plugin class - * specified in {@link DaoParameters} to instantiate a DAO instance. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class ApexDaoFactory { - // Get a reference to the logger - private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexDaoFactory.class); - - /** - * Return an Apex DAO for the required APEX DAO plugin class. - * - * @param daoParameters parameters to use to read the database configuration information - * @return the Apex DAO - * @throws ApexException on invalid JPA plugins - */ - public ApexDao createApexDao(final DaoParameters daoParameters) throws ApexException { - Assertions.argumentOfClassNotNull(daoParameters, ApexException.class, - "Parameter \"daoParameters\" may not be null"); - - // Get the class for the DAO using reflection - Object apexDaoObject = null; - try { - apexDaoObject = Class.forName(daoParameters.getPluginClass()).getDeclaredConstructor().newInstance(); - } catch (final Exception e) { - LOGGER.error("Apex DAO class not found for DAO plugin \"" + daoParameters.getPluginClass() + "\"", e); - throw new ApexException( - "Apex DAO class not found for DAO plugin \"" + daoParameters.getPluginClass() + "\"", e); - } - - // Check the class is an Apex DAO - if (!(apexDaoObject instanceof ApexDao)) { - LOGGER.error("Specified Apex DAO plugin class \"" + daoParameters.getPluginClass() - + "\" does not implement the ApexDao interface"); - throw new ApexException("Specified Apex DAO plugin class \"" + daoParameters.getPluginClass() - + "\" does not implement the ApexDao interface"); - } - - return (ApexDao) apexDaoObject; - } -} diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/DaoParameters.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/DaoParameters.java deleted file mode 100644 index c575bd7c0..000000000 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/DaoParameters.java +++ /dev/null @@ -1,123 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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. - * 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.policy.apex.model.basicmodel.dao; - -import java.util.Properties; - -/** - * This class is a POJO that holds properties for Apex DAOs. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class DaoParameters { - /** The default Apex DAO plugin class. */ - public static final String DEFAULT_PLUGIN_CLASS = "org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao"; - - private String pluginClass = DEFAULT_PLUGIN_CLASS; - private String persistenceUnit; - - private Properties jdbcProperties = new Properties(); - - /** - * Gets the DAO plugin class, this is the DAO class to use and it must implement the {@link ApexDao} interface. - * - * @return the DAO plugin class - */ - public String getPluginClass() { - return pluginClass; - } - - /** - * Sets the DAO plugin class, a class that implements the {@link ApexDao} interface. - * - * @param daoPluginClass the DAO plugin class - */ - public void setPluginClass(final String daoPluginClass) { - pluginClass = daoPluginClass; - } - - /** - * Gets the persistence unit for the DAO. The persistence unit defines the JDBC properties the DAO will use. The - * persistence unit must defined in the {@code META-INF/persistence.xml} resource file - * - * @return the persistence unit to use for JDBC access - */ - public String getPersistenceUnit() { - return persistenceUnit; - } - - /** - * Sets the persistence unit for the DAO. The persistence unit defines the JDBC properties the DAO will use. The - * persistence unit must defined in the {@code META-INF/persistence.xml} resource file - * - * @param daoPersistenceUnit the persistence unit to use for JDBC access - */ - public void setPersistenceUnit(final String daoPersistenceUnit) { - persistenceUnit = daoPersistenceUnit; - } - - /** - * Gets the JDBC properties. - * - * @return the JDBC properties - */ - public Properties getJdbcProperties() { - return jdbcProperties; - } - - /** - * Sets the JDBC properties. - * - * @param jdbcProperties the JDBC properties - */ - public void setJdbcProperties(final Properties jdbcProperties) { - this.jdbcProperties = jdbcProperties; - } - - /** - * Gets a single JDBC property. - * - * @param key the key of the property - * @return the JDBC property - */ - public String getJdbcProperty(final String key) { - return jdbcProperties.getProperty(key); - } - - /** - * Sets a single JDBC property. - * - * @param key the key of the property - * @param value the value of the JDBC property - */ - public void setJdbcProperty(final String key, final String value) { - jdbcProperties.setProperty(key, value); - } - - /** - * {@inheritDoc}. - */ - @Override - public String toString() { - return "DAOParameters [pluginClass=" + pluginClass + ", persistenceUnit=" + persistenceUnit - + ", jdbcProperties=" + jdbcProperties + "]"; - } -} diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/converters/CDataConditioner.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/converters/CDataConditioner.java deleted file mode 100644 index 460714683..000000000 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/converters/CDataConditioner.java +++ /dev/null @@ -1,83 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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. - * 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.policy.apex.model.basicmodel.dao.converters; - -import javax.persistence.AttributeConverter; -import javax.persistence.Converter; -import javax.xml.bind.annotation.adapters.XmlAdapter; - -/** - * The Class CDATAConditioner converts a CDATA String to and from database format by removing spaces at the ends of - * lines and platform-specific new line endings. - * - * @author John Keeney (John.Keeney@ericsson.com) - */ -@Converter -public class CDataConditioner extends XmlAdapter<String, String> implements AttributeConverter<String, String> { - - private static final String NL = "\n"; - - /** - * {@inheritDoc}. - */ - @Override - public String convertToDatabaseColumn(final String raw) { - return clean(raw); - } - - /** - * {@inheritDoc}. - */ - @Override - public String convertToEntityAttribute(final String db) { - return clean(db); - } - - /** - * {@inheritDoc}. - */ - @Override - public String unmarshal(final String value) throws Exception { - return this.convertToEntityAttribute(value); - } - - /** - * {@inheritDoc}. - */ - @Override - public String marshal(final String value) throws Exception { - return this.convertToDatabaseColumn(value); - } - - /** - * Clean. - * - * @param in the in - * @return the string - */ - public static final String clean(final String in) { - if (in == null) { - return null; - } else { - return in.replaceAll("\\s+$", "").replaceAll("\\r?\\n", NL); - } - } -} diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/converters/Uuid2String.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/converters/Uuid2String.java deleted file mode 100644 index f5fffdb00..000000000 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/converters/Uuid2String.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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. - * 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.policy.apex.model.basicmodel.dao.converters; - -import java.util.UUID; -import javax.persistence.AttributeConverter; -import javax.persistence.Converter; -import javax.xml.bind.annotation.adapters.XmlAdapter; - -/** - * The Class UUIDConverter converts a UUID to and from database format. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -@Converter -public class Uuid2String extends XmlAdapter<String, UUID> implements AttributeConverter<UUID, String> { - - /** - * {@inheritDoc}. - */ - @Override - public String convertToDatabaseColumn(final UUID uuid) { - String returnString; - if (uuid == null) { - returnString = ""; - } else { - returnString = uuid.toString(); - } - return returnString; - } - - /** - * {@inheritDoc}. - */ - @Override - public UUID convertToEntityAttribute(final String uuidString) { - return UUID.fromString(uuidString); - } - - /** - * {@inheritDoc}. - */ - @Override - public UUID unmarshal(final String value) throws Exception { - return this.convertToEntityAttribute(value); - } - - /** - * {@inheritDoc}. - */ - @Override - public String marshal(final UUID value) throws Exception { - return this.convertToDatabaseColumn(value); - } -} diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/converters/package-info.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/converters/package-info.java deleted file mode 100644 index f1e9a3ba7..000000000 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/converters/package-info.java +++ /dev/null @@ -1,28 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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. - * 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========================================================= - */ - -/** - * Contains converters used by APEX EclipseLink marshaling and unmarshaling of - * {@link org.onap.policy.apex.model.basicmodel.concepts.AxConcept} instances to and from files and databases. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ - -package org.onap.policy.apex.model.basicmodel.dao.converters; diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/impl/DefaultApexDao.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/impl/DefaultApexDao.java deleted file mode 100644 index 738d15ed3..000000000 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/impl/DefaultApexDao.java +++ /dev/null @@ -1,497 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2020 Nordix Foundation. - * Modifications Copyright (C) 2021 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. - * 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.policy.apex.model.basicmodel.dao.impl; - -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.Persistence; -import org.onap.policy.apex.model.basicmodel.concepts.ApexException; -import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxConcept; -import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; -import org.onap.policy.apex.model.basicmodel.dao.ApexDao; -import org.onap.policy.apex.model.basicmodel.dao.DaoParameters; -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; - -/** - * The Class DefaultApexDao is an JPA implementation of the {@link ApexDao} class for Apex concepts ({@link AxConcept}). - * It uses the default JPA implementation in the javax {@link Persistence} class. - * - * - * @author Sergey Sachkov (sergey.sachkov@ericsson.com) - */ -public class DefaultApexDao implements ApexDao { - private static final XLogger LOGGER = XLoggerFactory.getXLogger(DefaultApexDao.class); - - private static final String COL_LOCALNAME = "local_name"; - private static final String COL_PARENT_VERSION = "parent_version"; - private static final String COL_PARENT_NAME = "parent_name"; - private static final String COL_VERSION = "key_version"; - private static final String COL_NAME = "key_name"; - - private static final String SELECT_C_FROM = "SELECT c FROM "; - private static final String AND_C_KEY_LOCAL_NAME = " AND c.key.localName=:" + COL_LOCALNAME; - private static final String AND_C_KEY_PARENT_KEY_VERSION = " AND c.key.parentKeyVersion=:" + COL_PARENT_VERSION; - private static final String C_WHERE_C_KEY_PARENT_KEY_NAME = " c WHERE c.key.parentKeyName=:" + COL_PARENT_NAME; - private static final String AND_C_KEY_VERSION = " AND c.key.version=:" + COL_VERSION; - private static final String C_WHERE_C_KEY_NAME = " c WHERE c.key.name=:" + COL_NAME; - private static final String DELETE_FROM = "DELETE FROM "; - - // Entity manager for JPA - private EntityManagerFactory emf = null; - - /** - * {@inheritDoc}. - */ - @Override - public void init(final DaoParameters daoParameters) throws ApexException { - if (daoParameters == null || daoParameters.getPersistenceUnit() == null) { - LOGGER.error("Apex persistence unit parameter not set"); - throw new ApexException("Apex persistence unit parameter not set"); - } - - LOGGER.debug("Creating Apex persistence unit \"" + daoParameters.getPersistenceUnit() + "\" . . ."); - try { - emf = Persistence.createEntityManagerFactory(daoParameters.getPersistenceUnit(), - daoParameters.getJdbcProperties()); - } catch (final Exception e) { - LOGGER.warn("Creation of Apex persistence unit \"" + daoParameters.getPersistenceUnit() + "\" failed", e); - throw new ApexException( - "Creation of Apex persistence unit \"" + daoParameters.getPersistenceUnit() + "\" failed", e); - } - LOGGER.debug("Created Apex persistence unit \"" + daoParameters.getPersistenceUnit() + "\""); - } - - /** - * Gets the entity manager for this DAO. - * - * @return the entity manager - */ - protected final synchronized EntityManager getEntityManager() { - if (emf == null) { - LOGGER.warn("Apex DAO has not been initialized"); - throw new ApexRuntimeException("Apex DAO has not been initialized"); - } - - return emf.createEntityManager(); - } - - /** - * {@inheritDoc}. - */ - @Override - public final void close() { - if (emf != null) { - emf.close(); - } - } - - /** - * {@inheritDoc}. - */ - @Override - public <T extends AxConcept> void create(final T obj) { - if (obj == null) { - return; - } - final EntityManager mg = getEntityManager(); - try { - mg.getTransaction().begin(); - mg.merge(obj); - mg.getTransaction().commit(); - } finally { - mg.close(); - } - } - - /** - * {@inheritDoc}. - */ - @Override - public <T extends AxConcept> void delete(final T obj) { - if (obj == null) { - return; - } - final EntityManager mg = getEntityManager(); - try { - mg.getTransaction().begin(); - mg.remove(mg.contains(obj) ? obj : mg.merge(obj)); - mg.getTransaction().commit(); - } finally { - mg.close(); - } - } - - /** - * {@inheritDoc}. - */ - @Override - public <T extends AxConcept> void delete(final Class<T> someClass, final AxArtifactKey key) { - if (key == null) { - return; - } - final EntityManager mg = getEntityManager(); - try { - mg.getTransaction().begin(); - mg.createQuery(DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_NAME + AND_C_KEY_VERSION, someClass) - .setParameter(COL_NAME, key.getName()) - .setParameter(COL_VERSION, key.getVersion()) - .executeUpdate(); - mg.getTransaction().commit(); - } finally { - mg.close(); - } - } - - /** - * {@inheritDoc}. - */ - @Override - public <T extends AxConcept> void delete(final Class<T> someClass, final AxReferenceKey key) { - if (key == null) { - return; - } - final EntityManager mg = getEntityManager(); - try { - mg.getTransaction().begin(); - mg.createQuery(DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_PARENT_KEY_NAME - + AND_C_KEY_PARENT_KEY_VERSION - + AND_C_KEY_LOCAL_NAME, someClass) - .setParameter(COL_PARENT_NAME, key.getParentKeyName()) - .setParameter(COL_PARENT_VERSION, key.getParentKeyVersion()) - .setParameter(COL_LOCALNAME, key.getLocalName()) - .executeUpdate(); - mg.getTransaction().commit(); - } finally { - mg.close(); - } - } - - /** - * {@inheritDoc}. - */ - @Override - public <T extends AxConcept> void createCollection(final Collection<T> objs) { - if (objs == null || objs.isEmpty()) { - return; - } - final EntityManager mg = getEntityManager(); - try { - mg.getTransaction().begin(); - for (final T t : objs) { - mg.merge(t); - } - mg.getTransaction().commit(); - } finally { - mg.close(); - } - } - - /** - * {@inheritDoc}. - */ - @Override - public <T extends AxConcept> void deleteCollection(final Collection<T> objs) { - if (objs == null || objs.isEmpty()) { - return; - } - final EntityManager mg = getEntityManager(); - try { - mg.getTransaction().begin(); - for (final T t : objs) { - mg.remove(mg.contains(t) ? t : mg.merge(t)); - } - mg.getTransaction().commit(); - } finally { - mg.close(); - } - } - - /** - * {@inheritDoc}. - */ - @Override - public <T extends AxConcept> int deleteByArtifactKey(final Class<T> someClass, - final Collection<AxArtifactKey> keys) { - if (keys == null || keys.isEmpty()) { - return 0; - } - int deletedCount = 0; - final EntityManager mg = getEntityManager(); - try { - mg.getTransaction().begin(); - for (final AxArtifactKey key : keys) { - deletedCount += mg.createQuery(DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_NAME - + AND_C_KEY_VERSION, someClass) - .setParameter(COL_NAME, key.getName()) - .setParameter(COL_VERSION, key.getVersion()) - .executeUpdate(); - } - mg.getTransaction().commit(); - } finally { - mg.close(); - } - return deletedCount; - } - - /** - * {@inheritDoc}. - */ - @Override - public <T extends AxConcept> int deleteByReferenceKey(final Class<T> someClass, - final Collection<AxReferenceKey> keys) { - if (keys == null || keys.isEmpty()) { - return 0; - } - int deletedCount = 0; - final EntityManager mg = getEntityManager(); - try { - mg.getTransaction().begin(); - for (final AxReferenceKey key : keys) { - deletedCount += - mg.createQuery( - DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_PARENT_KEY_NAME - + AND_C_KEY_PARENT_KEY_VERSION + AND_C_KEY_LOCAL_NAME, someClass) - .setParameter(COL_PARENT_NAME, key.getParentKeyName()) - .setParameter(COL_PARENT_VERSION, key.getParentKeyVersion()) - .setParameter(COL_LOCALNAME, key.getLocalName()) - .executeUpdate(); - } - mg.getTransaction().commit(); - } finally { - mg.close(); - } - return deletedCount; - } - - /** - * {@inheritDoc}. - */ - @Override - public <T extends AxConcept> void deleteAll(final Class<T> someClass) { - final EntityManager mg = getEntityManager(); - try { - mg.getTransaction().begin(); - mg.createQuery(DELETE_FROM + someClass.getSimpleName() + " c ", someClass).executeUpdate(); - mg.getTransaction().commit(); - } finally { - mg.close(); - } - } - - /** - * {@inheritDoc}. - */ - @Override - public <T extends AxConcept> T get(final Class<T> someClass, final AxArtifactKey key) { - if (someClass == null) { - return null; - } - final EntityManager mg = getEntityManager(); - try { - final T t = mg.find(someClass, key); - if (t != null) { - // This clone is created to force the JPA DAO to recurse down through the object - try { - final T clonedT = someClass.getDeclaredConstructor().newInstance(); - t.copyTo(clonedT); - return clonedT; - } catch (final Exception e) { - LOGGER.warn("Could not clone object of class \"" + someClass.getName() + "\"", e); - return null; - } - } else { - return null; - } - } finally { - mg.close(); - } - } - - /** - * {@inheritDoc}. - */ - @Override - public <T extends AxConcept> T get(final Class<T> someClass, final AxReferenceKey key) { - if (someClass == null) { - return null; - } - final EntityManager mg = getEntityManager(); - try { - final T t = mg.find(someClass, key); - if (t != null) { - try { - final T clonedT = someClass.getDeclaredConstructor().newInstance(); - t.copyTo(clonedT); - return clonedT; - } catch (final Exception e) { - LOGGER.warn("Could not clone object of class \"" + someClass.getName() + "\"", e); - return null; - } - } else { - return null; - } - } finally { - mg.close(); - } - } - - /** - * {@inheritDoc}. - */ - @Override - public <T extends AxConcept> List<T> getAll(final Class<T> someClass) { - if (someClass == null) { - return Collections.emptyList(); - } - final EntityManager mg = getEntityManager(); - try { - return mg.createQuery(SELECT_C_FROM + someClass.getSimpleName() + " c", someClass).getResultList(); - } finally { - mg.close(); - } - } - - /** - * {@inheritDoc}. - */ - @Override - public <T extends AxConcept> List<T> getAll(final Class<T> someClass, final AxArtifactKey parentKey) { - if (someClass == null) { - return Collections.emptyList(); - } - final EntityManager mg = getEntityManager(); - try { - return mg - .createQuery( - SELECT_C_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_PARENT_KEY_NAME - + AND_C_KEY_PARENT_KEY_VERSION, someClass) - .setParameter(COL_PARENT_NAME, parentKey.getName()) - .setParameter(COL_PARENT_VERSION, parentKey.getVersion()) - .getResultList(); - } finally { - mg.close(); - } - } - - /** - * {@inheritDoc}. - */ - @Override - public <T extends AxConcept> T getArtifact(final Class<T> someClass, final AxArtifactKey key) { - if (someClass == null || key == null) { - return null; - } - final EntityManager mg = getEntityManager(); - List<T> ret; - try { - ret = mg.createQuery(SELECT_C_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_NAME - + AND_C_KEY_VERSION, someClass) - .setParameter(COL_NAME, key.getName()) - .setParameter(COL_VERSION, key.getVersion()) - .getResultList(); - } finally { - mg.close(); - } - if (ret == null || ret.isEmpty()) { - return null; - } - if (ret.size() > 1) { - throw new IllegalArgumentException("More than one result was returned for search for " + someClass - + " with key " + key.getId() + ": " + ret); - } - return ret.get(0); - } - - /** - * {@inheritDoc}. - */ - @Override - public <T extends AxConcept> T getArtifact(final Class<T> someClass, final AxReferenceKey key) { - if (someClass == null || key == null) { - return null; - } - final EntityManager mg = getEntityManager(); - List<T> ret; - try { - ret = mg.createQuery(SELECT_C_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_PARENT_KEY_NAME - + AND_C_KEY_PARENT_KEY_VERSION + AND_C_KEY_LOCAL_NAME, someClass) - .setParameter(COL_PARENT_NAME, key.getParentKeyName()) - .setParameter(COL_PARENT_VERSION, key.getParentKeyVersion()) - .setParameter(COL_LOCALNAME, key.getLocalName()) - .getResultList(); - } finally { - mg.close(); - } - if (ret == null || ret.isEmpty()) { - return null; - } - if (ret.size() > 1) { - throw new IllegalArgumentException("More than one result was returned for search for " + someClass - + " with key " + key.getId() + ": " + ret); - } - return ret.get(0); - } - - /** - * {@inheritDoc}. - */ - @Override - public <T extends AxConcept> T update(final T obj) { - final EntityManager mg = getEntityManager(); - T ret; - try { - mg.getTransaction().begin(); - ret = mg.merge(obj); - mg.flush(); - mg.getTransaction().commit(); - } finally { - mg.close(); - } - return ret; - } - - /** - * {@inheritDoc}. - */ - @Override - public <T extends AxConcept> long size(final Class<T> someClass) { - if (someClass == null) { - return 0; - } - final EntityManager mg = getEntityManager(); - long size = 0; - try { - size = mg.createQuery("SELECT COUNT(c) FROM " + someClass.getSimpleName() + " c", Long.class) - .getSingleResult(); - } finally { - mg.close(); - } - return size; - } -} diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/impl/package-info.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/impl/package-info.java deleted file mode 100644 index 5d585c247..000000000 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/impl/package-info.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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. - * 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========================================================= - */ - -/** - * Contains a default DAO implementation for APEX {@link org.onap.policy.apex.model.basicmodel.concepts.AxConcept} - * classes that uses javax persistence. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -package org.onap.policy.apex.model.basicmodel.dao.impl; diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/package-info.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/package-info.java deleted file mode 100644 index 505e4023a..000000000 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/package-info.java +++ /dev/null @@ -1,29 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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. - * 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========================================================= - */ - -/** - * Defines and implements the Data Access Object (DAO) that allows Apex - * {@link org.onap.policy.apex.model.basicmodel.concepts.AxConcept} concepts to be read from and written to databases - * over JDBC. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ - -package org.onap.policy.apex.model.basicmodel.dao; diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/test/TestApexModel.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/test/TestApexModel.java index 36ce4e438..5dbf69492 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/test/TestApexModel.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/test/TestApexModel.java @@ -1,7 +1,7 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2021 Nordix Foundation. + * Modifications Copyright (C) 2021-2022 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,8 +27,6 @@ import java.io.File; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; import org.onap.policy.apex.model.basicmodel.concepts.AxModel; import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; -import org.onap.policy.apex.model.basicmodel.dao.ApexDaoFactory; -import org.onap.policy.apex.model.basicmodel.dao.DaoParameters; import org.onap.policy.apex.model.basicmodel.handling.ApexModelFileWriter; import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader; import org.onap.policy.apex.model.basicmodel.handling.ApexModelWriter; @@ -174,29 +172,6 @@ public class TestApexModel<M extends AxModel> { } /** - * Test write and read of an Apex model to database using JPA. - * - * @param daoParameters the DAO parameters to use for JPA/JDBC - * @throws ApexException thrown on errors writing or reading the model to database - */ - public final void testApexModelWriteReadJpa(final DaoParameters daoParameters) throws ApexException { - LOGGER.debug("running testApexModelWriteReadJPA . . ."); - - final var model = modelCreator.getModel(); - - final var apexDao = new ApexDaoFactory().createApexDao(daoParameters); - apexDao.init(daoParameters); - - apexDao.create(model); - final var dbJpaModel = apexDao.get(rootModelClass, model.getKey()); - apexDao.close(); - - checkModelEquality(model, dbJpaModel, "test model does not equal model written and read using generic JPA"); - - LOGGER.debug("ran testApexModelWriteReadJPA"); - } - - /** * Test that an Apex model is valid. * * @return the result of the validation |