2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nordix Foundation.
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.dao;
24 import org.onap.policy.models.base.PfConcept;
25 import org.onap.policy.models.dao.impl.PfNonTimestampKeyFilter;
26 import org.onap.policy.models.dao.impl.PfReferenceTimestampKeyFilter;
27 import org.onap.policy.models.dao.impl.PfTimestampKeyFilter;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * This factory class creates a filter class for a key type.
33 * The filter returned depends on the type of class being used.
36 public class PfFilterFactory {
37 // Get a reference to the logger
38 private static final Logger LOGGER = LoggerFactory.getLogger(PfFilterFactory.class);
41 * Return a filter class for the input class.
43 * @param <T> the type of the object to get, a subclass of {@link PfConcept}*
44 * @return the filter type for the input class *
46 public <T extends PfConcept> PfFilter createFilter(final Class<T> someClass) {
47 PfFilter filter = null;
49 switch (getKeyName(someClass)) {
50 case "PfTimestampKey":
51 filter = new PfTimestampKeyFilter();
53 case "PfReferenceTimestampKey":
54 filter = new PfReferenceTimestampKeyFilter();
57 filter = new PfNonTimestampKeyFilter();
63 * Gets the name of the key class of the class invoking the DAO.
64 * @param someClass class that invoked Dao
65 * @return the name of the key class
67 private <T extends PfConcept> String getKeyName(final Class<T> someClass) {
69 var fullClassName = someClass.getDeclaredField("key").getType().toString();
70 return fullClassName.substring(fullClassName.lastIndexOf('.') + 1);
71 } catch (NoSuchFieldException e) {
72 LOGGER.error("Error getting the key:", e);
73 return "NON_TIMESTAMP_KEY";