use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class StatisticMgmtService method incBlogViewCount.
/**
* Blog statistic view count +1.
*
* <p>
* If it is a search engine bot made the specified request, will NOT increment blog statistic view count.
* </p>
*
* <p>
* There is a cron job (/console/stat/viewcnt) to flush the blog view count from cache to datastore.
* </p>
*
* @param request the specified request
* @param response the specified response
* @throws ServiceException service exception
* @see Requests#searchEngineBotRequest(javax.servlet.http.HttpServletRequest)
*/
public void incBlogViewCount(final HttpServletRequest request, final HttpServletResponse response) throws ServiceException {
if (Requests.searchEngineBotRequest(request)) {
return;
}
if (Requests.hasBeenServed(request, response)) {
return;
}
final Transaction transaction = statisticRepository.beginTransaction();
JSONObject statistic = null;
try {
statistic = statisticRepository.get(Statistic.STATISTIC);
if (null == statistic) {
return;
}
LOGGER.log(Level.TRACE, "Before inc blog view count[statistic={0}]", statistic);
int blogViewCnt = statistic.optInt(Statistic.STATISTIC_BLOG_VIEW_COUNT);
++blogViewCnt;
statistic.put(Statistic.STATISTIC_BLOG_VIEW_COUNT, blogViewCnt);
statisticRepository.update(Statistic.STATISTIC, statistic);
transaction.commit();
} catch (final RepositoryException e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Updates blog view count failed", e);
return;
}
LOGGER.log(Level.TRACE, "Inced blog view count[statistic={0}]", statistic);
}
use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class TagMgmtService method removeUnusedTags.
/**
* Removes all unused tags.
*
* @throws ServiceException if get tags failed, or remove failed
*/
public void removeUnusedTags() throws ServiceException {
final Transaction transaction = tagRepository.beginTransaction();
try {
final List<JSONObject> tags = tagQueryService.getTags();
for (int i = 0; i < tags.size(); i++) {
final JSONObject tag = tags.get(i);
final int tagRefCnt = tag.getInt(Tag.TAG_REFERENCE_COUNT);
if (0 == tagRefCnt) {
final String tagId = tag.getString(Keys.OBJECT_ID);
tagRepository.remove(tagId);
}
}
transaction.commit();
} catch (final Exception e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Removes unused tags failed", e);
throw new ServiceException(e);
}
}
use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class UserMgmtService method removeUser.
/**
* Removes a user specified by the given user id.
*
* @param userId the given user id
* @throws ServiceException service exception
*/
public void removeUser(final String userId) throws ServiceException {
final Transaction transaction = userRepository.beginTransaction();
try {
userRepository.remove(userId);
transaction.commit();
} catch (final RepositoryException e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Removes a user[id=" + userId + "] failed", e);
throw new ServiceException(e);
}
}
use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class UserMgmtService method updateUser.
/**
* Updates a user by the specified request json object.
*
* @param requestJSONObject the specified request json object, for example, <pre>
* {
* "oId": "",
* "userName": "",
* "userEmail": "",
* "userPassword": "", // Unhashed
* "userRole": "", // optional
* "userURL": "", // optional
* }
* </pre>
*
* @throws ServiceException service exception
*/
public void updateUser(final JSONObject requestJSONObject) throws ServiceException {
final Transaction transaction = userRepository.beginTransaction();
try {
final String oldUserId = requestJSONObject.optString(Keys.OBJECT_ID);
final JSONObject oldUser = userRepository.get(oldUserId);
if (null == oldUser) {
throw new ServiceException(langPropsService.get("updateFailLabel"));
}
final String userNewEmail = requestJSONObject.optString(User.USER_EMAIL).toLowerCase().trim();
// Check email is whether duplicated
final JSONObject mayBeAnother = userRepository.getByEmail(userNewEmail);
if (null != mayBeAnother && !mayBeAnother.optString(Keys.OBJECT_ID).equals(oldUserId)) {
// Exists someone else has the save email as requested
throw new ServiceException(langPropsService.get("duplicatedEmailLabel"));
}
// Update
final String userName = requestJSONObject.optString(User.USER_NAME);
final String userPassword = requestJSONObject.optString(User.USER_PASSWORD);
oldUser.put(User.USER_EMAIL, userNewEmail);
oldUser.put(User.USER_NAME, userName);
final boolean mybeHashed = HASHED_PASSWORD_LENGTH == userPassword.length();
final String newHashedPassword = MD5.hash(userPassword);
final String oldHashedPassword = oldUser.optString(User.USER_PASSWORD);
if (!"demo.b3log.org".equals(Latkes.getServerHost())) {
// Skips the Solo Online Demo (http://demo.b3log.org)
if (!mybeHashed || (!oldHashedPassword.equals(userPassword) && !oldHashedPassword.equals(newHashedPassword))) {
oldUser.put(User.USER_PASSWORD, newHashedPassword);
}
}
final String userRole = requestJSONObject.optString(User.USER_ROLE);
if (!Strings.isEmptyOrNull(userRole)) {
oldUser.put(User.USER_ROLE, userRole);
}
final String userURL = requestJSONObject.optString(User.USER_URL);
if (!Strings.isEmptyOrNull(userURL)) {
oldUser.put(User.USER_URL, userURL);
}
final String userAvatar = requestJSONObject.optString(UserExt.USER_AVATAR);
if (!StringUtils.equals(userAvatar, oldUser.optString(UserExt.USER_AVATAR))) {
oldUser.put(UserExt.USER_AVATAR, userAvatar);
}
userRepository.update(oldUserId, oldUser);
transaction.commit();
} catch (final RepositoryException e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Updates a user failed", e);
throw new ServiceException(e);
}
}
use of org.b3log.latke.repository.Transaction in project solo by b3log.
the class UserMgmtService method changeRole.
/**
* Swithches the user role between "defaultRole" and "visitorRole" by the specified user id.
*
* @param userId the specified user id
* @throws ServiceException exception
* @see User
*/
public void changeRole(final String userId) throws ServiceException {
final Transaction transaction = userRepository.beginTransaction();
try {
final JSONObject oldUser = userRepository.get(userId);
if (null == oldUser) {
throw new ServiceException(langPropsService.get("updateFailLabel"));
}
final String role = oldUser.optString(User.USER_ROLE);
if (Role.VISITOR_ROLE.equals(role)) {
oldUser.put(User.USER_ROLE, Role.DEFAULT_ROLE);
} else if (Role.DEFAULT_ROLE.equals(role)) {
oldUser.put(User.USER_ROLE, Role.VISITOR_ROLE);
}
userRepository.update(userId, oldUser);
transaction.commit();
} catch (final RepositoryException e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Updates a user failed", e);
throw new ServiceException(e);
}
}
Aggregations