final Section[] results = new Section[]{null};
HibernateTxFragment txFragment = new HibernateTxFragment() {
protected void txFragment(Session session) throws Exception {
SectionCopyOption copyOption = sco;
log.debug("Copying section " + section.getId() + " to Workspace " + workspace.getId());
if (copyOption == null) {
if (workspace.getId().equals(section.getWorkspace().getId()))
copyOption = CopyOption.DEFAULT_SECTION_COPY_OPTION_SAME_WORKSPACE;
else
copyOption = CopyOption.DEFAULT_SECTION_COPY_OPTION_OTHER_WORKSPACE;
}
Section sectionCopy = (Section) section.clone();
sectionCopy.setPosition(-1); //Let the workspace decide the position later
sectionCopy.setWorkspace(workspace);
boolean copyingToSameWorkspace = workspace.getId().equals(section.getWorkspace().getId());
if (copyingToSameWorkspace) {//Section in same workspace-> can't reuse the id
sectionCopy.setId(null);
} else {//Section in different workspace-> can reuse the url
sectionCopy.setFriendlyUrl(section.getFriendlyUrl());
}
if (log.isDebugEnabled())
log.debug("Storing basic section copy to workspace " + workspace.getId());
UIServices.lookup().getSectionsManager().store(sectionCopy);
// Add to destination workspace
if (log.isDebugEnabled())
log.debug("Adding cloned section (" + sectionCopy.getId() + ") to workspace " + workspace.getId());
workspace.addSection(sectionCopy);
UIServices.lookup().getWorkspacesManager().store(workspace);
//Resources
copyResources(section, sectionCopy);
// Panels inside section
LayoutRegion[] regions = section.getLayout().getRegions();
log.debug("Regions in section are " + Arrays.asList(regions));
Hashtable panelInstanceMappings = new Hashtable();
for (int i = 0; i < regions.length; i++) {
LayoutRegion region = regions[i];
Panel[] panels = section.getPanels(region);
if (log.isDebugEnabled())
log.debug("Copying " + panels.length + " panels in region " + region);
for (int j = 0; panels != null && j < panels.length; j++) {
Panel panelClone = null;
PanelInstance instanceClone = panels[j].getInstance();
String panelInstanceId = panels[j].getInstanceId().toString();
if (copyOption.isDuplicatePanelInstance(panelInstanceId)) { //Duplicate Panel instance for this panel.
if (panelInstanceMappings.containsKey(panelInstanceId)) {
instanceClone = (PanelInstance) panelInstanceMappings.get(panelInstanceId);
} else {
instanceClone = copy(panels[j].getInstance(), workspace);
panelInstanceMappings.put(panelInstanceId, instanceClone);