Search in sources :

Example 6 with UserMetadataServiceException

use of com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException in project metacat by Netflix.

the class MySqlTagService method list.

/**
     * Returns the list of <code>QualifiedName</code> of items that are tagged by the
     * given <code>includeTags</code> and do not contain the given <code>excludeTags</code>.
     *
     * @param includeTags  include items that contain tags
     * @param excludeTags  include items that do not contain tags
     * @param sourceName   catalog/source name
     * @param databaseName database name
     * @param tableName    table name
     * @return list of qualified names of the items
     */
@Override
public List<QualifiedName> list(final Set<String> includeTags, final Set<String> excludeTags, final String sourceName, final String databaseName, final String tableName) {
    Set<String> includedNames = Sets.newHashSet();
    final Set<String> excludedNames = Sets.newHashSet();
    final Connection connection = DBUtil.getReadConnection(getDataSource());
    try {
        final QueryRunner runner = new QueryRunner();
        final String wildCardName = QualifiedName.toWildCardString(sourceName, databaseName, tableName);
        //Includes
        String query = String.format(QUERY_SEARCH, "in ('" + Joiner.on("','").skipNulls().join(includeTags) + "')");
        final Object[] params = { includeTags.size() == 0 ? 1 : 0, wildCardName == null ? 1 : 0, wildCardName };
        includedNames.addAll(runner.query(connection, query, new ColumnListHandler<>("name"), params));
        if (!excludeTags.isEmpty()) {
            //Excludes
            query = String.format(QUERY_SEARCH, "in ('" + Joiner.on("','").skipNulls().join(excludeTags) + "')");
            final Object[] eParams = { excludeTags.size() == 0 ? 1 : 0, wildCardName == null ? 1 : 0, wildCardName };
            excludedNames.addAll(runner.query(connection, query, new ColumnListHandler<>("name"), eParams));
        }
    } catch (SQLException e) {
        final String message = String.format("Failed getting the list of qualified names for tags %s", includeTags);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    } finally {
        DBUtil.closeReadConnection(connection);
    }
    if (!excludeTags.isEmpty()) {
        includedNames = Sets.difference(includedNames, excludedNames);
    }
    return includedNames.stream().map(s -> QualifiedName.fromString(s, false)).collect(Collectors.toList());
}
Also used : Lookup(com.netflix.metacat.common.server.model.Lookup) UserMetadataService(com.netflix.metacat.common.server.usermetadata.UserMetadataService) Connection(java.sql.Connection) TagItem(com.netflix.metacat.common.server.model.TagItem) SQLException(java.sql.SQLException) TagService(com.netflix.metacat.common.server.usermetadata.TagService) ImmutableList(com.google.common.collect.ImmutableList) ScalarHandler(org.apache.commons.dbutils.handlers.ScalarHandler) Map(java.util.Map) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) DataSource(javax.sql.DataSource) LookupService(com.netflix.metacat.common.server.usermetadata.LookupService) Config(com.netflix.metacat.common.server.properties.Config) DataSourceManager(com.netflix.metacat.common.server.util.DataSourceManager) QueryRunner(org.apache.commons.dbutils.QueryRunner) Iterator(java.util.Iterator) MetacatJson(com.netflix.metacat.common.json.MetacatJson) BeanHandler(org.apache.commons.dbutils.handlers.BeanHandler) Set(java.util.Set) QualifiedName(com.netflix.metacat.common.QualifiedName) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) DBUtil(com.netflix.metacat.common.server.util.DBUtil) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) ColumnListHandler(org.apache.commons.dbutils.handlers.ColumnListHandler) ResultSetHandler(org.apache.commons.dbutils.ResultSetHandler) Joiner(com.google.common.base.Joiner) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ColumnListHandler(org.apache.commons.dbutils.handlers.ColumnListHandler) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 7 with UserMetadataServiceException

use of com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException in project metacat by Netflix.

the class MySqlTagService method remove.

private void remove(final QualifiedName name, final Set<String> tags, final boolean updateUserMetadata) {
    try {
        final Connection conn = getDataSource().getConnection();
        try {
            remove(conn, name, tags, updateUserMetadata);
            conn.commit();
        } catch (SQLException e) {
            conn.rollback();
            throw e;
        } finally {
            conn.close();
        }
    } catch (SQLException e) {
        final String message = String.format("Failed to remove tags for name %s", name);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    }
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 8 with UserMetadataServiceException

use of com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException in project metacat by Netflix.

the class MySqlTagService method rename.

@Override
public Void rename(final QualifiedName name, final String newTableName) {
    try {
        final Connection conn = getDataSource().getConnection();
        try {
            final QualifiedName newName = QualifiedName.ofTable(name.getCatalogName(), name.getDatabaseName(), newTableName);
            new QueryRunner().update(conn, SQL_UPDATE_TAG_ITEM, newName.toString(), name.toString());
            conn.commit();
        } catch (Exception e) {
            conn.rollback();
            throw e;
        } finally {
            conn.close();
        }
    } catch (SQLException e) {
        final String message = String.format("Failed to rename item name %s", name);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    }
    return null;
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) SQLException(java.sql.SQLException) QualifiedName(com.netflix.metacat.common.QualifiedName) Connection(java.sql.Connection) QueryRunner(org.apache.commons.dbutils.QueryRunner) SQLException(java.sql.SQLException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException)

Example 9 with UserMetadataServiceException

use of com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException in project metacat by Netflix.

the class MySqlTagService method search.

/**
     * Returns the list of <code>QualifiedName</code> of items that have tags containing the given tag text.
     *
     * @param tag          partial text of a tag
     * @param sourceName   source/catalog name
     * @param databaseName database name
     * @param tableName    table name
     * @return list of qualified names of the items
     */
@Override
public List<QualifiedName> search(final String tag, final String sourceName, final String databaseName, final String tableName) {
    final Connection connection = DBUtil.getReadConnection(getDataSource());
    try {
        final String wildCardName = QualifiedName.toWildCardString(sourceName, databaseName, tableName);
        //Includes
        final String query = String.format(QUERY_SEARCH, "like ?");
        final Object[] params = { tag == null ? 1 : 0, tag + "%", wildCardName == null ? 1 : 0, wildCardName };
        return new QueryRunner().query(connection, query, new ColumnListHandler<>("name"), params);
    } catch (SQLException e) {
        final String message = String.format("Failed getting the list of qualified names for tag %s", tag);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    } finally {
        DBUtil.closeReadConnection(connection);
    }
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 10 with UserMetadataServiceException

use of com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException in project metacat by Netflix.

the class MySqlLookupService method setValues.

/**
     * Saves the lookup value.
     *
     * @param name   lookup name
     * @param values multiple values
     * @return returns the lookup with the given name.
     */
@Override
public Lookup setValues(final String name, final Set<String> values) {
    Lookup lookup;
    try {
        final Connection conn = getDataSource().getConnection();
        try {
            lookup = findOrCreateLookupByName(name, conn);
            final Set<String> inserts;
            Set<String> deletes = Sets.newHashSet();
            final Set<String> lookupValues = lookup.getValues();
            if (lookupValues == null || lookupValues.isEmpty()) {
                inserts = values;
            } else {
                inserts = Sets.difference(values, lookupValues).immutableCopy();
                deletes = Sets.difference(lookupValues, values).immutableCopy();
            }
            lookup.setValues(values);
            if (!inserts.isEmpty()) {
                insertLookupValues(lookup.getId(), inserts, conn);
            }
            if (!deletes.isEmpty()) {
                deleteLookupValues(lookup.getId(), deletes, conn);
            }
            conn.commit();
        } catch (SQLException e) {
            conn.rollback();
            throw e;
        } finally {
            conn.close();
        }
    } catch (SQLException e) {
        final String message = String.format("Failed to set the lookup values for name %s", name);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    }
    return lookup;
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Lookup(com.netflix.metacat.common.server.model.Lookup)

Aggregations

UserMetadataServiceException (com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException)38 SQLException (java.sql.SQLException)33 Connection (java.sql.Connection)23 QualifiedName (com.netflix.metacat.common.QualifiedName)15 List (java.util.List)15 Set (java.util.Set)15 QueryRunner (org.apache.commons.dbutils.QueryRunner)15 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)10 Joiner (com.google.common.base.Joiner)10 Maps (com.google.common.collect.Maps)10 MetacatJson (com.netflix.metacat.common.json.MetacatJson)10 Config (com.netflix.metacat.common.server.properties.Config)10 Map (java.util.Map)10 Optional (java.util.Optional)10 Collectors (java.util.stream.Collectors)10 Slf4j (lombok.extern.slf4j.Slf4j)10 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)10 HasDataMetadata (com.netflix.metacat.common.dto.HasDataMetadata)9 HasDefinitionMetadata (com.netflix.metacat.common.dto.HasDefinitionMetadata)9 HasMetadata (com.netflix.metacat.common.dto.HasMetadata)9