diff options
Diffstat (limited to 'models-dao')
3 files changed, 46 insertions, 77 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 609afefd4..e635085ff 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 @@ -108,7 +108,7 @@ public interface PfDao { <T extends PfConcept> int deleteByConceptKey(Class<T> someClass, Collection<PfConceptKey> keys); /** - * policypolicypolicy Delete a collection of objects in the database referred to by reference key. + * Delete a collection of objects in the database referred to by reference key. * * @param <T> the type of the objects to delete, a subclass of {@link PfConcept} * @param someClass the class of the objects to delete, a subclass of {@link PfConcept} @@ -132,10 +132,11 @@ 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 key the key of the object to get + * @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 * @return the objects that was retrieved from the database */ - <T extends PfConcept> List<T> getFiltered(Class<T> someClass, PfConceptKey key); + <T extends PfConcept> List<T> getFiltered(Class<T> someClass, String name, String version); /** * Get an object from the database, referred to by concept key. @@ -187,25 +188,6 @@ public interface PfDao { <T extends PfConcept> List<T> getAllVersions(Class<T> someClass, final String name); /** - * Get latest version of objects in the database of a given type. - * - * @param <T> the type of the objects to get, a subclass of {@link PfConcept} - * @param someClass the class of the objects to get, a subclass of {@link PfConcept} - * @return the objects or null if no objects were retrieved - */ - <T extends PfConcept> List<T> getLatestVersions(Class<T> someClass); - - /** - * Get latest version of an object in the database of a given type. - * - * @param <T> the type of the objects to get, a subclass of {@link PfConcept} - * @param someClass the class of the objects to get, a subclass of {@link PfConcept} - * @param conceptName the name of the concept for which to get the latest version - * @return the objects or null if no objects were retrieved - */ - <T extends PfConcept> T getLatestVersion(Class<T> someClass, final String conceptName); - - /** * Get a concept from the database with the given concept key. * * @param <T> the type of the object to get, a subclass of {@link PfConcept} 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 f7659b2ce..182017693 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 @@ -67,7 +67,6 @@ public class DefaultPfDao implements PfDao { private static final String PARENT_NAME_FILTER = "c.key.parentKeyName = :parentname"; private static final String PARENT_VERSION_FILTER = "c.key.parentKeyVersion = :parentversion"; private static final String LOCAL_NAME_FILTER = "c.key.localName = :localname"; - private static final String MAX_VERISON_FILTER = "c.key.version = (SELECT MAX(c.key.version) FROM __TABLE__ c)"; private static final String DELETE_BY_CONCEPT_KEY = DELETE_FROM_TABLE + WHERE + NAME_FILTER + AND + VERSION_FILTER; @@ -80,12 +79,6 @@ public class DefaultPfDao implements PfDao { private static final String SELECT_ALL_VERSIONS = SELECT_FROM_TABLE + WHERE + NAME_FILTER; - private static final String SELECT_LATEST_VERSION = - SELECT_FROM_TABLE + WHERE + NAME_FILTER + AND + MAX_VERISON_FILTER; - - private static final String SELECT_LATEST_VERSIONS = - "SELECT c FROM __TABLE__ c WHERE c.key.version = (SELECT MAX(c.key.version) FROM __TABLE__ c)"; - private static final String SELECT_BY_CONCEPT_KEY = SELECT_FROM_TABLE + WHERE + NAME_FILTER + AND + VERSION_FILTER; @@ -307,16 +300,17 @@ public class DefaultPfDao implements PfDao { } @Override - public <T extends PfConcept> List<T> getFiltered(Class<T> someClass, PfConceptKey key) { - if (key.getName() == null) { + public <T extends PfConcept> List<T> getFiltered(final Class<T> someClass, final String name, + final String version) { + if (name == null) { return getAll(someClass); } - if (key.getVersion() == null) { - return getAllVersions(someClass, key.getName()); + if (version == null) { + return getAllVersions(someClass, name); } - T foundConcept = get(someClass, key); + T foundConcept = get(someClass, new PfConceptKey(name, version)); return (foundConcept == null ? Collections.emptyList() : Collections.singletonList(foundConcept)); } @@ -421,43 +415,6 @@ public class DefaultPfDao implements PfDao { } @Override - public <T extends PfConcept> List<T> getLatestVersions(final Class<T> someClass) { - if (someClass == null) { - return Collections.emptyList(); - } - final EntityManager mg = getEntityManager(); - List<T> ret; - try { - // @formatter:off - return mg.createQuery(setQueryTable(SELECT_LATEST_VERSIONS, someClass), someClass) - .getResultList(); - // @formatter:on - } finally { - mg.close(); - } - } - - @Override - public <T extends PfConcept> T getLatestVersion(final Class<T> someClass, final String conceptName) { - if (someClass == null || conceptName == null) { - return null; - } - final EntityManager mg = getEntityManager(); - List<T> ret; - try { - // @formatter:off - ret = mg.createQuery(setQueryTable(SELECT_LATEST_VERSION, someClass), someClass) - .setParameter(NAME, conceptName) - .getResultList(); - // @formatter:on - } finally { - mg.close(); - } - - return getSingleResult(someClass, conceptName, ret); - } - - @Override public <T extends PfConcept> T getConcept(final Class<T> someClass, final PfConceptKey key) { if (someClass == null || key == null) { return null; diff --git a/models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java b/models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java index a0ad5c21d..bab28c487 100644 --- a/models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java +++ b/models-dao/src/test/java/org/onap/policy/models/dao/EntityTest.java @@ -112,6 +112,8 @@ public class EntityTest { testVersionOps(); + testgetFilteredOps(); + pfDao.close(); } @@ -326,11 +328,39 @@ public class EntityTest { pfDao.create(keyInfo5); assertEquals(3, pfDao.getAllVersions(DummyConceptEntity.class, "AAA0").size()); - DummyConceptEntity latestVersionEntity = pfDao.getLatestVersion(DummyConceptEntity.class, "AAA0"); - assertEquals(aKey2, latestVersionEntity.getKey()); - List<DummyConceptEntity> returnedLatestVersions = pfDao.getLatestVersions(DummyConceptEntity.class); - assertEquals(2, returnedLatestVersions.size()); - assertEquals("0.0.3", returnedLatestVersions.get(0).getKey().getVersion()); - assertEquals("0.0.3", returnedLatestVersions.get(1).getKey().getVersion()); + } + + private void testgetFilteredOps() { + final PfConceptKey aKey0 = new PfConceptKey("AAA0", "0.0.1"); + final PfConceptKey aKey1 = new PfConceptKey("AAA0", "0.0.2"); + final PfConceptKey aKey2 = new PfConceptKey("AAA0", "0.0.3"); + final PfConceptKey bKey0 = new PfConceptKey("BBB0", "0.0.1"); + final PfConceptKey bKey1 = new PfConceptKey("BBB0", "0.0.2"); + final PfConceptKey bKey2 = new PfConceptKey("BBB0", "0.0.3"); + final DummyConceptEntity keyInfo0 = new DummyConceptEntity(aKey0, + UUID.fromString("00000000-0000-0000-0000-000000000000"), "key description 0"); + final DummyConceptEntity keyInfo1 = new DummyConceptEntity(aKey1, + UUID.fromString("00000000-0000-0000-0000-000000000001"), "key description 1"); + final DummyConceptEntity keyInfo2 = new DummyConceptEntity(aKey2, + UUID.fromString("00000000-0000-0000-0000-000000000002"), "key description 2"); + final DummyConceptEntity keyInfo3 = new DummyConceptEntity(bKey0, + UUID.fromString("00000000-0000-0000-0000-000000000000"), "key description 0"); + final DummyConceptEntity keyInfo4 = new DummyConceptEntity(bKey1, + UUID.fromString("00000000-0000-0000-0000-000000000001"), "key description 1"); + final DummyConceptEntity keyInfo5 = new DummyConceptEntity(bKey2, + UUID.fromString("00000000-0000-0000-0000-000000000002"), "key description 2"); + + pfDao.create(keyInfo0); + pfDao.create(keyInfo1); + pfDao.create(keyInfo2); + pfDao.create(keyInfo3); + pfDao.create(keyInfo4); + pfDao.create(keyInfo5); + + assertEquals(6, pfDao.getFiltered(DummyConceptEntity.class, null, null).size()); + assertEquals(3, pfDao.getFiltered(DummyConceptEntity.class, "AAA0", null).size()); + assertEquals(3, pfDao.getFiltered(DummyConceptEntity.class, "BBB0", null).size()); + assertEquals(1, pfDao.getFiltered(DummyConceptEntity.class, "BBB0", "0.0.3").size()); + assertEquals(6, pfDao.getFiltered(DummyConceptEntity.class, null, "0.0.3").size()); } } |