/b3log-solo/core/src/main/java/org/b3log/solo/repository/impl/TagRepositoryImpl.java

http://b3log-solo.googlecode.com/
Java | 115 lines | 58 code | 16 blank | 41 comment | 3 complexity | 153589bb0db681313396c6954957d751 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.ArrayList;
  18. import java.util.List;
  19. import java.util.logging.Logger;
  20. import org.b3log.solo.model.Tag;
  21. import org.b3log.solo.repository.TagRepository;
  22. import org.b3log.latke.Keys;
  23. import org.b3log.latke.repository.AbstractRepository;
  24. import org.b3log.latke.repository.FilterOperator;
  25. import org.b3log.latke.repository.Query;
  26. import org.b3log.latke.repository.RepositoryException;
  27. import org.b3log.latke.repository.SortDirection;
  28. import org.b3log.latke.util.CollectionUtils;
  29. import org.json.JSONArray;
  30. import org.json.JSONObject;
  31. /**
  32. * Tag repository.
  33. *
  34. * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
  35. * @version 1.0.1.1, Nov 29, 2011
  36. * @since 0.3.1
  37. */
  38. public final class TagRepositoryImpl extends AbstractRepository implements TagRepository {
  39. /**
  40. * Logger.
  41. */
  42. private static final Logger LOGGER = Logger.getLogger(TagRepositoryImpl.class.getName());
  43. /**
  44. * Singleton.
  45. */
  46. private static final TagRepositoryImpl SINGLETON = new TagRepositoryImpl(Tag.TAG);
  47. /**
  48. * Tag-Article relation repository.
  49. */
  50. private TagArticleRepositoryImpl tagArticleRepository = TagArticleRepositoryImpl.getInstance();
  51. @Override
  52. public JSONObject getByTitle(final String tagTitle) throws RepositoryException {
  53. final Query query = new Query().addFilter(Tag.TAG_TITLE, FilterOperator.EQUAL, tagTitle).
  54. setPageCount(1);
  55. final JSONObject result = get(query);
  56. final JSONArray array = result.optJSONArray(Keys.RESULTS);
  57. if (0 == array.length()) {
  58. return null;
  59. }
  60. return array.optJSONObject(0);
  61. }
  62. @Override
  63. public List<JSONObject> getMostUsedTags(final int num) throws RepositoryException {
  64. final Query query = new Query().addSort(Tag.TAG_PUBLISHED_REFERENCE_COUNT, SortDirection.DESCENDING).
  65. setCurrentPageNum(1).
  66. setPageSize(num).
  67. setPageCount(1);
  68. final JSONObject result = get(query);
  69. final JSONArray array = result.optJSONArray(Keys.RESULTS);
  70. return CollectionUtils.jsonArrayToList(array);
  71. }
  72. @Override
  73. public List<JSONObject> getByArticleId(final String articleId) throws RepositoryException {
  74. final List<JSONObject> ret = new ArrayList<JSONObject>();
  75. final List<JSONObject> tagArticleRelations = tagArticleRepository.getByArticleId(articleId);
  76. for (final JSONObject tagArticleRelation : tagArticleRelations) {
  77. final String tagId = tagArticleRelation.optString(Tag.TAG + "_" + Keys.OBJECT_ID);
  78. final JSONObject tag = get(tagId);
  79. ret.add(tag);
  80. }
  81. return ret;
  82. }
  83. /**
  84. * Gets the {@link TagRepositoryImpl} singleton.
  85. *
  86. * @return the singleton
  87. */
  88. public static TagRepositoryImpl getInstance() {
  89. return SINGLETON;
  90. }
  91. /**
  92. * Private constructor.
  93. *
  94. * @param name the specified name
  95. */
  96. private TagRepositoryImpl(final String name) {
  97. super(name);
  98. }
  99. }