Search in sources :

Example 1 with StorageException

use of io.apiman.manager.api.core.exceptions.StorageException in project apiman by apiman.

the class EsMarshalling method marshall.

/**
 * Marshals the given bean into the given map.
 * @param bean the bean
 * @return the content builder
 * @throws StorageException when a storage problem occurs while storing a bean
 */
public static XContentBuilder marshall(ApiDefinitionBean bean) throws StorageException {
    try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
        preMarshall(bean);
        builder.startObject().field("data", bean.getData()).endObject();
        postMarshall(bean);
        return builder;
    } catch (IOException e) {
        throw new StorageException(e);
    }
}
Also used : IOException(java.io.IOException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 2 with StorageException

use of io.apiman.manager.api.core.exceptions.StorageException in project apiman by apiman.

the class EsMarshalling method marshall.

/**
 * Marshals the given bean into the given map.
 * @param bean the bean
 * @return the content builder
 * @throws StorageException when a storage problem occurs while storing a bean
 */
public static XContentBuilder marshall(PlanBean bean) throws StorageException {
    try (XContentBuilder builder = XContentFactory.jsonBuilder()) {
        preMarshall(bean);
        builder.startObject().field("organizationId", bean.getOrganization().getId()).field("organizationName", bean.getOrganization().getName()).field("id", bean.getId()).field("name", bean.getName()).field("description", bean.getDescription()).field("createdBy", bean.getCreatedBy()).field("createdOn", bean.getCreatedOn().getTime()).endObject();
        postMarshall(bean);
        return builder;
    } catch (IOException e) {
        throw new StorageException(e);
    }
}
Also used : IOException(java.io.IOException) StorageException(io.apiman.manager.api.core.exceptions.StorageException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 3 with StorageException

use of io.apiman.manager.api.core.exceptions.StorageException in project apiman by apiman.

the class JpaStorage method getUserMemberships.

/**
 * @see io.apiman.manager.api.core.IStorageQuery#getUserMemberships(java.lang.String)
 */
@Override
public Set<RoleMembershipBean> getUserMemberships(String userId) throws StorageException {
    Set<RoleMembershipBean> memberships = new HashSet<>();
    beginTx();
    try {
        EntityManager entityManager = getActiveEntityManager();
        CriteriaBuilder builder = entityManager.getCriteriaBuilder();
        CriteriaQuery<RoleMembershipBean> criteriaQuery = builder.createQuery(RoleMembershipBean.class);
        Root<RoleMembershipBean> from = criteriaQuery.from(RoleMembershipBean.class);
        criteriaQuery.where(builder.equal(from.get("userId"), userId));
        TypedQuery<RoleMembershipBean> typedQuery = entityManager.createQuery(criteriaQuery);
        List<RoleMembershipBean> resultList = typedQuery.getResultList();
        memberships.addAll(resultList);
        return memberships;
    } catch (Throwable t) {
        logger.error(t.getMessage(), t);
        throw new StorageException(t);
    } finally {
        rollbackTx();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Example 4 with StorageException

use of io.apiman.manager.api.core.exceptions.StorageException in project apiman by apiman.

the class JpaStorage method getOrgs.

/**
 * @see io.apiman.manager.api.core.IStorageQuery#getOrgs(java.util.Set)
 */
@Override
public List<OrganizationSummaryBean> getOrgs(Set<String> orgIds) throws StorageException {
    List<OrganizationSummaryBean> orgs = new ArrayList<>();
    if (orgIds == null || orgIds.isEmpty()) {
        return orgs;
    }
    beginTx();
    try {
        EntityManager entityManager = getActiveEntityManager();
        String jpql = "SELECT o from OrganizationBean o WHERE o.id IN :orgs ORDER BY o.id ASC";
        Query query = entityManager.createQuery(jpql);
        query.setParameter("orgs", orgIds);
        List<OrganizationBean> qr = query.getResultList();
        for (OrganizationBean bean : qr) {
            OrganizationSummaryBean summary = new OrganizationSummaryBean();
            summary.setId(bean.getId());
            summary.setName(bean.getName());
            summary.setDescription(bean.getDescription());
            orgs.add(summary);
        }
        return orgs;
    } catch (Throwable t) {
        logger.error(t.getMessage(), t);
        throw new StorageException(t);
    } finally {
        rollbackTx();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) IStorageQuery(io.apiman.manager.api.core.IStorageQuery) OrganizationBean(io.apiman.manager.api.beans.orgs.OrganizationBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Example 5 with StorageException

use of io.apiman.manager.api.core.exceptions.StorageException in project apiman by apiman.

the class JpaStorage method getClientsInOrgs.

/**
 * @see io.apiman.manager.api.core.IStorageQuery#getClientsInOrgs(java.util.Set)
 */
@Override
public List<ClientSummaryBean> getClientsInOrgs(Set<String> orgIds) throws StorageException {
    List<ClientSummaryBean> rval = new ArrayList<>();
    if (orgIds == null || orgIds.isEmpty()) {
        return rval;
    }
    beginTx();
    try {
        EntityManager entityManager = getActiveEntityManager();
        String jpql = "SELECT a FROM ClientBean a JOIN a.organization o WHERE o.id IN :orgs ORDER BY a.id ASC";
        Query query = entityManager.createQuery(jpql);
        query.setParameter("orgs", orgIds);
        List<ClientBean> qr = query.getResultList();
        for (ClientBean bean : qr) {
            ClientSummaryBean summary = new ClientSummaryBean();
            summary.setId(bean.getId());
            summary.setName(bean.getName());
            summary.setDescription(bean.getDescription());
            // TODO find the number of contracts - probably need a native SQL query to pull that together
            summary.setNumContracts(0);
            OrganizationBean org = bean.getOrganization();
            summary.setOrganizationId(org.getId());
            summary.setOrganizationName(org.getName());
            rval.add(summary);
        }
        return rval;
    } catch (Throwable t) {
        logger.error(t.getMessage(), t);
        throw new StorageException(t);
    } finally {
        rollbackTx();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) IStorageQuery(io.apiman.manager.api.core.IStorageQuery) ClientBean(io.apiman.manager.api.beans.clients.ClientBean) OrganizationBean(io.apiman.manager.api.beans.orgs.OrganizationBean) StorageException(io.apiman.manager.api.core.exceptions.StorageException)

Aggregations

StorageException (io.apiman.manager.api.core.exceptions.StorageException)150 IOException (java.io.IOException)51 SystemErrorException (io.apiman.manager.api.rest.exceptions.SystemErrorException)43 EntityManager (javax.persistence.EntityManager)33 ApiVersionBean (io.apiman.manager.api.beans.apis.ApiVersionBean)25 ArrayList (java.util.ArrayList)24 ApiGatewayBean (io.apiman.manager.api.beans.apis.ApiGatewayBean)23 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)20 PolicyBean (io.apiman.manager.api.beans.policies.PolicyBean)19 Date (java.util.Date)19 OrganizationBean (io.apiman.manager.api.beans.orgs.OrganizationBean)18 ApiBean (io.apiman.manager.api.beans.apis.ApiBean)17 ApiPlanBean (io.apiman.manager.api.beans.apis.ApiPlanBean)17 RoleBean (io.apiman.manager.api.beans.idm.RoleBean)17 ClientVersionBean (io.apiman.manager.api.beans.clients.ClientVersionBean)16 PlanVersionBean (io.apiman.manager.api.beans.plans.PlanVersionBean)16 IStorageQuery (io.apiman.manager.api.core.IStorageQuery)16 PagingBean (io.apiman.manager.api.beans.search.PagingBean)14 ContractSummaryBean (io.apiman.manager.api.beans.summary.ContractSummaryBean)14 IGatewayLink (io.apiman.manager.api.gateway.IGatewayLink)14