@Test public void executeTestWithFeedBeingDeleted() { getEntityManager().createQuery("DELETE FROM FeedSubscriber WHERE id = 2").executeUpdate(); getEntityManager().createQuery("DELETE FROM FeedSubscriber WHERE id = 5").executeUpdate(); Long origTotalSubscriptionCount = (Long) getEntityManager().createQuery("SELECT COUNT(*) FROM FeedSubscriber") .getSingleResult(); Long origTargetSubscriptionCount = (Long) getEntityManager() .createQuery("SELECT COUNT(*) FROM FeedSubscriber WHERE type = :type AND entityId = :entityId") .setParameter("type", EntityType.GROUP).setParameter("entityId", entityId).getSingleResult(); sut.execute(new DeleteAllFeedSubscriberByEntityTypeAndIdRequest(entityId, EntityType.GROUP)); getEntityManager().flush(); getEntityManager().clear(); Long newTotalSubscriptionCount = (Long) getEntityManager().createQuery("SELECT COUNT(*) FROM FeedSubscriber") .getSingleResult(); Long newTargetSubscriptionCount = (Long) getEntityManager() .createQuery("SELECT COUNT(*) FROM FeedSubscriber WHERE type = :type AND entityId = :entityId") .setParameter("type", EntityType.GROUP).setParameter("entityId", entityId).getSingleResult(); assertEquals(0L, newTargetSubscriptionCount.longValue()); assertEquals(newTotalSubscriptionCount.longValue(), origTotalSubscriptionCount.longValue() - origTargetSubscriptionCount.longValue()); assertEquals(0L, getEntityManager().createQuery("SELECT COUNT(*) FROM Feed WHERE id=2").getSingleResult()); }
@Override public Boolean execute(final DeleteAllFeedSubscriberByEntityTypeAndIdRequest inRequest) { List<Long> feedIds = getEntityManager() .createQuery( "SELECT DISTINCT feed.id FROM FeedSubscriber where entityId = :entityId AND type = :entityType") .setParameter("entityId", inRequest.getEntityId()) .setParameter("entityType", inRequest.getEntityType()).getResultList(); getEntityManager() .createQuery("DELETE FROM FeedSubscriber fs WHERE fs.entityId = :entityId AND fs.type = :entityType") .setParameter("entityId", inRequest.getEntityId()) .setParameter("entityType", inRequest.getEntityType()).executeUpdate(); if (feedIds.size() > 0) { List<Object[]> feedIdAndSubscriberCounts = getEntityManager() .createQuery( "SELECT f.id, count(fs.id) FROM Feed f LEFT OUTER JOIN f.feedSubscribers fs " + "WHERE f.id IN (:feedIds) GROUP BY f.id").setParameter("feedIds", feedIds) .getResultList(); List<Long> feedsIdsToDelete = new ArrayList<Long>(); for (Object[] feedIdAndSubscriberCount : feedIdAndSubscriberCounts) { if ((Long) feedIdAndSubscriberCount[1] == 0) { feedsIdsToDelete.add((Long) feedIdAndSubscriberCount[0]); } } if (feedsIdsToDelete.size() > 0) { getEntityManager().createQuery("DELETE FROM Feed WHERE id IN (:feedIds)") .setParameter("feedIds", feedsIdsToDelete).executeUpdate(); } } return Boolean.TRUE; }
DeleteAllFeedSubscriberByEntityTypeAndId extends
        BaseArgDomainMapper<DeleteAllFeedSubscriberByEntityTypeAndIdRequest, Boolean> { @Override public Boolean execute(final DeleteAllFeedSubscriberByEntityTypeAndIdRequest inRequest) { List<Long> feedIds = getEntityManager() .createQuery( "SELECT DISTINCT feed.id FROM FeedSubscriber where entityId = :entityId AND type = :entityType") .setParameter("entityId", inRequest.getEntityId()) .setParameter("entityType", inRequest.getEntityType()).getResultList(); getEntityManager() .createQuery("DELETE FROM FeedSubscriber fs WHERE fs.entityId = :entityId AND fs.type = :entityType") .setParameter("entityId", inRequest.getEntityId()) .setParameter("entityType", inRequest.getEntityType()).executeUpdate(); if (feedIds.size() > 0) { List<Object[]> feedIdAndSubscriberCounts = getEntityManager() .createQuery( "SELECT f.id, count(fs.id) FROM Feed f LEFT OUTER JOIN f.feedSubscribers fs " + "WHERE f.id IN (:feedIds) GROUP BY f.id").setParameter("feedIds", feedIds) .getResultList(); List<Long> feedsIdsToDelete = new ArrayList<Long>(); for (Object[] feedIdAndSubscriberCount : feedIdAndSubscriberCounts) { if ((Long) feedIdAndSubscriberCount[1] == 0) { feedsIdsToDelete.add((Long) feedIdAndSubscriberCount[0]); } } if (feedsIdsToDelete.size() > 0) { getEntityManager().createQuery("DELETE FROM Feed WHERE id IN (:feedIds)") .setParameter("feedIds", feedsIdsToDelete).executeUpdate(); } } return Boolean.TRUE; } }
DeleteAllFeedSubscriberByEntityTypeAndId extends
        BaseArgDomainMapper<DeleteAllFeedSubscriberByEntityTypeAndIdRequest, Boolean> { @Override public Boolean execute(final DeleteAllFeedSubscriberByEntityTypeAndIdRequest inRequest) { List<Long> feedIds = getEntityManager() .createQuery( "SELECT DISTINCT feed.id FROM FeedSubscriber where entityId = :entityId AND type = :entityType") .setParameter("entityId", inRequest.getEntityId()) .setParameter("entityType", inRequest.getEntityType()).getResultList(); getEntityManager() .createQuery("DELETE FROM FeedSubscriber fs WHERE fs.entityId = :entityId AND fs.type = :entityType") .setParameter("entityId", inRequest.getEntityId()) .setParameter("entityType", inRequest.getEntityType()).executeUpdate(); if (feedIds.size() > 0) { List<Object[]> feedIdAndSubscriberCounts = getEntityManager() .createQuery( "SELECT f.id, count(fs.id) FROM Feed f LEFT OUTER JOIN f.feedSubscribers fs " + "WHERE f.id IN (:feedIds) GROUP BY f.id").setParameter("feedIds", feedIds) .getResultList(); List<Long> feedsIdsToDelete = new ArrayList<Long>(); for (Object[] feedIdAndSubscriberCount : feedIdAndSubscriberCounts) { if ((Long) feedIdAndSubscriberCount[1] == 0) { feedsIdsToDelete.add((Long) feedIdAndSubscriberCount[0]); } } if (feedsIdsToDelete.size() > 0) { getEntityManager().createQuery("DELETE FROM Feed WHERE id IN (:feedIds)") .setParameter("feedIds", feedsIdsToDelete).executeUpdate(); } } return Boolean.TRUE; }  }
DeleteAllFeedSubscriberByEntityTypeAndId extends
        BaseArgDomainMapper<DeleteAllFeedSubscriberByEntityTypeAndIdRequest, Boolean> { @Override public Boolean execute(final DeleteAllFeedSubscriberByEntityTypeAndIdRequest inRequest) { List<Long> feedIds = getEntityManager() .createQuery( "SELECT DISTINCT feed.id FROM FeedSubscriber where entityId = :entityId AND type = :entityType") .setParameter("entityId", inRequest.getEntityId()) .setParameter("entityType", inRequest.getEntityType()).getResultList(); getEntityManager() .createQuery("DELETE FROM FeedSubscriber fs WHERE fs.entityId = :entityId AND fs.type = :entityType") .setParameter("entityId", inRequest.getEntityId()) .setParameter("entityType", inRequest.getEntityType()).executeUpdate(); if (feedIds.size() > 0) { List<Object[]> feedIdAndSubscriberCounts = getEntityManager() .createQuery( "SELECT f.id, count(fs.id) FROM Feed f LEFT OUTER JOIN f.feedSubscribers fs " + "WHERE f.id IN (:feedIds) GROUP BY f.id").setParameter("feedIds", feedIds) .getResultList(); List<Long> feedsIdsToDelete = new ArrayList<Long>(); for (Object[] feedIdAndSubscriberCount : feedIdAndSubscriberCounts) { if ((Long) feedIdAndSubscriberCount[1] == 0) { feedsIdsToDelete.add((Long) feedIdAndSubscriberCount[0]); } } if (feedsIdsToDelete.size() > 0) { getEntityManager().createQuery("DELETE FROM Feed WHERE id IN (:feedIds)") .setParameter("feedIds", feedsIdsToDelete).executeUpdate(); } } return Boolean.TRUE; }  @Override Boolean execute(final DeleteAllFeedSubscriberByEntityTypeAndIdRequest inRequest); }
DeleteAllFeedSubscriberByEntityTypeAndId extends
        BaseArgDomainMapper<DeleteAllFeedSubscriberByEntityTypeAndIdRequest, Boolean> { @Override public Boolean execute(final DeleteAllFeedSubscriberByEntityTypeAndIdRequest inRequest) { List<Long> feedIds = getEntityManager() .createQuery( "SELECT DISTINCT feed.id FROM FeedSubscriber where entityId = :entityId AND type = :entityType") .setParameter("entityId", inRequest.getEntityId()) .setParameter("entityType", inRequest.getEntityType()).getResultList(); getEntityManager() .createQuery("DELETE FROM FeedSubscriber fs WHERE fs.entityId = :entityId AND fs.type = :entityType") .setParameter("entityId", inRequest.getEntityId()) .setParameter("entityType", inRequest.getEntityType()).executeUpdate(); if (feedIds.size() > 0) { List<Object[]> feedIdAndSubscriberCounts = getEntityManager() .createQuery( "SELECT f.id, count(fs.id) FROM Feed f LEFT OUTER JOIN f.feedSubscribers fs " + "WHERE f.id IN (:feedIds) GROUP BY f.id").setParameter("feedIds", feedIds) .getResultList(); List<Long> feedsIdsToDelete = new ArrayList<Long>(); for (Object[] feedIdAndSubscriberCount : feedIdAndSubscriberCounts) { if ((Long) feedIdAndSubscriberCount[1] == 0) { feedsIdsToDelete.add((Long) feedIdAndSubscriberCount[0]); } } if (feedsIdsToDelete.size() > 0) { getEntityManager().createQuery("DELETE FROM Feed WHERE id IN (:feedIds)") .setParameter("feedIds", feedsIdsToDelete).executeUpdate(); } } return Boolean.TRUE; }  @Override Boolean execute(final DeleteAllFeedSubscriberByEntityTypeAndIdRequest inRequest);  }