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