Search in sources :

Example 91 with RegistryException

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

the class WorkerResource method searchProjects.

/**
 * To search the projects of user with the given filter criteria and retrieve the results with
 * pagination support. Results can be ordered based on an identifier (i.e column) either ASC or
 * DESC. But in the current implementation ordering is only supported based on the project
 * creation time
 *
 * @param filters
 * @param limit
 * @param offset
 * @param orderByIdentifier
 * @param resultOrderType
 * @return
 * @throws org.apache.airavata.registry.cpi.RegistryException
 */
public List<org.apache.airavata.registry.core.experiment.catalog.resources.ProjectResource> searchProjects(Map<String, String> filters, List<String> accessibleIds, int limit, int offset, Object orderByIdentifier, ResultOrderType resultOrderType) throws RegistryException {
    List<org.apache.airavata.registry.core.experiment.catalog.resources.ProjectResource> result = new ArrayList<org.apache.airavata.registry.core.experiment.catalog.resources.ProjectResource>();
    EntityManager em = null;
    try {
        Map<String, Object> queryParameters = new HashMap<>();
        String query = "SELECT DISTINCT p from Project p WHERE ";
        // FIXME There is a performance bottleneck for using IN clause. Try using temporary tables ?
        if (accessibleIds != null && accessibleIds.size() > 0) {
            query += " p.projectId IN (";
            int accessibleIdIndex = 0;
            for (String id : accessibleIds) {
                String paramName = "accessibleId" + accessibleIdIndex;
                query += (":" + paramName + ",");
                queryParameters.put(paramName, id);
                accessibleIdIndex++;
            }
            query = query.substring(0, query.length() - 1) + ") AND ";
        } else if (ServerSettings.isEnableSharing() && (accessibleIds == null || accessibleIds.size() == 0)) {
            return new ArrayList<>();
        }
        if (filters != null && filters.size() != 0) {
            for (String field : filters.keySet()) {
                String filterVal = filters.get(field);
                if (field.equals(ProjectConstants.USERNAME)) {
                    query += "p." + field + "= :" + field + " AND ";
                    queryParameters.put(field, filterVal);
                } else if (field.equals(ProjectConstants.GATEWAY_ID)) {
                    query += "p." + field + "= :" + field + " AND ";
                    queryParameters.put(field, filterVal);
                } else {
                    if (filterVal.contains("*")) {
                        filterVal = filterVal.replaceAll("\\*", "");
                    }
                    query += "p." + field + " LIKE :" + field + " AND ";
                    queryParameters.put(field, "%" + filterVal + "%");
                }
            }
        }
        query = query.substring(0, query.length() - 5);
        // ordering
        if (orderByIdentifier != null && resultOrderType != null && orderByIdentifier.equals(Constants.FieldConstants.ProjectConstants.CREATION_TIME)) {
            String order = (resultOrderType == ResultOrderType.ASC) ? "ASC" : "DESC";
            query += " ORDER BY p." + ProjectConstants.CREATION_TIME + " " + order;
        }
        em = ExpCatResourceUtils.getEntityManager();
        em.getTransaction().begin();
        Query q;
        // pagination
        if (offset >= 0 && limit >= 0) {
            q = em.createQuery(query).setFirstResult(offset).setMaxResults(limit);
        } else {
            q = em.createQuery(query);
        }
        for (String parameterName : queryParameters.keySet()) {
            q.setParameter(parameterName, queryParameters.get(parameterName));
        }
        List resultList = q.getResultList();
        for (Object o : resultList) {
            Project project = (Project) o;
            org.apache.airavata.registry.core.experiment.catalog.resources.ProjectResource projectResource = (ProjectResource) Utils.getResource(ResourceType.PROJECT, project);
            result.add(projectResource);
        }
        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 92 with RegistryException

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

the class WorkerResource method get.

/**
 * @param type child resource type
 * @param name child resource name
 * @return child resource
 */
public ExperimentCatResource get(ResourceType type, Object name) throws RegistryException {
    ExperimentCatResource result = null;
    EntityManager em = null;
    try {
        em = ExpCatResourceUtils.getEntityManager();
        em.getTransaction().begin();
        QueryGenerator generator;
        Query q;
        switch(type) {
            case PROJECT:
                generator = new QueryGenerator(PROJECT);
                generator.setParameter(ProjectConstants.PROJECT_ID, name);
                q = generator.selectQuery(em);
                Project project = (Project) q.getSingleResult();
                result = Utils.getResource(ResourceType.PROJECT, project);
                break;
            case EXPERIMENT:
                generator = new QueryGenerator(EXPERIMENT);
                generator.setParameter(ExperimentConstants.EXPERIMENT_ID, name);
                q = generator.selectQuery(em);
                Experiment experiment = (Experiment) q.getSingleResult();
                result = Utils.getResource(ResourceType.EXPERIMENT, experiment);
                break;
            default:
                logger.error("Unsupported resource type for worker resource.", new IllegalArgumentException());
                break;
        }
        em.getTransaction().commit();
        em.close();
    } catch (Exception e) {
        throw new RegistryException(e);
    } finally {
        if (em != null && em.isOpen()) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            em.close();
        }
    }
    return result;
}
Also used : EntityManager(javax.persistence.EntityManager) QueryGenerator(org.apache.airavata.registry.core.experiment.catalog.utils.QueryGenerator) Query(javax.persistence.Query) RegistryException(org.apache.airavata.registry.cpi.RegistryException) RegistryException(org.apache.airavata.registry.cpi.RegistryException) ExperimentCatResource(org.apache.airavata.registry.core.experiment.catalog.ExperimentCatResource)

Example 93 with RegistryException

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

the class WorkerResource method get.

/**
 * Method get all results of the given child resource type with paginaltion and ordering
 *
 * @param type              child resource type
 * @param limit
 * @param offset
 * @param orderByIdentifier
 * @param resultOrderType
 * @return list of child resources
 * @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> result = new ArrayList<ExperimentCatResource>();
    EntityManager em = null;
    try {
        em = ExpCatResourceUtils.getEntityManager();
        em.getTransaction().begin();
        QueryGenerator generator;
        Query q;
        switch(type) {
            case PROJECT:
                generator = new QueryGenerator(PROJECT);
                UserPK userPK = new UserPK();
                userPK.setGatewayId(getGatewayId());
                userPK.setUserName(user);
                Users users = em.find(Users.class, userPK);
                Gateway gatewayModel = em.find(Gateway.class, gatewayId);
                generator.setParameter("users", users);
                if (gatewayModel != null) {
                    generator.setParameter("gateway", gatewayModel);
                }
                // 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);
                }
                for (Object o : q.getResultList()) {
                    Project project = (Project) o;
                    org.apache.airavata.registry.core.experiment.catalog.resources.ProjectResource projectResource = (org.apache.airavata.registry.core.experiment.catalog.resources.ProjectResource) Utils.getResource(ResourceType.PROJECT, project);
                    result.add(projectResource);
                }
                break;
            case EXPERIMENT:
                generator = new QueryGenerator(EXPERIMENT);
                generator.setParameter(ExperimentConstants.USER_NAME, getUser());
                // ordering - only supported only by CREATION_TIME
                if (orderByIdentifier != null && resultOrderType != null && orderByIdentifier.equals(Constants.FieldConstants.ProjectConstants.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);
                }
                for (Object o : q.getResultList()) {
                    Experiment experiment = (Experiment) o;
                    ExperimentResource experimentResource = (ExperimentResource) Utils.getResource(ResourceType.EXPERIMENT, experiment);
                    result.add(experimentResource);
                }
                break;
            default:
                logger.error("Unsupported resource type for worker 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();
        }
    }
    return result;
}
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) ExperimentCatResource(org.apache.airavata.registry.core.experiment.catalog.ExperimentCatResource)

Example 94 with RegistryException

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

the class ExpCatResourceUtils method getGateway.

public static ExperimentCatResource getGateway(String gatewayId) throws RegistryException {
    EntityManager em = null;
    try {
        if (isGatewayExist(gatewayId)) {
            em = getEntityManager();
            Gateway gateway = em.find(Gateway.class, gatewayId);
            GatewayResource gatewayResource = (GatewayResource) Utils.getResource(ResourceType.GATEWAY, gateway);
            em.close();
            return gatewayResource;
        }
    } 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 null;
}
Also used : RegistryException(org.apache.airavata.registry.cpi.RegistryException) ExperimentCatalogException(org.apache.airavata.registry.cpi.ExperimentCatalogException) RegistryException(org.apache.airavata.registry.cpi.RegistryException)

Example 95 with RegistryException

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

the class ExpCatResourceUtils method getWorker.

public static ExperimentCatResource getWorker(String gatewayId, String userName) throws RegistryException {
    EntityManager em = null;
    try {
        em = getEntityManager();
        GatewayWorkerPK gatewayWorkerPK = new GatewayWorkerPK();
        gatewayWorkerPK.setGatewayId(gatewayId);
        gatewayWorkerPK.setUserName(userName);
        GatewayWorker gatewayWorker = em.find(GatewayWorker.class, gatewayWorkerPK);
        WorkerResource workerResource = (WorkerResource) Utils.getResource(ResourceType.GATEWAY_WORKER, gatewayWorker);
        em.close();
        return workerResource;
    } 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 : RegistryException(org.apache.airavata.registry.cpi.RegistryException) ExperimentCatalogException(org.apache.airavata.registry.cpi.ExperimentCatalogException) 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