// wraps request to get session object
ActionRequestImpl reqImpl = (ActionRequestImpl) req;
HttpServletRequest httpReq = reqImpl.getHttpServletRequest();
// Retreiving the current HTMLPage
HTMLPage existingHTMLPage = (HTMLPage) req.getAttribute(WebKeys.HTMLPAGE_EDIT);
boolean previousShowMenu = existingHTMLPage.isShowOnMenu();
// parent folder or inode for this file
Folder parentFolder = APILocator.getFolderAPI().find(req.getParameter("parent"), user, false);
// http://jira.dotmarketing.net/browse/DOTCMS-5899
Identifier id = null;
if (UtilMethods.isSet(existingHTMLPage.getIdentifier()))
id = APILocator.getIdentifierAPI().find(existingHTMLPage);
if (id != null && UtilMethods.isSet(id.getInode())) {
String URI = id.getURI();
String uriPath = URI.substring(0, URI.lastIndexOf("/") + 1);
if (!uriPath.equals(APILocator.getIdentifierAPI().find(parentFolder).getPath())) {
id.setURI(APILocator.getIdentifierAPI().find(parentFolder).getPath() + existingHTMLPage.getPageUrl());
APILocator.getIdentifierAPI().save(id);
}
}
req.setAttribute(WebKeys.PARENT_FOLDER, parentFolder); // Since the
// above query
// is expensive,
// save it into
// request
// object
String template = req.getParameter("template");
if (!UtilMethods.isSet(template)) {
SessionMessages.add(httpReq, "error", "message.htmlpage.select.Template");
throw new Exception(LanguageUtil.get(user, "message.htmlpage.select.Template"));
}
// Adds template children from selected box
Identifier templateIdentifier = APILocator.getIdentifierAPI().find(template);
// Checking permissions
_checkPermissions(existingHTMLPage, parentFolder, user, httpReq);
// parent identifier for this file
Identifier identifier = null;
if (UtilMethods.isSet(existingHTMLPage.getInode()))
identifier = APILocator.getIdentifierAPI().find(existingHTMLPage);
// Creating the new page
HTMLPage newHtmlPage = new HTMLPage();
req.setAttribute(WebKeys.HTMLPAGE_FORM_EDIT, newHtmlPage);
BeanUtils.copyProperties(newHtmlPage, form);
if (UtilMethods.isSet(newHtmlPage.getFriendlyName())) {
newHtmlPage.setFriendlyName(newHtmlPage.getFriendlyName());
} else {
newHtmlPage.setFriendlyName(newHtmlPage.getTitle());
}
// add VELOCITY_PAGE_EXTENSION to the pagename
if (!newHtmlPage.getPageUrl().endsWith("." + Config.getStringProperty("VELOCITY_PAGE_EXTENSION"))) {
newHtmlPage.setPageUrl(newHtmlPage.getPageUrl() + "." + Config.getStringProperty("VELOCITY_PAGE_EXTENSION"));
}
// Some checks
// Get asset host based on the parentFolder of the asset
Host host = hostAPI.findParentHost(parentFolder, user, false);
// get an identifier based on this new uri
Identifier testIdentifier = APILocator.getIdentifierAPI().find(host, newHtmlPage.getURI(parentFolder));
// if this is a new htmlpage and there is already an identifier with
// this uri, return
if (!InodeUtils.isSet(existingHTMLPage.getInode()) && InodeUtils.isSet(testIdentifier.getInode())) {
existingHTMLPage.setParent(parentFolder.getInode());
SessionMessages.add(httpReq, "error", "message.htmlpage.error.htmlpage.exists");
throw new ActionException("Another page with the same page url exists in this folder");
}
// if this is an existing htmlpage and there is already an identifier
// with this uri, return
else if (InodeUtils.isSet(existingHTMLPage.getInode()) && (!testIdentifier.getInode().equalsIgnoreCase(identifier.getInode()))
&& InodeUtils.isSet(testIdentifier.getInode())) {
SessionMessages.add(httpReq, "error", "message.htmlpage.error.htmlpage.exists");
// when there is an error saving should unlock working asset
WebAssetFactory.unLockAsset(existingHTMLPage);
throw new ActionException("Another page with the same page url exists in this folder");
}
if (UtilMethods.isSet(template)) {
newHtmlPage.setTemplateId(templateIdentifier.getInode());
}
HTMLPage workingAsset = null;
// Versioning
if (InodeUtils.isSet(existingHTMLPage.getInode())) {
// Creation the version asset
WebAssetFactory.createAsset(newHtmlPage, user.getUserId(), parentFolder, identifier, false);
HibernateUtil.flush();
LiveCache.removeAssetFromCache(existingHTMLPage);
workingAsset = (HTMLPage) WebAssetFactory.saveAsset(newHtmlPage, identifier);
// if we need to update the identifier
if (InodeUtils.isSet(parentFolder.getInode()) && !workingAsset.getURI(parentFolder).equals(identifier.getURI())) {
// assets cache
LiveCache.removeAssetFromCache(newHtmlPage);
LiveCache.removeAssetFromCache(existingHTMLPage);
LiveCache.clearCache(host.getIdentifier());
WorkingCache.removeAssetFromCache(newHtmlPage);
CacheLocator.getIdentifierCache().removeFromCacheByVersionable(newHtmlPage);
CacheLocator.getIdentifierCache().removeFromCacheByVersionable(existingHTMLPage);
APILocator.getIdentifierAPI().updateIdentifierURI(workingAsset, parentFolder);
}
CacheLocator.getHTMLPageCache().remove(workingAsset);
} // Creating the new page
else {
WebAssetFactory.createAsset(newHtmlPage, user.getUserId(), parentFolder);
workingAsset = newHtmlPage;
}
HibernateUtil.flush();
HibernateUtil.getSession().refresh(workingAsset);
if (RefreshMenus.shouldRefreshMenus(workingAsset)) {
RefreshMenus.deleteMenu(workingAsset);
if(identifier!=null)
CacheLocator.getNavToolCache().removeNavByPath(identifier.getHostId(), identifier.getParentPath());
}
// Setting the new working page to publish tasks
req.setAttribute(WebKeys.HTMLPAGE_FORM_EDIT, workingAsset);
String this_page = workingAsset.getURI(parentFolder);
req.setAttribute(WebKeys.HTMLPAGE_REFERER, this_page);
ActivityLogger.logInfo(this.getClass(), "save HTMLpage action", "User " + user.getPrimaryKey() + " save page " + workingAsset.getTitle(), HostUtil.hostNameUtil(req, _getUser(req)));
}