/core/src/main/java/org/b3log/solo/repository/impl/TagArticleRepositoryImpl.java

http://github.com/b3log/b3log-solo · Java · 89 lines · 41 code · 10 blank · 38 comment · 0 complexity · b7d24892312d638dcbb7abd0453eee29 MD5 · raw file

  1. /*
  2. * Copyright (c) 2009, 2010, 2011, 2012, B3log Team
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.b3log.solo.repository.impl;
  17. import java.util.List;
  18. import java.util.logging.Logger;
  19. import org.b3log.solo.model.Article;
  20. import org.b3log.solo.model.Tag;
  21. import org.b3log.solo.repository.TagArticleRepository;
  22. import org.b3log.latke.Keys;
  23. import org.b3log.latke.repository.*;
  24. import org.b3log.latke.util.CollectionUtils;
  25. import org.json.JSONArray;
  26. import org.json.JSONObject;
  27. /**
  28. * Tag-Article relation repository.
  29. *
  30. * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
  31. * @version 1.0.0.9, Nov 9, 2011
  32. * @since 0.3.1
  33. */
  34. public final class TagArticleRepositoryImpl extends AbstractRepository implements TagArticleRepository {
  35. /**
  36. * Logger.
  37. */
  38. private static final Logger LOGGER = Logger.getLogger(TagArticleRepositoryImpl.class.getName());
  39. /**
  40. * Singleton.
  41. */
  42. private static final TagArticleRepositoryImpl SINGLETON = new TagArticleRepositoryImpl(Tag.TAG + "_" + Article.ARTICLE);
  43. @Override
  44. public List<JSONObject> getByArticleId(final String articleId) throws RepositoryException {
  45. final Query query = new Query().setFilter(
  46. new PropertyFilter(Article.ARTICLE + "_" + Keys.OBJECT_ID, FilterOperator.EQUAL, articleId)).
  47. setPageCount(1);
  48. final JSONObject result = get(query);
  49. final JSONArray array = result.optJSONArray(Keys.RESULTS);
  50. return CollectionUtils.jsonArrayToList(array);
  51. }
  52. @Override
  53. public JSONObject getByTagId(final String tagId, final int currentPageNum, final int pageSize)
  54. throws RepositoryException {
  55. final Query query = new Query().setFilter(
  56. new PropertyFilter(Tag.TAG + "_" + Keys.OBJECT_ID, FilterOperator.EQUAL, tagId)).
  57. addSort(Article.ARTICLE + "_" + Keys.OBJECT_ID, SortDirection.DESCENDING).
  58. setCurrentPageNum(currentPageNum).
  59. setPageSize(pageSize).
  60. setPageCount(1);
  61. return get(query);
  62. }
  63. /**
  64. * Gets the {@link TagArticleRepositoryImpl} singleton.
  65. *
  66. * @return the singleton
  67. */
  68. public static TagArticleRepositoryImpl getInstance() {
  69. return SINGLETON;
  70. }
  71. /**
  72. * Private constructor.
  73. *
  74. * @param name the specified name
  75. */
  76. private TagArticleRepositoryImpl(final String name) {
  77. super(name);
  78. }
  79. }