use of com.netflix.metacat.common.server.model.Lookup in project metacat by Netflix.
the class MySqlLookupService method findOrCreateLookupByName.
/**
* findOrCreateLookupByName.
*
* @param name name to find or create
* @return Look up object
* @throws SQLException sql exception
*/
private Lookup findOrCreateLookupByName(final String name) throws SQLException {
Lookup lookup = get(name);
if (lookup == null) {
final KeyHolder holder = new GeneratedKeyHolder();
jdbcTemplate.update(connection -> {
final PreparedStatement ps = connection.prepareStatement(SQL_INSERT_LOOKUP, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, name);
ps.setString(2, STRING_TYPE);
ps.setString(3, config.getLookupServiceUserAdmin());
ps.setString(4, config.getLookupServiceUserAdmin());
return ps;
}, holder);
final Long lookupId = holder.getKey().longValue();
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 get.
/**
* Returns the lookup for the given <code>name</code>.
*
* @param name lookup name
* @return lookup
*/
@Override
@Transactional(readOnly = true)
public Lookup get(final String name) {
try {
return jdbcTemplate.queryForObject(SQL_GET_LOOKUP, new Object[] { name }, new int[] { Types.VARCHAR }, (rs, rowNum) -> {
final Lookup lookup = new Lookup();
lookup.setId(rs.getLong("id"));
lookup.setName(rs.getString("name"));
lookup.setType(rs.getString("type"));
lookup.setCreatedBy(rs.getString("createdBy"));
lookup.setLastUpdated(rs.getDate("lastUpdated"));
lookup.setLastUpdatedBy(rs.getString("lastUpdatedBy"));
lookup.setDateCreated(rs.getDate("dateCreated"));
lookup.setValues(getValues(rs.getLong("id")));
return lookup;
});
} catch (EmptyResultDataAccessException e) {
return null;
} 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);
}
}
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) {
try {
final Lookup lookup = findOrCreateLookupByName(name);
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);
}
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);
}
}
Aggregations