@Test public void testRefreshGadgetDefinitionUserCounts() { PagedSet<GadgetDefinition> results = jpaGadgetDefinitionMapper.findSortedByPopularity(0, 3); assertEquals("Size of result set is invalid", 4, results.getPagedSet().size()); assertEquals("Most popular before refresh is not 4", 4, results.getPagedSet().get(0) .getNumberOfUsers()); assertEquals("Most popular before refresh is not 3", 3, results.getPagedSet().get(1) .getNumberOfUsers()); assertEquals("Most popular before refresh is not 2", 2, results.getPagedSet().get(2) .getNumberOfUsers()); assertEquals("Most popular before refresh is not 1", 1, results.getPagedSet().get(3) .getNumberOfUsers()); jpaGadgetDefinitionMapper.refreshGadgetDefinitionUserCounts(); getEntityManager().clear(); PagedSet<GadgetDefinition> refreshedResults = jpaGadgetDefinitionMapper.findSortedByPopularity(0, 3); assertEquals("Size of refreshed result set is invalid", 4, refreshedResults.getPagedSet().size()); assertEquals("Most popular after refresh is not 4", 3, refreshedResults.getPagedSet().get(0) .getNumberOfUsers()); assertEquals("Most popular after refresh is not 4", 2, refreshedResults.getPagedSet().get(1) .getNumberOfUsers()); assertEquals("Most popular after refresh is not 1", 1, refreshedResults.getPagedSet().get(2) .getNumberOfUsers()); assertEquals("Most popular after refresh is not 0", 0, refreshedResults.getPagedSet().get(3) .getNumberOfUsers()); }
@SuppressWarnings("unchecked") public void refreshGadgetDefinitionUserCounts() { Query q = getEntityManager().createQuery("from GadgetDefinition gd"); List<GadgetDefinition> gadgetDefs = (List<GadgetDefinition>) q.getResultList(); logger.debug("Retrieved: " + gadgetDefs.size() + " Gadget defs from db"); Query gadgetQuery; int numUsersCount; for (GadgetDefinition currentGadgetDef : gadgetDefs) { gadgetQuery = getEntityManager().createQuery( "select g.owner.id " + "from Gadget g " + "where g.gadgetDefinition.id=:gadgetDefId " + "and g.owner.id is not null " + "group by g.owner.id, g.gadgetDefinition.id") .setParameter("gadgetDefId", currentGadgetDef.getId()); try { numUsersCount = gadgetQuery.getResultList().size(); logger.debug("Retrieved: " + numUsersCount + " users for the current gadget def: " + currentGadgetDef.getUrl()); } catch (NoResultException nrex) { numUsersCount = 0; } currentGadgetDef.setNumberOfUsers(numUsersCount); } getEntityManager().flush(); }
GadgetDefinitionMapper extends CommonGadgetDefinitionMapper<GadgetDefinition> implements
        GalleryItemMapper<GadgetDefinition> { @SuppressWarnings("unchecked") public void refreshGadgetDefinitionUserCounts() { Query q = getEntityManager().createQuery("from GadgetDefinition gd"); List<GadgetDefinition> gadgetDefs = (List<GadgetDefinition>) q.getResultList(); logger.debug("Retrieved: " + gadgetDefs.size() + " Gadget defs from db"); Query gadgetQuery; int numUsersCount; for (GadgetDefinition currentGadgetDef : gadgetDefs) { gadgetQuery = getEntityManager().createQuery( "select g.owner.id " + "from Gadget g " + "where g.gadgetDefinition.id=:gadgetDefId " + "and g.owner.id is not null " + "group by g.owner.id, g.gadgetDefinition.id") .setParameter("gadgetDefId", currentGadgetDef.getId()); try { numUsersCount = gadgetQuery.getResultList().size(); logger.debug("Retrieved: " + numUsersCount + " users for the current gadget def: " + currentGadgetDef.getUrl()); } catch (NoResultException nrex) { numUsersCount = 0; } currentGadgetDef.setNumberOfUsers(numUsersCount); } getEntityManager().flush(); } }
GadgetDefinitionMapper extends CommonGadgetDefinitionMapper<GadgetDefinition> implements
        GalleryItemMapper<GadgetDefinition> { @SuppressWarnings("unchecked") public void refreshGadgetDefinitionUserCounts() { Query q = getEntityManager().createQuery("from GadgetDefinition gd"); List<GadgetDefinition> gadgetDefs = (List<GadgetDefinition>) q.getResultList(); logger.debug("Retrieved: " + gadgetDefs.size() + " Gadget defs from db"); Query gadgetQuery; int numUsersCount; for (GadgetDefinition currentGadgetDef : gadgetDefs) { gadgetQuery = getEntityManager().createQuery( "select g.owner.id " + "from Gadget g " + "where g.gadgetDefinition.id=:gadgetDefId " + "and g.owner.id is not null " + "group by g.owner.id, g.gadgetDefinition.id") .setParameter("gadgetDefId", currentGadgetDef.getId()); try { numUsersCount = gadgetQuery.getResultList().size(); logger.debug("Retrieved: " + numUsersCount + " users for the current gadget def: " + currentGadgetDef.getUrl()); } catch (NoResultException nrex) { numUsersCount = 0; } currentGadgetDef.setNumberOfUsers(numUsersCount); } getEntityManager().flush(); } GadgetDefinitionMapper(final QueryOptimizer inQueryOptimizer); }
GadgetDefinitionMapper extends CommonGadgetDefinitionMapper<GadgetDefinition> implements
        GalleryItemMapper<GadgetDefinition> { @SuppressWarnings("unchecked") public void refreshGadgetDefinitionUserCounts() { Query q = getEntityManager().createQuery("from GadgetDefinition gd"); List<GadgetDefinition> gadgetDefs = (List<GadgetDefinition>) q.getResultList(); logger.debug("Retrieved: " + gadgetDefs.size() + " Gadget defs from db"); Query gadgetQuery; int numUsersCount; for (GadgetDefinition currentGadgetDef : gadgetDefs) { gadgetQuery = getEntityManager().createQuery( "select g.owner.id " + "from Gadget g " + "where g.gadgetDefinition.id=:gadgetDefId " + "and g.owner.id is not null " + "group by g.owner.id, g.gadgetDefinition.id") .setParameter("gadgetDefId", currentGadgetDef.getId()); try { numUsersCount = gadgetQuery.getResultList().size(); logger.debug("Retrieved: " + numUsersCount + " users for the current gadget def: " + currentGadgetDef.getUrl()); } catch (NoResultException nrex) { numUsersCount = 0; } currentGadgetDef.setNumberOfUsers(numUsersCount); } getEntityManager().flush(); } GadgetDefinitionMapper(final QueryOptimizer inQueryOptimizer); @SuppressWarnings("unchecked") void refreshGadgetDefinitionUserCounts(); @Override void delete(final GadgetDefinition inGadgetDefinition); }
GadgetDefinitionMapper extends CommonGadgetDefinitionMapper<GadgetDefinition> implements
        GalleryItemMapper<GadgetDefinition> { @SuppressWarnings("unchecked") public void refreshGadgetDefinitionUserCounts() { Query q = getEntityManager().createQuery("from GadgetDefinition gd"); List<GadgetDefinition> gadgetDefs = (List<GadgetDefinition>) q.getResultList(); logger.debug("Retrieved: " + gadgetDefs.size() + " Gadget defs from db"); Query gadgetQuery; int numUsersCount; for (GadgetDefinition currentGadgetDef : gadgetDefs) { gadgetQuery = getEntityManager().createQuery( "select g.owner.id " + "from Gadget g " + "where g.gadgetDefinition.id=:gadgetDefId " + "and g.owner.id is not null " + "group by g.owner.id, g.gadgetDefinition.id") .setParameter("gadgetDefId", currentGadgetDef.getId()); try { numUsersCount = gadgetQuery.getResultList().size(); logger.debug("Retrieved: " + numUsersCount + " users for the current gadget def: " + currentGadgetDef.getUrl()); } catch (NoResultException nrex) { numUsersCount = 0; } currentGadgetDef.setNumberOfUsers(numUsersCount); } getEntityManager().flush(); } GadgetDefinitionMapper(final QueryOptimizer inQueryOptimizer); @SuppressWarnings("unchecked") void refreshGadgetDefinitionUserCounts(); @Override void delete(final GadgetDefinition inGadgetDefinition);  }