org.springframework.dao.support.DataAccessUtils Java Examples

The following examples show how to use org.springframework.dao.support.DataAccessUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UserDao.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
public User getByName (final String name)
{
   User user = (User)DataAccessUtils.uniqueResult (
      getHibernateTemplate ().find (
      "From User u where u.username=?", name));   

   // Optimization user extraction: most of the users uses case-sensitive
   // match for the login. A Requirement of the project asked for non-case
   // sensitive match. The extraction of non-case sensitive login from
   // database requires conversions and forbid the usage of indexes, so it
   // is much more slow.
   // This Fix aims to first try the extraction of the user with exact match
   // equals operator, then if not match use the toLower conversion.
   if (user==null)
      user = (User)DataAccessUtils.uniqueResult (
         getHibernateTemplate ().find (
         "From User u where lower(u.username)=lower(?)", name));
   return user;
}
 
Example #2
Source File: ProductDao.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Returns the number of Product records whose `processed` field is `true`.
 * @param filter an optional additionnal `where` clause (without the "where" token).
 * @param collection_uuid an optional parent collection ID.
 * @return a number of rows in table `PRODUCTS`.
 */
public int count (String filter, final String collection_uuid)
{
   StringBuilder sb = new StringBuilder("select count(*) ");
   if (collection_uuid != null)
   {
         sb.append("from Collection c left outer join c.products p ");
         sb.append("where c.uuid='").append(collection_uuid).append("' and ");
   }
   else
   {
      sb.append("from Product p where ");
   }
   sb.append("p.processed=true");

   if (filter != null && !filter.isEmpty())
   {
      sb.append(" and ").append(filter);
   }

   return DataAccessUtils.intResult(find(sb.toString()));
}
 
Example #3
Source File: ReactivePersistenceExceptionTranslationInterceptor.java    From sdn-rx with Apache License 2.0 6 votes vote down vote up
@Override
public Object invoke(MethodInvocation mi) throws Throwable {

	// Invoke the method potentially returning a reactive type
	Object m = mi.proceed();

	PersistenceExceptionTranslator translator = getPersistenceExceptionTranslator();
	if (translator == null) {
		return m;
	} else {
		// Add the translation. Nothing will happen if no-one subscribe the reactive result.
		Function<RuntimeException, Throwable> errorMappingFunction =
			t -> t instanceof DataAccessException ? t : DataAccessUtils.translateIfNecessary(t, translator);
		if (m instanceof Mono) {
			return ((Mono<?>) m).onErrorMap(RuntimeException.class, errorMappingFunction);
		} else if (m instanceof Flux) {
			return ((Flux<?>) m).onErrorMap(RuntimeException.class, errorMappingFunction);
		} else {
			return m;
		}
	}
}
 
Example #4
Source File: HibernateDao.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
@SuppressWarnings ({ "unchecked", "rawtypes" })
private String getSystemByName (final String name, final int index)
{
   return DataAccessUtils.uniqueResult (getHibernateTemplate ().execute (
      new HibernateCallback<List>()
      {
         @Override
         public List doInHibernate(Session session) 
            throws HibernateException, SQLException
         {
            String sql = 
               "SELECT " + name +
               " FROM INFORMATION_SCHEMA.SYSTEM_SESSIONS" +
               " LIMIT  1 OFFSET " + index;
            SQLQuery query = session.createSQLQuery (sql);
            return query.list ();
         }
      })).toString ();
}
 
Example #5
Source File: HibernateDao.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
@SuppressWarnings ("rawtypes")
private int countOpenSessions ()
{
   return DataAccessUtils.intResult (getHibernateTemplate ().execute (
      new HibernateCallback<List>()
      {
         @Override
         public List doInHibernate(Session session) 
            throws HibernateException, SQLException
         {
            String sql = 
               "SELECT count (*) FROM INFORMATION_SCHEMA.SYSTEM_SESSIONS";
            SQLQuery query = session.createSQLQuery (sql);
            return query.list ();
         }
      }));
}
 
Example #6
Source File: CollectionDao.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Counts collections whose the given user is authorized.
 * @param user the given user.
 * @return number of authorized collection for user.
 */
public int count (User user)
{
   String userString = "";
   // Bypass for Data Right Managers. They can see all products and
   // collections.
   if ( !cfgManager.isDataPublic () && (user != null) &&
      !user.getRoles ().contains (Role.DATA_MANAGER))
   {
      userString =
         "WHERE ('" + user.getUUID () + "' in elements(authorizedUsers) OR '" +
                  userDao.getPublicData ().getUUID () +
                  "' in elements(authorizedUsers))";
   }

   return DataAccessUtils
      .intResult (find ("select count(*) FROM Collection " + userString));
}
 
Example #7
Source File: BaseDAO.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
public int countByEntityUK(Map<String, Object> ukMap) throws Exception {
	if (ukMap==null || ukMap.size()<1) {
		return 0;
	}		
	Query query=this.getQueryByKeyMap("select count(*)", ukMap);
	return DataAccessUtils.intResult(query.list());
}
 
Example #8
Source File: UserDao.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
public int countNotDeleted (String filter)
{
   return DataAccessUtils.intResult (find (
      "select count(*) FROM " + entityClass.getName () +
         " u WHERE u.deleted is false AND u.username LIKE '%" + filter +
         "%' and " + "not u.username='" +
         cfgManager.getAdministratorConfiguration ().getName () + "'" +
         " and not u.username LIKE '"+getPublicData ().getUsername ()+"' "));
}
 
Example #9
Source File: BaseDAO.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
public int countByPK(Map<String, Object> pkMap) throws Exception {
	if (pkMap==null || pkMap.size()<1) {
		return 0;
	}		
	Query query=this.getQueryByKeyMap("select count(*)", pkMap);
	return DataAccessUtils.intResult(query.list());		
}
 
Example #10
Source File: BaseDAO.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public int countByPK(PK pk) throws Exception {
	return DataAccessUtils.intResult(
			this.getCurrentSession().createQuery(
					" select count(*) from "+this.getPersisentName() + 
					" where " + BaseEntityUtil.getPKOneName((BaseEntity<PK>)entityClass.newInstance()) + "=?0 ")
					.setString("0", (String)pk).list());		
}
 
Example #11
Source File: BaseDAO.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
@Override
public int count(String hql, Object... args) throws Exception {
	Query query=this.getCurrentSession().createQuery(hql);
	for (int position=0; args!=null && position<args.length; position++) {
		this.setQueryParams(query, Integer.toString(position), args[position]);
	}
	return DataAccessUtils.intResult(query.list());
}
 
Example #12
Source File: BaseDAO.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
public int countByEntityUK(Map<String, Object> ukMap) throws Exception {
	if (ukMap==null || ukMap.size()<1) {
		return 0;
	}		
	Query query=this.getQueryByKeyMap("select count(*)", ukMap);
	return DataAccessUtils.intResult(query.list());
}
 
Example #13
Source File: BaseDAO.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
public int countByPK(Map<String, Object> pkMap) throws Exception {
	if (pkMap==null || pkMap.size()<1) {
		return 0;
	}		
	Query query=this.getQueryByKeyMap("select count(*)", pkMap);
	return DataAccessUtils.intResult(query.list());		
}
 
Example #14
Source File: BaseDAO.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public int countByPK(PK pk) throws Exception {
	return DataAccessUtils.intResult(
			this.getCurrentSession().createQuery(
					" select count(*) from "+this.getPersisentName() + 
					" where " + BaseEntityUtil.getPKOneName((BaseEntity<PK>)entityClass.newInstance()) + "=?0 ")
					.setString("0", (String)pk).list());		
}
 
Example #15
Source File: NamedParameterJdbcTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T queryForObject(String sql, SqlParameterSource paramSource, RowMapper<T> rowMapper)
		throws DataAccessException {

	List<T> results = getJdbcOperations().query(getPreparedStatementCreator(sql, paramSource), rowMapper);
	return DataAccessUtils.requiredSingleResult(results);
}
 
Example #16
Source File: JdbcTemplate.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper)
		throws DataAccessException {

	List<T> results = query(sql, args, argTypes, new RowMapperResultSetExtractor<T>(rowMapper, 1));
	return DataAccessUtils.requiredSingleResult(results);
}
 
Example #17
Source File: JpaTransactionManager.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void flush() {
	try {
		this.entityManagerHolder.getEntityManager().flush();
	}
	catch (RuntimeException ex) {
		throw DataAccessUtils.translateIfNecessary(ex, getJpaDialect());
	}
}
 
Example #18
Source File: NamedParameterJdbcTemplate.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@Nullable
public <T> T queryForObject(String sql, SqlParameterSource paramSource, RowMapper<T> rowMapper)
		throws DataAccessException {

	List<T> results = getJdbcOperations().query(getPreparedStatementCreator(sql, paramSource), rowMapper);
	return DataAccessUtils.nullableSingleResult(results);
}
 
Example #19
Source File: GenericBaseCommonDao.java    From jeewx with Apache License 2.0 5 votes vote down vote up
/**
 * 查询指定实体的总记录数
 * 
 * @param clazz
 * @return
 */
public int getCount(Class<T> clazz) {

	int count = DataAccessUtils.intResult(getSession().createQuery(
			"select count(*) from " + clazz.getName()).list());
	return count;
}
 
Example #20
Source File: BaseDAO.java    From bamboobsc with Apache License 2.0 5 votes vote down vote up
@Override
public int count(String hql, Object... args) throws Exception {
	Query query=this.getCurrentSession().createQuery(hql);
	for (int position=0; args!=null && position<args.length; position++) {
		this.setQueryParams(query, Integer.toString(position), args[position]);
	}
	return DataAccessUtils.intResult(query.list());
}
 
Example #21
Source File: UserDao.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
public int countForDataRight (String filter)
{
   return DataAccessUtils.intResult (find (
      "select count(*) FROM " + entityClass.getName () +
         " u WHERE u.deleted is false AND u.username LIKE '%" + filter +
         "%' and " + "not u.username='" +
         cfgManager.getAdministratorConfiguration ().getName () + "' "));
}
 
Example #22
Source File: UserDao.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
public int countAll (String filter)
{
   return DataAccessUtils.intResult (find (
      "select count(*) FROM " + entityClass.getName () +
         " u WHERE u.username LIKE '%" + filter + "%'" +
         " and not u.username LIKE '" + getPublicData ().getUsername () +
         "' " ));
}
 
Example #23
Source File: UserDao.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
public int countNotDeletedByFilter (String filter)
{
   return DataAccessUtils.intResult (find (
      "select count(*) FROM " + entityClass.getName () +
         " u WHERE u.deleted is false AND (u.username LIKE '%" + filter +
      "%'  OR lower(u.firstname) LIKE '%"+filter.toLowerCase()+ "%'  OR lower(u.lastname) LIKE '%"+filter.toLowerCase()+
      "%'  OR lower(u.email) LIKE '%"+filter.toLowerCase()+ "%') and not u.username='" +
         cfgManager.getAdministratorConfiguration ().getName () + "'" +
         " and not u.username LIKE '"+getPublicData ().getUsername ()+"' "));
}
 
Example #24
Source File: FileScannerDao.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Retrieve the owner user of a file scanner.
 * @param fs the scanner to retrieve the user. 
 * @return the owner.
 */
public User getUserFromScanner (FileScanner fs)
{
   return (User)DataAccessUtils.uniqueResult (getHibernateTemplate ().find (
      "select u from User u where ? in elements(u.preferences.fileScanners)",
      fs));
}
 
Example #25
Source File: ProductDao.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
public Product getProductByPath (final URL path)
{
   if (path == null)
      return null;

   Product p = (Product)DataAccessUtils.uniqueResult(getHibernateTemplate().
      find("from Product where path=? AND processed=true",path));
   
   return p;
}
 
Example #26
Source File: ProductDao.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Does the product corresponding to the given url exist in the database ?
 * Processed or not.
 */
public boolean exists (URL url)
{
   if (url == null)
      return false;

   Product p = (Product)DataAccessUtils.uniqueResult(getHibernateTemplate().
      find("from Product where path=?", url));

   return p != null;
}
 
Example #27
Source File: ProductDao.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
public Product getProductByUuid (String uuid)
{
   @SuppressWarnings ("unchecked")
   Product product = (Product) DataAccessUtils.uniqueResult (
         find ("from Product p where p.uuid='" + uuid +
               "' AND p.processed=true"));
   return product;
}
 
Example #28
Source File: JdbcTemplate.java    From effectivejava with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper)
		throws DataAccessException {

	List<T> results = query(sql, args, argTypes, new RowMapperResultSetExtractor<T>(rowMapper, 1));
	return DataAccessUtils.requiredSingleResult(results);
}
 
Example #29
Source File: NamedParameterJdbcTemplate.java    From effectivejava with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T queryForObject(String sql, SqlParameterSource paramSource, RowMapper<T> rowMapper)
		throws DataAccessException {

	List<T> results = getJdbcOperations().query(getPreparedStatementCreator(sql, paramSource), rowMapper);
	return DataAccessUtils.requiredSingleResult(results);
}
 
Example #30
Source File: GenericBaseCommonDao.java    From jeecg with Apache License 2.0 5 votes vote down vote up
/**
 * 查询指定实体的总记录数
 *
 * @param clazz
 * @return
 */
public int getCount(Class<T> clazz) {

	int count = DataAccessUtils.intResult(getSession().createQuery(
			"select count(*) from " + clazz.getName()).list());
	return count;
}