use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class OptionMgmtService method removeOption.
/**
* Removes the option specified by the given option id.
*
* @param optionId the given option id
* @throws ServiceException service exception
*/
public void removeOption(final String optionId) throws ServiceException {
final Transaction transaction = optionRepository.beginTransaction();
try {
optionRepository.remove(optionId);
transaction.commit();
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
throw new ServiceException(e);
}
}
use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class PageMgmtService method changeOrder.
/**
* Changes the order of a page specified by the given page id with the specified direction.
*
* @param pageId the given page id
* @param direction the specified direction, "up"/"down"
* @throws ServiceException service exception
*/
public void changeOrder(final String pageId, final String direction) throws ServiceException {
final Transaction transaction = pageRepository.beginTransaction();
try {
final JSONObject srcPage = pageRepository.get(pageId);
final int srcPageOrder = srcPage.getInt(Page.PAGE_ORDER);
JSONObject targetPage;
if ("up".equals(direction)) {
targetPage = pageRepository.getUpper(pageId);
} else {
// Down
targetPage = pageRepository.getUnder(pageId);
}
if (null == targetPage) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.WARN, "Cant not find the target page of source page[order={0}]", srcPageOrder);
return;
}
// Swaps
srcPage.put(Page.PAGE_ORDER, targetPage.getInt(Page.PAGE_ORDER));
targetPage.put(Page.PAGE_ORDER, srcPageOrder);
pageRepository.update(srcPage.getString(Keys.OBJECT_ID), srcPage);
pageRepository.update(targetPage.getString(Keys.OBJECT_ID), targetPage);
transaction.commit();
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Changes page's order failed", e);
throw new ServiceException(e);
}
}
use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class PageMgmtService method removePage.
/**
* Removes a page specified by the given page id.
*
* @param pageId the given page id
* @throws ServiceException service exception
*/
public void removePage(final String pageId) throws ServiceException {
final Transaction transaction = pageRepository.beginTransaction();
try {
LOGGER.log(Level.DEBUG, "Removing a page[id={0}]", pageId);
removePageComments(pageId);
pageRepository.remove(pageId);
transaction.commit();
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Removes a page[id=" + pageId + "] failed", e);
throw new ServiceException(e);
}
}
use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class PageMgmtService method updatePage.
/**
* Updates a page by the specified request json object.
*
* @param requestJSONObject the specified request json object, for example, <pre>
* {
* "page": {
* "oId": "",
* "pageTitle": "",
* "pageContent": "",
* "pageOrder": int,
* "pageCommentCount": int,
* "pagePermalink": "",
* "pageCommentable": boolean,
* "pageType": "",
* "pageOpenTarget": "",
* "pageEditorType": "" // optional, preference specified if not exists this key
* }
* }, see {@link Page} for more details
* </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);
}
// Set editor type
if (!newPage.has(Page.PAGE_EDITOR_TYPE)) {
final JSONObject preference = preferenceQueryService.getPreference();
newPage.put(Page.PAGE_EDITOR_TYPE, preference.optString(Option.ID_C_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);
}
}
use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class PageMgmtService method addPage.
/**
* Adds a page with the specified request json object.
*
* @param requestJSONObject the specified request json object, for example, <pre>
* {
* "page": {
* "pageTitle": "",
* "pageContent": "",
* "pageOpenTarget": "",
* "pageCommentable": boolean,
* "pageType": "",
* "pagePermalink": "", // optional
* "pageEditorType": "" // optional, preference specified if not exists this key
* }
* }, see {@link Page} for more details
* </pre>
*
* @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 (PermalinkQueryService.invalidPagePermalinkFormat(permalink)) {
if (transaction.isActive()) {
transaction.rollback();
}
throw new ServiceException(langPropsService.get("invalidPermalinkFormatLabel"));
}
if (permalinkQueryService.exist(permalink)) {
if (transaction.isActive()) {
transaction.rollback();
}
throw new ServiceException(langPropsService.get("duplicatedPermalinkLabel"));
}
}
page.put(Page.PAGE_PERMALINK, permalink.replaceAll(" ", "-"));
// Set editor type
if (!page.has(Page.PAGE_EDITOR_TYPE)) {
final JSONObject preference = preferenceQueryService.getPreference();
page.put(Page.PAGE_EDITOR_TYPE, preference.optString(Option.ID_C_EDITOR_TYPE));
}
final String ret = pageRepository.add(page);
transaction.commit();
return ret;
} catch (final JSONException e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
if (transaction.isActive()) {
transaction.rollback();
}
throw new ServiceException(e);
} catch (final RepositoryException e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
if (transaction.isActive()) {
transaction.rollback();
}
throw new ServiceException(e);
}
}
Aggregations