@Override
@Transactional
public Page updatePage(long pageId, String name, String pageLayoutCode) {
Page page = pageRepository.get(pageId);
PageLayout newLayout = pageLayoutRepository.getByPageLayoutCode(pageLayoutCode);
PageLayout curLayout = page.getPageLayout();
//if the region lengths of the layouts do not match then adjust the new layout
if (isLayoutAdjustmentNeeded(newLayout, curLayout)) {
//if the new layout has fewer regions than the previous layout the widgets from the
//deleted regions need to be appended to the last valid region in the new layout
if (curLayout.getNumberOfRegions() > newLayout.getNumberOfRegions()) {
reduceRegionsForPage(page, newLayout.getNumberOfRegions());
}
//otherwise the new layout has more regions that the previous layout and
//new regions need to be added to the page
else {
long numberOfNewRegionsToAdd = newLayout.getNumberOfRegions() - curLayout.getNumberOfRegions();
createAdditionalRegionsForPage(page, numberOfNewRegionsToAdd);
}
}
//save the new page properties