use of com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException in project metacat by Netflix.
the class MysqlUserMetadataService method searchDefinitionMetadatas.
@Override
public List<DefinitionMetadataDto> searchDefinitionMetadatas(final Set<String> propertyNames, final String type, final String name, final String sortBy, final String sortOrder, final Integer offset, final Integer limit) {
final List<DefinitionMetadataDto> result = Lists.newArrayList();
final StringBuilder query = new StringBuilder(SQL.SEARCH_DEFINITION_METADATAS);
final List<Object> paramList = Lists.newArrayList();
if (type != null) {
String typeRegex = null;
switch(type) {
case "database":
typeRegex = "^[^/]*/[^/]*$";
break;
case "table":
typeRegex = "^[^/]*/[^/]*/[^/]*$";
break;
case "partition":
typeRegex = "^[^/]*/[^/]*/[^/]*/.*$";
break;
default:
}
if (typeRegex != null) {
query.append(" and name rlike ?");
paramList.add(typeRegex);
}
}
if (propertyNames != null && !propertyNames.isEmpty()) {
propertyNames.forEach(propertyName -> {
query.append(" and data like ?");
paramList.add("%\"" + propertyName + "\":%");
});
}
if (!Strings.isNullOrEmpty(name)) {
query.append(" and name like ?");
paramList.add(name);
}
if (!Strings.isNullOrEmpty(sortBy)) {
query.append(" order by ").append(sortBy);
if (!Strings.isNullOrEmpty(sortOrder)) {
query.append(" ").append(sortOrder);
}
}
if (limit != null) {
query.append(" limit ");
if (offset != null) {
query.append(offset).append(",");
}
query.append(limit);
}
final Object[] params = new Object[paramList.size()];
final Connection connection = DBUtil.getReadConnection(poolingDataSource);
try {
// Handler for reading the result set
final ResultSetHandler<Void> handler = rs -> {
while (rs.next()) {
final String definitionName = rs.getString("name");
final String data = rs.getString("data");
final DefinitionMetadataDto definitionMetadataDto = new DefinitionMetadataDto();
definitionMetadataDto.setName(QualifiedName.fromString(definitionName));
definitionMetadataDto.setDefinitionMetadata(metacatJson.parseJsonObject(data));
result.add(definitionMetadataDto);
}
return null;
};
new QueryRunner().query(connection, query.toString(), handler, paramList.toArray(params));
} catch (SQLException e) {
log.error("Sql exception", e);
throw new UserMetadataServiceException("Failed to get definition data", e);
} finally {
DBUtil.closeReadConnection(connection);
}
return result;
}
use of com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException in project metacat by Netflix.
the class MySqlLookupService method get.
/**
* Returns the lookup for the given <code>name</code>.
*
* @param name lookup name
* @return lookup
*/
@Override
public Lookup get(final String name) {
Lookup result;
final Connection connection = DBUtil.getReadConnection(getDataSource());
try {
final ResultSetHandler<Lookup> handler = new BeanHandler<>(Lookup.class);
result = new QueryRunner().query(connection, SQL_GET_LOOKUP, handler, name);
if (result != null) {
result.setValues(getValues(result.getId()));
}
} catch (Exception e) {
final String message = String.format("Failed to get the lookup 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.usermetadata.UserMetadataServiceException in project metacat by Netflix.
the class MySqlLookupService method addValues.
/**
* Saves the lookup value.
*
* @param name lookup name
* @param values multiple values
* @return returns the lookup with the given name.
*/
@Override
public Lookup addValues(final String name, final Set<String> values) {
Lookup lookup;
try {
final Connection conn = getDataSource().getConnection();
try {
lookup = findOrCreateLookupByName(name, conn);
final Set<String> inserts;
final Set<String> lookupValues = lookup.getValues();
if (lookupValues == null || lookupValues.isEmpty()) {
inserts = values;
lookup.setValues(values);
} else {
inserts = Sets.difference(values, lookupValues);
}
if (!inserts.isEmpty()) {
insertLookupValues(lookup.getId(), inserts, conn);
}
conn.commit();
} catch (SQLException e) {
conn.rollback();
throw e;
} finally {
conn.close();
}
} catch (SQLException 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);
}
return lookup;
}
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
*/
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.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 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;
}
Aggregations