aboutsummaryrefslogtreecommitdiffstats
path: root/models-dao/src/main/java/org/onap
diff options
context:
space:
mode:
authorliamfallon <liam.fallon@est.tech>2019-03-05 09:35:16 +0000
committerliamfallon <liam.fallon@est.tech>2019-03-05 09:35:16 +0000
commitb694be156d4b022e24347259f494ee3b46d47bdb (patch)
tree157848f4edaf56233e49890cda02e339d1d49558 /models-dao/src/main/java/org/onap
parentec6fe7ea854632943a2c3c30ffa49e799bb1fc49 (diff)
Add DAO module for Models
This patch set fixes some typos and license headers. Issue-ID: POLICY-1195 Change-Id: I2d15b00fcb50ea92746ab7c5a87b57efa07196fe Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'models-dao/src/main/java/org/onap')
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/DaoParameters.java126
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java205
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/PfDaoFactory.java68
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/converters/CDataConditioner.java69
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/converters/Uuid2String.java60
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/converters/package-info.java26
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java415
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/impl/package-info.java25
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/package-info.java27
9 files changed, 1021 insertions, 0 deletions
diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/DaoParameters.java b/models-dao/src/main/java/org/onap/policy/models/dao/DaoParameters.java
new file mode 100644
index 000000000..18ae74ae1
--- /dev/null
+++ b/models-dao/src/main/java/org/onap/policy/models/dao/DaoParameters.java
@@ -0,0 +1,126 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 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.models.dao;
+
+import java.util.Properties;
+
+/**
+ * This class is a POJO that holds properties for PF DAOs.
+ */
+public class DaoParameters {
+ /** The default PF DAO plugin class. */
+ public static final String DEFAULT_PLUGIN_CLASS = "org.onap.policy.models.dao.impl.DefaultPfDao";
+
+ 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 PfDao} interface.
+ *
+ * @return the DAO plugin class
+ */
+ public String getPluginClass() {
+ return pluginClass;
+ }
+
+ /**
+ * Sets the DAO plugin class, a class that implements the {@link PfDao} 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);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString() {
+ return "DAOParameters [pluginClass=" + pluginClass + ", persistenceUnit=" + persistenceUnit
+ + ", jdbcProperties=" + jdbcProperties + "]";
+ }
+}
diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java b/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java
new file mode 100644
index 000000000..85c971ce7
--- /dev/null
+++ b/models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java
@@ -0,0 +1,205 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 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.models.dao;
+
+import java.util.Collection;
+import java.util.List;
+
+import org.onap.policy.models.base.PfConcept;
+import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.base.PfReferenceKey;
+
+/**
+ * The Interface PfDao describes the DAO interface for reading and writing Policy Framework
+ * {@link PfConcept} concepts to and from databases using JDBC.
+ */
+public interface PfDao {
+
+ /**
+ * Initialize the Policy Framework DAO with the given parameters.
+ *
+ * @param daoParameters parameters to use to access the database
+ * @throws PfModelException on initialization errors
+ */
+ void init(DaoParameters daoParameters) throws PfModelException;
+
+ /**
+ * Close the Policy Framework DAO.
+ */
+ void close();
+
+ /**
+ * Creates an Policy Framework concept on the database.
+ *
+ * @param <T> the type of the object to create, a subclass of {@link PfConcept}
+ * @param obj the object to create
+ */
+ <T extends PfConcept> void create(T obj);
+
+ /**
+ * Delete an Policy Framework concept on the database.
+ *
+ * @param <T> the type of the object to delete, a subclass of {@link PfConcept}
+ * @param obj the object to delete
+ */
+ <T extends PfConcept> void delete(T obj);
+
+ /**
+ * Delete an Policy Framework concept on the database.
+ *
+ * @param <T> the type of the object to delete, a subclass of {@link PfConcept}
+ * @param someClass the class of the object to delete, a subclass of {@link PfConcept}
+ * @param key the key of the object to delete
+ */
+ <T extends PfConcept> void delete(Class<T> someClass, PfConceptKey key);
+
+ /**
+ * Delete an Policy Framework concept on the database.
+ *
+ * @param <T> the type of the object to delete, a subclass of {@link PfConcept}
+ * @param someClass the class of the object to delete, a subclass of {@link PfConcept}
+ * @param key the key of the object to delete
+ */
+ <T extends PfConcept> void delete(Class<T> someClass, PfReferenceKey key);
+
+ /**
+ * Create a collection of objects in the database.
+ *
+ * @param <T> the type of the object to create, a subclass of {@link PfConcept}
+ * @param objs the objects to create
+ */
+ <T extends PfConcept> 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 PfConcept}
+ * @param objs the objects to delete
+ */
+ <T extends PfConcept> void deleteCollection(Collection<T> objs);
+
+ /**
+ * Delete a collection of objects in the database referred to by concept key.
+ *
+ * @param <T> the type of the objects to delete, a subclass of {@link PfConcept}
+ * @param someClass the class of the objects to delete, a subclass of {@link PfConcept}
+ * @param keys the keys of the objects to delete
+ * @return the number of objects deleted
+ */
+ <T extends PfConcept> int deleteByConceptKey(Class<T> someClass, Collection<PfConceptKey> 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 PfConcept}
+ * @param someClass the class of the objects to delete, a subclass of {@link PfConcept}
+ * @param keys the keys of the objects to delete
+ * @return the number of objects deleted
+ */
+ <T extends PfConcept> int deleteByReferenceKey(Class<T> someClass, Collection<PfReferenceKey> keys);
+
+ /**
+ * Delete all objects of a given class in the database.
+ *
+ * @param <T> the type of the objects to delete, a subclass of {@link PfConcept}
+ * @param someClass the class of the objects to delete, a subclass of {@link PfConcept}
+ */
+ <T extends PfConcept> void deleteAll(Class<T> someClass);
+
+ /**
+ * Get an object from the database, referred to by concept key.
+ *
+ * @param <T> the type of the object to get, a subclass of {@link PfConcept}
+ * @param someClass the class of the object to get, a subclass of {@link PfConcept}
+ * @param key the key of the object to get
+ * @return the object that was retrieved from the database
+ */
+ <T extends PfConcept> T get(Class<T> someClass, PfConceptKey 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 PfConcept}
+ * @param someClass the class of the object to get, a subclass of {@link PfConcept}
+ * @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 PfConcept> T get(Class<T> someClass, PfReferenceKey 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 PfConcept}
+ * @param someClass the class of the objects to get, a subclass of {@link PfConcept}
+ * @return the objects or null if no objects were retrieved
+ */
+ <T extends PfConcept> List<T> getAll(Class<T> someClass);
+
+ /**
+ * Get all the objects in the database of the given type with the given parent concept key.
+ *
+ * @param <T> the type of the objects to get, a subclass of {@link PfConcept}
+ * @param someClass the class of the objects to get, a subclass of {@link PfConcept}
+ * @param parentKey the parent key of the concepts to get
+ * @return the all
+ */
+ <T extends PfConcept> List<T> getAll(Class<T> someClass, PfConceptKey parentKey);
+
+ /**
+ * Get a concept from the database with the given concept key.
+ *
+ * @param <T> the type of the object to get, a subclass of {@link PfConcept}
+ * @param someClass the class of the object to get, a subclass of {@link PfConcept}
+ * @param conceptId the concept key of the concept to get
+ * @return the concept that matches the key or null if the concept is not retrieved
+ */
+ <T extends PfConcept> T getConcept(Class<T> someClass, PfConceptKey conceptId);
+
+ /**
+ * Get a concept from the database with the given reference key.
+ *
+ * @param <T> the type of the object to get, a subclass of {@link PfConcept}
+ * @param someClass the class of the object to get, a subclass of {@link PfConcept}
+ * @param conceptId the concept key of the concept to get
+ * @return the concept that matches the key or null if the concept is not retrieved
+ */
+ <T extends PfConcept> T getConcept(Class<T> someClass, PfReferenceKey conceptId);
+
+ /**
+ * 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 PfConcept}
+ * @param someClass the class of the object to get, a subclass of {@link PfConcept}
+ * @return the number of instances of the concept in the database
+ */
+ <T extends PfConcept> long size(Class<T> someClass);
+
+ /**
+ * Update a concept in the database.
+ *
+ * @param <T> the type of the object to get, a subclass of {@link PfConcept}
+ * @param obj the object to update
+ * @return the updated object
+ */
+ <T extends PfConcept> T update(T obj);
+}
diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/PfDaoFactory.java b/models-dao/src/main/java/org/onap/policy/models/dao/PfDaoFactory.java
new file mode 100644
index 000000000..3b7e31eb8
--- /dev/null
+++ b/models-dao/src/main/java/org/onap/policy/models/dao/PfDaoFactory.java
@@ -0,0 +1,68 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 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.models.dao;
+
+import org.onap.policy.common.utils.validation.Assertions;
+import org.onap.policy.models.base.PfModelException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * This factory class returns a Policy Framework DAO for the configured persistence mechanism. The
+ * factory uses the plugin class specified in {@link DaoParameters} to instantiate a DAO instance.
+ */
+public class PfDaoFactory {
+ // Get a reference to the logger
+ private static final Logger LOGGER = LoggerFactory.getLogger(PfDaoFactory.class);
+
+ /**
+ * Return a Policy Framework DAO for the required Policy Framework DAO plugin class.
+ *
+ * @param daoParameters parameters to use to read the database configuration information
+ * @return the Policy Framework DAO
+ * @throws PfModelException on invalid JPA plugins
+ */
+ public PfDao createPfDao(final DaoParameters daoParameters) throws PfModelException {
+ Assertions.argumentOfClassNotNull(daoParameters, PfModelException.class,
+ "Parameter \"daoParameters\" may not be null");
+
+ // Get the class for the DAO using reflection
+ Object pfDaoObject = null;
+ try {
+ pfDaoObject = Class.forName(daoParameters.getPluginClass()).newInstance();
+ } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
+ String errorMessage =
+ "Policy Framework DAO class not found for DAO plugin \"" + daoParameters.getPluginClass() + "\"";
+ LOGGER.error(errorMessage, e);
+ throw new PfModelException(errorMessage, e);
+ }
+
+ // Check the class is a Policy Framework DAO
+ if (!(pfDaoObject instanceof PfDao)) {
+ String errorMessage = "Specified DAO plugin class \"" + daoParameters.getPluginClass()
+ + "\" does not implement the PfDao interface";
+ LOGGER.error(errorMessage);
+ throw new PfModelException(errorMessage);
+ }
+
+ return (PfDao) pfDaoObject;
+ }
+}
diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/converters/CDataConditioner.java b/models-dao/src/main/java/org/onap/policy/models/dao/converters/CDataConditioner.java
new file mode 100644
index 000000000..327f65ece
--- /dev/null
+++ b/models-dao/src/main/java/org/onap/policy/models/dao/converters/CDataConditioner.java
@@ -0,0 +1,69 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 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.models.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.
+ */
+@Converter
+public class CDataConditioner extends XmlAdapter<String, String> implements AttributeConverter<String, String> {
+
+ private static final String NL = "\n";
+
+ @Override
+ public String convertToDatabaseColumn(final String raw) {
+ return clean(raw);
+ }
+
+ @Override
+ public String convertToEntityAttribute(final String db) {
+ return clean(db);
+ }
+
+ @Override
+ public String unmarshal(final String value) throws Exception {
+ return this.convertToEntityAttribute(value);
+ }
+
+ @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/models-dao/src/main/java/org/onap/policy/models/dao/converters/Uuid2String.java b/models-dao/src/main/java/org/onap/policy/models/dao/converters/Uuid2String.java
new file mode 100644
index 000000000..a2b1c085a
--- /dev/null
+++ b/models-dao/src/main/java/org/onap/policy/models/dao/converters/Uuid2String.java
@@ -0,0 +1,60 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 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.models.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.
+ */
+@Converter
+public class Uuid2String extends XmlAdapter<String, UUID> implements AttributeConverter<UUID, String> {
+
+ @Override
+ public String convertToDatabaseColumn(final UUID uuid) {
+ String returnString;
+ if (uuid == null) {
+ returnString = "";
+ } else {
+ returnString = uuid.toString();
+ }
+ return returnString;
+ }
+
+ @Override
+ public UUID convertToEntityAttribute(final String uuidString) {
+ return UUID.fromString(uuidString);
+ }
+
+ @Override
+ public UUID unmarshal(final String value) throws Exception {
+ return this.convertToEntityAttribute(value);
+ }
+
+ @Override
+ public String marshal(final UUID value) throws Exception {
+ return this.convertToDatabaseColumn(value);
+ }
+}
diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/converters/package-info.java b/models-dao/src/main/java/org/onap/policy/models/dao/converters/package-info.java
new file mode 100644
index 000000000..416eff20c
--- /dev/null
+++ b/models-dao/src/main/java/org/onap/policy/models/dao/converters/package-info.java
@@ -0,0 +1,26 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 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=========================================================
+ */
+
+/**
+ * Contains converters used by PF EclipseLink marshaling and unmarshaling of
+ * {@link org.onap.policy.models.base.PfConcept} instances to and from files and databases.
+ */
+
+package org.onap.policy.models.dao.converters;
diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java b/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java
new file mode 100644
index 000000000..429632db0
--- /dev/null
+++ b/models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java
@@ -0,0 +1,415 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 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.models.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.models.base.PfConcept;
+import org.onap.policy.models.base.PfConceptKey;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.base.PfModelRuntimeException;
+import org.onap.policy.models.base.PfReferenceKey;
+import org.onap.policy.models.dao.DaoParameters;
+import org.onap.policy.models.dao.PfDao;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The Class DefaultPfDao is an JPA implementation of the {@link PfDao} class for Policy Framework
+ * concepts ({@link PfConcept}). It uses the default JPA implementation in the javax
+ * {@link Persistence} class.
+ */
+public class DefaultPfDao implements PfDao {
+ private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPfDao.class);
+
+ private static final String SELECT_C_FROM = "SELECT c FROM ";
+ private static final String AND_C_KEY_LOCAL_NAME = "' AND c.key.localName='";
+ private static final String AND_C_KEY_PARENT_KEY_VERSION = "' AND c.key.parentKeyVersion='";
+ private static final String C_WHERE_C_KEY_PARENT_KEY_NAME = " c WHERE c.key.parentKeyName='";
+ private static final String AND_C_KEY_VERSION = "' AND c.key.version='";
+ private static final String C_WHERE_C_KEY_NAME = " c WHERE c.key.name='";
+ private static final String DELETE_FROM = "DELETE FROM ";
+
+ // Entity manager for JPA
+ private EntityManagerFactory emf = null;
+
+ @Override
+ public void init(final DaoParameters daoParameters) throws PfModelException {
+ if (daoParameters == null || daoParameters.getPersistenceUnit() == null) {
+ LOGGER.error("Policy Framework persistence unit parameter not set");
+ throw new PfModelException("Policy Framework persistence unit parameter not set");
+ }
+
+ LOGGER.debug("Creating Policy Framework persistence unit \"{}\" . . .", daoParameters.getPersistenceUnit());
+ try {
+ emf = Persistence.createEntityManagerFactory(daoParameters.getPersistenceUnit(),
+ daoParameters.getJdbcProperties());
+ } catch (final Exception ex) {
+ String errorMessage = "Creation of Policy Framework persistence unit \""
+ + daoParameters.getPersistenceUnit() + "\" failed";
+ LOGGER.warn(errorMessage, ex);
+ throw new PfModelException(errorMessage, ex);
+ }
+ LOGGER.debug("Created Policy Framework 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("Policy Framework DAO has not been initialized");
+ throw new PfModelRuntimeException("Policy Framework DAO has not been initialized");
+ }
+
+ return emf.createEntityManager();
+ }
+
+ @Override
+ public final void close() {
+ if (emf != null) {
+ emf.close();
+ }
+ }
+
+ @Override
+ public <T extends PfConcept> 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();
+ }
+ }
+
+ @Override
+ public <T extends PfConcept> 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();
+ }
+ }
+
+ @Override
+ public <T extends PfConcept> void delete(final Class<T> someClass, final PfConceptKey key) {
+ if (key == null) {
+ return;
+ }
+ final EntityManager mg = getEntityManager();
+ try {
+ mg.getTransaction().begin();
+ mg.createQuery(DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_NAME + key.getName()
+ + AND_C_KEY_VERSION + key.getVersion() + "'", someClass).executeUpdate();
+ mg.getTransaction().commit();
+ } finally {
+ mg.close();
+ }
+ }
+
+ @Override
+ public <T extends PfConcept> void delete(final Class<T> someClass, final PfReferenceKey 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
+ + key.getParentKeyName() + AND_C_KEY_PARENT_KEY_VERSION + key.getParentKeyVersion()
+ + AND_C_KEY_LOCAL_NAME + key.getLocalName() + "'", someClass).executeUpdate();
+ mg.getTransaction().commit();
+ } finally {
+ mg.close();
+ }
+ }
+
+ @Override
+ public <T extends PfConcept> 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();
+ }
+ }
+
+ @Override
+ public <T extends PfConcept> 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();
+ }
+ }
+
+ @Override
+ public <T extends PfConcept> int deleteByConceptKey(final Class<T> someClass,
+ final Collection<PfConceptKey> keys) {
+ if (keys == null || keys.isEmpty()) {
+ return 0;
+ }
+ int deletedCount = 0;
+ final EntityManager mg = getEntityManager();
+ try {
+ mg.getTransaction().begin();
+ for (final PfConceptKey key : keys) {
+ deletedCount += mg.createQuery(DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_NAME
+ + key.getName() + AND_C_KEY_VERSION + key.getVersion() + "'", someClass).executeUpdate();
+ }
+ mg.getTransaction().commit();
+ } finally {
+ mg.close();
+ }
+ return deletedCount;
+ }
+
+ @Override
+ public <T extends PfConcept> int deleteByReferenceKey(final Class<T> someClass,
+ final Collection<PfReferenceKey> keys) {
+ if (keys == null || keys.isEmpty()) {
+ return 0;
+ }
+ int deletedCount = 0;
+ final EntityManager mg = getEntityManager();
+ try {
+ mg.getTransaction().begin();
+ for (final PfReferenceKey key : keys) {
+ deletedCount +=
+ mg.createQuery(
+ DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_PARENT_KEY_NAME
+ + key.getParentKeyName() + AND_C_KEY_PARENT_KEY_VERSION
+ + key.getParentKeyVersion() + AND_C_KEY_LOCAL_NAME + key.getLocalName() + "'",
+ someClass).executeUpdate();
+ }
+ mg.getTransaction().commit();
+ } finally {
+ mg.close();
+ }
+ return deletedCount;
+ }
+
+ @Override
+ public <T extends PfConcept> 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();
+ }
+ }
+
+ @Override
+ public <T extends PfConcept> T get(final Class<T> someClass, final PfConceptKey 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.newInstance();
+ t.copyTo(clonedT);
+ return clonedT;
+ } catch (final Exception e) {
+ LOGGER.warn("Could not clone object of class \"" + someClass.getCanonicalName() + "\"", e);
+ return null;
+ }
+ } else {
+ return null;
+ }
+ } finally {
+ mg.close();
+ }
+ }
+
+ @Override
+ public <T extends PfConcept> T get(final Class<T> someClass, final PfReferenceKey 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.newInstance();
+ t.copyTo(clonedT);
+ return clonedT;
+ } catch (final Exception e) {
+ LOGGER.warn("Could not clone object of class \"" + someClass.getCanonicalName() + "\"", e);
+ return null;
+ }
+ } else {
+ return null;
+ }
+ } finally {
+ mg.close();
+ }
+ }
+
+ @Override
+ public <T extends PfConcept> 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();
+ }
+ }
+
+ @Override
+ public <T extends PfConcept> List<T> getAll(final Class<T> someClass, final PfConceptKey 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
+ + parentKey.getName() + AND_C_KEY_PARENT_KEY_VERSION + parentKey.getVersion() + "'",
+ someClass)
+ .getResultList();
+ } finally {
+ mg.close();
+ }
+ }
+
+ @Override
+ public <T extends PfConcept> T getConcept(final Class<T> someClass, final PfConceptKey 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 + key.getName()
+ + AND_C_KEY_VERSION + key.getVersion() + "'", someClass).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);
+ }
+
+ @Override
+ public <T extends PfConcept> T getConcept(final Class<T> someClass, final PfReferenceKey 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
+ + key.getParentKeyName() + AND_C_KEY_PARENT_KEY_VERSION + key.getParentKeyVersion()
+ + AND_C_KEY_LOCAL_NAME + key.getLocalName() + "'", someClass).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);
+ }
+
+ @Override
+ public <T extends PfConcept> 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;
+ }
+
+ @Override
+ public <T extends PfConcept> 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/models-dao/src/main/java/org/onap/policy/models/dao/impl/package-info.java b/models-dao/src/main/java/org/onap/policy/models/dao/impl/package-info.java
new file mode 100644
index 000000000..0d27628a0
--- /dev/null
+++ b/models-dao/src/main/java/org/onap/policy/models/dao/impl/package-info.java
@@ -0,0 +1,25 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 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=========================================================
+ */
+
+/**
+ * Contains a default DAO implementation for the PF {@link org.onap.policy.models.base.PfConcept}
+ * classes that uses javax persistence.
+ */
+package org.onap.policy.models.dao.impl;
diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/package-info.java b/models-dao/src/main/java/org/onap/policy/models/dao/package-info.java
new file mode 100644
index 000000000..e8cfbe4e7
--- /dev/null
+++ b/models-dao/src/main/java/org/onap/policy/models/dao/package-info.java
@@ -0,0 +1,27 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2019 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=========================================================
+ */
+
+/**
+ * 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.
+ */
+
+package org.onap.policy.models.dao;