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);
}
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();
}
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();
}
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);
}
}
}
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);
}
}
}
};
}
Aggregations