/*-
* ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
- * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 Nordix Foundation.
+ * Modifications Copyright (C) 2019-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.
package org.onap.policy.models.dao.impl;
+import java.sql.Timestamp;
+import java.time.Instant;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
-
+import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
+import javax.persistence.TypedQuery;
import javax.ws.rs.core.Response;
-
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.base.PfReferenceTimestampKey;
+import org.onap.policy.models.base.PfTimestampKey;
+import org.onap.policy.models.base.PfUtils;
import org.onap.policy.models.dao.DaoParameters;
import org.onap.policy.models.dao.PfDao;
import org.slf4j.Logger;
// @formatter:off
private static final String NAME = "name";
private static final String VERSION = "version";
+ private static final String TIMESTAMP = "timeStamp";
private static final String PARENT_NAME = "parentname";
private static final String PARENT_VERSION = "parentversion";
private static final String LOCAL_NAME = "localname";
private static final String WHERE = " WHERE ";
private static final String AND = " AND ";
+ private static final String ORDER = " ORDER BY ";
+
+ private static final String NAME_FILTER = "c.key.name = :name";
+ private static final String VERSION_FILTER = "c.key.version = :version";
+ private static final String TIMESTAMP_FILTER = "c.key.timeStamp = :timeStamp";
+ private static final String TIMESTAMP_START_FILTER = "c.key.timeStamp >= :startTime";
+ private static final String TIMESTAMP_END_FILTER = "c.key.timeStamp <= :endTime";
+ private static final String PARENT_NAME_FILTER = "c.key.parentKeyName = :parentname";
+ private static final String PARENT_VERSION_FILTER = "c.key.parentKeyVersion = :parentversion";
+ private static final String LOCAL_NAME_FILTER = "c.key.localName = :localname";
+
+ private static final String PARENT_NAME_REF_FILTER = "c.key.referenceKey.parentKeyName = :parentKeyName";
- private static final String NAME_FILTER = "c.key.name = :name";
- private static final String VERSION_FILTER = "c.key.version = :version";
- private static final String PARENT_NAME_FILTER = "c.key.parentKeyName = :parentname";
- private static final String PARENT_VERSION_FILTER = "c.key.parentKeyVersion = :parentversion";
- private static final String LOCAL_NAME_FILTER = "c.key.localName = :localname";
+ private static final String CLONE_ERR_MSG = "Could not clone object of class \"{}\"";
private static final String DELETE_BY_CONCEPT_KEY =
DELETE_FROM_TABLE + WHERE + NAME_FILTER + AND + VERSION_FILTER;
+ private static final String DELETE_BY_TIMESTAMP_KEY =
+ DELETE_FROM_TABLE + WHERE + NAME_FILTER + AND + VERSION_FILTER + AND + TIMESTAMP_FILTER;
+
private static final String DELETE_BY_REFERENCE_KEY =
DELETE_FROM_TABLE + WHERE + PARENT_NAME_FILTER + AND + PARENT_VERSION_FILTER + AND + LOCAL_NAME_FILTER;
private static final String SELECT_ALL_FOR_PARENT =
SELECT_FROM_TABLE + WHERE + PARENT_NAME_FILTER + AND + PARENT_VERSION_FILTER;
+ private static final String SELECT_ALL_VERSIONS_FOR_PARENT =
+ SELECT_FROM_TABLE + WHERE + PARENT_NAME_FILTER;
+
private static final String SELECT_ALL_VERSIONS = SELECT_FROM_TABLE + WHERE + NAME_FILTER;
private static final String SELECT_BY_CONCEPT_KEY =
} catch (final Exception ex) {
String errorMessage = "Creation of Policy Framework persistence unit \""
+ daoParameters.getPersistenceUnit() + "\" failed";
- LOGGER.warn(errorMessage, ex);
+ LOGGER.warn(errorMessage);
throw new PfModelException(Response.Status.INTERNAL_SERVER_ERROR, errorMessage, ex);
}
LOGGER.debug("Created Policy Framework persistence unit \"{}\"", daoParameters.getPersistenceUnit());
}
}
+ @Override
+ public <T extends PfConcept> void delete(final Class<T> someClass, final PfTimestampKey key) {
+ if (key == null) {
+ return;
+ }
+ final EntityManager mg = getEntityManager();
+ try {
+ // @formatter:off
+ mg.getTransaction().begin();
+ mg.createQuery(setQueryTable(DELETE_BY_TIMESTAMP_KEY, someClass), someClass)
+ .setParameter(NAME, key.getName())
+ .setParameter(VERSION, key.getVersion())
+ .setParameter(TIMESTAMP, key.getTimeStamp())
+ .executeUpdate();
+ mg.getTransaction().commit();
+ // @formatter:on
+ } finally {
+ mg.close();
+ }
+ }
+
@Override
public <T extends PfConcept> void createCollection(final Collection<T> objs) {
if (objs == null || objs.isEmpty()) {
}
@Override
- public <T extends PfConcept> T get(final Class<T> someClass, final PfConceptKey key) {
- if (someClass == null) {
- return null;
- }
+ public <T extends PfConcept> List<T> getFiltered(final Class<T> someClass, final String name, final String version,
+ final Instant startTime, final Instant endTime, final Map<String, Object> filterMap, final String sortOrder,
+ final int getRecordNum) {
final EntityManager mg = getEntityManager();
+
+ String filterQueryString = SELECT_FROM_TABLE + WHERE;
+
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.getName() + "\"", e);
- return null;
+ if (filterMap != null) {
+ filterQueryString = buildFilter(filterMap, filterQueryString);
+ }
+ filterQueryString = addKeyFilterString(filterQueryString, name, startTime, endTime,
+ isRefTimestampKey(someClass));
+ if (getRecordNum > 0) {
+ filterQueryString += ORDER + " c.key.timeStamp " + sortOrder;
+ }
+ TypedQuery<T> query = mg.createQuery(setQueryTable(filterQueryString, someClass), someClass);
+
+ if (filterMap != null) {
+ for (Map.Entry<String, Object> entry : filterMap.entrySet()) {
+ query.setParameter(entry.getKey(), entry.getValue());
+ }
+ }
+ if (name != null) {
+ if (isRefTimestampKey(someClass)) {
+ query.setParameter("parentKeyName", name);
+ } else {
+ query.setParameter("name", name);
+ }
+ }
+ if (startTime != null) {
+ if (endTime != null) {
+ query.setParameter("startTime", Timestamp.from(startTime));
+ query.setParameter("endTime", Timestamp.from(endTime));
+ } else {
+ query.setParameter("startTime", Timestamp.from(startTime));
}
} else {
- return null;
+ if (endTime != null) {
+ query.setParameter("endTime", Timestamp.from(endTime));
+ }
}
- } finally {
+ if (getRecordNum > 0) {
+ query.setMaxResults(getRecordNum);
+ }
+
+ LOGGER.debug("filterQueryString is \"{}\"", filterQueryString);
+ return query.getResultList();
+ } finally {
mg.close();
}
}
+ /**
+ * This method checks if the class invoking the DAO is using PfReferenceTimestamp Key.
+ * @param someClass class that invoked Dao
+ * @return true if the key is PfReferenceTimestampKey.
+ */
+ private <T extends PfConcept> boolean isRefTimestampKey(final Class<T> someClass) {
+ try {
+ return PfReferenceTimestampKey.class.isAssignableFrom(someClass.getDeclaredField("key").getType());
+ } catch (NoSuchFieldException e) {
+ LOGGER.error("Error verifying the key for reference timestamp:", e);
+ return false;
+ }
+ }
+
+ private String buildFilter(final Map<String, Object> filterMap, String filterQueryString) {
+ StringBuilder bld = new StringBuilder(filterQueryString);
+ for (String key : filterMap.keySet()) {
+ bld.append("c." + key + "= :" + key + AND);
+ }
+ return bld.toString();
+ }
+
+ @Override
+ public <T extends PfConcept> T get(final Class<T> someClass, final PfConceptKey key) {
+ return genericGet(someClass, key);
+ }
+
@Override
public <T extends PfConcept> T get(final Class<T> someClass, final PfReferenceKey key) {
+ return genericGet(someClass, key);
+ }
+
+ @Override
+ public <T extends PfConcept> T get(final Class<T> someClass, final PfTimestampKey key) {
+ return genericGet(someClass, key);
+ }
+
+ @Override
+ public <T extends PfConcept> T get(final Class<T> someClass, final PfReferenceTimestampKey key) {
+ return genericGet(someClass, key);
+ }
+
+ private <T extends PfConcept> T genericGet(final Class<T> someClass, final Object key) {
if (someClass == null) {
return null;
}
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.getName() + "\"", e);
- return null;
- }
- } else {
- return null;
+ mg.refresh(t);
}
+ return checkAndReturn(someClass, t);
} finally {
mg.close();
}
}
}
+ @Override
+ public <T extends PfConcept> List<T> getAllVersionsByParent(final Class<T> someClass, final String parentKeyName) {
+ if (someClass == null || parentKeyName == null) {
+ return Collections.emptyList();
+ }
+ final EntityManager mg = getEntityManager();
+ try {
+ // @formatter:off
+ return mg.createQuery(setQueryTable(SELECT_ALL_VERSIONS_FOR_PARENT, someClass), someClass)
+ .setParameter(PARENT_NAME, parentKeyName)
+ .getResultList();
+ // @formatter:on
+ } finally {
+ mg.close();
+ }
+ }
+
@Override
public <T extends PfConcept> List<T> getAllVersions(final Class<T> someClass, final String conceptName) {
if (someClass == null || conceptName == null) {
* @return the updated query string
*/
private <T extends PfConcept> String setQueryTable(final String queryString, final Class<T> tableClass) {
- return queryString.replaceAll(TABLE_TOKEN, tableClass.getSimpleName());
+ return queryString.replace(TABLE_TOKEN, tableClass.getSimpleName());
}
/**
}
return ret.get(0);
}
+
+ /**
+ * generate filter string with the filter value in TimestampKey.
+ *
+ * @param inputFilterString current filterString generated from FilterMap
+ * @param name the pdp name the start timeStamp to filter from database, filter rule: startTime <= filteredRecord
+ * timeStamp <= endTime. null for ignore start time.
+ * @param endTime the end timeStamp to filter from database, filter rule: startTime <= filteredRecord timeStamp <=
+ * endTime. null for ignore end time
+ * @param isRefTimestampKey boolean value, set to true if the query invoked for pfReferenceTimestampKey
+ * @return the filter string to query database
+ */
+ private String addKeyFilterString(String inputFilterString, final String name, final Instant startTime,
+ final Instant endTime, final boolean isRefTimestampKey) {
+ String filterQueryString;
+ String inputFilter = inputFilterString;
+ if (name != null) {
+ if (isRefTimestampKey) {
+ inputFilter += PARENT_NAME_REF_FILTER + AND;
+ } else {
+ inputFilter += NAME_FILTER + AND;
+ }
+ }
+ if (startTime != null) {
+ if (endTime != null) {
+ filterQueryString = inputFilter + TIMESTAMP_START_FILTER + AND + TIMESTAMP_END_FILTER;
+ } else {
+ filterQueryString = inputFilter + TIMESTAMP_START_FILTER;
+ }
+ } else {
+ if (endTime != null) {
+ filterQueryString = inputFilter + TIMESTAMP_END_FILTER;
+ } else {
+ filterQueryString = inputFilter.substring(0, inputFilter.length() - AND.length());
+ }
+ }
+
+ return filterQueryString;
+ }
+
+ /**
+ * check the result get from database and return the object.
+ *
+ * @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 t the object that was retrieved from the database
+ * @return the checked object or null
+ */
+ private <T extends PfConcept> T checkAndReturn(final Class<T> someClass, final T objToCheck) {
+ if (objToCheck != null) {
+ try {
+ return PfUtils.makeCopy(objToCheck);
+ } catch (final Exception e) {
+ LOGGER.warn(CLONE_ERR_MSG, someClass.getName(), e);
+ }
+ }
+ return null;
+ }
}