aboutsummaryrefslogtreecommitdiffstats
path: root/models-dao/src/main/java/org/onap
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2021-06-15 14:33:56 -0400
committerJim Hahn <jrh3@att.com>2021-06-15 16:32:37 -0400
commit27ac6bce15318a074d4fb53606571eb30e46bf07 (patch)
tree54b32ac79e62049b50f1439a3c756a287606b5f2 /models-dao/src/main/java/org/onap
parentbb744354e343ca05a0b5921ddb60b4cd2a134ccc (diff)
Replace method parameters with filter parameters
Added more methods taking filter parameters instead of individual method parameters. Removed the old methods. Added genOrderClause(). Issue-ID: POLICY-3094 Change-Id: Ie9d8c13a8d4a7f81e284f07fce3d96a35918a114 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'models-dao/src/main/java/org/onap')
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/PfFilter.java92
-rw-r--r--models-dao/src/main/java/org/onap/policy/models/dao/impl/DefaultPfDao.java4
2 files changed, 66 insertions, 30 deletions
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 e00e1d6bf..7afe243a7 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
@@ -26,9 +26,9 @@ 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.
- *
*/
@Data
@@ -48,37 +48,48 @@ public abstract class PfFilter {
* Generates the "WHERE" (and "ORDER BY") clause for a JPA query.
*/
public String genWhereClause(PfFilterParametersIntfc parameters) {
- var filterQueryString = new StringBuilder(WHERE);
+ if (parameters == null) {
+ return "";
+ }
+
+ var builder = new ClauseBuilder(WHERE, AND);
+
if (parameters.getFilterMap() != null) {
for (String key : parameters.getFilterMap().keySet()) {
- filterQueryString.append(getKeyPrefix() + key + "= :" + key + AND);
+ builder.addCondition(getKeyPrefix(), key, "= :", key);
}
}
if (parameters.getName() != null) {
- filterQueryString.append(getNameFilter() + AND);
+ builder.addCondition(getNameFilter());
}
if (parameters.getStartTime() != null) {
- if (parameters.getEndTime() != null) {
- filterQueryString.append(getTimeStampStartFilter());
- filterQueryString.append(AND);
- filterQueryString.append(getTimeStampEndFilter());
- } else {
- filterQueryString.append(getTimeStampStartFilter());
- }
- } else {
- if (parameters.getEndTime() != null) {
- filterQueryString.append(getTimeStampEndFilter());
- } else {
- filterQueryString.delete(filterQueryString.length() - AND.length(), filterQueryString.length());
- }
+ builder.addCondition(getTimeStampStartFilter());
+ }
+
+ if (parameters.getEndTime() != null) {
+ builder.addCondition(getTimeStampEndFilter());
+ }
+
+ return builder.toString();
+ }
+
+ /**
+ * Generates the "ORDER BY" clause for a JPA query.
+ */
+ public String genOrderClause(PfFilterParametersIntfc parameters) {
+ if (parameters == null) {
+ return "";
}
+ var builder = new ClauseBuilder(ORDER, ", ");
+
if (parameters.getRecordNum() > 0) {
- filterQueryString.append(ORDER + getTimeStampFilter() + parameters.getSortOrder());
+ builder.addCondition(getTimeStampFilter(), parameters.getSortOrder());
}
- return filterQueryString.toString();
+
+ return builder.toString();
}
/**
@@ -86,6 +97,9 @@ public abstract class PfFilter {
* @param query query to populate
*/
public <T extends PfConcept> void setParams(TypedQuery<T> query, PfFilterParametersIntfc parameters) {
+ if (parameters == null) {
+ return;
+ }
if (parameters.getFilterMap() != null) {
for (Map.Entry<String, Object> entry : parameters.getFilterMap().entrySet()) {
@@ -96,19 +110,39 @@ public abstract class PfFilter {
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()));
- }
+ query.setParameter("startTime", Timestamp.from(parameters.getStartTime()));
+ }
+ if (parameters.getEndTime() != null) {
+ query.setParameter("endTime", Timestamp.from(parameters.getEndTime()));
}
if (parameters.getRecordNum() > 0) {
query.setMaxResults(parameters.getRecordNum());
}
}
+
+ private static class ClauseBuilder {
+ private final StringBuilder builder = new StringBuilder();
+ private final String separator;
+
+ private String currentSeparator;
+
+ public ClauseBuilder(String clause, String separator) {
+ this.separator = separator;
+ this.currentSeparator = clause;
+ }
+
+ @Override
+ public String toString() {
+ return builder.toString();
+ }
+
+ public void addCondition(String...condition) {
+ builder.append(currentSeparator);
+ currentSeparator = separator;
+
+ for (String text: condition) {
+ builder.append(text);
+ }
+ }
+ }
}
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 95a5bf3fa..16680d800 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
@@ -393,7 +393,9 @@ public class DefaultPfDao implements PfDao {
try {
PfFilter filter = new PfFilterFactory().createFilter(someClass);
- String filterQueryString = SELECT_FROM_TABLE + filter.genWhereClause(filterParams);
+ String filterQueryString = SELECT_FROM_TABLE
+ + filter.genWhereClause(filterParams)
+ + filter.genOrderClause(filterParams);
TypedQuery<T> query = mg.createQuery(setQueryTable(filterQueryString, someClass), someClass);
filter.setParams(query, filterParams);