Search in sources :

Example 6 with Lookup

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;
}
Also used : GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder) Lookup(com.netflix.metacat.common.server.model.Lookup) PreparedStatement(java.sql.PreparedStatement) KeyHolder(org.springframework.jdbc.support.KeyHolder) GeneratedKeyHolder(org.springframework.jdbc.support.GeneratedKeyHolder)

Example 7 with 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);
    }
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) Lookup(com.netflix.metacat.common.server.model.Lookup) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) SQLException(java.sql.SQLException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with Lookup

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);
    }
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) Lookup(com.netflix.metacat.common.server.model.Lookup) SQLException(java.sql.SQLException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Aggregations

Lookup (com.netflix.metacat.common.server.model.Lookup)8 UserMetadataServiceException (com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException)6 SQLException (java.sql.SQLException)6 Connection (java.sql.Connection)3 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)3 QueryRunner (org.apache.commons.dbutils.QueryRunner)2 PreparedStatement (java.sql.PreparedStatement)1 BeanHandler (org.apache.commons.dbutils.handlers.BeanHandler)1 GeneratedKeyHolder (org.springframework.jdbc.support.GeneratedKeyHolder)1 KeyHolder (org.springframework.jdbc.support.KeyHolder)1 Transactional (org.springframework.transaction.annotation.Transactional)1