Search in sources :

Example 16 with UserMetadataServiceException

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

the class MySqlTagService method remove.

/**
 * remove.
 *
 * @param name               qualifiedName
 * @param tags               tags
 * @param updateUserMetadata flag to update user metadata
 */
public void remove(final QualifiedName name, final Set<String> tags, final boolean updateUserMetadata) {
    try {
        jdbcTemplate.update(String.format(SQL_DELETE_TAG_ITEM_TAGS_BY_NAME_TAGS, "'" + Joiner.on("','").skipNulls().join(tags) + "'"), new SqlParameterValue(Types.VARCHAR, 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);
        }
    } 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);
    }
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) TagItem(com.netflix.metacat.common.server.model.TagItem) Set(java.util.Set) SqlParameterValue(org.springframework.jdbc.core.SqlParameterValue) SQLException(java.sql.SQLException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Example 17 with UserMetadataServiceException

use of com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException 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
 */
@Transactional(readOnly = true)
public TagItem get(final String name) {
    try {
        return jdbcTemplate.queryForObject(SQL_GET_TAG_ITEM, new Object[] { name }, new int[] { Types.VARCHAR }, (rs, rowNum) -> {
            final TagItem tagItem = new TagItem();
            tagItem.setId(rs.getLong("id"));
            tagItem.setName(rs.getString("name"));
            tagItem.setCreatedBy(rs.getString("createdBy"));
            tagItem.setLastUpdated(rs.getDate("lastUpdated"));
            tagItem.setLastUpdatedBy(rs.getString("lastUpdatedBy"));
            tagItem.setDateCreated(rs.getDate("dateCreated"));
            tagItem.setValues(getValues(rs.getLong("id")));
            return tagItem;
        });
    } catch (EmptyResultDataAccessException e) {
        return null;
    } catch (Exception e) {
        final String message = String.format("Failed to get the tag for name %s", name);
        log.error(message, e);
        throw new UserMetadataServiceException(message, e);
    }
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) TagItem(com.netflix.metacat.common.server.model.TagItem) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) SQLException(java.sql.SQLException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 18 with UserMetadataServiceException

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

the class MysqlUserMetadataService method getJsonForKey.

private Optional<ObjectNode> getJsonForKey(final String query, final String keyValue) {
    Optional<ObjectNode> result = Optional.empty();
    String json = null;
    final Connection connection = DBUtil.getReadConnection(poolingDataSource);
    try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
        preparedStatement.setString(1, keyValue);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                final String key = resultSet.getString(1);
                if (keyValue.equalsIgnoreCase(key)) {
                    json = resultSet.getString(2);
                    if (Strings.isNullOrEmpty(json)) {
                        return Optional.empty();
                    }
                    result = Optional.ofNullable(metacatJson.parseJsonObject(json));
                    break;
                }
            }
        }
    } catch (SQLException e) {
        log.error("Sql exception", e);
        throw new UserMetadataServiceException(String.format("Failed to get data for %s", keyValue), e);
    } catch (MetacatJsonException e) {
        log.error("Invalid json '{}' for keyValue '{}'", json, keyValue, e);
        throw new UserMetadataServiceException(String.format("Invalid json %s for name %s", json, keyValue), e);
    } finally {
        DBUtil.closeReadConnection(connection);
    }
    return result;
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) SQLException(java.sql.SQLException) MetacatJsonException(com.netflix.metacat.common.json.MetacatJsonException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 19 with UserMetadataServiceException

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

the class MysqlUserMetadataService method executeUpdateForKey.

private int executeUpdateForKey(final String query, final String... keyValues) {
    int result;
    try {
        final Connection conn = poolingDataSource.getConnection();
        try {
            result = new QueryRunner().update(conn, query, (Object[]) keyValues);
            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 to save data for %s", Arrays.toString(keyValues)), e);
    }
    return result;
}
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 20 with UserMetadataServiceException

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

the class MysqlUserMetadataService method deleteMetadatas.

@Override
public void deleteMetadatas(final String userId, final List<HasMetadata> holders) {
    try {
        final Connection conn = poolingDataSource.getConnection();
        try {
            final List<List<HasMetadata>> subLists = Lists.partition(holders, config.getUserMetadataMaxInClauseItems());
            for (List<HasMetadata> hasMetadatas : subLists) {
                final List<QualifiedName> names = hasMetadatas.stream().filter(m -> m instanceof HasDefinitionMetadata).map(m -> ((HasDefinitionMetadata) m).getDefinitionName()).collect(Collectors.toList());
                if (!names.isEmpty()) {
                    _deleteDefinitionMetadatas(conn, names);
                }
                if (config.canSoftDeleteDataMetadata()) {
                    final List<String> uris = hasMetadatas.stream().filter(m -> m instanceof HasDataMetadata && ((HasDataMetadata) m).isDataExternal()).map(m -> ((HasDataMetadata) m).getDataUri()).collect(Collectors.toList());
                    if (!uris.isEmpty()) {
                        _softDeleteDataMetadatas(conn, userId, uris);
                    }
                }
            }
            conn.commit();
        } catch (SQLException e) {
            conn.rollback();
            throw e;
        } finally {
            conn.close();
        }
    } catch (SQLException e) {
        log.error("Sql exception", e);
        throw new UserMetadataServiceException("Failed deleting data metadata", e);
    }
}
Also used : Arrays(java.util.Arrays) Connection(java.sql.Connection) URL(java.net.URL) Date(java.util.Date) HasDataMetadata(com.netflix.metacat.common.dto.HasDataMetadata) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DefinitionMetadataDto(com.netflix.metacat.common.dto.DefinitionMetadataDto) Strings(com.google.common.base.Strings) SQLException(java.sql.SQLException) Lists(com.google.common.collect.Lists) ResultSet(java.sql.ResultSet) Map(java.util.Map) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) DataSource(javax.sql.DataSource) Config(com.netflix.metacat.common.server.properties.Config) BaseUserMetadataService(com.netflix.metacat.common.server.usermetadata.BaseUserMetadataService) Nonnull(javax.annotation.Nonnull) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) Charsets(com.google.common.base.Charsets) DataSourceManager(com.netflix.metacat.common.server.util.DataSourceManager) Properties(java.util.Properties) QueryRunner(org.apache.commons.dbutils.QueryRunner) MetacatJson(com.netflix.metacat.common.json.MetacatJson) Files(java.nio.file.Files) HasDefinitionMetadata(com.netflix.metacat.common.dto.HasDefinitionMetadata) Set(java.util.Set) QualifiedName(com.netflix.metacat.common.QualifiedName) Reader(java.io.Reader) PreparedStatement(java.sql.PreparedStatement) Maps(com.google.common.collect.Maps) MetacatJsonException(com.netflix.metacat.common.json.MetacatJsonException) Collectors(java.util.stream.Collectors) HasMetadata(com.netflix.metacat.common.dto.HasMetadata) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) DBUtil(com.netflix.metacat.common.server.util.DBUtil) Paths(java.nio.file.Paths) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) ColumnListHandler(org.apache.commons.dbutils.handlers.ColumnListHandler) Collections(java.util.Collections) ResultSetHandler(org.apache.commons.dbutils.ResultSetHandler) FileSystems(java.nio.file.FileSystems) Joiner(com.google.common.base.Joiner) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) HasMetadata(com.netflix.metacat.common.dto.HasMetadata) HasDataMetadata(com.netflix.metacat.common.dto.HasDataMetadata) SQLException(java.sql.SQLException) QualifiedName(com.netflix.metacat.common.QualifiedName) Connection(java.sql.Connection) List(java.util.List) HasDefinitionMetadata(com.netflix.metacat.common.dto.HasDefinitionMetadata)

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