{
template = getTabTemplateByGadgetId(gadgetId, true);
}
catch (Exception ex)
{
throw new GadgetUndeletionException("Could not find either the specified gadget or tab for gadgetId="
+ gadgetId, gadgetId);
}
/* get the deleted gadget from the database */
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
{
/*
* bump up the zone index of each gadget in the same zone as the gadget to be undeleted
*/
for (Gadget currentGadget : template.getGadgets())
{
if (currentGadget.getZoneNumber() == gadgetToUndelete.getZoneNumber()
&& currentGadget.getZoneIndex() >= gadgetToUndelete.getZoneIndex())
{
currentGadget.setZoneIndex(currentGadget.getZoneIndex() + 1);
}
}
/* add the gadget back into the collection */
template.getGadgets().add(gadgetToUndelete);
/* update the status of the undeleted gadget in the database */
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);
}
}