respectFrontendRoles)) {
throw new DotSecurityException("You don't have permission to wirte in the destination folder.");
}
boolean isNew = false;
HTMLPage newHTMLPage;
if (forceOverwrite) {
newHTMLPage = getWorkingHTMLPageByPageURL(source.getPageUrl(), destination);
if (newHTMLPage == null) {
isNew = true;
}
} else {
isNew = true;
}
newHTMLPage = new HTMLPage();
newHTMLPage.copy(source);
// gets page url before extension
String pageURL = UtilMethods.getFileName(source.getPageUrl());
// gets file extension
String fileExtension = UtilMethods.getFileExtension(source.getPageUrl());
if (!forceOverwrite) {
newHTMLPage.setPageUrl(getCopyHTMLPageName(pageURL, fileExtension, destination));
if (!UtilMethods.getFileName(newHTMLPage.getPageUrl()).equals(pageURL))
newHTMLPage.setFriendlyName(source.getFriendlyName() + " (COPY)");
}
Template destinationTemplate = reMapping.getDestinationTemplate();
List<MultiTree> associatedSourceContentlets = null;
//Checking if contentlets just need to be remapped or need to be copied on destination
if (copyMode == HTMLPageAPI.CopyMode.USE_SOURCE_CONTENT) {
associatedSourceContentlets = getHTMLPageMultiTree(source);
} else if (copyMode == HTMLPageAPI.CopyMode.COPY_SOURCE_CONTENT) {
associatedSourceContentlets = getHTMLPageMultiTree(source);
Contentlet contentlet;
FolderAPI folderAPI = APILocator.getFolderAPI();
Host systemHost = hostAPI.findSystemHost(user, respectFrontendRoles);
Folder systemFolder = folderAPI.findSystemFolder();
Host destinationHost = hostAPI.find(destination.getHostId(), user, respectFrontendRoles);
for (MultiTree multiTree : associatedSourceContentlets) {
contentlet = contentletAPI.findContentletByIdentifier(multiTree.getChild(), false, 0, user,
respectFrontendRoles);
Host contentletHost = null;
if(!UtilMethods.isSet(contentlet.getHost()) && !contentlet.getHost().equals(systemHost.getInode())) {
contentletHost = hostAPI.find(contentlet.getHost(), user, respectFrontendRoles);
}
Folder contentletFolder = null;
if(!UtilMethods.isSet(contentlet.getFolder()) && !contentlet.getFolder().equals(systemFolder.getInode())) {
contentletFolder = folderAPI.find(contentlet.getFolder(),user,false);
}
if (contentletFolder != null) {
Folder contentletDestFolder = folderAPI.createFolders(APILocator.getIdentifierAPI().find(contentletFolder).getPath(), destinationHost,user,false);
contentlet = contentletAPI.copyContentlet(contentlet, contentletDestFolder, user, respectFrontendRoles);
} else if (contentletHost != null) {
contentlet = contentletAPI.copyContentlet(contentlet, destinationHost, user, respectFrontendRoles);
} else {
contentlet = contentletAPI.copyContentlet(contentlet, user, respectFrontendRoles);
}
multiTree.setChild(contentlet.getIdentifier());
}
}
//Creating the new pages associations mapping with containers
List<MultiTree> newContentletAssociation = new LinkedList<MultiTree>();
if (copyMode != HTMLPageAPI.CopyMode.BLANK_HTMLPAGE) {
for (MultiTree multiTree : associatedSourceContentlets) {
String sourceContainerId = multiTree.getParent2();
String destinationContainerId = null;
for (int i = 0; i < reMapping.getContainersRemap().size(); i++) {
ContainerRemapTuple tuple = reMapping.getContainersRemap().get(i);
if(tuple.getSourceContainer().getIdentifier().equals(sourceContainerId)) {
destinationContainerId = tuple.getDestinationContainer().getIdentifier();
}
}
if(destinationContainerId != null) {
newContentletAssociation.add(new MultiTree("", destinationContainerId, multiTree.getChild()));
}
}
}
newHTMLPage.setTemplateId(destinationTemplate.getIdentifier());
//Persisting the new page
if (isNew) {
// creates new identifier for this webasset and persists it
Identifier newIdentifier = com.dotmarketing.business.APILocator.getIdentifierAPI().createNew(newHTMLPage, destination);
newHTMLPage.setIdentifier(newIdentifier.getInode());
// persists the webasset
save(newHTMLPage);
} else {
saveHTMLPage(newHTMLPage, destinationTemplate, destination, user, respectFrontendRoles);
}
if(source.isLive()){
APILocator.getVersionableAPI().setWorking(newHTMLPage);
APILocator.getVersionableAPI().setLive(newHTMLPage);
}
//Saving the new content mapping
if (copyMode != HTMLPageAPI.CopyMode.BLANK_HTMLPAGE) {
MultiTree newMultiTree;
for (MultiTree multiTree : newContentletAssociation) {
newMultiTree = new MultiTree(newHTMLPage.getIdentifier(), multiTree.getParent2(), multiTree.getChild());
MultiTreeFactory.saveMultiTree(newMultiTree);
}
}
// Copy permissions