Search in sources :

Example 66 with NamedParameterJdbcTemplate

use of org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate in project bamboobsc by billchen198318.

the class ManualJdbcTemplateFactory method getManualJdbcTemplate.

public static NamedParameterJdbcTemplate getManualJdbcTemplate(String confOid) throws ServiceException, Exception {
    if (jdbcTemplateTreadLocal.get() != null && jdbcTemplateTreadLocal.get().getDataSourceConf().getOid().equals(confOid)) {
        return jdbcTemplateTreadLocal.get().getJdbcTemplate();
    }
    DataSourceConfVO conf = new DataSourceConfVO();
    conf.setOid(confOid);
    DefaultResult<DataSourceConfVO> confResult = dataSourceConfService.findObjectByOid(conf);
    if (confResult.getValue() == null) {
        throw new ServiceException(confResult.getSystemMessage().getValue());
    }
    conf = confResult.getValue();
    DataSourceDriverVO driver = new DataSourceDriverVO();
    driver.setId(conf.getDriverId());
    DefaultResult<DataSourceDriverVO> driverResult = dataSourceDriverService.findByUK(driver);
    if (driverResult.getValue() == null) {
        throw new ServiceException(driverResult.getSystemMessage().getValue());
    }
    driver = driverResult.getValue();
    NamedParameterJdbcTemplate jdbcTemplate = DataUtils.getManualJdbcTemplate(Class.forName(driver.getClassName()), conf.getJdbcUrl(), conf.getDbAccount(), conf.getDbPassword());
    DataProperty dataProperty = new DataProperty();
    dataProperty.setDataSourceConf(conf);
    dataProperty.setJdbcTemplate(jdbcTemplate);
    jdbcTemplateTreadLocal.set(dataProperty);
    return jdbcTemplate;
}
Also used : DataSourceConfVO(com.netsteadfast.greenstep.vo.DataSourceConfVO) ServiceException(com.netsteadfast.greenstep.base.exception.ServiceException) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) DataSourceDriverVO(com.netsteadfast.greenstep.vo.DataSourceDriverVO)

Example 67 with NamedParameterJdbcTemplate

use of org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate in project lavagna by digitalfondue.

the class PersistenceAndServiceConfig method simpleJdbcTemplate.

@Bean
public NamedParameterJdbcTemplate simpleJdbcTemplate(LavagnaEnvironment env, DataSource dataSource) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    // mysql does not support check constraints
    if ("MYSQL".equals(env.getProperty("datasource.dialect"))) {
        CustomSQLErrorCodesTranslator tr = new CustomSQLErrorCodesTranslator();
        tr.setDataSource(dataSource);
        jdbcTemplate.setExceptionTranslator(tr);
    }
    return new NamedParameterJdbcTemplate(jdbcTemplate);
}
Also used : NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Bean(org.springframework.context.annotation.Bean)

Example 68 with NamedParameterJdbcTemplate

use of org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate in project nextprot-api by calipho-sib.

the class UserDaoImpl method getUserByUsername.

@Override
public User getUserByUsername(String username) throws DataAccessException {
    Map<String, String> namedParams = new HashMap<String, String>();
    namedParams.put("user_name", username);
    String sql = sqlDictionary.getSQLQuery("read-user-by-name");
    NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(dsLocator.getUserDataSource());
    List<User> users = template.query(sql, namedParams, new UsersExtractor());
    if (users.isEmpty())
        throw new EmptyResultDataAccessException(1);
    return users.get(0);
}
Also used : User(org.nextprot.api.user.domain.User) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Example 69 with NamedParameterJdbcTemplate

use of org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate in project nextprot-api by calipho-sib.

the class UserDaoImpl method updateUser.

@Override
public void updateUser(User src) {
    final String UPDATE_SQL = sqlDictionary.getSQLQuery("update-user");
    MapSqlParameterSource namedParameters = new MapSqlParameterSource();
    // key to identify application to be updated
    namedParameters.addValue("user_id", src.getId());
    // values to update
    namedParameters.addValue("user_name", src.getUsername());
    namedParameters.addValue("first_name", src.getFirstName());
    namedParameters.addValue("last_name", src.getLastName());
    NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate(dsLocator.getUserDataSource());
    int affectedRows = jdbcTemplate.update(UPDATE_SQL, namedParameters);
    if (affectedRows != 1) {
        String msg = "oops something wrong occurred" + affectedRows + " rows were affected instead of only 1.";
        Logger.error(msg);
        throw new NextProtException(msg);
    }
    if (src.getAuthorities() != null && !src.getAuthorities().isEmpty()) {
        // 1. delete all roles for this user if roles exist in user_roles table of src
        deleteUserRoles(src.getId());
        // 2. insert roles with insertUserRoles(src.getKey(), src.getRoles())
        insertUserAuthorities(src.getId(), src.getAuthorities());
    }
}
Also used : NextProtException(org.nextprot.api.commons.exception.NextProtException) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate)

Example 70 with NamedParameterJdbcTemplate

use of org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate in project nextprot-api by calipho-sib.

the class UserDaoImpl method deleteUserRoles.

private void deleteUserRoles(final long userId) {
    final String DELETE_SQL = sqlDictionary.getSQLQuery("delete-user-roles");
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("user_id", userId);
    new NamedParameterJdbcTemplate(dsLocator.getUserDataSource()).update(DELETE_SQL, params);
}
Also used : NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate)

Aggregations

NamedParameterJdbcTemplate (org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate)119 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)66 SqlParameterSource (org.springframework.jdbc.core.namedparam.SqlParameterSource)42 HashMap (java.util.HashMap)30 ResultSet (java.sql.ResultSet)16 SQLException (java.sql.SQLException)16 DbXref (org.nextprot.api.core.domain.DbXref)8 NextProtException (org.nextprot.api.commons.exception.NextProtException)7 PublicationDbXref (org.nextprot.api.core.domain.PublicationDbXref)7 ArrayList (java.util.ArrayList)5 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)5 List (java.util.List)4 Map (java.util.Map)4 CvTerm (org.nextprot.api.core.domain.CvTerm)4 GargoyleException (com.kyj.fx.voeditor.visual.exceptions.GargoyleException)3 NotSupportException (com.kyj.fx.voeditor.visual.exceptions.NotSupportException)3 DataSource (javax.sql.DataSource)3 UserProteinList (org.nextprot.api.user.domain.UserProteinList)3 ServiceException (com.netsteadfast.greenstep.base.exception.ServiceException)2 Date (java.util.Date)2