PageRenderTime 13ms CodeModel.GetById 8ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 0ms

/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
Possible License(s): Apache-2.0, LGPL-2.1
 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 */
16package org.b3log.solo.repository.impl;
17
18import java.util.List;
19import java.util.logging.Logger;
20import org.b3log.solo.model.Article;
21import org.b3log.solo.model.Tag;
22import org.b3log.solo.repository.TagArticleRepository;
23import org.b3log.latke.Keys;
24import org.b3log.latke.repository.*;
25import org.b3log.latke.util.CollectionUtils;
26import org.json.JSONArray;
27import org.json.JSONObject;
28
29/**
30 * Tag-Article relation repository.
31 *
32 * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
33 * @version 1.0.0.9, Nov 9, 2011
34 * @since 0.3.1
35 */
36public final class TagArticleRepositoryImpl extends AbstractRepository implements TagArticleRepository {
37
38    /**
39     * Logger.
40     */
41    private static final Logger LOGGER = Logger.getLogger(TagArticleRepositoryImpl.class.getName());
42    /**
43     * Singleton.
44     */
45    private static final TagArticleRepositoryImpl SINGLETON = new TagArticleRepositoryImpl(Tag.TAG + "_" + Article.ARTICLE);
46
47    @Override
48    public List<JSONObject> getByArticleId(final String articleId) throws RepositoryException {
49        final Query query = new Query().setFilter(
50                new PropertyFilter(Article.ARTICLE + "_" + Keys.OBJECT_ID, FilterOperator.EQUAL, articleId)).
51                setPageCount(1);
52
53        final JSONObject result = get(query);
54        final JSONArray array = result.optJSONArray(Keys.RESULTS);
55
56        return CollectionUtils.jsonArrayToList(array);
57    }
58
59    @Override
60    public JSONObject getByTagId(final String tagId, final int currentPageNum, final int pageSize)
61            throws RepositoryException {
62        final Query query = new Query().setFilter(
63                new PropertyFilter(Tag.TAG + "_" + Keys.OBJECT_ID, FilterOperator.EQUAL, tagId)).
64                addSort(Article.ARTICLE + "_" + Keys.OBJECT_ID, SortDirection.DESCENDING).
65                setCurrentPageNum(currentPageNum).
66                setPageSize(pageSize).
67                setPageCount(1);
68
69        return get(query);
70    }
71
72    /**
73     * Gets the {@link TagArticleRepositoryImpl} singleton.
74     *
75     * @return the singleton
76     */
77    public static TagArticleRepositoryImpl getInstance() {
78        return SINGLETON;
79    }
80
81    /**
82     * Private constructor.
83     * 
84     * @param name the specified name
85     */
86    private TagArticleRepositoryImpl(final String name) {
87        super(name);
88    }
89}