Search in sources :

Example 41 with RegistryException

use of org.apache.airavata.registry.cpi.RegistryException in project airavata by apache.

the class WorkerResource method getExperimentStatisticsForState.

private List<ExperimentSummaryResource> getExperimentStatisticsForState(ExperimentState expState, String gatewayId, Timestamp fromTime, Timestamp toTime, String userName, String applicationName, String resourceHostName) throws RegistryException {
    EntityManager em = null;
    List<ExperimentSummaryResource> result = new ArrayList();
    try {
        Map<String, Object> queryParameters = new HashMap<>();
        String query = "SELECT e FROM ExperimentSummary e " + "WHERE ";
        if (expState != null) {
            query += "e.state='" + expState.toString() + "' AND ";
        }
        query += "e.creationTime > '" + fromTime + "' " + "AND e.creationTime <'" + toTime + "' AND ";
        query += "e." + ExperimentConstants.GATEWAY_ID + "= '" + gatewayId + "' ";
        if (userName != null) {
            query += "AND e.userName LIKE :userName ";
            queryParameters.put("userName", "%" + userName + "%");
        }
        if (applicationName != null) {
            query += "AND e.executionId LIKE :applicationName ";
            queryParameters.put("applicationName", "%" + applicationName + "%");
        }
        if (resourceHostName != null) {
            query += "AND e.resourceHostId LIKE :resourceHostName ";
            queryParameters.put("resourceHostName", "%" + resourceHostName + "%");
        }
        query += "ORDER BY e.creationTime DESC";
        em = ExpCatResourceUtils.getEntityManager();
        em.getTransaction().begin();
        Query q = em.createQuery(query);
        for (String parameterName : queryParameters.keySet()) {
            q.setParameter(parameterName, queryParameters.get(parameterName));
        }
        List resultList = q.getResultList();
        for (Object o : resultList) {
            ExperimentSummary experimentSummary = (ExperimentSummary) o;
            ExperimentSummaryResource experimentSummaryResource = (ExperimentSummaryResource) Utils.getResource(ResourceType.EXPERIMENT_SUMMARY, experimentSummary);
            result.add(experimentSummaryResource);
        }
        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 result;
}
Also used : Query(javax.persistence.Query) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RegistryException(org.apache.airavata.registry.cpi.RegistryException) RegistryException(org.apache.airavata.registry.cpi.RegistryException) EntityManager(javax.persistence.EntityManager) ArrayList(java.util.ArrayList) List(java.util.List)

Example 42 with RegistryException

use of org.apache.airavata.registry.cpi.RegistryException in project airavata by apache.

the class ProcessStatusResource method save.

public void save() throws RegistryException {
    EntityManager em = null;
    try {
        if (processId == null || statusId == null) {
            throw new RegistryException("Does not have the process id or status id");
        }
        em = ExpCatResourceUtils.getEntityManager();
        ProcessStatus processStatus;
        ProcessStatusPK processStatusPK = new ProcessStatusPK();
        processStatusPK.setStatusId(statusId);
        processStatusPK.setProcessId(processId);
        ProcessStatus existingStatus = em.find(ProcessStatus.class, processStatusPK);
        if (em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
        em = ExpCatResourceUtils.getEntityManager();
        em.getTransaction().begin();
        if (existingStatus == null) {
            processStatus = new ProcessStatus();
        } else {
            processStatus = existingStatus;
        }
        processStatus.setStatusId(statusId);
        processStatus.setProcessId(processId);
        processStatus.setState(state);
        processStatus.setReason(reason);
        if (timeOfStateChange == null) {
            timeOfStateChange = AiravataUtils.getCurrentTimestamp();
        }
        processStatus.setTimeOfStateChange(timeOfStateChange);
        if (existingStatus == null) {
            em.persist(processStatus);
        } else {
            em.merge(processStatus);
        }
        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) ProcessStatus(org.apache.airavata.registry.core.experiment.catalog.model.ProcessStatus) RegistryException(org.apache.airavata.registry.cpi.RegistryException) ProcessStatusPK(org.apache.airavata.registry.core.experiment.catalog.model.ProcessStatusPK) RegistryException(org.apache.airavata.registry.cpi.RegistryException)

Example 43 with RegistryException

use of org.apache.airavata.registry.cpi.RegistryException in project airavata by apache.

the class ProjectResource method get.

/**
 * @param type child resource type
 * @return list of child resources
 */
@Override
public List<ExperimentCatResource> get(ResourceType type) 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 = generator.selectQuery(em);
            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 = generator.selectQuery(em);
            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 44 with RegistryException

use of org.apache.airavata.registry.cpi.RegistryException 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 45 with RegistryException

use of org.apache.airavata.registry.cpi.RegistryException in project airavata by apache.

the class ProjectUserResource method save.

public void save() throws RegistryException {
    EntityManager em = null;
    try {
        em = ExpCatResourceUtils.getEntityManager();
        ProjectUserPK projectUserPK = new ProjectUserPK();
        projectUserPK.setProjectId(projectId);
        projectUserPK.setUserName(userName);
        ProjectUser existingPrUser = em.find(ProjectUser.class, projectUserPK);
        em.close();
        em = ExpCatResourceUtils.getEntityManager();
        em.getTransaction().begin();
        ProjectUser prUser = new ProjectUser();
        prUser.setProjectId(projectId);
        prUser.setUserName(userName);
        Project project = em.find(Project.class, projectId);
        prUser.setProject(project);
        if (existingPrUser != null) {
            existingPrUser.setProjectId(projectId);
            existingPrUser.setUserName(userName);
            existingPrUser.setProject(project);
            em.merge(existingPrUser);
        } else {
            em.persist(prUser);
        }
        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) RegistryException(org.apache.airavata.registry.cpi.RegistryException) RegistryException(org.apache.airavata.registry.cpi.RegistryException)

Aggregations

RegistryException (org.apache.airavata.registry.cpi.RegistryException)134 EntityManager (javax.persistence.EntityManager)54 Query (javax.persistence.Query)29 QueryGenerator (org.apache.airavata.registry.core.experiment.catalog.utils.QueryGenerator)29 ArrayList (java.util.ArrayList)15 List (java.util.List)12 ExperimentCatResource (org.apache.airavata.registry.core.experiment.catalog.ExperimentCatResource)12 OutputDataObjectType (org.apache.airavata.model.application.io.OutputDataObjectType)11 ExperimentCatalogException (org.apache.airavata.registry.cpi.ExperimentCatalogException)8 AiravataException (org.apache.airavata.common.exception.AiravataException)6 GFacException (org.apache.airavata.gfac.core.GFacException)6 InputDataObjectType (org.apache.airavata.model.application.io.InputDataObjectType)6 Node (org.apache.airavata.workflow.model.graph.Node)6 DynamicNode (org.apache.airavata.workflow.model.graph.dynamic.DynamicNode)6 SubWorkflowNode (org.apache.airavata.workflow.model.graph.subworkflow.SubWorkflowNode)6 WSNode (org.apache.airavata.workflow.model.graph.ws.WSNode)6 Timestamp (java.sql.Timestamp)5 ApplicationSettingsException (org.apache.airavata.common.exception.ApplicationSettingsException)4 DataPort (org.apache.airavata.workflow.model.graph.DataPort)4 HashMap (java.util.HashMap)3