Search in sources :

Example 11 with UserMetadataServiceException

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

the class MysqlUserMetadataService method softDeleteDataMetadatas.

@Override
public void softDeleteDataMetadatas(final String user, @Nonnull final List<String> uris) {
    try {
        final Connection conn = poolingDataSource.getConnection();
        try {
            final List<List<String>> subLists = Lists.partition(uris, config.getUserMetadataMaxInClauseItems());
            for (List<String> subUris : subLists) {
                _softDeleteDataMetadatas(conn, user, subUris);
            }
            conn.commit();
        } catch (SQLException e) {
            conn.rollback();
            throw e;
        } finally {
            conn.close();
        }
    } catch (SQLException e) {
        log.error("Sql exception", e);
        throw new UserMetadataServiceException(String.format("Failed deleting the data metadata for %s", uris), e);
    }
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) List(java.util.List)

Example 12 with UserMetadataServiceException

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

the class MysqlUserMetadataService method deleteDefinitionMetadatas.

@Override
public void deleteDefinitionMetadatas(@Nonnull final List<QualifiedName> names) {
    try {
        final Connection conn = poolingDataSource.getConnection();
        try {
            final List<List<QualifiedName>> subLists = Lists.partition(names, config.getUserMetadataMaxInClauseItems());
            for (List<QualifiedName> subNames : subLists) {
                _deleteDefinitionMetadatas(conn, subNames);
            }
            conn.commit();
        } catch (SQLException e) {
            conn.rollback();
            throw e;
        } finally {
            conn.close();
        }
    } catch (SQLException e) {
        log.error("Sql exception", e);
        throw new UserMetadataServiceException(String.format("Failed deleting the definition metadata for %s", names), e);
    }
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) SQLException(java.sql.SQLException) QualifiedName(com.netflix.metacat.common.QualifiedName) Connection(java.sql.Connection) List(java.util.List)

Example 13 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) {
    try {
        final Lookup lookup = findOrCreateLookupByName(name);
        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);
        }
        if (!deletes.isEmpty()) {
            deleteLookupValues(lookup.getId(), deletes);
        }
        return lookup;
    } catch (Exception 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);
    }
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) Lookup(com.netflix.metacat.common.server.model.Lookup) SQLException(java.sql.SQLException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Example 14 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
@Transactional(readOnly = true)
public List<QualifiedName> list(@Nullable final Set<String> includeTags, @Nullable final Set<String> excludeTags, @Nullable final String sourceName, @Nullable final String databaseName, @Nullable final String tableName) {
    Set<String> includedNames = Sets.newHashSet();
    final Set<String> excludedNames = Sets.newHashSet();
    final String wildCardName = QualifiedName.toWildCardString(sourceName, databaseName, tableName);
    // Includes
    final Set<String> localIncludes = includeTags != null ? includeTags : Sets.newHashSet();
    try {
        String query = String.format(QUERY_SEARCH, "in ('" + Joiner.on("','").skipNulls().join(localIncludes) + "')");
        final Object[] params = { localIncludes.size() == 0 ? 1 : 0, wildCardName == null ? 1 : 0, wildCardName };
        includedNames.addAll(jdbcTemplate.query(query, params, new int[] { Types.INTEGER, Types.INTEGER, Types.VARCHAR }, (rs, rowNum) -> rs.getString("name")));
        if (excludeTags != null && !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(jdbcTemplate.query(query, eParams, new int[] { Types.INTEGER, Types.INTEGER, Types.VARCHAR }, (rs, rowNum) -> rs.getString("name")));
        }
    } catch (Exception 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);
    }
    if (excludeTags != null && !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) TagItem(com.netflix.metacat.common.server.model.TagItem) KeyHolder(org.springframework.jdbc.support.KeyHolder) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) SQLException(java.sql.SQLException) TagService(com.netflix.metacat.common.server.usermetadata.TagService) Map(java.util.Map) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) LookupService(com.netflix.metacat.common.server.usermetadata.LookupService) Config(com.netflix.metacat.common.server.properties.Config) Nullable(javax.annotation.Nullable) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) MetacatJson(com.netflix.metacat.common.json.MetacatJson) Set(java.util.Set) QualifiedName(com.netflix.metacat.common.QualifiedName) PreparedStatement(java.sql.PreparedStatement) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Statement(java.sql.Statement) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) Joiner(com.google.common.base.Joiner) Transactional(org.springframework.transaction.annotation.Transactional) Types(java.sql.Types) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) SQLException(java.sql.SQLException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 15 with UserMetadataServiceException

use of com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException 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 TagItem tagItem = findOrCreateTagItemByName(name.toString());
        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);
        }
        if (!deletes.isEmpty()) {
            removeTagItemTags(tagItem.getId(), deletes);
        }
        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);
        }
    } catch (Exception 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) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

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