Search in sources :

Example 1 with 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) {
    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;
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Lookup(com.netflix.metacat.common.server.model.Lookup)

Example 2 with 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;
}
Also used : Lookup(com.netflix.metacat.common.server.model.Lookup) QueryRunner(org.apache.commons.dbutils.QueryRunner)

Example 3 with 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);
    }
}
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)

Example 4 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
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;
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) Connection(java.sql.Connection) BeanHandler(org.apache.commons.dbutils.handlers.BeanHandler) Lookup(com.netflix.metacat.common.server.model.Lookup) QueryRunner(org.apache.commons.dbutils.QueryRunner) SQLException(java.sql.SQLException) UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException)

Example 5 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) {
    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;
}
Also used : UserMetadataServiceException(com.netflix.metacat.common.server.usermetadata.UserMetadataServiceException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Lookup(com.netflix.metacat.common.server.model.Lookup)

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