* @return generated page id
* @throws ServiceException if permalink format checks failed or persists
* failed
*/
public String addPage(final JSONObject requestJSONObject) throws ServiceException {
final Transaction transaction = pageRepository.beginTransaction();
try {
final JSONObject page = requestJSONObject.getJSONObject(Page.PAGE);
page.put(Page.PAGE_COMMENT_COUNT, 0);
final int maxOrder = pageRepository.getMaxOrder();
page.put(Page.PAGE_ORDER, maxOrder + 1);
String permalink = page.optString(Page.PAGE_PERMALINK);
if (Strings.isEmptyOrNull(permalink)) {
permalink = "/pages/" + Ids.genTimeMillisId() + ".html";
}
if (Page.PAGE.equals(page.getString(Page.PAGE_TYPE))) {
if (!permalink.startsWith("/")) {
permalink = "/" + permalink;
}
if (Permalinks.invalidPagePermalinkFormat(permalink)) {
if (transaction.isActive()) {
transaction.rollback();
}
throw new ServiceException(langPropsService.get("invalidPermalinkFormatLabel"));
}
if (permalinks.exist(permalink)) {
if (transaction.isActive()) {
transaction.rollback();
}
throw new ServiceException(langPropsService.get("duplicatedPermalinkLabel"));
}
}
// TODO: SBC case
page.put(Page.PAGE_PERMALINK, permalink.replaceAll(" ", "-"));
// Editor type
final JSONObject preference = preferenceQueryService.getPreference();
page.put(Page.PAGE_EDITOR_TYPE, preference.optString(Preference.EDITOR_TYPE));
final String ret = pageRepository.add(page);
transaction.commit();
return ret;
} catch (final JSONException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
if (transaction.isActive()) {
transaction.rollback();
}
throw new ServiceException(e);
} catch (final RepositoryException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
if (transaction.isActive()) {
transaction.rollback();
}
throw new ServiceException(e);
}
}