Search in sources :

Example 1 with UserStoragePreference

use of org.apache.airavata.registry.core.app.catalog.model.UserStoragePreference in project airavata by apache.

the class UserStoragePreferenceResource method get.

@Override
public AppCatalogResource get(Object identifier) throws AppCatalogException {
    CompositeIdentifier ids;
    if (identifier instanceof CompositeIdentifier) {
        ids = (CompositeIdentifier) identifier;
    } else {
        logger.error("Identifier should be a instance of CompositeIdentifier class");
        throw new AppCatalogException("Identifier should be a instance of CompositeIdentifier class");
    }
    EntityManager em = null;
    try {
        em = AppCatalogJPAUtils.getEntityManager();
        em.getTransaction().begin();
        AppCatalogQueryGenerator generator = new AppCatalogQueryGenerator(USER_STORAGE_PREFERENCE);
        generator.setParameter(UserStoragePreferenceConstants.STORAGE_ID, ids.getTopLevelIdentifier().toString());
        generator.setParameter(UserStoragePreferenceConstants.USER_ID, ids.getSecondLevelIdentifier().toString());
        generator.setParameter(UserStoragePreferenceConstants.GATEWAY_ID, ids.getThirdLevelIdentifier().toString());
        Query q = generator.selectQuery(em);
        UserStoragePreference preference = (UserStoragePreference) q.getSingleResult();
        UserStoragePreferenceResource preferenceResource = (UserStoragePreferenceResource) AppCatalogJPAUtils.getResource(AppCatalogResourceType.USER_STORAGE_PREFERENCE, preference);
        em.getTransaction().commit();
        if (em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
        return preferenceResource;
    } catch (ApplicationSettingsException e) {
        logger.error(e.getMessage(), e);
        throw new AppCatalogException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
}
Also used : ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) AppCatalogException(org.apache.airavata.registry.cpi.AppCatalogException) EntityManager(javax.persistence.EntityManager) CompositeIdentifier(org.apache.airavata.registry.cpi.CompositeIdentifier) Query(javax.persistence.Query) AppCatalogQueryGenerator(org.apache.airavata.registry.core.app.catalog.util.AppCatalogQueryGenerator) UserStoragePreference(org.apache.airavata.registry.core.app.catalog.model.UserStoragePreference)

Example 2 with UserStoragePreference

use of org.apache.airavata.registry.core.app.catalog.model.UserStoragePreference in project airavata by apache.

the class UserStoragePreferenceResource method get.

@Override
public List<AppCatalogResource> get(String fieldName, Object value) throws AppCatalogException {
    List<AppCatalogResource> preferenceResourceList = new ArrayList<AppCatalogResource>();
    EntityManager em = null;
    try {
        em = AppCatalogJPAUtils.getEntityManager();
        em.getTransaction().begin();
        Query q;
        AppCatalogQueryGenerator generator = new AppCatalogQueryGenerator(USER_STORAGE_PREFERENCE);
        List results;
        if (fieldName.equals(UserStoragePreferenceConstants.STORAGE_ID)) {
            generator.setParameter(UserStoragePreferenceConstants.STORAGE_ID, value);
            q = generator.selectQuery(em);
            results = q.getResultList();
            if (results.size() != 0) {
                for (Object result : results) {
                    UserStoragePreference preference = (UserStoragePreference) result;
                    if (preference.getStorageResourceId() != null) {
                        UserStoragePreferenceResource preferenceResource = (UserStoragePreferenceResource) AppCatalogJPAUtils.getResource(AppCatalogResourceType.USER_STORAGE_PREFERENCE, preference);
                        preferenceResourceList.add(preferenceResource);
                    }
                }
            }
        } else if (fieldName.equals(UserStoragePreferenceConstants.USER_ID)) {
            CompositeIdentifier ids;
            if (value instanceof CompositeIdentifier) {
                ids = (CompositeIdentifier) value;
            } else {
                logger.error("Identifier should be a instance of CompositeIdentifier class");
                throw new AppCatalogException("Identifier should be a instance of CompositeIdentifier class");
            }
            generator.setParameter(UserStoragePreferenceConstants.USER_ID, ids.getTopLevelIdentifier().toString());
            generator.setParameter(UserStoragePreferenceConstants.GATEWAY_ID, ids.getSecondLevelIdentifier().toString());
            q = generator.selectQuery(em);
            results = q.getResultList();
            if (results.size() != 0) {
                for (Object result : results) {
                    UserStoragePreference preference = (UserStoragePreference) result;
                    if (preference.getStorageResourceId() != null) {
                        UserStoragePreferenceResource preferenceResource = (UserStoragePreferenceResource) AppCatalogJPAUtils.getResource(AppCatalogResourceType.USER_STORAGE_PREFERENCE, preference);
                        preferenceResourceList.add(preferenceResource);
                    }
                }
            }
        } else {
            em.getTransaction().commit();
            if (em.isOpen()) {
                if (em.getTransaction().isActive()) {
                    em.getTransaction().rollback();
                }
                em.close();
            }
            logger.error("Unsupported field name for data storage preference Resource.", new IllegalArgumentException());
            throw new IllegalArgumentException("Unsupported field name for data storage preference Resource.");
        }
        em.getTransaction().commit();
        if (em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new AppCatalogException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
    return preferenceResourceList;
}
Also used : CompositeIdentifier(org.apache.airavata.registry.cpi.CompositeIdentifier) Query(javax.persistence.Query) ArrayList(java.util.ArrayList) UserStoragePreference(org.apache.airavata.registry.core.app.catalog.model.UserStoragePreference) AppCatalogException(org.apache.airavata.registry.cpi.AppCatalogException) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException) EntityManager(javax.persistence.EntityManager) AppCatalogException(org.apache.airavata.registry.cpi.AppCatalogException) ArrayList(java.util.ArrayList) List(java.util.List) AppCatalogQueryGenerator(org.apache.airavata.registry.core.app.catalog.util.AppCatalogQueryGenerator)

Example 3 with UserStoragePreference

use of org.apache.airavata.registry.core.app.catalog.model.UserStoragePreference in project airavata by apache.

the class UserStoragePreferenceResource method isExists.

@Override
public boolean isExists(Object identifier) throws AppCatalogException {
    CompositeIdentifier ids;
    if (identifier instanceof CompositeIdentifier) {
        ids = (CompositeIdentifier) identifier;
    } else {
        logger.error("Identifier should be a instance of CompositeIdentifier class");
        throw new AppCatalogException("Identifier should be a instance of CompositeIdentifier class");
    }
    EntityManager em = null;
    try {
        em = AppCatalogJPAUtils.getEntityManager();
        String storageResourceId = ids.getTopLevelIdentifier().toString();
        String userId = ids.getSecondLevelIdentifier().toString();
        String gatewayID = ids.getThirdLevelIdentifier().toString();
        UserStoragePreference existingPreference = em.find(UserStoragePreference.class, new UserStoragePreferencePK(userId, gatewayID, storageResourceId));
        if (em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
        return existingPreference != null;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new AppCatalogException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
}
Also used : UserStoragePreferencePK(org.apache.airavata.registry.core.app.catalog.model.UserStoragePreferencePK) AppCatalogException(org.apache.airavata.registry.cpi.AppCatalogException) EntityManager(javax.persistence.EntityManager) CompositeIdentifier(org.apache.airavata.registry.cpi.CompositeIdentifier) UserStoragePreference(org.apache.airavata.registry.core.app.catalog.model.UserStoragePreference) AppCatalogException(org.apache.airavata.registry.cpi.AppCatalogException) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException)

Example 4 with UserStoragePreference

use of org.apache.airavata.registry.core.app.catalog.model.UserStoragePreference in project airavata by apache.

the class UserStoragePreferenceResource method save.

@Override
public void save() throws AppCatalogException {
    EntityManager em = null;
    try {
        em = AppCatalogJPAUtils.getEntityManager();
        UserStoragePreference existingPreference = em.find(UserStoragePreference.class, new UserStoragePreferencePK(userId, gatewayID, storageResourceId));
        if (em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
        em = AppCatalogJPAUtils.getEntityManager();
        em.getTransaction().begin();
        UserResourceProfile userResourceProfile = em.find(UserResourceProfile.class, new UserResourceProfilePK(userId, gatewayID));
        if (existingPreference != null) {
            existingPreference.setStorageResourceId(storageResourceId);
            existingPreference.setUserId(userId);
            existingPreference.setGatewayID(gatewayID);
            existingPreference.setUserResourceProfile(userResourceProfile);
            existingPreference.setLoginUserName(loginUserName);
            existingPreference.setComputeResourceCSToken(resourceCSToken);
            existingPreference.setFsRootLocation(fsRootLocation);
            em.merge(existingPreference);
        } else {
            UserStoragePreference resourcePreference = new UserStoragePreference();
            resourcePreference.setStorageResourceId(storageResourceId);
            resourcePreference.setGatewayID(gatewayID);
            resourcePreference.setUserId(userId);
            resourcePreference.setUserResourceProfile(userResourceProfile);
            resourcePreference.setLoginUserName(loginUserName);
            resourcePreference.setComputeResourceCSToken(resourceCSToken);
            resourcePreference.setFsRootLocation(fsRootLocation);
            em.persist(resourcePreference);
        }
        em.getTransaction().commit();
        if (em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new AppCatalogException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
}
Also used : UserStoragePreferencePK(org.apache.airavata.registry.core.app.catalog.model.UserStoragePreferencePK) UserResourceProfilePK(org.apache.airavata.registry.core.app.catalog.model.UserResourceProfilePK) EntityManager(javax.persistence.EntityManager) AppCatalogException(org.apache.airavata.registry.cpi.AppCatalogException) UserStoragePreference(org.apache.airavata.registry.core.app.catalog.model.UserStoragePreference) UserResourceProfile(org.apache.airavata.registry.core.app.catalog.model.UserResourceProfile) AppCatalogException(org.apache.airavata.registry.cpi.AppCatalogException) ApplicationSettingsException(org.apache.airavata.common.exception.ApplicationSettingsException)

Aggregations

EntityManager (javax.persistence.EntityManager)4 ApplicationSettingsException (org.apache.airavata.common.exception.ApplicationSettingsException)4 UserStoragePreference (org.apache.airavata.registry.core.app.catalog.model.UserStoragePreference)4 AppCatalogException (org.apache.airavata.registry.cpi.AppCatalogException)4 CompositeIdentifier (org.apache.airavata.registry.cpi.CompositeIdentifier)3 Query (javax.persistence.Query)2 UserStoragePreferencePK (org.apache.airavata.registry.core.app.catalog.model.UserStoragePreferencePK)2 AppCatalogQueryGenerator (org.apache.airavata.registry.core.app.catalog.util.AppCatalogQueryGenerator)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 UserResourceProfile (org.apache.airavata.registry.core.app.catalog.model.UserResourceProfile)1 UserResourceProfilePK (org.apache.airavata.registry.core.app.catalog.model.UserResourceProfilePK)1