Commit 4d2ecb1a authored by Liang Ding's avatar Liang Ding

Updated javadoc..

parent d14ddcda
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration> <project-shared-configuration>
<!-- <!--
This file contains additional configuration written by modules in the NetBeans IDE. This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout. therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether. Without this configuration present, some functionality in the IDE may be limited or fail altogether.
--> -->
<spellchecker-wordlist xmlns="http://www.netbeans.org/ns/spellchecker-wordlist/1"> <spellchecker-wordlist xmlns="http://www.netbeans.org/ns/spellchecker-wordlist/1">
<word>Admin</word> <word>Admin</word>
<word>app</word> <word>app</word>
<word>blog</word> <word>blog</word>
<word>blogger</word> <word>blogger</word>
<word>blogging</word> <word>blogging</word>
<word>captcha</word> <word>captcha</word>
<word>Captcha</word> <word>Captcha</word>
<word>Captchas</word> <word>Captchas</word>
<word>cron</word> <word>cron</word>
<word>datastore</word> <word>datastore</word>
<word>ftl</word> <word>ftl</word>
<word>Gmail</word> <word>Gmail</word>
<word>http</word> <word>http</word>
<word>json</word> <word>json</word>
<word>Latke</word> <word>Latke</word>
<word>loc</word> <word>loc</word>
<word>memcache</word> <word>memcache</word>
<word>permalink</word> <word>permalink</word>
<word>permalinks</word> <word>permalinks</word>
<word>Picasa</word> <word>Picasa</word>
<word>Plugin</word> <word>Plugin</word>
<word>plugin</word> <word>plugin</word>
<word>Pluginable</word> <word>Pluginable</word>
<word>Plugins</word> <word>Plugins</word>
<word>plugins</word> <word>plugins</word>
<word>servlet</word> <word>servlet</word>
<word>servlets</word> <word>servlets</word>
<word>sitemap</word> <word>sitemap</word>
<word>struct</word> <word>struct</word>
<word>structs</word> <word>structs</word>
<word>Upgrader</word> <word>Swithches</word>
<word>Upgraders</word> <word>Upgrader</word>
</spellchecker-wordlist> <word>Upgraders</word>
</project-shared-configuration> </spellchecker-wordlist>
</project-shared-configuration>
/* /*
* Copyright (c) 2009, 2010, 2011, 2012, 2013, B3log Team * Copyright (c) 2009, 2010, 2011, 2012, 2013, B3log Team
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.b3log.solo.service; package org.b3log.solo.service;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.b3log.latke.Keys; import org.b3log.latke.Keys;
import org.b3log.latke.model.Role; import org.b3log.latke.model.Role;
import org.b3log.latke.model.User; import org.b3log.latke.model.User;
import org.b3log.latke.repository.RepositoryException; import org.b3log.latke.repository.RepositoryException;
import org.b3log.latke.repository.Transaction; import org.b3log.latke.repository.Transaction;
import org.b3log.latke.service.LangPropsService; import org.b3log.latke.service.LangPropsService;
import org.b3log.latke.service.ServiceException; import org.b3log.latke.service.ServiceException;
import org.b3log.latke.util.MD5; import org.b3log.latke.util.MD5;
import org.b3log.solo.model.UserExt; import org.b3log.solo.model.UserExt;
import org.b3log.solo.repository.UserRepository; import org.b3log.solo.repository.UserRepository;
import org.b3log.solo.repository.impl.UserRepositoryImpl; import org.b3log.solo.repository.impl.UserRepositoryImpl;
import org.json.JSONObject; import org.json.JSONObject;
/** /**
* User management service. * User management service.
* *
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a> * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @author <a href="mailto:385321165@qq.com">DASHU</a> * @author <a href="mailto:385321165@qq.com">DASHU</a>
* @version 1.0.0.4, Mar 30, 2013 * @version 1.0.0.4, Mar 30, 2013
* @since 0.4.0 * @since 0.4.0
*/ */
public final class UserMgmtService { public final class UserMgmtService {
/** /**
* Logger. * Logger.
*/ */
private static final Logger LOGGER = Logger.getLogger(UserMgmtService.class.getName()); private static final Logger LOGGER = Logger.getLogger(UserMgmtService.class.getName());
/** /**
* User repository. * User repository.
*/ */
private UserRepository userRepository = UserRepositoryImpl.getInstance(); private UserRepository userRepository = UserRepositoryImpl.getInstance();
/** /**
* Language service. * Language service.
*/ */
private LangPropsService langPropsService = LangPropsService.getInstance(); private LangPropsService langPropsService = LangPropsService.getInstance();
/** /**
* Updates a user by the specified request json object. * Updates a user by the specified request json object.
* *
* @param requestJSONObject the specified request json object, for example, * @param requestJSONObject the specified request json object, for example,
* <pre> * <pre>
* { * {
* "oId": "", * "oId": "",
* "userName": "", * "userName": "",
* "userEmail": "", * "userEmail": "",
* "userPassword": "", // Unhashed * "userPassword": "", // Unhashed
* "userRole": "" * "userRole": ""
* } * }
* </pre> * </pre>
* @throws ServiceException service exception * @throws ServiceException service exception
*/ */
public void updateUser(final JSONObject requestJSONObject) throws ServiceException { public void updateUser(final JSONObject requestJSONObject) throws ServiceException {
final Transaction transaction = userRepository.beginTransaction(); final Transaction transaction = userRepository.beginTransaction();
try { try {
final String oldUserId = requestJSONObject.optString(Keys.OBJECT_ID); final String oldUserId = requestJSONObject.optString(Keys.OBJECT_ID);
final JSONObject oldUser = userRepository.get(oldUserId); final JSONObject oldUser = userRepository.get(oldUserId);
if (null == oldUser) { if (null == oldUser) {
throw new ServiceException(langPropsService.get("updateFailLabel")); throw new ServiceException(langPropsService.get("updateFailLabel"));
} }
final String userNewEmail = requestJSONObject.optString(User.USER_EMAIL).toLowerCase().trim(); final String userNewEmail = requestJSONObject.optString(User.USER_EMAIL).toLowerCase().trim();
// Check email is whether duplicated // Check email is whether duplicated
final JSONObject mayBeAnother = userRepository.getByEmail(userNewEmail); final JSONObject mayBeAnother = userRepository.getByEmail(userNewEmail);
if (null != mayBeAnother && !mayBeAnother.optString(Keys.OBJECT_ID).equals(oldUserId)) { if (null != mayBeAnother && !mayBeAnother.optString(Keys.OBJECT_ID).equals(oldUserId)) {
// Exists someone else has the save email as requested // Exists someone else has the save email as requested
throw new ServiceException(langPropsService.get("duplicatedEmailLabel")); throw new ServiceException(langPropsService.get("duplicatedEmailLabel"));
} }
// Update // Update
final String userName = requestJSONObject.optString(User.USER_NAME); final String userName = requestJSONObject.optString(User.USER_NAME);
final String userPassword = requestJSONObject.optString(User.USER_PASSWORD); final String userPassword = requestJSONObject.optString(User.USER_PASSWORD);
oldUser.put(User.USER_EMAIL, userNewEmail); oldUser.put(User.USER_EMAIL, userNewEmail);
oldUser.put(User.USER_NAME, userName); oldUser.put(User.USER_NAME, userName);
oldUser.put(User.USER_PASSWORD, MD5.hash(userPassword)); oldUser.put(User.USER_PASSWORD, MD5.hash(userPassword));
// Unchanges the default role // Unchanges the default role
userRepository.update(oldUserId, oldUser); userRepository.update(oldUserId, oldUser);
transaction.commit(); transaction.commit();
} catch (final RepositoryException e) { } catch (final RepositoryException e) {
if (transaction.isActive()) { if (transaction.isActive()) {
transaction.rollback(); transaction.rollback();
} }
LOGGER.log(Level.SEVERE, "Updates a user failed", e); LOGGER.log(Level.SEVERE, "Updates a user failed", e);
throw new ServiceException(e); throw new ServiceException(e);
} }
} }
/** /**
* change user role by the specified userid. * Swithches the user role between "defaultRole" and "visitorRole" by the specified user id.
* *
* @param userId the specified userid * @param userId the specified user id
* @throws ServiceException exception * @throws ServiceException exception
*/ * @see User
public void changeRole(final String userId) throws ServiceException { */
final Transaction transaction = userRepository.beginTransaction(); public void changeRole(final String userId) throws ServiceException {
final Transaction transaction = userRepository.beginTransaction();
try {
final JSONObject oldUser = userRepository.get(userId); try {
final JSONObject oldUser = userRepository.get(userId);
if (null == oldUser) {
throw new ServiceException(langPropsService.get("updateFailLabel")); if (null == oldUser) {
} throw new ServiceException(langPropsService.get("updateFailLabel"));
}
final String role = oldUser.optString(User.USER_ROLE);
final String role = oldUser.optString(User.USER_ROLE);
if (Role.VISITOR_ROLE.equals(role)) {
oldUser.put(User.USER_ROLE, Role.DEFAULT_ROLE); if (Role.VISITOR_ROLE.equals(role)) {
} else if (Role.DEFAULT_ROLE.equals(role)) { oldUser.put(User.USER_ROLE, Role.DEFAULT_ROLE);
oldUser.put(User.USER_ROLE, Role.VISITOR_ROLE); } else if (Role.DEFAULT_ROLE.equals(role)) {
} oldUser.put(User.USER_ROLE, Role.VISITOR_ROLE);
}
userRepository.update(userId, oldUser);
transaction.commit(); userRepository.update(userId, oldUser);
} catch (final RepositoryException e) { transaction.commit();
if (transaction.isActive()) { } catch (final RepositoryException e) {
transaction.rollback(); if (transaction.isActive()) {
} transaction.rollback();
}
LOGGER.log(Level.SEVERE, "Updates a user failed", e);
throw new ServiceException(e); LOGGER.log(Level.SEVERE, "Updates a user failed", e);
} throw new ServiceException(e);
} }
}
/**
* Adds a user with the specified request json object. /**
* * Adds a user with the specified request json object.
* @param requestJSONObject the specified request json object, for example, *
* <pre> * @param requestJSONObject the specified request json object, for example,
* { * <pre>
* "userName": "", * {
* "userEmail": "", * "userName": "",
* "userPassword": "", // Unhashed * "userEmail": "",
* "userRole": "" // optional, uses {@value Role#DEFAULT_ROLE} instead, * "userPassword": "", // Unhashed
* if not speciffied * "userRole": "" // optional, uses {@value Role#DEFAULT_ROLE} instead,
* } * if not speciffied
* </pre>,see {@link User} for more details * }
* @return generated user id * </pre>,see {@link User} for more details
* @throws ServiceException service exception * @return generated user id
*/ * @throws ServiceException service exception
public String addUser(final JSONObject requestJSONObject) throws ServiceException { */
final Transaction transaction = userRepository.beginTransaction(); public String addUser(final JSONObject requestJSONObject) throws ServiceException {
final Transaction transaction = userRepository.beginTransaction();
try {
final JSONObject user = new JSONObject(); try {
final String userEmail = requestJSONObject.optString(User.USER_EMAIL).trim().toLowerCase(); final JSONObject user = new JSONObject();
final JSONObject duplicatedUser = userRepository.getByEmail(userEmail); final String userEmail = requestJSONObject.optString(User.USER_EMAIL).trim().toLowerCase();
final JSONObject duplicatedUser = userRepository.getByEmail(userEmail);
if (null != duplicatedUser) {
if (transaction.isActive()) { if (null != duplicatedUser) {
transaction.rollback(); if (transaction.isActive()) {
} transaction.rollback();
}
throw new ServiceException(langPropsService.get("duplicatedEmailLabel"));
} throw new ServiceException(langPropsService.get("duplicatedEmailLabel"));
}
final String userName = requestJSONObject.optString(User.USER_NAME);
final String userName = requestJSONObject.optString(User.USER_NAME);
user.put(User.USER_EMAIL, userEmail);
user.put(User.USER_NAME, userName); user.put(User.USER_EMAIL, userEmail);
final String userPassword = requestJSONObject.optString(User.USER_PASSWORD); user.put(User.USER_NAME, userName);
final String userPassword = requestJSONObject.optString(User.USER_PASSWORD);
user.put(User.USER_PASSWORD, MD5.hash(userPassword));
final String roleName = requestJSONObject.optString(User.USER_ROLE, Role.DEFAULT_ROLE); user.put(User.USER_PASSWORD, MD5.hash(userPassword));
final String roleName = requestJSONObject.optString(User.USER_ROLE, Role.DEFAULT_ROLE);
user.put(User.USER_ROLE, roleName);
user.put(UserExt.USER_ARTICLE_COUNT, 0); user.put(User.USER_ROLE, roleName);
user.put(UserExt.USER_PUBLISHED_ARTICLE_COUNT, 0); user.put(UserExt.USER_ARTICLE_COUNT, 0);
user.put(UserExt.USER_PUBLISHED_ARTICLE_COUNT, 0);
userRepository.add(user);
userRepository.add(user);
transaction.commit();
transaction.commit();
return user.optString(Keys.OBJECT_ID);
} catch (final RepositoryException e) { return user.optString(Keys.OBJECT_ID);
if (transaction.isActive()) { } catch (final RepositoryException e) {
transaction.rollback(); if (transaction.isActive()) {
} transaction.rollback();
}
LOGGER.log(Level.SEVERE, "Adds a user failed", e);
throw new ServiceException(e); LOGGER.log(Level.SEVERE, "Adds a user failed", e);
} throw new ServiceException(e);
} }
}
/**
* Removes a user specified by the given user id. /**
* * Removes a user specified by the given user id.
* @param userId the given user id *
* @throws ServiceException service exception * @param userId the given user id
*/ * @throws ServiceException service exception
public void removeUser(final String userId) throws ServiceException { */
final Transaction transaction = userRepository.beginTransaction(); public void removeUser(final String userId) throws ServiceException {
final Transaction transaction = userRepository.beginTransaction();
try {
userRepository.remove(userId); try {
userRepository.remove(userId);
transaction.commit();
} catch (final RepositoryException e) { transaction.commit();
if (transaction.isActive()) { } catch (final RepositoryException e) {
transaction.rollback(); if (transaction.isActive()) {
} transaction.rollback();
}
LOGGER.log(Level.SEVERE, "Removes a user[id=" + userId + "] failed", e);
throw new ServiceException(e); LOGGER.log(Level.SEVERE, "Removes a user[id=" + userId + "] failed", e);
} throw new ServiceException(e);
} }
}
/**
* Gets the {@link UserMgmtService} singleton. /**
* * Gets the {@link UserMgmtService} singleton.
* @return the singleton *
*/ * @return the singleton
public static UserMgmtService getInstance() { */
return SingletonHolder.SINGLETON; public static UserMgmtService getInstance() {
} return SingletonHolder.SINGLETON;
}
/**
* Private constructor. /**
*/ * Private constructor.
private UserMgmtService() {} */
private UserMgmtService() {}
/**
* Singleton holder. /**
* * Singleton holder.
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a> *
* @version 1.0.0.0, Oct 28, 2011 * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
*/ * @version 1.0.0.0, Oct 28, 2011
private static final class SingletonHolder { */
private static final class SingletonHolder {
/**
* Singleton. /**
*/ * Singleton.
private static final UserMgmtService SINGLETON = new UserMgmtService(); */
private static final UserMgmtService SINGLETON = new UserMgmtService();
/**
* Private default constructor. /**
*/ * Private default constructor.
private SingletonHolder() {} */
} private SingletonHolder() {}
} }
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment