Search in sources :

Example 61 with NoResultException

use of javax.persistence.NoResultException in project Asqatasun by Asqatasun.

the class StatisticsDAOImpl method findResultCountByResultTypeAndTheme.

/**
     * Native sql query :
     * SELECT ts.$testSolution FROM THEME_STATISTICS as ts,
     *       WEB_RESOURCE_STATISTICS as wrs
     *       WHERE ts.Id_Web_Resource_Statistics=wrs.Id_Web_Resource_Statistics
     *       AND wrs.Id_Web_Resource=:idWebResource
     *       AND wrs.Id_Audit=:idAudit
     *       AND ts.Id_Theme=:idTheme";
     *
     * where $testSolution is computed on the fly for the given the testSolution
     * passed in argument.
     *
     * @param webResource
     * @param audit
     * @param testSolution
     * @param theme
     * @param manualAudit
     * @return
     *      the number of elements for a given result type and theme
     */
@Override
public Long findResultCountByResultTypeAndTheme(WebResource webResource, Audit audit, TestSolution testSolution, Theme theme, boolean manualAudit) {
    StringBuilder queryString = new StringBuilder();
    queryString.append(SELECT_STR);
    queryString.append(THEME_STATISTICS_TABLE_STR);
    queryString = selectNbField(queryString, testSolution);
    queryString.append(RETRIEVE_COUNT_BY_RESULT_TYPE_AND_THEME_QUERY);
    queryString.append(" and wrs.manual_audit = ");
    queryString.append(manualAudit ? "1" : "0");
    Query query = entityManager.createNativeQuery(queryString.toString());
    query.setParameter("idWebResource", webResource.getId());
    query.setParameter("idAudit", audit.getId());
    query.setParameter("idTheme", theme.getId());
    try {
        return ((Integer) query.getSingleResult()).longValue();
    } catch (NoResultException e) {
        return (long) 0;
    }
}
Also used : BigInteger(java.math.BigInteger) Query(javax.persistence.Query) NoResultException(javax.persistence.NoResultException)

Example 62 with NoResultException

use of javax.persistence.NoResultException in project Asqatasun by Asqatasun.

the class OptionElementDAOImpl method findOptionElementFromUser.

@Override
public Collection<OptionElement> findOptionElementFromUser(User user) {
    Query query = entityManager.createQuery("SELECT u.optionElementSet FROM " + getUserEntityClass().getName() + " u" + " WHERE u=:user");
    query.setParameter("user", user);
    try {
        return (Collection<OptionElement>) query.getResultList();
    } catch (NoResultException | NonUniqueResultException nre) {
        return null;
    }
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) Query(javax.persistence.Query) Collection(java.util.Collection) NoResultException(javax.persistence.NoResultException)

Example 63 with NoResultException

use of javax.persistence.NoResultException in project Asqatasun by Asqatasun.

the class OptionElementDAOImpl method findOptionElementFromValueAndOption.

/**
     * 
     * @param value
     * @param option
     * @return 
     */
@Override
public OptionElement findOptionElementFromValueAndOption(String value, Option option) {
    Query query = entityManager.createQuery("SELECT oe FROM " + getEntityClass().getName() + " oe" + " WHERE oe.value=:value" + " AND oe.option=:option");
    query.setParameter("value", value);
    query.setParameter("option", option);
    try {
        return (OptionElement) query.getSingleResult();
    } catch (NoResultException | NonUniqueResultException nre) {
        return null;
    }
}
Also used : NonUniqueResultException(javax.persistence.NonUniqueResultException) Query(javax.persistence.Query) OptionElement(org.asqatasun.webapp.entity.option.OptionElement) NoResultException(javax.persistence.NoResultException)

Example 64 with NoResultException

use of javax.persistence.NoResultException in project ORCID-Source by ORCID.

the class OrcidSecurityManagerImpl method checkProfile.

/**
     * Checks a record status and throw an exception indicating if the profile
     * have any of the following conditions: - The record is not claimed and is
     * not old enough nor being accessed by its creator - It is locked - It is
     * deprecated - It is deactivated
     * 
     * @throws OrcidDeprecatedException
     *             in case the account is deprecated
     * @throws OrcidNotClaimedException
     *             in case the account is not claimed
     * @throws LockedException
     *             in the case the account is locked
     */
@Override
public void checkProfile(String orcid) throws NoResultException, OrcidDeprecatedException, OrcidNotClaimedException, LockedException {
    ProfileEntity profile = null;
    try {
        profile = profileEntityCacheManager.retrieve(orcid);
    } catch (IllegalArgumentException e) {
        throw new NoResultException();
    }
    // Check if the user record is deprecated
    if (profile.getPrimaryRecord() != null) {
        StringBuffer primary = new StringBuffer(baseUrl).append("/").append(profile.getPrimaryRecord().getId());
        Map<String, String> params = new HashMap<String, String>();
        params.put(OrcidDeprecatedException.ORCID, primary.toString());
        if (profile.getDeprecatedDate() != null) {
            XMLGregorianCalendar calendar = DateUtils.convertToXMLGregorianCalendar(profile.getDeprecatedDate());
            params.put(OrcidDeprecatedException.DEPRECATED_DATE, calendar.toString());
        }
        throw new OrcidDeprecatedException(params);
    }
    // Check if the profile is not claimed and not old enough
    if ((profile.getClaimed() == null || Boolean.FALSE.equals(profile.getClaimed())) && !isOldEnough(profile)) {
        // Let the creator access the profile even if it is not claimed and
        // not old enough
        SourceEntity currentSourceEntity = sourceManager.retrieveSourceEntity();
        String profileSource = profile.getSource() == null ? null : profile.getSource().getSourceId();
        String currentSource = currentSourceEntity == null ? null : currentSourceEntity.getSourceId();
        // the profile source, throw an exception
        if (profileSource == null || !Objects.equals(profileSource, currentSource)) {
            throw new OrcidNotClaimedException();
        }
    }
    // Check if the record is locked
    if (!profile.isAccountNonLocked()) {
        LockedException lockedException = new LockedException();
        lockedException.setOrcid(profile.getId());
        throw lockedException;
    }
}
Also used : XMLGregorianCalendar(javax.xml.datatype.XMLGregorianCalendar) LockedException(org.orcid.core.security.aop.LockedException) HashMap(java.util.HashMap) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) OrcidDeprecatedException(org.orcid.core.exception.OrcidDeprecatedException) OrcidNotClaimedException(org.orcid.core.exception.OrcidNotClaimedException) NoResultException(javax.persistence.NoResultException) ProfileEntity(org.orcid.persistence.jpa.entities.ProfileEntity)

Example 65 with NoResultException

use of javax.persistence.NoResultException in project ORCID-Source by ORCID.

the class OrcidRefreshTokenTokenGranterTest method tryToCreateRefreshTokenWithInvalidParentTokenValueTest.

@Test
public void tryToCreateRefreshTokenWithInvalidParentTokenValueTest() {
    // Create token, try to create refresh token with invalid parent token
    // value, fail
    long time = System.currentTimeMillis();
    String parentScope = "/person/update";
    String tokenValue = "parent-token-" + time;
    String refreshTokenValue = "refresh-token-" + time;
    Boolean revokeOld = true;
    Date parentTokenExpiration = new Date(time + 10000);
    Long expireIn = null;
    OrcidOauth2TokenDetail parent = createToken(CLIENT_ID_1, USER_ORCID, tokenValue, refreshTokenValue, parentTokenExpiration, parentScope);
    try {
        //Change the value we are going to use for the refresh token
        parent.setTokenValue("invalid-value");
        generateRefreshToken(parent, null, revokeOld, expireIn, parentScope);
        fail();
    } catch (NoResultException e) {
    } catch (Exception e) {
        fail();
    }
}
Also used : NoResultException(javax.persistence.NoResultException) Date(java.util.Date) OrcidOauth2TokenDetail(org.orcid.persistence.jpa.entities.OrcidOauth2TokenDetail) NoResultException(javax.persistence.NoResultException) InvalidScopeException(org.springframework.security.oauth2.common.exceptions.InvalidScopeException) InvalidTokenException(org.springframework.security.oauth2.common.exceptions.InvalidTokenException) DBUnitTest(org.orcid.test.DBUnitTest) Test(org.junit.Test)

Aggregations

NoResultException (javax.persistence.NoResultException)169 Query (javax.persistence.Query)130 EntityManager (javax.persistence.EntityManager)40 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)17 NonUniqueResultException (javax.persistence.NonUniqueResultException)15 Test (org.junit.Test)14 Transactional (org.springframework.transaction.annotation.Transactional)14 UnitOfWork (com.google.inject.persist.UnitOfWork)12 ConfigurationStoreException (org.nhindirect.config.store.ConfigurationStoreException)9 TblMle (com.intel.mtwilson.as.data.TblMle)8 IOException (java.io.IOException)8 TblModuleManifest (com.intel.mtwilson.as.data.TblModuleManifest)7 WebResource (org.asqatasun.entity.subject.WebResource)6 ASException (com.intel.mountwilson.as.common.ASException)5 IllegalOrphanException (com.intel.mtwilson.as.controller.exceptions.IllegalOrphanException)5 NonexistentEntityException (com.intel.mtwilson.as.controller.exceptions.NonexistentEntityException)5 UserAccount (com.jappstart.model.auth.UserAccount)5 List (java.util.List)5 Parameter (org.asqatasun.entity.parameterization.Parameter)5 Transactional (com.google.inject.persist.Transactional)4