@Authenticated @Override public String jpqlExecuteUpdate(String queryString, boolean softDeletion) { try { Transaction tx = persistence.createTransaction(); try { EntityManager em = persistence.getEntityManager(); em.setSoftDeletion(softDeletion); Query query = em.createQuery(queryString); int count = query.executeUpdate(); tx.commit(); return "Done: " + count + " entities affected, softDeletion=" + softDeletion; } finally { tx.end(); } } catch (Throwable e) { log.error("jpqlExecuteUpdate error", e); return ExceptionUtils.getStackTrace(e); } }
@Override public EntityManager getEntityManager(String store) { if (!TransactionSynchronizationManager.isActualTransactionActive()) throw new IllegalStateException("No active transaction"); EntityManagerFactory emf; if (Stores.isMain(store)) emf = this.jpaEmf; else emf = beanLocator.get("entityManagerFactory_" + store); javax.persistence.EntityManager jpaEm = EntityManagerFactoryUtils.doGetTransactionalEntityManager(emf, null, true); if (!jpaEm.isJoinedToTransaction()) throw new IllegalStateException("No active transaction for " + store + " database"); EntityManager entityManager = createEntityManager(jpaEm); EntityManagerContext ctx = contextHolder.get(store); if (ctx != null) { entityManager.setSoftDeletion(ctx.isSoftDeletion()); } else { ctx = new EntityManagerContext(); ctx.setSoftDeletion(isSoftDeletion()); contextHolder.set(ctx, store); entityManager.setSoftDeletion(isSoftDeletion()); } EntityManager emProxy = (EntityManager) Proxy.newProxyInstance( getClass().getClassLoader(), new Class[]{EntityManager.class}, new EntityManagerInvocationHandler(entityManager, store) ); return emProxy; }
@Override public void restoreEntities(Collection<Entity> entities) { for (Entity entity : entities) { if (!(entity instanceof SoftDelete)) continue; String storeName = metadata.getTools().getStoreName(metadata.getClassNN(entity.getClass())); if (storeName == null) { log.warn("Unable to restore entity {}: cannot determine data store", entity); continue; } Transaction tx = persistence.createTransaction(storeName); try { persistence.getEntityManager(storeName).setSoftDeletion(false); restoreEntity(entity, storeName); tx.commit(); } finally { tx.end(); } } }
protected void copyUserFolders(User fromUser, User toUser, Map<UUID, Presentation> presentationsMap) { try (Transaction tx = persistence.createTransaction()) { MetaClass effectiveMetaClass = metadata.getExtendedEntities().getEffectiveMetaClass(SearchFolder.class); EntityManager em = persistence.getEntityManager(); try { em.setSoftDeletion(false); Query deleteSettingsQuery = em.createQuery( String.format("delete from %s s where s.user.id = ?1", effectiveMetaClass.getName()) ); deleteSettingsQuery.setParameter(1, toUser.getId()); deleteSettingsQuery.executeUpdate(); } finally { em.setSoftDeletion(true); } TypedQuery<SearchFolder> q = em.createQuery( String.format("select s from %s s where s.user.id = ?1", effectiveMetaClass.getName()), SearchFolder.class); q.setParameter(1, fromUser.getId()); List<SearchFolder> fromUserFolders = q.getResultList(); Map<SearchFolder, SearchFolder> copiedFolders = new HashMap<>(); for (SearchFolder searchFolder : fromUserFolders) { copyFolder(searchFolder, toUser, copiedFolders, presentationsMap); } tx.commit(); } }
em.setSoftDeletion(false); Query deleteFiltersQuery = em.createQuery( String.format("delete from %s f where f.user.id = ?1", effectiveMetaClass.getName()) deleteFiltersQuery.executeUpdate(); } finally { em.setSoftDeletion(true);
try { EntityManager em = persistence.getEntityManager(); em.setSoftDeletion(loadContext.isSoftDeletion());
em.setSoftDeletion(context.isSoftDeletion());
try { EntityManager em = persistence.getEntityManager(); em.setSoftDeletion(false); Folder existingFolder = em.find(Folder.class, folder.getId()); if (existingFolder != null) {
em.setSoftDeletion(false); persistence.getEntityManagerContext(storeName).setDbHints(context.getDbHints());
try (Transaction tx = getLoadTransaction(context.isJoinTransaction())) { EntityManager em = persistence.getEntityManager(storeName); em.setSoftDeletion(context.isSoftDeletion()); persistence.getEntityManagerContext(storeName).setDbHints(context.getDbHints()); try (Transaction tx = getLoadTransaction(context.isJoinTransaction())) { EntityManager em = persistence.getEntityManager(storeName); em.setSoftDeletion(context.isSoftDeletion()); persistence.getEntityManagerContext(storeName).setDbHints(context.getDbHints());
try (Transaction tx = getLoadTransaction(context.isJoinTransaction())) { EntityManager em = persistence.getEntityManager(storeName); em.setSoftDeletion(context.isSoftDeletion()); persistence.getEntityManagerContext(storeName).setDbHints(context.getDbHints());
em.setSoftDeletion(false); Report existingReport; List<ReportTemplate> existingTemplates = null; em.setSoftDeletion(true);