Set all cross references of policy/models
[policy/models.git] / models-dao / src / main / java / org / onap / policy / models / dao / PfFilterFactory.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.dao;
23
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;
30
31 /**
32  * This factory class creates a filter class for a key type.
33  * The filter returned depends on the type of class being used.
34  *
35  */
36 public class PfFilterFactory {
37     // Get a reference to the logger
38     private static final Logger LOGGER = LoggerFactory.getLogger(PfFilterFactory.class);
39
40     /**
41      * Return a filter class for the input class.
42      *
43      * @param <T> the type of the object to get, a subclass of {@link PfConcept}*
44      * @return the filter type for the input class   *
45      */
46     public <T extends PfConcept> PfFilter createFilter(final Class<T> someClass) {
47         PfFilter filter = null;
48
49         switch (getKeyName(someClass)) {
50             case "PfTimestampKey":
51                 filter = new PfTimestampKeyFilter();
52                 break;
53             case "PfReferenceTimestampKey":
54                 filter = new PfReferenceTimestampKeyFilter();
55                 break;
56             default:
57                 filter = new PfNonTimestampKeyFilter();
58         }
59         return filter;
60     }
61
62     /**
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
66      */
67     private <T extends PfConcept> String getKeyName(final Class<T> someClass) {
68         try {
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";
74         }
75     }
76 }