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);
}
}
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);
}
}
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;
}
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;
}
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);
}
}
Aggregations