Long targetTabId = request.getCurrentTabId();
Long gadgetId = request.getGadgetId();
Integer targetZoneNumber = request.getTargetZoneNumber();
Integer targetZoneIndex = request.getTargetZoneIndex();
TabTemplate sourceTemplate = tabMapper.findByGadgetId(gadgetId);
Tab destinationTab = tabMapper.findById(targetTabId);
// Ensure that the destination tab exists.
if (destinationTab == null)
{
vex.addError("invalidTab", "Destination zone does not exist.");
throw vex;
}
TabTemplate destinationTemplate = destinationTab.getTemplate();
Layout destinationLayout = destinationTemplate.getTabLayout();
// Save the Source and Destination TabTemplate to state so they can be reused in execution.
inActionContext.getState().put("destinationTemplate", destinationTemplate);
inActionContext.getState().put("sourceTemplate", sourceTemplate);
// Destination zone is within the valid number of destination zones.
if (targetZoneNumber + 1 > destinationLayout.getNumberOfZones())
{
vex.addError("invalidZone", "ReorderGadgetAction told to move a gadget to a nonexistent zone.");
throw vex;
}
// Ensure that the gadget to be moved exists.
if (sourceTemplate == null)
{
vex.addError("invalidGadget", "Gadget to be moved is invalid.");
throw vex;
}
// Create a map of the zonenumbers and a list of the corresponding zone indexes.
HashMap<Integer, List<Integer>> gadgetZoneIndexes = new HashMap<Integer, List<Integer>>();
for (Gadget currentGadget : destinationTemplate.getGadgets())
{
if (gadgetZoneIndexes.containsKey(currentGadget.getZoneNumber()))
{
gadgetZoneIndexes.get(currentGadget.getZoneNumber()).add(currentGadget.getZoneIndex());
}