Search in sources :

Example 1 with JoinedQueryResult

use of com.fathomdb.jpa.impl.JoinedQueryResult in project platformlayer by platformlayer.

the class JdbcManagedItemRepository method findRoots.

@Override
@JdbcTransaction
public List<ItemBase> findRoots(ProjectId project, boolean fetchTags, SecretProvider secretProvider) throws RepositoryException {
    DbHelper db = new DbHelper(project);
    try {
        // TODO: Push-down logic for item selection as well
        JoinedQueryResult result = db.listRoots();
        List<ItemBase> roots = mapItemsAndTags(project, secretProvider, db, result);
        for (ItemBase root : roots) {
            // A little bit of paranoia
            boolean isRoot = true;
            for (Tag tag : root.getTags()) {
                boolean tagIsParent = Tag.PARENT.getKey().equals(tag.getKey());
                if (tagIsParent) {
                    isRoot = false;
                    break;
                }
            }
            assert isRoot;
            if (!isRoot) {
                throw new IllegalStateException();
            }
        }
        return roots;
    } catch (SQLException e) {
        throw new RepositoryException("Error fetching items", e);
    } finally {
        db.close();
    }
}
Also used : ItemBase(org.platformlayer.core.model.ItemBase) SQLException(java.sql.SQLException) RepositoryException(org.platformlayer.RepositoryException) Tag(org.platformlayer.core.model.Tag) JoinedQueryResult(com.fathomdb.jpa.impl.JoinedQueryResult) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 2 with JoinedQueryResult

use of com.fathomdb.jpa.impl.JoinedQueryResult in project platformlayer by platformlayer.

the class JdbcManagedItemRepository method findAll.

@Override
@JdbcTransaction
public <T extends ItemBase> List<T> findAll(ModelClass<T> modelClass, ProjectId project, boolean fetchTags, SecretProvider secretProvider, Filter filter) throws RepositoryException {
    DbHelper db = new DbHelper(modelClass, project);
    try {
        int projectId = db.mapToValue(project);
        int modelId = db.mapToValue(modelClass.getItemType());
        int serviceId = db.mapToValue(modelClass.getServiceType());
        String filterKey = null;
        JoinedQueryResult result = db.queries.listItems(serviceId, modelId, projectId, filterKey);
        List<T> items = mapItemsAndTags(project, secretProvider, db, result);
        return applyFilter(items, filter);
    } catch (SQLException e) {
        throw new RepositoryException("Error fetching items", e);
    } finally {
        db.close();
    }
}
Also used : SQLException(java.sql.SQLException) RepositoryException(org.platformlayer.RepositoryException) JoinedQueryResult(com.fathomdb.jpa.impl.JoinedQueryResult) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Example 3 with JoinedQueryResult

use of com.fathomdb.jpa.impl.JoinedQueryResult in project platformlayer by platformlayer.

the class JdbcManagedItemRepository method fetchItem.

private <T extends ItemBase> T fetchItem(DbHelper db, ServiceType serviceType, ItemType itemType, ProjectId project, ManagedItemId itemId, Class<T> modelClass, SecretProvider secretProvider, boolean fetchTags) throws SQLException, RepositoryException {
    int projectId = db.mapToValue(project);
    int modelId = db.mapToValue(itemType);
    int serviceId = db.mapToValue(serviceType);
    String filterKey = itemId.getKey();
    JoinedQueryResult result = db.queries.listItems(serviceId, modelId, projectId, filterKey);
    List<T> items = mapItemsAndTags(project, secretProvider, db, result);
    if (items.size() == 0) {
        return null;
    }
    if (items.size() != 1) {
        throw new IllegalStateException();
    }
    return items.get(0);
}
Also used : JoinedQueryResult(com.fathomdb.jpa.impl.JoinedQueryResult)

Example 4 with JoinedQueryResult

use of com.fathomdb.jpa.impl.JoinedQueryResult in project platformlayer by platformlayer.

the class JdbcManagedItemRepository method listAll.

@Override
@JdbcTransaction
public List<ItemBase> listAll(ProjectId project, Filter filter, SecretProvider secretProvider) throws RepositoryException {
    DbHelper db = new DbHelper(project);
    try {
        log.debug("listAll with filter: {}", filter);
        // TODO: Use this logic for item selection as well
        List<Tag> requiredTags = filter.getRequiredTags();
        JoinedQueryResult result;
        if (!requiredTags.isEmpty()) {
            Tag requiredTag = requiredTags.get(0);
            int projectId = db.mapToValue(project);
            result = db.queries.listAllItemsWithTag(projectId, projectId, requiredTag.getKey(), requiredTag.getValue());
        } else {
            log.warn("Unable to optimize filter; selecting all items.  Filter={}", filter);
            result = db.listAllItems();
        }
        List<ItemBase> items = mapItemsAndTags(project, secretProvider, db, result);
        return applyFilter(items, filter);
    } catch (SQLException e) {
        throw new RepositoryException("Error fetching items", e);
    } finally {
        db.close();
    }
}
Also used : ItemBase(org.platformlayer.core.model.ItemBase) SQLException(java.sql.SQLException) RepositoryException(org.platformlayer.RepositoryException) Tag(org.platformlayer.core.model.Tag) JoinedQueryResult(com.fathomdb.jpa.impl.JoinedQueryResult) JdbcTransaction(com.fathomdb.jdbc.JdbcTransaction)

Aggregations

JoinedQueryResult (com.fathomdb.jpa.impl.JoinedQueryResult)4 JdbcTransaction (com.fathomdb.jdbc.JdbcTransaction)3 SQLException (java.sql.SQLException)3 RepositoryException (org.platformlayer.RepositoryException)3 ItemBase (org.platformlayer.core.model.ItemBase)2 Tag (org.platformlayer.core.model.Tag)2