From 8cbad257df2d5f5c585ce37e61cfd16e402be738 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Thu, 10 Jun 2021 17:38:58 -0400 Subject: Fix sonars in policy-models Fixed: - too many parameters in method call Fixed it by introducing PfFilterParameters. However, classes having the Builder annotation are not easily subclassed, so introduced an interface, too. Issue-ID: POLICY-3094 Change-Id: Ida99522a542b1296b367c55b7e8f8e83783c2e4f Signed-off-by: Jim Hahn --- .../java/org/onap/policy/models/dao/PfDao.java | 14 +---- .../java/org/onap/policy/models/dao/PfFilter.java | 71 ++++++++++++++-------- .../onap/policy/models/dao/PfFilterParameters.java | 40 ++++++++++++ .../policy/models/dao/PfFilterParametersIntfc.java | 40 ++++++++++++ .../onap/policy/models/dao/impl/DefaultPfDao.java | 38 ++---------- 5 files changed, 133 insertions(+), 70 deletions(-) create mode 100644 models-dao/src/main/java/org/onap/policy/models/dao/PfFilterParameters.java create mode 100644 models-dao/src/main/java/org/onap/policy/models/dao/PfFilterParametersIntfc.java (limited to 'models-dao/src/main/java/org') 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 */ - List getFiltered(Class someClass, String name, String version, Instant startTime, - Instant endTime, Map filterMap, String sortOrder, int getRecordNum); + List getFiltered(Class 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 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 void setParams(TypedQuery query, PfFilterParametersIntfc parameters) { + + if (parameters.getFilterMap() != null) { + for (Map.Entry 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 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 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 List getFiltered(final Class someClass, final String name, final String version, - final Instant startTime, final Instant endTime, final Map filterMap, final String sortOrder, - final int getRecordNum) { + public List getFiltered(final Class 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 query = mg.createQuery(setQueryTable(filterQueryString, someClass), someClass); - - if (filterMap != null) { - for (Map.Entry 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(); -- cgit 1.2.3-korg