Search in sources :

Example 6 with Transactional

use of javax.transaction.Transactional in project ORCID-Source by ORCID.

the class ProfileKeywordManagerImpl method updateKeyword.

@Override
@Transactional
public Keyword updateKeyword(String orcid, Long putCode, Keyword keyword, boolean isApiRequest) {
    SourceEntity sourceEntity = sourceManager.retrieveSourceEntity();
    ProfileKeywordEntity updatedEntity = profileKeywordDao.getProfileKeyword(orcid, putCode);
    Visibility originalVisibility = Visibility.fromValue(updatedEntity.getVisibility().value());
    //Save the original source
    String existingSourceId = updatedEntity.getSourceId();
    String existingClientSourceId = updatedEntity.getClientSourceId();
    // Validate the keyword
    PersonValidator.validateKeyword(keyword, sourceEntity, false, isApiRequest, originalVisibility);
    // Validate it is not duplicated
    List<ProfileKeywordEntity> existingKeywords = profileKeywordDao.getProfileKeywors(orcid, getLastModified(orcid));
    for (ProfileKeywordEntity existing : existingKeywords) {
        if (isDuplicated(existing, keyword, sourceEntity)) {
            Map<String, String> params = new HashMap<String, String>();
            params.put("type", "keyword");
            params.put("value", keyword.getContent());
            throw new OrcidDuplicatedElementException(params);
        }
    }
    orcidSecurityManager.checkSource(updatedEntity);
    adapter.toProfileKeywordEntity(keyword, updatedEntity);
    updatedEntity.setLastModified(new Date());
    //Be sure it doesn't overwrite the source
    updatedEntity.setSourceId(existingSourceId);
    updatedEntity.setClientSourceId(existingClientSourceId);
    profileKeywordDao.merge(updatedEntity);
    return adapter.toKeyword(updatedEntity);
}
Also used : ProfileKeywordEntity(org.orcid.persistence.jpa.entities.ProfileKeywordEntity) HashMap(java.util.HashMap) SourceEntity(org.orcid.persistence.jpa.entities.SourceEntity) OrcidDuplicatedElementException(org.orcid.core.exception.OrcidDuplicatedElementException) Visibility(org.orcid.jaxb.model.common_v2.Visibility) Date(java.util.Date) Transactional(javax.transaction.Transactional)

Example 7 with Transactional

use of javax.transaction.Transactional in project ORCID-Source by ORCID.

the class IdentityProviderManagerImpl method retrieveFreshIdentitifyProviderName.

@Override
@Transactional
public String retrieveFreshIdentitifyProviderName(String providerid, Locale locale) {
    IdentityProviderEntity idp = identityProviderDao.findByProviderid(providerid);
    List<IdentityProviderNameEntity> names = idp.getNames();
    if (names != null) {
        Optional<IdentityProviderNameEntity> idpNameEntity = names.stream().filter(n -> n.getLang().equals(locale.getLanguage())).findFirst();
        if (idpNameEntity.isPresent()) {
            return idpNameEntity.get().getDisplayName();
        }
    }
    return idp.getDisplayName();
}
Also used : IdentityProviderEntity(org.orcid.persistence.jpa.entities.IdentityProviderEntity) XPathExpressionException(javax.xml.xpath.XPathExpressionException) XPath(javax.xml.xpath.XPath) XPathConstants(javax.xml.xpath.XPathConstants) ClientResponse(com.sun.jersey.api.client.ClientResponse) LoggerFactory(org.slf4j.LoggerFactory) IdentityProviderManager(org.orcid.core.manager.IdentityProviderManager) XPathExpression(javax.xml.xpath.XPathExpression) Function(java.util.function.Function) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) MediaType(javax.ws.rs.core.MediaType) Locale(java.util.Locale) Document(org.w3c.dom.Document) Map(java.util.Map) Node(org.w3c.dom.Node) IdentityProviderNameEntity(org.orcid.persistence.jpa.entities.IdentityProviderNameEntity) WebResource(com.sun.jersey.api.client.WebResource) IdentityProviderDao(org.orcid.persistence.dao.IdentityProviderDao) Logger(org.slf4j.Logger) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) NodeList(org.w3c.dom.NodeList) SelfPopulatingCache(net.sf.ehcache.constructs.blocking.SelfPopulatingCache) Transactional(javax.transaction.Transactional) Resource(javax.annotation.Resource) Collectors(java.util.stream.Collectors) NamespaceMap(org.orcid.core.utils.NamespaceMap) XPathFactory(javax.xml.xpath.XPathFactory) LocaleManager(org.orcid.core.locale.LocaleManager) List(java.util.List) Element(org.w3c.dom.Element) Client(com.sun.jersey.api.client.Client) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionStatus(org.springframework.transaction.TransactionStatus) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) ReleaseNameUtils(org.orcid.utils.ReleaseNameUtils) IdentityProviderNameEntity(org.orcid.persistence.jpa.entities.IdentityProviderNameEntity) IdentityProviderEntity(org.orcid.persistence.jpa.entities.IdentityProviderEntity) Transactional(javax.transaction.Transactional)

Example 8 with Transactional

use of javax.transaction.Transactional in project ORCID-Source by ORCID.

the class IdentityProviderDaoImpl method incrementFailedCount.

@Override
@Transactional
public void incrementFailedCount(String providerid) {
    Query query = entityManager.createQuery("update IdentityProviderEntity set lastFailed = now(), failedCount = failedCount + 1 where providerid = :providerid");
    query.setParameter("providerid", providerid);
    query.executeUpdate();
}
Also used : Query(javax.persistence.Query) TypedQuery(javax.persistence.TypedQuery) Transactional(javax.transaction.Transactional)

Example 9 with Transactional

use of javax.transaction.Transactional in project tomee by apache.

the class InterceptorBase method intercept.

protected Object intercept(final InvocationContext ic) throws Exception {
    TransactionPolicy policy = null;
    final boolean forbidsUt = doesForbidUtUsage();
    final RuntimeException oldEx;
    final IllegalStateException illegalStateException;
    if (forbidsUt) {
        illegalStateException = ILLEGAL_STATE_EXCEPTION;
        oldEx = CoreUserTransaction.error(illegalStateException);
    } else {
        illegalStateException = null;
        oldEx = null;
    }
    try {
        policy = getPolicy();
        final Object proceed = ic.proceed();
        // force commit there to ensure we can catch synchro exceptions
        policy.commit();
        return proceed;
    } catch (final Exception e) {
        if (illegalStateException == e) {
            throw e;
        }
        Exception error = unwrap(e);
        if (error != null && (!HANDLE_EXCEPTION_ONLY_FOR_CLIENT || policy.isNewTransaction())) {
            final Method method = ic.getMethod();
            if (rollback == null) {
                synchronized (this) {
                    if (rollback == null) {
                        rollback = new ConcurrentHashMap<>();
                    }
                }
            }
            Boolean doRollback = rollback.get(method);
            if (doRollback != null) {
                if (doRollback && policy != null && policy.isTransactionActive()) {
                    policy.setRollbackOnly();
                }
            } else {
                // computed lazily but we could cache it later for sure if that's really a normal case
                final AnnotatedType<?> annotatedType = CDI.current().getBeanManager().createAnnotatedType(method.getDeclaringClass());
                Transactional tx = null;
                for (final AnnotatedMethod<?> m : annotatedType.getMethods()) {
                    if (method.equals(m.getJavaMember())) {
                        tx = m.getAnnotation(Transactional.class);
                        break;
                    }
                }
                if (tx == null) {
                    tx = annotatedType.getAnnotation(Transactional.class);
                }
                if (tx != null) {
                    doRollback = new ExceptionPriotiryRules(tx.rollbackOn(), tx.dontRollbackOn()).accept(error, method.getExceptionTypes());
                    rollback.putIfAbsent(method, doRollback);
                    if (doRollback && policy != null && policy.isTransactionActive()) {
                        policy.setRollbackOnly();
                    }
                }
            }
        }
        if (policy != null) {
            try {
                policy.commit();
            } catch (final Exception ex) {
                // no-op: swallow to keep the right exception
                final Logger logger = Logger.getLogger(getClass().getName());
                if (logger.isLoggable(Level.FINE)) {
                    logger.fine("Swallowing: " + ex.getMessage());
                }
            }
        }
        if (error == null || TransactionRequiredException.class.isInstance(error)) {
            throw new TransactionalException(e.getMessage(), error);
        }
        throw error;
    } finally {
        if (forbidsUt) {
            CoreUserTransaction.resetError(oldEx);
        }
    }
}
Also used : AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) TransactionalException(javax.transaction.TransactionalException) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) Method(java.lang.reflect.Method) AnnotatedMethod(javax.enterprise.inject.spi.AnnotatedMethod) Logger(java.util.logging.Logger) TransactionRequiredException(javax.transaction.TransactionRequiredException) TransactionalException(javax.transaction.TransactionalException) SystemException(org.apache.openejb.SystemException) RollbackException(javax.transaction.RollbackException) TransactionRolledbackException(org.apache.openejb.core.transaction.TransactionRolledbackException) ApplicationException(org.apache.openejb.ApplicationException) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Transactional(javax.transaction.Transactional)

Example 10 with Transactional

use of javax.transaction.Transactional in project tomee by apache.

the class TransactionRule method apply.

@Override
public Statement apply(final Statement base, final Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            final TransactionAttribute annotation = description.getAnnotation(TransactionAttribute.class);
            final Transactional annotation2 = description.getAnnotation(Transactional.class);
            if (annotation == null && annotation2 == null) {
                base.evaluate();
                return;
            }
            final BeanContext beanContext = getBeanContext();
            final Method method = beanContext.getManagedClass().getMethod(description.getMethodName());
            final TransactionType transactionType = TransactionType.get(annotation == null ? TransactionAttributeType.valueOf(annotation2.value().name()) : annotation.value());
            beanContext.getMethodContext(method).setTransactionType(transactionType);
            ThreadContext tc = ThreadContext.getThreadContext();
            final boolean tcCreated;
            if (tc == null) {
                tcCreated = true;
                tc = ThreadContext.enter(new ThreadContext(beanContext, null));
            } else {
                tcCreated = false;
            }
            final TransactionPolicy policy = EjbTransactionUtil.createTransactionPolicy(transactionType, tc);
            try {
                base.evaluate();
            } finally {
                if (rollback) {
                    policy.setRollbackOnly();
                }
                EjbTransactionUtil.afterInvoke(policy, tc);
                if (tcCreated) {
                    ThreadContext.exit(tc);
                }
            }
        }
    };
}
Also used : BeanContext(org.apache.openejb.BeanContext) TransactionType(org.apache.openejb.core.transaction.TransactionType) TransactionAttribute(javax.ejb.TransactionAttribute) Statement(org.junit.runners.model.Statement) ThreadContext(org.apache.openejb.core.ThreadContext) TransactionPolicy(org.apache.openejb.core.transaction.TransactionPolicy) Method(java.lang.reflect.Method) Transactional(javax.transaction.Transactional)

Aggregations

Transactional (javax.transaction.Transactional)10 OrcidDuplicatedElementException (org.orcid.core.exception.OrcidDuplicatedElementException)4 Method (java.lang.reflect.Method)3 Date (java.util.Date)3 SourceEntity (org.orcid.persistence.jpa.entities.SourceEntity)3 HashMap (java.util.HashMap)2 TransactionPolicy (org.apache.openejb.core.transaction.TransactionPolicy)2 AddressEntity (org.orcid.persistence.jpa.entities.AddressEntity)2 ProfileKeywordEntity (org.orcid.persistence.jpa.entities.ProfileKeywordEntity)2 Client (com.sun.jersey.api.client.Client)1 ClientResponse (com.sun.jersey.api.client.ClientResponse)1 WebResource (com.sun.jersey.api.client.WebResource)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 Optional (java.util.Optional)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Function (java.util.function.Function)1 Logger (java.util.logging.Logger)1