use of com.netflix.metacat.common.server.model.Lookup 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) {
Lookup lookup;
try {
final Connection conn = getDataSource().getConnection();
try {
lookup = findOrCreateLookupByName(name, conn);
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, conn);
}
if (!deletes.isEmpty()) {
deleteLookupValues(lookup.getId(), deletes, 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.model.Lookup in project metacat by Netflix.
the class MySqlLookupService method findOrCreateLookupByName.
private Lookup findOrCreateLookupByName(final String name, final Connection conn) throws SQLException {
Lookup lookup = get(name);
if (lookup == null) {
final Object[] params = { name, STRING_TYPE, config.getLookupServiceUserAdmin(), config.getLookupServiceUserAdmin() };
final Long lookupId = new QueryRunner().insert(conn, SQL_INSERT_LOOKUP, new ScalarHandler<>(1), params);
lookup = new Lookup();
lookup.setName(name);
lookup.setId(lookupId);
}
return lookup;
}
use of com.netflix.metacat.common.server.model.Lookup 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);
}
}
use of com.netflix.metacat.common.server.model.Lookup 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.model.Lookup 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;
}
Aggregations