/**
   * Gets links 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]
   *     },
   *     "links": [{
   *         "oId": "",
   *         "linkTitle": "",
   *         "linkAddress": "",
   *         ""linkDescription": ""
   *      }, ....]
   * }
   * </pre>
   *
   * @throws ServiceException service exception
   * @see Pagination
   */
  public JSONObject getLinks(final JSONObject requestJSONObject) throws ServiceException {
    final JSONObject ret = new JSONObject();

    try {
      final int currentPageNum = requestJSONObject.getInt(Pagination.PAGINATION_CURRENT_PAGE_NUM);
      final int pageSize = requestJSONObject.getInt(Pagination.PAGINATION_PAGE_SIZE);
      final int windowSize = requestJSONObject.getInt(Pagination.PAGINATION_WINDOW_SIZE);

      final Query query =
          new Query()
              .setCurrentPageNum(currentPageNum)
              .setPageSize(pageSize)
              .addSort(Link.LINK_ORDER, SortDirection.ASCENDING);
      final JSONObject result = linkRepository.get(query);
      final int pageCount =
          result.getJSONObject(Pagination.PAGINATION).getInt(Pagination.PAGINATION_PAGE_COUNT);

      final JSONObject pagination = new JSONObject();
      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 links = result.getJSONArray(Keys.RESULTS);

      ret.put(Pagination.PAGINATION, pagination);
      ret.put(Link.LINKS, links);

      return ret;
    } catch (final Exception e) {
      LOGGER.log(Level.ERROR, "Gets links failed", e);
      throw new ServiceException(e);
    }
  }
  /**
   * Gets a link by the specified link id.
   *
   * @param linkId the specified link id
   * @return for example,
   *     <pre>
   * {
   *     "link": {
   *         "oId": "",
   *         "linkTitle": "",
   *         "linkAddress": "",
   *         "linkDescription": ""
   *     }
   * }
   * </pre>
   *     , returns {@code null} if not found
   * @throws ServiceException service exception
   */
  public JSONObject getLink(final String linkId) throws ServiceException {
    final JSONObject ret = new JSONObject();

    try {
      final JSONObject link = linkRepository.get(linkId);

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

      ret.put(Link.LINK, link);

      return ret;
    } catch (final Exception e) {
      LOGGER.log(Level.ERROR, "Gets a link failed", e);

      throw new ServiceException(e);
    }
  }
  /**
   * Tests.
   *
   * @throws Exception exception
   */
  @Test
  public void test() throws Exception {
    final LinkRepository linkRepository = getLinkRepository();

    final int link1Order = 1, link2Order = 2, link3Order = 3;

    JSONObject link1 = new JSONObject();

    link1.put(Link.LINK_TITLE, "link title");
    link1.put(Link.LINK_DESCRIPTION, "link description");
    link1.put(Link.LINK_ADDRESS, "link address");
    link1.put(Link.LINK_ORDER, link1Order);

    Transaction transaction = linkRepository.beginTransaction();
    linkRepository.add(link1);
    transaction.commit();

    Assert.assertNull(linkRepository.getByAddress("test"));
    Assert.assertNotNull(linkRepository.getByAddress("link address"));

    Assert.assertNull(linkRepository.getByOrder(0));
    Assert.assertNotNull(linkRepository.getByOrder(link1Order));

    final JSONObject link2 = new JSONObject();

    link2.put(Link.LINK_TITLE, "link title");
    link2.put(Link.LINK_DESCRIPTION, "link description");
    link2.put(Link.LINK_ADDRESS, "link address");
    link2.put(Link.LINK_ORDER, link2Order);

    transaction = linkRepository.beginTransaction();
    final String link2Id = linkRepository.add(link2);
    transaction.commit();

    Assert.assertEquals(linkRepository.getMaxOrder(), link2Order);

    JSONObject link3 = new JSONObject();

    link3.put(Link.LINK_TITLE, "link title");
    link3.put(Link.LINK_DESCRIPTION, "link description");
    link3.put(Link.LINK_ADDRESS, "link address");
    link3.put(Link.LINK_ORDER, link3Order);
    transaction = linkRepository.beginTransaction();

    linkRepository.add(link3);

    transaction.commit();

    final int total = 3;
    Assert.assertEquals(linkRepository.count(), total);

    link1 = linkRepository.getUpper(link2Id);
    Assert.assertNotNull(link1);
    Assert.assertEquals(link1.getInt(Link.LINK_ORDER), link1Order);

    link3 = linkRepository.getUnder(link2Id);
    Assert.assertNotNull(link3);
    Assert.assertEquals(link3.getInt(Link.LINK_ORDER), link3Order);
  }