Example usage for org.springframework.dao.support DataAccessUtils uniqueResult

List of usage examples for org.springframework.dao.support DataAccessUtils uniqueResult

Introduction

In this page you can find the example usage for org.springframework.dao.support DataAccessUtils uniqueResult.

Prototype

@Nullable
public static <T> T uniqueResult(@Nullable Collection<T> results)
        throws IncorrectResultSizeDataAccessException 

Source Link

Document

Return a unique result object from the given Collection.

Usage

From source file:org.ambraproject.service.user.UserServiceImpl.java

/**
 * {@inheritDoc}//from   w ww  . j  a va2s.com
 */
@Override
@Transactional
public void saveUserOrcid(Long userProfileId, OrcidAuthorization orcidAuthorization)
        throws DuplicateOrcidException {
    UserOrcid userOrcid = (UserOrcid) DataAccessUtils
            .uniqueResult(hibernateTemplate.findByCriteria(DetachedCriteria.forClass(UserOrcid.class)
                    .add(Restrictions.eq("orcid", orcidAuthorization.getOrcid()))
                    .add(Restrictions.ne("ID", userProfileId))
                    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)));

    if (userOrcid != null) {
        throw new DuplicateOrcidException(
                "ORCiD: '" + orcidAuthorization.getOrcid() + "' is already in use by another account");
    }

    userOrcid = (UserOrcid) DataAccessUtils.uniqueResult(hibernateTemplate
            .findByCriteria(DetachedCriteria.forClass(UserOrcid.class).add(Restrictions.eq("ID", userProfileId))
                    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)));

    boolean isNew = (userOrcid == null);
    if (isNew) {
        userOrcid = new UserOrcid();
        userOrcid.setID(userProfileId);
    }

    //Note we don't store the token type property of the OrcidAuthorization object.
    //http://support.orcid.org/knowledgebase/articles/119985-post-oauth-token
    //The token type for our purposes will always be "bearer"

    userOrcid.setOrcid(orcidAuthorization.getOrcid());
    userOrcid.setAccessToken(orcidAuthorization.getAccessToken());
    userOrcid.setRefreshToken(orcidAuthorization.getRefreshToken());
    userOrcid.setTokenScope(orcidAuthorization.getScope());

    Calendar newExpiresIn = Calendar.getInstance();
    //expires-in is "seconds from now"
    newExpiresIn.setTimeInMillis(newExpiresIn.getTimeInMillis() + (orcidAuthorization.getExpiresIn() * 1000));
    userOrcid.setTokenExpires(newExpiresIn);

    if (isNew) {
        hibernateTemplate.save(userOrcid);
    } else {
        hibernateTemplate.update(userOrcid);
    }
}

From source file:org.ambraproject.service.user.UserServiceImpl.java

/**
 * {@inheritDoc}/*ww w .  j  a  v a  2s .co m*/
 */
@Override
@Transactional
public void removeUserOrcid(Long userProfileId) {
    UserOrcid userOrcid = (UserOrcid) DataAccessUtils.uniqueResult(hibernateTemplate
            .findByCriteria(DetachedCriteria.forClass(UserOrcid.class).add(Restrictions.eq("ID", userProfileId))
                    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)));

    if (userOrcid != null) {
        hibernateTemplate.delete(userOrcid);
    }
}

From source file:org.ambraproject.service.user.UserServiceImpl.java

/**
 * {@inheritDoc}/*from www . j a v  a2s  . c  om*/
 */
@Override
@Transactional
public UserOrcid getUserOrcid(Long userProfileId) {
    UserOrcid userOrcid = (UserOrcid) DataAccessUtils.uniqueResult(hibernateTemplate
            .findByCriteria(DetachedCriteria.forClass(UserOrcid.class).add(Restrictions.eq("ID", userProfileId))
                    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)));

    return userOrcid;
}

From source file:org.apereo.portal.fragment.subscribe.dao.jpa.JpaUserFragmentSubscriptionDaoImpl.java

@Override
public IUserFragmentSubscription getUserFragmentInfo(IPerson person, IPerson fragmentOwner) {
    final TypedQuery<UserFragmentSubscriptionImpl> query = createCachedQuery(
            this.findUserFragmentInfoByPersonAndOwnerQuery);
    query.setParameter(this.userIdParameter, person.getID());
    query.setParameter(this.fragmentOwnerParameter, fragmentOwner.getUserName());

    final List<UserFragmentSubscriptionImpl> fragmentSubscriptions = query.getResultList();
    return DataAccessUtils.uniqueResult(fragmentSubscriptions);

}

From source file:org.apereo.portal.i18n.dao.jpa.JpaMessageDao.java

@Override
public Message getMessage(String code, Locale locale) {
    final TypedQuery<MessageImpl> query = createCachedQuery(findMessageByCodeAndLocaleQuery);
    query.setParameter(this.codeParameter, code);
    query.setParameter(this.localeParameter, locale);

    final List<MessageImpl> messages = query.getResultList();
    return DataAccessUtils.uniqueResult(messages);
}

From source file:org.apereo.portal.layout.dlm.FragmentDefinitionDao.java

@Override
public FragmentDefinition getFragmentDefinition(String name) {
    final TypedQuery<FragmentDefinition> query = this.createCachedQuery(this.findFragmentByNameQuery);
    query.setParameter(this.nameParameter, name);

    final List<FragmentDefinition> list = query.getResultList();
    final FragmentDefinition rslt = DataAccessUtils.uniqueResult(list);
    return rslt;/*w  w  w. j a  v  a2 s .  c o m*/

}

From source file:org.apereo.portal.layout.dlm.FragmentDefinitionDao.java

@Override
public FragmentDefinition getFragmentDefinitionByOwner(String ownerId) {
    final TypedQuery<FragmentDefinition> query = this.createCachedQuery(this.findFragmentByOwnerQuery);
    query.setParameter(this.ownerParameter, ownerId);

    final List<FragmentDefinition> list = query.getResultList();
    final FragmentDefinition rslt = DataAccessUtils.uniqueResult(list);
    return rslt;/* ww  w .java 2 s.c om*/
}

From source file:org.apereo.portal.persondir.dao.jpa.JpaLocalAccountDaoImpl.java

@Override
public ILocalAccountPerson getPerson(String username) {
    final TypedQuery<LocalAccountPersonImpl> query = this.createCachedQuery(this.findAccountByNameQuery);
    query.setParameter(this.nameParameter, username);

    final List<LocalAccountPersonImpl> accounts = query.getResultList();
    return DataAccessUtils.uniqueResult(accounts);
}

From source file:org.apereo.portal.portlet.dao.jpa.JpaPortletDefinitionDao.java

@Override
@DialectAwareTransactional(value = PostgreSQL81Dialect.class, exclude = false)
@PortalTransactionalReadOnly/*from w w w  .ja  v a2s  . c  om*/
@OpenEntityManager(unitName = PERSISTENCE_UNIT_NAME)
public IPortletDefinition getPortletDefinitionByName(String name) {
    final TypedQuery<PortletDefinitionImpl> query = this.createCachedQuery(this.findDefinitionByNameQuery);
    query.setParameter(this.nameParameter, name);

    final List<PortletDefinitionImpl> portletDefinitions = query.getResultList();
    return DataAccessUtils.uniqueResult(portletDefinitions);
}

From source file:org.encuestame.persistence.dao.imp.AccountDaoImp.java

public final UserAccount getUserByUsername(final String username) throws HibernateException {
    log.trace("getUserByUsername by :{" + username);
    final DetachedCriteria criteria = DetachedCriteria.forClass(UserAccount.class);
    criteria.add(Restrictions.eq("username", username));
    getHibernateTemplate().setQueryCacheRegion("query.user.by.username");
    getHibernateTemplate().setCacheQueries(true);
    final UserAccount userAccount = (UserAccount) DataAccessUtils
            .uniqueResult(getHibernateTemplate().findByCriteria(criteria));
    return userAccount;
}