airavata-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From machris...@apache.org
Subject [airavata] 01/02: AIRAVATA-2823 Refactor into common AbstractRepository
Date Tue, 12 Jun 2018 16:06:25 GMT
This is an automated email from the ASF dual-hosted git repository.

machristie pushed a commit to branch group-based-auth
in repository https://gitbox.apache.org/repos/asf/airavata.git

commit 2c489dc72ed88f6b223ca2615c5510f637a01553
Author: Marcus Christie <machristie@apache.org>
AuthorDate: Tue Jun 12 12:03:46 2018 -0400

    AIRAVATA-2823 Refactor into common AbstractRepository
---
 .../core/repositories/AbstractRepository.java      |  62 +++++++++---
 .../appcatalog/AppCatAbstractRepository.java       | 105 ++------------------
 .../appcatalog/StoragePrefRepository.java          |   1 -
 .../expcatalog/ExpCatAbstractRepository.java       | 104 ++------------------
 .../replicacatalog/RepCatAbstractRepository.java   | 108 ++-------------------
 .../WorkflowCatAbstractRepository.java             | 106 ++------------------
 .../workspacecatalog/UserProfileRepository.java    |  72 --------------
 7 files changed, 83 insertions(+), 475 deletions(-)

diff --git a/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/AbstractRepository.java
b/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/AbstractRepository.java
index a83928d..ee4b3c9 100644
--- a/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/AbstractRepository.java
+++ b/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/AbstractRepository.java
@@ -1,5 +1,4 @@
 /*
- *
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
@@ -17,15 +16,17 @@
  * specific language governing permissions and limitations
  * under the License.
  *
-*/
+ */
 package org.apache.airavata.registry.core.repositories;
 
-import org.apache.airavata.registry.core.utils.JPAUtils;
+import org.apache.airavata.registry.core.utils.Committer;
+import org.apache.airavata.registry.core.utils.DBConstants;
 import org.apache.airavata.registry.core.utils.ObjectMapperSingleton;
 import org.dozer.Mapper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import javax.persistence.EntityManager;
 import javax.persistence.Query;
 import java.util.ArrayList;
 import java.util.List;
@@ -49,12 +50,15 @@ public abstract class AbstractRepository<T, E, Id> {
     public T update(T t) {
         Mapper mapper = ObjectMapperSingleton.getInstance();
         E entity = mapper.map(t, dbEntityGenericClass);
-        E persistedCopy = JPAUtils.execute(entityManager -> entityManager.merge(entity));
+//        if (entity instanceof ParentIdPopulater) {
+//            ((ParentIdPopulater) entity).populateParentIds();
+//        }
+        E persistedCopy = execute(entityManager -> entityManager.merge(entity));
         return mapper.map(persistedCopy, thriftGenericClass);
     }
 
     public boolean delete(Id id) {
-        JPAUtils.execute(entityManager -> {
+        execute(entityManager -> {
             E entity = entityManager.find(dbEntityGenericClass, id);
             entityManager.remove(entity);
             return entity;
@@ -63,15 +67,17 @@ public abstract class AbstractRepository<T, E, Id> {
     }
 
     public T get(Id id) {
-        E entity = JPAUtils.execute(entityManager -> entityManager
+        E entity = execute(entityManager -> entityManager
                 .find(dbEntityGenericClass, id));
+        if(entity == null)
+            return null;
         Mapper mapper = ObjectMapperSingleton.getInstance();
         return mapper.map(entity, thriftGenericClass);
     }
 
-    public List<T> select(String query, int limit, int offset) {
-        List resultSet = (List) JPAUtils.execute(entityManager -> entityManager.createQuery(query).setFirstResult(offset)
-                .setMaxResults(offset).getResultList());
+    public List<T> select(String query, int offset) {
+        List resultSet = (List) execute(entityManager -> entityManager.createQuery(query).setFirstResult(offset)
+                .getResultList());
         Mapper mapper = ObjectMapperSingleton.getInstance();
         List<T> gatewayList = new ArrayList<>();
         resultSet.stream().forEach(rs -> gatewayList.add(mapper.map(rs, thriftGenericClass)));
@@ -79,7 +85,9 @@ public abstract class AbstractRepository<T, E, Id> {
     }
 
     public List<T> select(String query, int limit, int offset, Map<String, Object>
queryParams) {
-        List resultSet = (List) JPAUtils.execute(entityManager -> {
+        int newLimit = limit < 0 ? DBConstants.SELECT_MAX_ROWS: limit;
+
+        List resultSet = (List) execute(entityManager -> {
             Query jpaQuery = entityManager.createQuery(query);
 
             for (Map.Entry<String, Object> entry : queryParams.entrySet()) {
@@ -87,7 +95,7 @@ public abstract class AbstractRepository<T, E, Id> {
                 jpaQuery.setParameter(entry.getKey(), entry.getValue());
             }
 
-            return jpaQuery.setFirstResult(offset).setMaxResults(limit).getResultList();
+            return jpaQuery.setFirstResult(offset).setMaxResults(newLimit).getResultList();
 
         });
         Mapper mapper = ObjectMapperSingleton.getInstance();
@@ -95,4 +103,36 @@ public abstract class AbstractRepository<T, E, Id> {
         resultSet.stream().forEach(rs -> gatewayList.add(mapper.map(rs, thriftGenericClass)));
         return gatewayList;
     }
+
+    public boolean isExists(Id id) {
+        return get(id) != null;
+    }
+
+    public <R> R execute(Committer<EntityManager, R> committer){
+        EntityManager entityManager = null;
+        try {
+            entityManager = getEntityManager();
+        } catch (Exception e) {
+            throw new RuntimeException("Failed to get EntityManager", e);
+        }
+        try {
+            entityManager.getTransaction().begin();
+            R r = committer.commit(entityManager);
+            entityManager.getTransaction().commit();
+            return  r;
+        } catch(Exception e) {
+            logger.error("Failed to execute transaction", e);
+            throw e;
+        }finally {
+            if (entityManager != null && entityManager.isOpen()) {
+                if (entityManager.getTransaction().isActive()) {
+                    entityManager.getTransaction().rollback();
+                }
+                entityManager.close();
+            }
+        }
+    }
+
+    abstract protected EntityManager getEntityManager();
+
 }
\ No newline at end of file
diff --git a/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/appcatalog/AppCatAbstractRepository.java
b/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/appcatalog/AppCatAbstractRepository.java
index f27dc76..1468dd8 100644
--- a/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/appcatalog/AppCatAbstractRepository.java
+++ b/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/appcatalog/AppCatAbstractRepository.java
@@ -20,116 +20,27 @@
 */
 package org.apache.airavata.registry.core.repositories.appcatalog;
 
-import org.apache.airavata.registry.core.utils.Committer;
-import org.apache.airavata.registry.core.utils.DBConstants;
+import org.apache.airavata.common.exception.ApplicationSettingsException;
+import org.apache.airavata.registry.core.repositories.AbstractRepository;
 import org.apache.airavata.registry.core.utils.JPAUtil.AppCatalogJPAUtils;
-import org.apache.airavata.registry.core.utils.ObjectMapperSingleton;
-import org.dozer.Mapper;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import javax.persistence.EntityManager;
-import javax.persistence.Query;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
 
-public abstract class AppCatAbstractRepository<T, E, Id> {
+public class AppCatAbstractRepository<T, E, Id> extends AbstractRepository<T, E,
Id> {
     private final static Logger logger = LoggerFactory.getLogger(AppCatAbstractRepository.class);
 
-    private Class<T> thriftGenericClass;
-    private Class<E> dbEntityGenericClass;
-
     public AppCatAbstractRepository(Class<T> thriftGenericClass, Class<E> dbEntityGenericClass)
{
-        this.thriftGenericClass = thriftGenericClass;
-        this.dbEntityGenericClass = dbEntityGenericClass;
-    }
-
-    public T create(T t) {
-        return update(t);
-    }
-
-    public T update(T t) {
-        Mapper mapper = ObjectMapperSingleton.getInstance();
-        E entity = mapper.map(t, dbEntityGenericClass);
-        E persistedCopy = execute(entityManager -> entityManager.merge(entity));
-        return mapper.map(persistedCopy, thriftGenericClass);
-    }
-
-    public boolean delete(Id id) {
-        execute(entityManager -> {
-            E entity = entityManager.find(dbEntityGenericClass, id);
-            entityManager.remove(entity);
-            return entity;
-        });
-        return true;
-    }
-
-    public T get(Id id) {
-        E entity = execute(entityManager -> entityManager
-                .find(dbEntityGenericClass, id));
-        if(entity == null)
-            return null;
-        Mapper mapper = ObjectMapperSingleton.getInstance();
-        return mapper.map(entity, thriftGenericClass);
+        super(thriftGenericClass, dbEntityGenericClass);
     }
 
-    public List<T> select(String query, int offset) {
-        List resultSet = (List) execute(entityManager -> entityManager.createQuery(query).setFirstResult(offset)
-                .getResultList());
-        Mapper mapper = ObjectMapperSingleton.getInstance();
-        List<T> gatewayList = new ArrayList<>();
-        resultSet.stream().forEach(rs -> gatewayList.add(mapper.map(rs, thriftGenericClass)));
-        return gatewayList;
-    }
-
-    public List<T> select(String query, int limit, int offset, Map<String, Object>
queryParams) {
-        int newLimit = limit < 0 ? DBConstants.SELECT_MAX_ROWS: limit;
-
-        List resultSet = (List) execute(entityManager -> {
-            Query jpaQuery = entityManager.createQuery(query);
-
-            for (Map.Entry<String, Object> entry : queryParams.entrySet()) {
-
-                jpaQuery.setParameter(entry.getKey(), entry.getValue());
-            }
-
-            return jpaQuery.setFirstResult(offset).setMaxResults(newLimit).getResultList();
-
-        });
-        Mapper mapper = ObjectMapperSingleton.getInstance();
-        List<T> gatewayList = new ArrayList<>();
-        resultSet.stream().forEach(rs -> gatewayList.add(mapper.map(rs, thriftGenericClass)));
-        return gatewayList;
-    }
-
-    public boolean isExists(Id id) {
-        return get(id) != null;
-    }
-
-    public <R> R execute(Committer<EntityManager, R> committer){
-        EntityManager entityManager = null;
+    @Override
+    protected EntityManager getEntityManager() {
         try {
-            entityManager = AppCatalogJPAUtils.getEntityManager();
-        } catch (Exception e) {
+            return AppCatalogJPAUtils.getEntityManager();
+        } catch (ApplicationSettingsException e) {
             throw new RuntimeException("Failed to get App Catalog EntityManager", e);
         }
-        try {
-            entityManager.getTransaction().begin();
-            R r = committer.commit(entityManager);
-            entityManager.getTransaction().commit();
-            return  r;
-        } catch(Exception e) {
-            logger.error("Failed to execute App Catalog transaction", e);
-            throw e;
-        }finally {
-            if (entityManager != null && entityManager.isOpen()) {
-                if (entityManager.getTransaction().isActive()) {
-                    entityManager.getTransaction().rollback();
-                }
-                entityManager.close();
-            }
-        }
     }
-
 }
\ No newline at end of file
diff --git a/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/appcatalog/StoragePrefRepository.java
b/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/appcatalog/StoragePrefRepository.java
index 347ee03..d1e3a71 100644
--- a/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/appcatalog/StoragePrefRepository.java
+++ b/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/appcatalog/StoragePrefRepository.java
@@ -22,7 +22,6 @@ package org.apache.airavata.registry.core.repositories.appcatalog;
 import org.apache.airavata.model.appcatalog.gatewayprofile.StoragePreference;
 import org.apache.airavata.registry.core.entities.appcatalog.StoragePreferenceEntity;
 import org.apache.airavata.registry.core.entities.appcatalog.StoragePreferencePK;
-import org.apache.airavata.registry.core.repositories.AbstractRepository;
 
 public class StoragePrefRepository extends AppCatAbstractRepository<StoragePreference,
StoragePreferenceEntity, StoragePreferencePK> {
 
diff --git a/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/expcatalog/ExpCatAbstractRepository.java
b/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/expcatalog/ExpCatAbstractRepository.java
index f4e110b..e85491a 100644
--- a/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/expcatalog/ExpCatAbstractRepository.java
+++ b/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/expcatalog/ExpCatAbstractRepository.java
@@ -1,111 +1,23 @@
 package org.apache.airavata.registry.core.repositories.expcatalog;
 
-import org.apache.airavata.registry.core.utils.Committer;
-import org.apache.airavata.registry.core.utils.DBConstants;
+import org.apache.airavata.common.exception.ApplicationSettingsException;
+import org.apache.airavata.registry.core.repositories.AbstractRepository;
 import org.apache.airavata.registry.core.utils.JPAUtil.ExpCatalogJPAUtils;
-import org.apache.airavata.registry.core.utils.ObjectMapperSingleton;
-import org.dozer.Mapper;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import javax.persistence.EntityManager;
-import javax.persistence.Query;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
 
-public abstract class ExpCatAbstractRepository<T, E, Id> {
-    private final static Logger logger = LoggerFactory.getLogger(ExpCatAbstractRepository.class);
-
-    private Class<T> thriftGenericClass;
-    private Class<E> dbEntityGenericClass;
+public class ExpCatAbstractRepository<T, E, Id> extends AbstractRepository<T, E,
Id> {
 
     public ExpCatAbstractRepository(Class<T> thriftGenericClass, Class<E> dbEntityGenericClass)
{
-        this.thriftGenericClass = thriftGenericClass;
-        this.dbEntityGenericClass = dbEntityGenericClass;
-    }
-
-    public T create(T t) {
-        return update(t);
-    }
-
-    public T update(T t) {
-        Mapper mapper = ObjectMapperSingleton.getInstance();
-        E entity = mapper.map(t, dbEntityGenericClass);
-        E persistedCopy = execute(entityManager -> entityManager.merge(entity));
-        return mapper.map(persistedCopy, thriftGenericClass);
-    }
-
-    public boolean delete(Id id) {
-        execute(entityManager -> {
-            E entity = entityManager.find(dbEntityGenericClass, id);
-            entityManager.remove(entity);
-            return entity;
-        });
-        return true;
-    }
-
-    public T get(Id id) {
-        E entity = execute(entityManager -> entityManager
-                .find(dbEntityGenericClass, id));
-        if(entity == null)
-            return null;
-        Mapper mapper = ObjectMapperSingleton.getInstance();
-        return mapper.map(entity, thriftGenericClass);
-    }
-
-    public List<T> select(String query, int offset) {
-        List resultSet = (List) execute(entityManager -> entityManager.createQuery(query).setFirstResult(offset)
-                .getResultList());
-        Mapper mapper = ObjectMapperSingleton.getInstance();
-        List<T> gatewayList = new ArrayList<>();
-        resultSet.stream().forEach(rs -> gatewayList.add(mapper.map(rs, thriftGenericClass)));
-        return gatewayList;
+        super(thriftGenericClass, dbEntityGenericClass);
     }
 
-    public List<T> select(String query, int limit, int offset, Map<String, Object>
queryParams) {
-        int newLimit = limit < 0 ? DBConstants.SELECT_MAX_ROWS: limit;
-
-        List resultSet = (List) execute(entityManager -> {
-            Query jpaQuery = entityManager.createQuery(query);
-
-            for (Map.Entry<String, Object> entry : queryParams.entrySet()) {
-
-                jpaQuery.setParameter(entry.getKey(), entry.getValue());
-            }
-
-            return jpaQuery.setFirstResult(offset).setMaxResults(newLimit).getResultList();
-
-        });
-        Mapper mapper = ObjectMapperSingleton.getInstance();
-        List<T> gatewayList = new ArrayList<>();
-        resultSet.stream().forEach(rs -> gatewayList.add(mapper.map(rs, thriftGenericClass)));
-        return gatewayList;
-    }
-
-    public boolean isExists(Id id) {
-        return get(id) != null;
-    }
-
-    public <R> R execute(Committer<EntityManager, R> committer){
-        EntityManager entityManager = null;
+    @Override
+    protected EntityManager getEntityManager() {
         try {
-            entityManager = ExpCatalogJPAUtils.getEntityManager();
-        } catch (Exception e) {
+            return ExpCatalogJPAUtils.getEntityManager();
+        } catch (ApplicationSettingsException e) {
             throw new RuntimeException("Failed to get Experiment Catalog EntityManager",
e);
         }
-        try {
-            entityManager.getTransaction().begin();
-            R r = committer.commit(entityManager);
-            entityManager.getTransaction().commit();
-            return  r;
-        }finally {
-            if (entityManager != null && entityManager.isOpen()) {
-                if (entityManager.getTransaction().isActive()) {
-                    entityManager.getTransaction().rollback();
-                }
-                entityManager.close();
-            }
-        }
     }
 }
diff --git a/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/replicacatalog/RepCatAbstractRepository.java
b/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/replicacatalog/RepCatAbstractRepository.java
index ceeb228..31e6e5f 100644
--- a/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/replicacatalog/RepCatAbstractRepository.java
+++ b/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/replicacatalog/RepCatAbstractRepository.java
@@ -20,116 +20,24 @@
 */
 package org.apache.airavata.registry.core.repositories.replicacatalog;
 
-import org.apache.airavata.registry.core.utils.Committer;
-import org.apache.airavata.registry.core.utils.DBConstants;
+import org.apache.airavata.common.exception.ApplicationSettingsException;
+import org.apache.airavata.registry.core.repositories.AbstractRepository;
 import org.apache.airavata.registry.core.utils.JPAUtil.RepCatalogJPAUtils;
-import org.apache.airavata.registry.core.utils.ObjectMapperSingleton;
-import org.dozer.Mapper;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import javax.persistence.EntityManager;
-import javax.persistence.Query;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
 
-public abstract class RepCatAbstractRepository<T, E, Id> {
-    private final static Logger logger = LoggerFactory.getLogger(RepCatAbstractRepository.class);
-
-    private Class<T> thriftGenericClass;
-    private Class<E> dbEntityGenericClass;
+public class RepCatAbstractRepository<T, E, Id> extends AbstractRepository<T, E,
Id> {
 
     public RepCatAbstractRepository(Class<T> thriftGenericClass, Class<E> dbEntityGenericClass)
{
-        this.thriftGenericClass = thriftGenericClass;
-        this.dbEntityGenericClass = dbEntityGenericClass;
-    }
-
-    public T create(T t) {
-        return update(t);
-    }
-
-    public T update(T t) {
-        Mapper mapper = ObjectMapperSingleton.getInstance();
-        E entity = mapper.map(t, dbEntityGenericClass);
-        E persistedCopy = execute(entityManager -> entityManager.merge(entity));
-        return mapper.map(persistedCopy, thriftGenericClass);
-    }
-
-    public boolean delete(Id id) {
-        execute(entityManager -> {
-            E entity = entityManager.find(dbEntityGenericClass, id);
-            entityManager.remove(entity);
-            return entity;
-        });
-        return true;
-    }
-
-    public T get(Id id) {
-        E entity = execute(entityManager -> entityManager
-                .find(dbEntityGenericClass, id));
-        if(entity == null)
-            return null;
-        Mapper mapper = ObjectMapperSingleton.getInstance();
-        return mapper.map(entity, thriftGenericClass);
+        super(thriftGenericClass, dbEntityGenericClass);
     }
 
-    public List<T> select(String query, int offset) {
-        List resultSet = (List) execute(entityManager -> entityManager.createQuery(query).setFirstResult(offset)
-                .getResultList());
-        Mapper mapper = ObjectMapperSingleton.getInstance();
-        List<T> gatewayList = new ArrayList<>();
-        resultSet.stream().forEach(rs -> gatewayList.add(mapper.map(rs, thriftGenericClass)));
-        return gatewayList;
-    }
-
-    public List<T> select(String query, int limit, int offset, Map<String, Object>
queryParams) {
-        int newLimit = limit < 0 ? DBConstants.SELECT_MAX_ROWS: limit;
-
-        List resultSet = (List) execute(entityManager -> {
-            Query jpaQuery = entityManager.createQuery(query);
-
-            for (Map.Entry<String, Object> entry : queryParams.entrySet()) {
-
-                jpaQuery.setParameter(entry.getKey(), entry.getValue());
-            }
-
-            return jpaQuery.setFirstResult(offset).setMaxResults(newLimit).getResultList();
-
-        });
-        Mapper mapper = ObjectMapperSingleton.getInstance();
-        List<T> gatewayList = new ArrayList<>();
-        resultSet.stream().forEach(rs -> gatewayList.add(mapper.map(rs, thriftGenericClass)));
-        return gatewayList;
-    }
-
-    public boolean isExists(Id id) {
-        return get(id) != null;
-    }
-
-    public <R> R execute(Committer<EntityManager, R> committer){
-        EntityManager entityManager = null;
+    @Override
+    protected EntityManager getEntityManager() {
         try {
-            entityManager = RepCatalogJPAUtils.getEntityManager();
-        } catch (Exception e) {
+            return RepCatalogJPAUtils.getEntityManager();
+        } catch (ApplicationSettingsException e) {
             throw new RuntimeException("Failed to get Replica Catalog EntityManager", e);
         }
-        try {
-            entityManager.getTransaction().begin();
-            R r = committer.commit(entityManager);
-            entityManager.getTransaction().commit();
-            return r;
-        } catch(Exception e) {
-            logger.error("Failed to execute Replica Catalog transaction", e);
-            throw e;
-        }finally {
-            if (entityManager != null && entityManager.isOpen()) {
-                if (entityManager.getTransaction().isActive()) {
-                    entityManager.getTransaction().rollback();
-                }
-                entityManager.close();
-            }
-        }
     }
-
 }
diff --git a/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/workflowcatalog/WorkflowCatAbstractRepository.java
b/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/workflowcatalog/WorkflowCatAbstractRepository.java
index 94df040..93adbc5 100644
--- a/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/workflowcatalog/WorkflowCatAbstractRepository.java
+++ b/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/workflowcatalog/WorkflowCatAbstractRepository.java
@@ -20,114 +20,24 @@
 */
 package org.apache.airavata.registry.core.repositories.workflowcatalog;
 
-import org.apache.airavata.registry.core.utils.Committer;
-import org.apache.airavata.registry.core.utils.DBConstants;
-import org.apache.airavata.registry.core.utils.JPAUtil.RepCatalogJPAUtils;
+import org.apache.airavata.common.exception.ApplicationSettingsException;
+import org.apache.airavata.registry.core.repositories.AbstractRepository;
 import org.apache.airavata.registry.core.utils.JPAUtil.WorkflowCatalogJPAUtils;
-import org.apache.airavata.registry.core.utils.ObjectMapperSingleton;
-import org.dozer.Mapper;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import javax.persistence.EntityManager;
-import javax.persistence.Query;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
 
-public abstract class WorkflowCatAbstractRepository<T, E, Id> {
-    private final static Logger logger = LoggerFactory.getLogger(WorkflowCatAbstractRepository.class);
-
-    private Class<T> thriftGenericClass;
-    private Class<E> dbEntityGenericClass;
+public class WorkflowCatAbstractRepository<T, E, Id> extends AbstractRepository<T,
E, Id> {
 
     public WorkflowCatAbstractRepository(Class<T> thriftGenericClass, Class<E>
dbEntityGenericClass) {
-        this.thriftGenericClass = thriftGenericClass;
-        this.dbEntityGenericClass = dbEntityGenericClass;
-    }
-
-    public T create(T t) {
-        return update(t);
-    }
-
-    public T update(T t) {
-        Mapper mapper = ObjectMapperSingleton.getInstance();
-        E entity = mapper.map(t, dbEntityGenericClass);
-        E persistedCopy = execute(entityManager -> entityManager.merge(entity));
-        return mapper.map(persistedCopy, thriftGenericClass);
-    }
-
-    public boolean delete(Id id) {
-        execute(entityManager -> {
-            E entity = entityManager.find(dbEntityGenericClass, id);
-            entityManager.remove(entity);
-            return entity;
-        });
-        return true;
-    }
-
-    public T get(Id id) {
-        E entity = execute(entityManager -> entityManager
-                .find(dbEntityGenericClass, id));
-        if(entity == null)
-            return null;
-        Mapper mapper = ObjectMapperSingleton.getInstance();
-        return mapper.map(entity, thriftGenericClass);
+        super(thriftGenericClass, dbEntityGenericClass);
     }
 
-    public List<T> select(String query, int offset) {
-        List resultSet = (List) execute(entityManager -> entityManager.createQuery(query).setFirstResult(offset)
-                .getResultList());
-        Mapper mapper = ObjectMapperSingleton.getInstance();
-        List<T> gatewayList = new ArrayList<>();
-        resultSet.stream().forEach(rs -> gatewayList.add(mapper.map(rs, thriftGenericClass)));
-        return gatewayList;
-    }
-
-    public List<T> select(String query, int limit, int offset, Map<String, Object>
queryParams) {
-        int newLimit = limit < 0 ? DBConstants.SELECT_MAX_ROWS: limit;
-
-        List resultSet = (List) execute(entityManager -> {
-            Query jpaQuery = entityManager.createQuery(query);
-
-            for (Map.Entry<String, Object> entry : queryParams.entrySet()) {
-
-                jpaQuery.setParameter(entry.getKey(), entry.getValue());
-            }
-
-            return jpaQuery.setFirstResult(offset).setMaxResults(newLimit).getResultList();
-
-        });
-        Mapper mapper = ObjectMapperSingleton.getInstance();
-        List<T> gatewayList = new ArrayList<>();
-        resultSet.stream().forEach(rs -> gatewayList.add(mapper.map(rs, thriftGenericClass)));
-        return gatewayList;
-    }
-
-    public boolean isExists(Id id) {
-        return get(id) != null;
-    }
-
-    public <R> R execute(Committer<EntityManager, R> committer){
-        EntityManager entityManager = null;
+    @Override
+    protected EntityManager getEntityManager() {
         try {
-            entityManager = WorkflowCatalogJPAUtils.getEntityManager();
-        } catch (Exception e) {
+            return WorkflowCatalogJPAUtils.getEntityManager();
+        } catch (ApplicationSettingsException e) {
             throw new RuntimeException("Failed to get Workflow Catalog EntityManager", e);
         }
-        try {
-            entityManager.getTransaction().begin();
-            R r = committer.commit(entityManager);
-            entityManager.getTransaction().commit();
-            return  r;
-        }finally {
-            if (entityManager != null && entityManager.isOpen()) {
-                if (entityManager.getTransaction().isActive()) {
-                    entityManager.getTransaction().rollback();
-                }
-                entityManager.close();
-            }
-        }
     }
-
 }
diff --git a/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/workspacecatalog/UserProfileRepository.java
b/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/workspacecatalog/UserProfileRepository.java
deleted file mode 100644
index 3fb71f0..0000000
--- a/modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/workspacecatalog/UserProfileRepository.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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.
- *
-*/
-package org.apache.airavata.registry.core.repositories.workspacecatalog;
-
-import org.apache.airavata.model.user.UserProfile;
-import org.apache.airavata.registry.core.entities.workspacecatalog.UserProfileEntity;
-import org.apache.airavata.registry.core.repositories.AbstractRepository;
-import org.apache.airavata.registry.core.utils.QueryConstants;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-public class UserProfileRepository extends AbstractRepository<UserProfile, UserProfileEntity,
String> {
-    private final static Logger logger = LoggerFactory.getLogger(UserProfileRepository.class);
-
-    public UserProfileRepository(Class<UserProfile> thriftGenericClass, Class<UserProfileEntity>
dbEntityGenericClass) {
-        super(thriftGenericClass, dbEntityGenericClass);
-    }
-
-    @Override
-    public List<UserProfile> select(String query, int offset, int limit) {
-        throw new UnsupportedOperationException("Due to performance overheads this method
is not supported. Instead use" +
-                " UserProfileSummaryRepository");
-    }
-
-    public UserProfile getUserProfileByIdAndGateWay(String userId, String gatewayId)   {
-
-        UserProfile userProfile = null;
-
-        Map<String, Object> queryParam = new HashMap<String, Object>();
-        queryParam.put(UserProfile._Fields.USER_ID.getFieldName(), userId);
-        queryParam.put(UserProfile._Fields.GATEWAY_ID.getFieldName(), gatewayId);
-        List<UserProfile> resultList = select(QueryConstants.FIND_USER_PROFILE_BY_USER_ID,
0, 1, queryParam);
-
-        if (resultList != null && resultList.size() > 0)
-            userProfile =  resultList.get(0);
-
-
-        return userProfile;
-    }
-
-    public List<UserProfile> getAllUserProfilesInGateway(String gatewayId, int offset,
int limit)  {
-
-        Map<String, Object> queryParam = new HashMap<String, Object>();
-        queryParam.put(UserProfile._Fields.GATEWAY_ID.getFieldName(), gatewayId);
-
-        List<UserProfile> resultList = select(QueryConstants.FIND_ALL_USER_PROFILES_BY_GATEWAY_ID,
limit, offset, queryParam);
-
-        return  resultList;
-    }
-}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
machristie@apache.org.

Mime
View raw message