コード例 #1
0
 /**
  * Gets a user by the specified email.
  *
  * @param email the specified email
  * @return user, returns {@code null} if not found
  * @throws ServiceException service exception
  */
 public JSONObject getUserByEmail(final String email) throws ServiceException {
   try {
     return userRepository.getByEmail(email);
   } catch (final RepositoryException e) {
     LOGGER.log(Level.ERROR, "Gets user by email[" + email + "] failed", e);
     throw new ServiceException(e);
   }
 }
コード例 #2
0
 /**
  * Gets the administrator.
  *
  * @return administrator, returns {@code null} if not found
  * @throws ServiceException service exception
  */
 public JSONObject getAdmin() throws ServiceException {
   try {
     return userRepository.getAdmin();
   } catch (final RepositoryException e) {
     LOGGER.log(Level.ERROR, "Gets admin failed", e);
     throw new ServiceException(e);
   }
 }
コード例 #3
0
ファイル: Users.java プロジェクト: jonesgithub/b3log-solo
  /**
   * Determines whether the specified email is a user's email of this Solo application.
   *
   * @param email the specified email
   * @return {@code true} if it is, {@code false} otherwise
   */
  public boolean isSoloUser(final String email) {
    try {
      final Query query = new Query().setPageCount(1);
      final JSONObject result = userRepository.get(query);
      final JSONArray users = result.getJSONArray(Keys.RESULTS);

      return existEmail(email, users);
    } catch (final Exception e) {
      LOGGER.log(Level.SEVERE, e.getMessage(), e);
      return false;
    }
  }
コード例 #4
0
  /**
   * Determines whether if exists multiple users in current Solo.
   *
   * @return {@code true} if exists, {@code false} otherwise
   * @throws ServiceException service exception
   */
  public boolean hasMultipleUsers() throws ServiceException {
    final Query query = new Query().setPageCount(1);

    try {
      final JSONArray users = userRepository.get(query).getJSONArray(Keys.RESULTS);

      return 1 != users.length();
    } catch (final RepositoryException e) {
      LOGGER.log(Level.ERROR, "Determines multiple users failed", e);

      throw new ServiceException(e);
    } catch (final JSONException e) {
      LOGGER.log(Level.ERROR, "Determines multiple users failed", e);

      throw new ServiceException(e);
    }
  }
コード例 #5
0
  /**
   * Gets the current user.
   *
   * @param request the specified request
   * @return the current user, {@code null} if not found
   */
  public JSONObject getCurrentUser(final HttpServletRequest request) {
    final GeneralUser currentUser = userService.getCurrentUser(request);

    if (null == currentUser) {
      return null;
    }

    final String email = currentUser.getEmail();

    try {
      return userRepository.getByEmail(email);
    } catch (final RepositoryException e) {
      LOGGER.log(Level.ERROR, "Gets current user by request failed, returns null", e);

      return null;
    }
  }
コード例 #6
0
  /**
   * Gets a user by the specified user id.
   *
   * @param userId the specified user id
   * @return for example,
   *     <pre>
   * {
   *     "user": {
   *         "oId": "",
   *         "userName": "",
   *         "userEmail": "",
   *         "userPassword": ""
   *     }
   * }
   * </pre>
   *     , returns {@code null} if not found
   * @throws ServiceException service exception
   */
  public JSONObject getUser(final String userId) throws ServiceException {
    final JSONObject ret = new JSONObject();

    JSONObject user = null;

    try {
      user = userRepository.get(userId);
    } catch (final RepositoryException e) {
      LOGGER.log(Level.ERROR, "Gets a user failed", e);
      throw new ServiceException(e);
    }

    if (null == user) {
      return null;
    }

    ret.put(User.USER, user);

    return ret;
  }
コード例 #7
0
  /**
   * Gets users by the specified request json object.
   *
   * @param requestJSONObject the specified request json object, for example,
   *     <pre>
   * {
   *     "paginationCurrentPageNum": 1,
   *     "paginationPageSize": 20,
   *     "paginationWindowSize": 10,
   * }, see {@link Pagination} for more details
   * </pre>
   *
   * @return for example,
   *     <pre>
   * {
   *     "pagination": {
   *         "paginationPageCount": 100,
   *         "paginationPageNums": [1, 2, 3, 4, 5]
   *     },
   *     "users": [{
   *         "oId": "",
   *         "userName": "",
   *         "userEmail": "",
   *         "userPassword": "",
   *         "roleName": ""
   *      }, ....]
   * }
   * </pre>
   *
   * @throws ServiceException service exception
   * @see Pagination
   */
  public JSONObject getUsers(final JSONObject requestJSONObject) throws ServiceException {
    final JSONObject ret = new JSONObject();

    final int currentPageNum = requestJSONObject.optInt(Pagination.PAGINATION_CURRENT_PAGE_NUM);
    final int pageSize = requestJSONObject.optInt(Pagination.PAGINATION_PAGE_SIZE);
    final int windowSize = requestJSONObject.optInt(Pagination.PAGINATION_WINDOW_SIZE);
    final Query query = new Query().setCurrentPageNum(currentPageNum).setPageSize(pageSize);

    JSONObject result = null;

    try {
      result = userRepository.get(query);
    } catch (final RepositoryException e) {
      LOGGER.log(Level.ERROR, "Gets users failed", e);

      throw new ServiceException(e);
    }

    final int pageCount =
        result.optJSONObject(Pagination.PAGINATION).optInt(Pagination.PAGINATION_PAGE_COUNT);

    final JSONObject pagination = new JSONObject();

    ret.put(Pagination.PAGINATION, pagination);
    final List<Integer> pageNums =
        Paginator.paginate(currentPageNum, pageSize, pageCount, windowSize);

    pagination.put(Pagination.PAGINATION_PAGE_COUNT, pageCount);
    pagination.put(Pagination.PAGINATION_PAGE_NUMS, pageNums);

    final JSONArray users = result.optJSONArray(Keys.RESULTS);

    ret.put(User.USERS, users);

    return ret;
  }