summaryrefslogtreecommitdiffstats
path: root/models-dao/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'models-dao/src/main/java')
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/PfDao.java14
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/PfFilter.java71
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/PfFilterParameters.java40
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/PfFilterParametersIntfc.java40
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java38
5 files changed, 133 insertions, 70 deletions
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
index 378ae59b6..7358ab11b 100644
--- 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
@@ -24,7 +24,6 @@ package org.onap.policy.models.dao;
import java.time.Instant;
import java.util.Collection;
import java.util.List;
-import java.util.Map;
import org.onap.policy.models.base.PfConcept;
import org.onap.policy.models.base.PfConceptKey;
import org.onap.policy.models.base.PfGeneratedIdKey;
@@ -168,19 +167,10 @@ public interface PfDao {
* @param someClass the class of the object to get, a subclass of {@link PfConcept}, if name is null, all concepts
* of type T are returned, if name is not null and version is null, all versions of that concept matching the
* name are returned.
- * @param name the name of the object to get, null returns all objects
- * @param version the version the object to get, null returns all objects for a specified name
- * @param startTime 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 filterMap Map store extra key/value used to filter from database, can be null.
- * @param sortOrder sortOrder to query database
- * @param getRecordNum Total query count from database
+ * @param filterParams filter parameters
* @return the objects that was retrieved from the database
*/
- <T extends PfConcept> List<T> getFiltered(Class<T> someClass, String name, String version, Instant startTime,
- Instant endTime, Map<String, Object> filterMap, String sortOrder, int getRecordNum);
+ <T extends PfConcept> List<T> getFiltered(Class<T> someClass, PfFilterParametersIntfc filterParams);
/**
* Get an object from the database, referred to by concept key.
diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/PfFilter.java b/models-dao/src/main/java/org/onap/policy/models/dao/PfFilter.java
index 9ba2b4ee6..e00e1d6bf 100644
--- a/models-dao/src/main/java/org/onap/policy/models/dao/PfFilter.java
+++ b/models-dao/src/main/java/org/onap/policy/models/dao/PfFilter.java
@@ -21,9 +21,11 @@
package org.onap.policy.models.dao;
-import java.time.Instant;
+import java.sql.Timestamp;
import java.util.Map;
+import javax.persistence.TypedQuery;
import lombok.Data;
+import org.onap.policy.models.base.PfConcept;
/**
* This abstract class is used as a base for the filter implementations.
*
@@ -31,6 +33,7 @@ import lombok.Data;
@Data
public abstract class PfFilter {
+ private static final String WHERE = " WHERE ";
private static final String AND = " AND ";
private static final String ORDER = " ORDER BY ";
@@ -42,35 +45,22 @@ public abstract class PfFilter {
private String keyPrefix;
/**
- * Generates filter string.
- *
- * @param inputFilterString current filterString generated from FilterMap
- * @param name the pdpInstance name for the PDP statistics to get
- * @param startTime the start timeStamp to filter from database, filter rule:
- * startTime <= filteredRecord timeStamp <= endTime. null for ignore end time
- * @param endTime the end timeStamp to filter from database, filter rule:
- * startTime <= filteredRecord timeStamp <= endTime. null for ignore end time
- * @param filterMap Map store extra key/value used to filter from database, can be null *
- * @param sortOrder sortOrder to query database
- * @param getRecordNum Total query count from database
-
+ * Generates the "WHERE" (and "ORDER BY") clause for a JPA query.
*/
- public String addFilter(final String inputFilterString,
- final String name, final Instant startTime, final Instant endTime,
- final Map<String, Object> filterMap, final String sortOrder, final int getRecordNum) {
- var filterQueryString = new StringBuilder(inputFilterString);
- if (filterMap != null) {
- for (String key : filterMap.keySet()) {
+ public String genWhereClause(PfFilterParametersIntfc parameters) {
+ var filterQueryString = new StringBuilder(WHERE);
+ if (parameters.getFilterMap() != null) {
+ for (String key : parameters.getFilterMap().keySet()) {
filterQueryString.append(getKeyPrefix() + key + "= :" + key + AND);
}
}
- if (name != null) {
+ if (parameters.getName() != null) {
filterQueryString.append(getNameFilter() + AND);
}
- if (startTime != null) {
- if (endTime != null) {
+ if (parameters.getStartTime() != null) {
+ if (parameters.getEndTime() != null) {
filterQueryString.append(getTimeStampStartFilter());
filterQueryString.append(AND);
filterQueryString.append(getTimeStampEndFilter());
@@ -78,16 +68,47 @@ public abstract class PfFilter {
filterQueryString.append(getTimeStampStartFilter());
}
} else {
- if (endTime != null) {
+ if (parameters.getEndTime() != null) {
filterQueryString.append(getTimeStampEndFilter());
} else {
filterQueryString.delete(filterQueryString.length() - AND.length(), filterQueryString.length());
}
}
- if (getRecordNum > 0) {
- filterQueryString.append(ORDER + getTimeStampFilter() + sortOrder);
+ if (parameters.getRecordNum() > 0) {
+ filterQueryString.append(ORDER + getTimeStampFilter() + parameters.getSortOrder());
}
return filterQueryString.toString();
}
+
+ /**
+ * Sets the JPA query parameters, based on the filter parameters.
+ * @param query query to populate
+ */
+ public <T extends PfConcept> void setParams(TypedQuery<T> query, PfFilterParametersIntfc parameters) {
+
+ if (parameters.getFilterMap() != null) {
+ for (Map.Entry<String, Object> entry : parameters.getFilterMap().entrySet()) {
+ query.setParameter(entry.getKey(), entry.getValue());
+ }
+ }
+ if (parameters.getName() != null) {
+ query.setParameter(this.getNameParameter(), parameters.getName());
+ }
+ if (parameters.getStartTime() != null) {
+ if (parameters.getEndTime() != null) {
+ query.setParameter("startTime", Timestamp.from(parameters.getStartTime()));
+ query.setParameter("endTime", Timestamp.from(parameters.getEndTime()));
+ } else {
+ query.setParameter("startTime", Timestamp.from(parameters.getStartTime()));
+ }
+ } else {
+ if (parameters.getEndTime() != null) {
+ query.setParameter("endTime", Timestamp.from(parameters.getEndTime()));
+ }
+ }
+ if (parameters.getRecordNum() > 0) {
+ query.setMaxResults(parameters.getRecordNum());
+ }
+ }
}
diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/PfFilterParameters.java b/models-dao/src/main/java/org/onap/policy/models/dao/PfFilterParameters.java
new file mode 100644
index 000000000..24a2ad2e4
--- /dev/null
+++ b/models-dao/src/main/java/org/onap/policy/models/dao/PfFilterParameters.java
@@ -0,0 +1,40 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.dao;
+
+import java.time.Instant;
+import java.util.Map;
+import lombok.Builder;
+import lombok.Getter;
+
+@Getter
+@Builder
+public class PfFilterParameters implements PfFilterParametersIntfc {
+ private String name;
+ private String version;
+ private Instant startTime;
+ private Instant endTime;
+ private Map<String, Object> filterMap;
+
+ private int recordNum;
+ @Builder.Default
+ private String sortOrder = "DESC";
+}
diff --git a/models-dao/src/main/java/org/onap/policy/models/dao/PfFilterParametersIntfc.java b/models-dao/src/main/java/org/onap/policy/models/dao/PfFilterParametersIntfc.java
new file mode 100644
index 000000000..7a383cd9c
--- /dev/null
+++ b/models-dao/src/main/java/org/onap/policy/models/dao/PfFilterParametersIntfc.java
@@ -0,0 +1,40 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.models.dao;
+
+import java.time.Instant;
+import java.util.Map;
+
+public interface PfFilterParametersIntfc {
+ String getName();
+
+ String getVersion();
+
+ Instant getStartTime();
+
+ Instant getEndTime();
+
+ Map<String, Object> getFilterMap();
+
+ int getRecordNum();
+
+ String getSortOrder();
+}
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
index 336abf7df..95a5bf3fa 100644
--- 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
@@ -26,7 +26,6 @@ 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;
@@ -46,6 +45,7 @@ import org.onap.policy.models.dao.DaoParameters;
import org.onap.policy.models.dao.PfDao;
import org.onap.policy.models.dao.PfFilter;
import org.onap.policy.models.dao.PfFilterFactory;
+import org.onap.policy.models.dao.PfFilterParametersIntfc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -388,43 +388,15 @@ public class DefaultPfDao implements PfDao {
}
@Override
- 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) {
+ public <T extends PfConcept> List<T> getFiltered(final Class<T> someClass, PfFilterParametersIntfc filterParams) {
final var mg = getEntityManager();
- var filterQueryString = SELECT_FROM_TABLE + WHERE;
-
try {
- PfFilter timeStampFilter = new PfFilterFactory().createFilter(someClass);
- filterQueryString = timeStampFilter.addFilter(filterQueryString, name, startTime, endTime, filterMap,
- sortOrder, getRecordNum);
+ PfFilter filter = new PfFilterFactory().createFilter(someClass);
+ String filterQueryString = SELECT_FROM_TABLE + filter.genWhereClause(filterParams);
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) {
- query.setParameter(timeStampFilter.getNameParameter(), 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 {
- if (endTime != null) {
- query.setParameter("endTime", Timestamp.from(endTime));
- }
- }
- if (getRecordNum > 0) {
- query.setMaxResults(getRecordNum);
- }
+ filter.setParams(query, filterParams);
LOGGER.debug("filterQueryString is \"{}\"", filterQueryString);
return query.getResultList();