Search in sources :

Example 6 with QueryGenerator

use of org.apache.airavata.registry.core.experiment.catalog.utils.QueryGenerator in project airavata by apache.

the class ProjectResource method get.

/**
 * Get results with pagination and ordering
 *
 * @param type
 * @param limit
 * @param offset
 * @param orderByIdentifier
 * @return
 * @throws org.apache.airavata.registry.cpi.RegistryException
 */
public List<ExperimentCatResource> get(ResourceType type, int limit, int offset, Object orderByIdentifier, ResultOrderType resultOrderType) throws RegistryException {
    List<ExperimentCatResource> resourceList = new ArrayList<ExperimentCatResource>();
    EntityManager em = null;
    try {
        if (type == ResourceType.EXPERIMENT) {
            em = ExpCatResourceUtils.getEntityManager();
            em.getTransaction().begin();
            QueryGenerator generator = new QueryGenerator(EXPERIMENT);
            generator.setParameter(ExperimentConstants.PROJECT_ID, id);
            Query q;
            // ordering - supported only by CREATION_TIME
            if (orderByIdentifier != null && resultOrderType != null && orderByIdentifier.equals(Constants.FieldConstants.ExperimentConstants.CREATION_TIME)) {
                q = generator.selectQuery(em, ExperimentConstants.CREATION_TIME, resultOrderType);
            } else {
                q = generator.selectQuery(em);
            }
            // pagination
            if (limit > 0 && offset >= 0) {
                q.setFirstResult(offset);
                q.setMaxResults(limit);
            }
            List<?> results = q.getResultList();
            if (results.size() != 0) {
                for (Object result : results) {
                    Experiment experiment = (Experiment) result;
                    ExperimentResource experimentResource = (ExperimentResource) Utils.getResource(ResourceType.EXPERIMENT, experiment);
                    resourceList.add(experimentResource);
                }
            }
            em.getTransaction().commit();
            em.close();
        } else if (type == ResourceType.PROJECT_USER) {
            em = ExpCatResourceUtils.getEntityManager();
            em.getTransaction().begin();
            QueryGenerator generator = new QueryGenerator(PROJECT_USER);
            generator.setParameter(ProjectUserConstants.PROJECT_ID, id);
            Query q;
            // ordering - only supported only by CREATION_TIME
            if (orderByIdentifier != null && resultOrderType != null && orderByIdentifier.equals(Constants.FieldConstants.ProjectConstants.CREATION_TIME)) {
                q = generator.selectQuery(em, ProjectConstants.CREATION_TIME, resultOrderType);
            } else {
                q = generator.selectQuery(em);
            }
            // pagination
            if (limit > 0 && offset >= 0) {
                q.setFirstResult(offset);
                q.setMaxResults(limit);
            }
            List<?> results = q.getResultList();
            if (results.size() != 0) {
                for (Object result : results) {
                    ProjectUser projectUser = (ProjectUser) result;
                    ProjectUserResource pr = (ProjectUserResource) Utils.getResource(ResourceType.PROJECT_USER, projectUser);
                    resourceList.add(pr);
                }
            }
            em.getTransaction().commit();
            em.close();
        } else {
            logger.error("Unsupported resource type for project resource.", new IllegalArgumentException());
            throw new IllegalArgumentException("Unsupported resource type for project resource.");
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RegistryException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
    return resourceList;
}
Also used : Query(javax.persistence.Query) ArrayList(java.util.ArrayList) RegistryException(org.apache.airavata.registry.cpi.RegistryException) RegistryException(org.apache.airavata.registry.cpi.RegistryException) EntityManager(javax.persistence.EntityManager) QueryGenerator(org.apache.airavata.registry.core.experiment.catalog.utils.QueryGenerator) ArrayList(java.util.ArrayList) List(java.util.List) ExperimentCatResource(org.apache.airavata.registry.core.experiment.catalog.ExperimentCatResource)

Example 7 with QueryGenerator

use of org.apache.airavata.registry.core.experiment.catalog.utils.QueryGenerator in project airavata by apache.

the class TaskResource method remove.

public void remove(ResourceType type, Object name) throws RegistryException {
    EntityManager em = null;
    try {
        em = ExpCatResourceUtils.getEntityManager();
        em.getTransaction().begin();
        Query q;
        QueryGenerator generator;
        switch(type) {
            case TASK_STATUS:
                generator = new QueryGenerator(TASK_STATUS);
                generator.setParameter(TaskStatusConstants.STATUS_ID, name);
                q = generator.deleteQuery(em);
                q.executeUpdate();
                break;
            case TASK_ERROR:
                generator = new QueryGenerator(TASK_ERROR);
                generator.setParameter(TaskErrorConstants.ERROR_ID, name);
                q = generator.deleteQuery(em);
                q.executeUpdate();
                break;
            case JOB:
                generator = new QueryGenerator(JOB);
                generator.setParameter(JobConstants.JOB_ID, name);
                generator.setParameter(JobConstants.TASK_ID, taskId);
                q = generator.deleteQuery(em);
                q.executeUpdate();
                break;
            default:
                logger.error("Unsupported resource type for job details resource.", new IllegalArgumentException());
                break;
        }
        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 RegistryException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) QueryGenerator(org.apache.airavata.registry.core.experiment.catalog.utils.QueryGenerator) RegistryException(org.apache.airavata.registry.cpi.RegistryException) RegistryException(org.apache.airavata.registry.cpi.RegistryException)

Example 8 with QueryGenerator

use of org.apache.airavata.registry.core.experiment.catalog.utils.QueryGenerator in project airavata by apache.

the class TaskResource method get.

public ExperimentCatResource get(ResourceType type, Object name) throws RegistryException {
    EntityManager em = null;
    try {
        em = ExpCatResourceUtils.getEntityManager();
        em.getTransaction().begin();
        QueryGenerator generator;
        Query q;
        switch(type) {
            case TASK_STATUS:
                generator = new QueryGenerator(TASK_STATUS);
                generator.setParameter(TaskStatusConstants.STATUS_ID, name);
                q = generator.selectQuery(em);
                TaskStatus status = (TaskStatus) q.getSingleResult();
                TaskStatusResource statusResource = (TaskStatusResource) Utils.getResource(ResourceType.TASK_STATUS, status);
                em.getTransaction().commit();
                if (em.isOpen()) {
                    if (em.getTransaction().isActive()) {
                        em.getTransaction().rollback();
                    }
                    em.close();
                }
                return statusResource;
            case TASK_ERROR:
                generator = new QueryGenerator(TASK_ERROR);
                generator.setParameter(TaskErrorConstants.ERROR_ID, name);
                q = generator.selectQuery(em);
                TaskError error = (TaskError) q.getSingleResult();
                TaskErrorResource errorResource = (TaskErrorResource) Utils.getResource(ResourceType.TASK_ERROR, error);
                em.getTransaction().commit();
                if (em.isOpen()) {
                    if (em.getTransaction().isActive()) {
                        em.getTransaction().rollback();
                    }
                    em.close();
                }
                return errorResource;
            case JOB:
                generator = new QueryGenerator(JOB);
                generator.setParameter(JobConstants.JOB_ID, name);
                generator.setParameter(JobConstants.TASK_ID, taskId);
                q = generator.selectQuery(em);
                Job job = (Job) q.getSingleResult();
                JobResource jobResource = (JobResource) Utils.getResource(ResourceType.JOB, job);
                em.getTransaction().commit();
                if (em.isOpen()) {
                    if (em.getTransaction().isActive()) {
                        em.getTransaction().rollback();
                    }
                    em.close();
                }
                return jobResource;
            default:
                em.getTransaction().commit();
                if (em.isOpen()) {
                    if (em.getTransaction().isActive()) {
                        em.getTransaction().rollback();
                    }
                    em.close();
                }
                logger.error("Unsupported resource type for Task resource.", new IllegalArgumentException());
                throw new IllegalArgumentException("Unsupported resource type for Task resource.");
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RegistryException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
}
Also used : EntityManager(javax.persistence.EntityManager) QueryGenerator(org.apache.airavata.registry.core.experiment.catalog.utils.QueryGenerator) Query(javax.persistence.Query) TaskError(org.apache.airavata.registry.core.experiment.catalog.model.TaskError) TaskStatus(org.apache.airavata.registry.core.experiment.catalog.model.TaskStatus) Job(org.apache.airavata.registry.core.experiment.catalog.model.Job) RegistryException(org.apache.airavata.registry.cpi.RegistryException) RegistryException(org.apache.airavata.registry.cpi.RegistryException)

Example 9 with QueryGenerator

use of org.apache.airavata.registry.core.experiment.catalog.utils.QueryGenerator in project airavata by apache.

the class GatewayResource method remove.

/**
 * Child resources can be removed from a gateway
 * @param type child resource type
 * @param name child resource name
 */
public void remove(ResourceType type, Object name) throws RegistryException {
    EntityManager em = null;
    try {
        em = ExpCatResourceUtils.getEntityManager();
        em.getTransaction().begin();
        Query q;
        QueryGenerator generator;
        switch(type) {
            case USER:
                generator = new QueryGenerator(USERS);
                generator.setParameter(UserConstants.USERNAME, name);
                q = generator.deleteQuery(em);
                q.executeUpdate();
                break;
            case EXPERIMENT:
                generator = new QueryGenerator(EXPERIMENT);
                generator.setParameter(ExperimentConstants.EXPERIMENT_ID, name);
                q = generator.deleteQuery(em);
                q.executeUpdate();
                break;
            default:
                logger.error("Unsupported resource type for gateway resource.", new IllegalArgumentException());
                break;
        }
        em.getTransaction().commit();
        em.close();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RegistryException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) QueryGenerator(org.apache.airavata.registry.core.experiment.catalog.utils.QueryGenerator) RegistryException(org.apache.airavata.registry.cpi.RegistryException) RegistryException(org.apache.airavata.registry.cpi.RegistryException)

Example 10 with QueryGenerator

use of org.apache.airavata.registry.core.experiment.catalog.utils.QueryGenerator in project airavata by apache.

the class GatewayResource method get.

/**
 * @param type child resource type
 * @return list of child resources
 */
public List<ExperimentCatResource> get(ResourceType type) throws RegistryException {
    List<ExperimentCatResource> resourceList = new ArrayList<ExperimentCatResource>();
    EntityManager em = null;
    try {
        em = ExpCatResourceUtils.getEntityManager();
        em.getTransaction().begin();
        Query q;
        QueryGenerator generator;
        List results;
        switch(type) {
            case PROJECT:
                generator = new QueryGenerator(PROJECT);
                Gateway gatewayModel = em.find(Gateway.class, gatewayId);
                generator.setParameter("gateway", gatewayModel);
                q = generator.selectQuery(em);
                results = q.getResultList();
                if (results.size() != 0) {
                    for (Object result : results) {
                        Project project = (Project) result;
                        ProjectResource projectResource = (ProjectResource) Utils.getResource(ResourceType.PROJECT, project);
                        resourceList.add(projectResource);
                    }
                }
                break;
            case GATEWAY_WORKER:
                generator = new QueryGenerator(GATEWAY_WORKER);
                generator.setParameter(GatewayWorkerConstants.GATEWAY_ID, gatewayId);
                q = generator.selectQuery(em);
                results = q.getResultList();
                if (results.size() != 0) {
                    for (Object result : results) {
                        GatewayWorker gatewayWorker = (GatewayWorker) result;
                        WorkerResource workerResource = (WorkerResource) Utils.getResource(ResourceType.GATEWAY_WORKER, gatewayWorker);
                        resourceList.add(workerResource);
                    }
                }
                break;
            case USER:
                generator = new QueryGenerator(USERS);
                q = generator.selectQuery(em);
                for (Object o : q.getResultList()) {
                    Users user = (Users) o;
                    UserResource userResource = (UserResource) Utils.getResource(ResourceType.USER, user);
                    resourceList.add(userResource);
                }
                break;
            default:
                logger.error("Unsupported resource type for gateway resource.", new IllegalArgumentException());
                throw new IllegalArgumentException("Unsupported resource type for gateway resource.");
        }
        em.getTransaction().commit();
        em.close();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        throw new RegistryException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
    return resourceList;
}
Also used : Query(javax.persistence.Query) ArrayList(java.util.ArrayList) RegistryException(org.apache.airavata.registry.cpi.RegistryException) RegistryException(org.apache.airavata.registry.cpi.RegistryException) EntityManager(javax.persistence.EntityManager) QueryGenerator(org.apache.airavata.registry.core.experiment.catalog.utils.QueryGenerator) ArrayList(java.util.ArrayList) List(java.util.List) ExperimentCatResource(org.apache.airavata.registry.core.experiment.catalog.ExperimentCatResource)

Aggregations

QueryGenerator (org.apache.airavata.registry.core.experiment.catalog.utils.QueryGenerator)31 RegistryException (org.apache.airavata.registry.cpi.RegistryException)31 EntityManager (javax.persistence.EntityManager)25 Query (javax.persistence.Query)25 ArrayList (java.util.ArrayList)11 ExperimentCatResource (org.apache.airavata.registry.core.experiment.catalog.ExperimentCatResource)10 List (java.util.List)8 ExperimentCatalogException (org.apache.airavata.registry.cpi.ExperimentCatalogException)6 Job (org.apache.airavata.registry.core.experiment.catalog.model.Job)2 JobStatus (org.apache.airavata.registry.core.experiment.catalog.model.JobStatus)2 Notification (org.apache.airavata.registry.core.experiment.catalog.model.Notification)2 Process (org.apache.airavata.registry.core.experiment.catalog.model.Process)2 TaskError (org.apache.airavata.registry.core.experiment.catalog.model.TaskError)2 TaskStatus (org.apache.airavata.registry.core.experiment.catalog.model.TaskStatus)2