HttpServletRequest httpReq = reqImpl.getHttpServletRequest();
LinkForm linkForm = (LinkForm) form;
//gets the new information for the link from the request object
Link link = new Link();
link.setTitle(((LinkForm) form).getTitle());
BeanUtils.copyProperties(link,form);
req.setAttribute(WebKeys.LINK_FORM_EDIT,link);
boolean previousShowMenu = link.isShowOnMenu();
//gets the current link being edited from the request object
Link currentLink = (Link) req.getAttribute(WebKeys.LINK_EDIT);
//parent folder or inode for this file
Folder parent = APILocator.getFolderAPI().find(req.getParameter("parent"), user, false);
//http://jira.dotmarketing.net/browse/DOTCMS-5899
if(UtilMethods.isSet(currentLink.getInode())){
Identifier id = APILocator.getIdentifierAPI().find(currentLink);
String URI = id.getURI();
String uriPath = URI.substring(0,URI.lastIndexOf("/")+1);
if(!uriPath.equals(APILocator.getIdentifierAPI().find(parent).getPath())){
id.setURI(APILocator.getIdentifierAPI().find(parent).getPath()+currentLink.getProtocal() + currentLink.getUrl());
APILocator.getIdentifierAPI().save(id);
}
}
//Checking permissions
_checkPermissions(currentLink, parent, user, httpReq);
//gets user id from request for mod user
String userId = user.getUserId();
// take care of internal links
if (InodeUtils.isSet(linkForm.getInternalLinkIdentifier())) {
Identifier internalLinkIdentifier = identifierAPI.findFromInode(linkForm.getInternalLinkIdentifier());
//link.setLinkType(LinkType.INTERNAL.toString());
link.setInternalLinkIdentifier(internalLinkIdentifier.getInode());
link.setProtocal("http://");
StringBuffer myURL = new StringBuffer();
if(InodeUtils.isSet(internalLinkIdentifier.getHostId())) {
Host host = hostAPI.find(internalLinkIdentifier.getHostId(), user, false);
myURL.append(host.getHostname());
}
myURL.append(internalLinkIdentifier.getURI());
link.setUrl(myURL.toString());
}
if(link.getLinkType().equals(LinkType.CODE.toString()))
link.setProtocal("");
Link workingLink = null;
//it saves or updates the asset
if (InodeUtils.isSet(currentLink.getInode())) {
Identifier identifier = APILocator.getIdentifierAPI().find(currentLink);
WebAssetFactory
.createAsset(link, userId, parent, identifier, false);
workingLink = (Link) WebAssetFactory.saveAsset(link, identifier);
currentLink = link;
link = workingLink;
req.setAttribute(WebKeys.LINK_FORM_EDIT,link);
if (!currentLink.getTarget().equals(link.getTarget())) {
//create new identifier, with the URI
APILocator.getIdentifierAPI().updateIdentifierURI(workingLink,(Folder) parent);
}
} else {
WebAssetFactory.createAsset(link, userId, parent);
req.setAttribute(WebKeys.LINK_EDIT, link);
workingLink = link;
}
// Get parents of the old version so you can update the working
// information to this new version.
List<Object> parents = (List<Object>) InodeFactory.getParentsOfClass(currentLink, Category.class);
parents.addAll(InodeFactory.getParentsOfClass(currentLink, Contentlet.class));
List<Inode> children = (List<Inode>) InodeFactory.getChildrenClass(currentLink, Category.class);
children.addAll(InodeFactory.getChildrenClass(currentLink, Contentlet.class));
java.util.Iterator parentsIterator = parents.iterator();
//update parents to new version delete old versions parents if not
// live.
while (parentsIterator.hasNext()) {
Object obj = parentsIterator.next();
if(obj instanceof Inode){
Inode parentInode = (Inode) obj;
parentInode.addChild(workingLink);
//to keep relation types from parent only if it exists
Tree tree = com.dotmarketing.factories.TreeFactory.getTree(
parentInode, currentLink);
if ((tree.getRelationType() != null)
&& (tree.getRelationType().length() != 0)) {
Tree newTree = com.dotmarketing.factories.TreeFactory.getTree(
parentInode, workingLink);
newTree.setRelationType(tree.getRelationType());
newTree.setTreeOrder(0);
TreeFactory.saveTree(newTree);
}
}
/*if(obj instanceof Identifier){
Identifier parentIdentifier = (Identifier) obj;
parentIdentifier.addChild(workingLink);
//to keep relation types from parent only if it exists
Tree tree = com.dotmarketing.factories.TreeFactory.getTree(
parentIdentifier.getInode(), currentLink.getInode());
if ((tree.getRelationType() != null)
&& (tree.getRelationType().length() != 0)) {
Tree newTree = com.dotmarketing.factories.TreeFactory.getTree(
parentIdentifier.getInode(), workingLink.getInode());
newTree.setRelationType(tree.getRelationType());
newTree.setTreeOrder(0);
TreeFactory.saveTree(newTree);
}
//checks type of parent and deletes child if not live version.
if (!currentLink.isLive()) {
if (!(parentIdentifier instanceof Identifier)) {
parentIdentifier.deleteChild(currentLink);
}
}
}
*/
// //Republishing parents working contentlets
// if (parentInode instanceof Contentlet) {
// Contentlet cont = (Contentlet) parentInode;
//
// if (cont.isWorking()) {
// //calls the asset factory edit
// ContentletServices.writeContentletToFile(cont, true);
// ContentletMapServices.writeContentletMapToFile(cont, true);
// }
// }
}
if (req.getParameter("popup") != null) {
req.setAttribute("inode", String.valueOf(workingLink.getInode()));
}
//Refreshing the menues
if (!(!previousShowMenu && !link.isShowOnMenu()))
{