Search in sources :

Example 1 with TagItem

use of com.netflix.metacat.common.server.model.TagItem in project metacat by Netflix.

the class MySqlTagService method remove.

private void remove(final Connection conn, final QualifiedName name, final Set<String> tags, final boolean updateUserMetadata) throws SQLException {
    new QueryRunner().update(conn, String.format(SQL_DELETE_TAG_ITEM_TAGS_BY_NAME_TAGS, "'" + Joiner.on("','").skipNulls().join(tags) + "'"), name.toString());
    if (updateUserMetadata) {
        final TagItem tagItem = get(name);
        tagItem.getValues().removeAll(tags);
        final Map<String, Set<String>> data = Maps.newHashMap();
        data.put(NAME_TAGS, tagItem.getValues());
        userMetadataService.saveDefinitionMetadata(name, "admin", Optional.of(metacatJson.toJsonObject(data)), true);
    }
}
Also used : TagItem(com.netflix.metacat.common.server.model.TagItem) Set(java.util.Set) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 2 with TagItem

use of com.netflix.metacat.common.server.model.TagItem in project metacat by Netflix.

the class MySqlTagService method get.

/**
     * Returns the TagItem for the given <code>name</code>.
     *
     * @param name tag name
     * @return TagItem
     */
public TagItem get(final String name) {
    TagItem result = null;
    final Connection connection = DBUtil.getReadConnection(getDataSource());
    try {
        final ResultSetHandler<TagItem> handler = new BeanHandler<>(TagItem.class);
        result = new QueryRunner().query(connection, SQL_GET_TAG_ITEM, handler, name);
        if (result != null) {
            result.setValues(getValues(result.getId()));
        }
    } catch (Exception e) {
        final String message = String.format("Failed to get the tag item for name %s", name);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    } finally {
        DBUtil.closeReadConnection(connection);
    }
    return result;
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) TagItem(com.netflix.metacat.common.server.model.TagItem) Connection(java.sql.Connection) BeanHandler(org.apache.commons.dbutils.handlers.BeanHandler) QueryRunner(org.apache.commons.dbutils.QueryRunner) SQLException(java.sql.SQLException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException)

Example 3 with TagItem

use of com.netflix.metacat.common.server.model.TagItem in project metacat by Netflix.

the class MySqlTagService method setTableTags.

/**
     * Tags the given table with the given <code>tags</code>.
     *
     * @param name table name
     * @param tags list of tags
     * @return return the complete list of tags associated with the table
     */
@Override
public Set<String> setTableTags(final QualifiedName name, final Set<String> tags, final boolean updateUserMetadata) {
    addTags(tags);
    try {
        final Connection conn = getDataSource().getConnection();
        try {
            final TagItem tagItem = findOrCreateTagItemByName(name.toString(), conn);
            final Set<String> inserts;
            Set<String> deletes = Sets.newHashSet();
            Set<String> values = tagItem.getValues();
            if (values == null || values.isEmpty()) {
                inserts = tags;
            } else {
                inserts = Sets.difference(tags, values).immutableCopy();
                deletes = Sets.difference(values, tags).immutableCopy();
            }
            values = tags;
            if (!inserts.isEmpty()) {
                insertTagItemTags(tagItem.getId(), inserts, conn);
            }
            if (!deletes.isEmpty()) {
                removeTagItemTags(tagItem.getId(), deletes, conn);
            }
            if (updateUserMetadata) {
                // Set the tags in user metadata
                final Map<String, Set<String>> data = Maps.newHashMap();
                data.put(NAME_TAGS, values);
                userMetadataService.saveDefinitionMetadata(name, "admin", Optional.of(metacatJson.toJsonObject(data)), true);
            }
            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);
    }
    return tags;
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) TagItem(com.netflix.metacat.common.server.model.TagItem) Set(java.util.Set) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 4 with TagItem

use of com.netflix.metacat.common.server.model.TagItem in project metacat by Netflix.

the class MySqlTagService method findOrCreateTagItemByName.

private TagItem findOrCreateTagItemByName(final String name, final Connection conn) throws SQLException {
    TagItem result = get(name);
    if (result == null) {
        final Object[] params = { name, config.getTagServiceUserAdmin(), config.getTagServiceUserAdmin() };
        final Long id = new QueryRunner().insert(conn, SQL_INSERT_TAG_ITEM, new ScalarHandler<>(1), params);
        result = new TagItem();
        result.setName(name);
        result.setId(id);
    }
    return result;
}
Also used : TagItem(com.netflix.metacat.common.server.model.TagItem) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Aggregations

TagItem (com.netflix.metacat.common.server.model.TagItem)4 QueryRunner (org.apache.commons.dbutils.QueryRunner)3 UserMetadataServiceException (com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException)2 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 Set (java.util.Set)2 BeanHandler (org.apache.commons.dbutils.handlers.BeanHandler)1