@Test public void testUndeleteThrowsExceptionWhenNotDeleted() throws GadgetUndeletionException { boolean exceptionOccurred = false; try { jpaTabMapper.undeleteGadget(fordsFirstTabFirstGadgetId); } catch (GadgetUndeletionException ex) { exceptionOccurred = true; assertEquals("GadgetUndeletionException was thrown, but didn't contain the " + " gadget id that the caller was trying to delete from.", fordsFirstTabFirstGadgetId, ex .getGadgetId()); } assertTrue("GadgetUndeletionException didn't occur when trying to delete a gadget that wasn't deleted.", exceptionOccurred); }
public Gadget undeleteGadget(final long gadgetId) throws GadgetUndeletionException { TabTemplate template = null; try { template = getTabTemplateByGadgetId(gadgetId, true); } catch (Exception ex) { throw new GadgetUndeletionException("Could not find either the specified gadget or tab for gadgetId=" + gadgetId, gadgetId); } Gadget gadgetToUndelete = (Gadget) entityManager.createQuery( "from Gadget where id = :gadgetId and deleted = true").setParameter("gadgetId", gadgetId) .getSingleResult(); if (gadgetToUndelete == null) { throw new GadgetUndeletionException("Failure when trying to get gadget with id=" + gadgetId, gadgetId); } try { for (Gadget currentGadget : template.getGadgets()) { if (currentGadget.getZoneNumber() == gadgetToUndelete.getZoneNumber() && currentGadget.getZoneIndex() >= gadgetToUndelete.getZoneIndex()) { currentGadget.setZoneIndex(currentGadget.getZoneIndex() + 1); } } template.getGadgets().add(gadgetToUndelete); entityManager.createQuery( "update versioned Gadget set deleted = false, " + "dateDeleted = null, tabTemplateId = :tabId " + "where id = :gadgetId").setParameter("gadgetId", gadgetToUndelete.getId()).setParameter( "tabId", template.getId()).executeUpdate(); return gadgetToUndelete; } catch (Exception ex) { throw new GadgetUndeletionException("An error occurred while trying to undelete the gadget with gadgetId=" + gadgetId, gadgetId); } }
TabMapper { public Gadget undeleteGadget(final long gadgetId) throws GadgetUndeletionException { TabTemplate template = null; try { template = getTabTemplateByGadgetId(gadgetId, true); } catch (Exception ex) { throw new GadgetUndeletionException("Could not find either the specified gadget or tab for gadgetId=" + gadgetId, gadgetId); } Gadget gadgetToUndelete = (Gadget) entityManager.createQuery( "from Gadget where id = :gadgetId and deleted = true").setParameter("gadgetId", gadgetId) .getSingleResult(); if (gadgetToUndelete == null) { throw new GadgetUndeletionException("Failure when trying to get gadget with id=" + gadgetId, gadgetId); } try { for (Gadget currentGadget : template.getGadgets()) { if (currentGadget.getZoneNumber() == gadgetToUndelete.getZoneNumber() && currentGadget.getZoneIndex() >= gadgetToUndelete.getZoneIndex()) { currentGadget.setZoneIndex(currentGadget.getZoneIndex() + 1); } } template.getGadgets().add(gadgetToUndelete); entityManager.createQuery( "update versioned Gadget set deleted = false, " + "dateDeleted = null, tabTemplateId = :tabId " + "where id = :gadgetId").setParameter("gadgetId", gadgetToUndelete.getId()).setParameter( "tabId", template.getId()).executeUpdate(); return gadgetToUndelete; } catch (Exception ex) { throw new GadgetUndeletionException("An error occurred while trying to undelete the gadget with gadgetId=" + gadgetId, gadgetId); } } }
TabMapper { public Gadget undeleteGadget(final long gadgetId) throws GadgetUndeletionException { TabTemplate template = null; try { template = getTabTemplateByGadgetId(gadgetId, true); } catch (Exception ex) { throw new GadgetUndeletionException("Could not find either the specified gadget or tab for gadgetId=" + gadgetId, gadgetId); } Gadget gadgetToUndelete = (Gadget) entityManager.createQuery( "from Gadget where id = :gadgetId and deleted = true").setParameter("gadgetId", gadgetId) .getSingleResult(); if (gadgetToUndelete == null) { throw new GadgetUndeletionException("Failure when trying to get gadget with id=" + gadgetId, gadgetId); } try { for (Gadget currentGadget : template.getGadgets()) { if (currentGadget.getZoneNumber() == gadgetToUndelete.getZoneNumber() && currentGadget.getZoneIndex() >= gadgetToUndelete.getZoneIndex()) { currentGadget.setZoneIndex(currentGadget.getZoneIndex() + 1); } } template.getGadgets().add(gadgetToUndelete); entityManager.createQuery( "update versioned Gadget set deleted = false, " + "dateDeleted = null, tabTemplateId = :tabId " + "where id = :gadgetId").setParameter("gadgetId", gadgetToUndelete.getId()).setParameter( "tabId", template.getId()).executeUpdate(); return gadgetToUndelete; } catch (Exception ex) { throw new GadgetUndeletionException("An error occurred while trying to undelete the gadget with gadgetId=" + gadgetId, gadgetId); } } TabMapper(final QueryOptimizer inQueryOptimizer); }
TabMapper { public Gadget undeleteGadget(final long gadgetId) throws GadgetUndeletionException { TabTemplate template = null; try { template = getTabTemplateByGadgetId(gadgetId, true); } catch (Exception ex) { throw new GadgetUndeletionException("Could not find either the specified gadget or tab for gadgetId=" + gadgetId, gadgetId); } Gadget gadgetToUndelete = (Gadget) entityManager.createQuery( "from Gadget where id = :gadgetId and deleted = true").setParameter("gadgetId", gadgetId) .getSingleResult(); if (gadgetToUndelete == null) { throw new GadgetUndeletionException("Failure when trying to get gadget with id=" + gadgetId, gadgetId); } try { for (Gadget currentGadget : template.getGadgets()) { if (currentGadget.getZoneNumber() == gadgetToUndelete.getZoneNumber() && currentGadget.getZoneIndex() >= gadgetToUndelete.getZoneIndex()) { currentGadget.setZoneIndex(currentGadget.getZoneIndex() + 1); } } template.getGadgets().add(gadgetToUndelete); entityManager.createQuery( "update versioned Gadget set deleted = false, " + "dateDeleted = null, tabTemplateId = :tabId " + "where id = :gadgetId").setParameter("gadgetId", gadgetToUndelete.getId()).setParameter( "tabId", template.getId()).executeUpdate(); return gadgetToUndelete; } catch (Exception ex) { throw new GadgetUndeletionException("An error occurred while trying to undelete the gadget with gadgetId=" + gadgetId, gadgetId); } } TabMapper(final QueryOptimizer inQueryOptimizer); TabTemplate getTabTemplate(final String type); void setUndeleteGadgetWindowInMinutes(final int undeleteWindowInMinutes); @PersistenceContext void setEntityManager(final EntityManager inEntityManager); Tab findById(final Long tabId); TabTemplate findByGadgetId(final Long gadgetId); Tab findTabByGadgetId(final Long gadgetId); Tab findById(final Integer tabId); void flush(); void deleteGadget(final Gadget gadgetToDelete); Gadget undeleteGadget(final long gadgetId); @SuppressWarnings("unchecked") void moveGadget(final Long gadgetId, final Long sourceTabTemplateId, final Integer sourceZoneIndex,
            final Integer sourceZoneNumber, final Long targetTabTemplateId, final Integer targetZoneIndex,
            final Integer targetZoneNumber); }
TabMapper { public Gadget undeleteGadget(final long gadgetId) throws GadgetUndeletionException { TabTemplate template = null; try { template = getTabTemplateByGadgetId(gadgetId, true); } catch (Exception ex) { throw new GadgetUndeletionException("Could not find either the specified gadget or tab for gadgetId=" + gadgetId, gadgetId); } Gadget gadgetToUndelete = (Gadget) entityManager.createQuery( "from Gadget where id = :gadgetId and deleted = true").setParameter("gadgetId", gadgetId) .getSingleResult(); if (gadgetToUndelete == null) { throw new GadgetUndeletionException("Failure when trying to get gadget with id=" + gadgetId, gadgetId); } try { for (Gadget currentGadget : template.getGadgets()) { if (currentGadget.getZoneNumber() == gadgetToUndelete.getZoneNumber() && currentGadget.getZoneIndex() >= gadgetToUndelete.getZoneIndex()) { currentGadget.setZoneIndex(currentGadget.getZoneIndex() + 1); } } template.getGadgets().add(gadgetToUndelete); entityManager.createQuery( "update versioned Gadget set deleted = false, " + "dateDeleted = null, tabTemplateId = :tabId " + "where id = :gadgetId").setParameter("gadgetId", gadgetToUndelete.getId()).setParameter( "tabId", template.getId()).executeUpdate(); return gadgetToUndelete; } catch (Exception ex) { throw new GadgetUndeletionException("An error occurred while trying to undelete the gadget with gadgetId=" + gadgetId, gadgetId); } } TabMapper(final QueryOptimizer inQueryOptimizer); TabTemplate getTabTemplate(final String type); void setUndeleteGadgetWindowInMinutes(final int undeleteWindowInMinutes); @PersistenceContext void setEntityManager(final EntityManager inEntityManager); Tab findById(final Long tabId); TabTemplate findByGadgetId(final Long gadgetId); Tab findTabByGadgetId(final Long gadgetId); Tab findById(final Integer tabId); void flush(); void deleteGadget(final Gadget gadgetToDelete); Gadget undeleteGadget(final long gadgetId); @SuppressWarnings("unchecked") void moveGadget(final Long gadgetId, final Long sourceTabTemplateId, final Integer sourceZoneIndex,
            final Integer sourceZoneNumber, final Long targetTabTemplateId, final Integer targetZoneIndex,
            final Integer targetZoneNumber);  }