* </pre>
* @throws ServiceException service exception
*/
public void updatePage(final JSONObject requestJSONObject) throws ServiceException {
final Transaction transaction = pageRepository.beginTransaction();
try {
final JSONObject page = requestJSONObject.getJSONObject(Page.PAGE);
final String pageId = page.getString(Keys.OBJECT_ID);
final JSONObject oldPage = pageRepository.get(pageId);
final JSONObject newPage = new JSONObject(page, JSONObject.getNames(page));
newPage.put(Page.PAGE_ORDER, oldPage.getInt(Page.PAGE_ORDER));
newPage.put(Page.PAGE_COMMENT_COUNT, oldPage.getInt(Page.PAGE_COMMENT_COUNT));
String permalink = page.optString(Page.PAGE_PERMALINK).trim();
final String oldPermalink = oldPage.getString(Page.PAGE_PERMALINK);
if (!oldPermalink.equals(permalink)) {
if (Strings.isEmptyOrNull(permalink)) {
permalink = "/pages/" + pageId + ".html";
}
if (Page.PAGE.equals(page.getString(Page.PAGE_TYPE))) {
if (!permalink.startsWith("/")) {
permalink = "/" + permalink;
}
if (PermalinkQueryService.invalidPagePermalinkFormat(permalink)) {
if (transaction.isActive()) {
transaction.rollback();
}
throw new ServiceException(langPropsService.get("invalidPermalinkFormatLabel"));
}
if (!oldPermalink.equals(permalink) && permalinkQueryService.exist(permalink)) {
if (transaction.isActive()) {
transaction.rollback();
}
throw new ServiceException(langPropsService.get("duplicatedPermalinkLabel"));
}
}
}
newPage.put(Page.PAGE_PERMALINK, permalink.replaceAll(" ", "-"));
if (!oldPage.getString(Page.PAGE_PERMALINK).equals(permalink)) { // The permalink has been updated
// Updates related comments' links
processCommentsForPageUpdate(newPage);
}
// Editor type
final JSONObject preference = preferenceQueryService.getPreference();
newPage.put(Page.PAGE_EDITOR_TYPE, preference.optString(Preference.EDITOR_TYPE));
pageRepository.update(pageId, newPage);
transaction.commit();
LOGGER.log(Level.DEBUG, "Updated a page[id={0}]", pageId);
} catch (final Exception e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
if (transaction.isActive()) {
transaction.rollback();
}
throw new ServiceException(e);
}
}