Commit 65f8a5e9 authored by Liang Ding's avatar Liang Ding

Fixed #34

升级 GAE SDK 到 1.7.0,Latke 中加入了对 OR 查询的支持。
parent bd44f5d3
/* /*
* Copyright (c) 2009, 2010, 2011, 2012, B3log Team * Copyright (c) 2009, 2010, 2011, 2012, B3log Team
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.b3log.solo.repository.impl; package org.b3log.solo.repository.impl;
import org.b3log.latke.Keys; import org.b3log.latke.Keys;
import org.b3log.latke.repository.AbstractRepository; import org.b3log.latke.repository.AbstractRepository;
import org.b3log.latke.repository.FilterOperator; import org.b3log.latke.repository.FilterOperator;
import org.b3log.latke.repository.Query; import org.b3log.latke.repository.Query;
import org.b3log.solo.model.Article; import org.b3log.solo.model.Article;
import org.b3log.latke.repository.RepositoryException; import org.b3log.latke.repository.RepositoryException;
import org.b3log.latke.repository.SortDirection; import org.b3log.latke.repository.SortDirection;
import org.b3log.solo.model.ArchiveDate; import org.b3log.solo.model.ArchiveDate;
import org.b3log.solo.repository.ArchiveDateArticleRepository; import org.b3log.solo.repository.ArchiveDateArticleRepository;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
/** /**
* Archive date-Article relation repository. * Archive date-Article relation repository.
* *
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a> * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.6, Nov 9, 2011 * @version 1.0.0.6, Nov 9, 2011
* @since 0.3.1 * @since 0.3.1
*/ */
public final class ArchiveDateArticleRepositoryImpl extends AbstractRepository implements ArchiveDateArticleRepository { public final class ArchiveDateArticleRepositoryImpl extends AbstractRepository implements ArchiveDateArticleRepository {
/** /**
* Singleton. * Singleton.
*/ */
private static final ArchiveDateArticleRepositoryImpl SINGLETON = private static final ArchiveDateArticleRepositoryImpl SINGLETON =
new ArchiveDateArticleRepositoryImpl(ArchiveDate.ARCHIVE_DATE + "_" + Article.ARTICLE); new ArchiveDateArticleRepositoryImpl(ArchiveDate.ARCHIVE_DATE + "_" + Article.ARTICLE);
@Override @Override
public JSONObject getByArchiveDateId(final String archiveDateId, final int currentPageNum, final int pageSize) public JSONObject getByArchiveDateId(final String archiveDateId, final int currentPageNum, final int pageSize)
throws RepositoryException { throws RepositoryException {
final Query query = new Query().addFilter(ArchiveDate.ARCHIVE_DATE + "_" + Keys.OBJECT_ID, final Query query = new Query().addFilter(ArchiveDate.ARCHIVE_DATE + "_" + Keys.OBJECT_ID,
FilterOperator.EQUAL, archiveDateId). FilterOperator.EQUAL, archiveDateId).
addSort(Article.ARTICLE + "_" + Keys.OBJECT_ID, addSort(Article.ARTICLE + "_" + Keys.OBJECT_ID,
SortDirection.DESCENDING). SortDirection.DESCENDING).
setCurrentPageNum(currentPageNum). setCurrentPageNum(currentPageNum).
setPageSize(pageSize). setPageSize(pageSize).
setPageCount(1); setPageCount(1);
return get(query); return get(query);
} }
@Override @Override
public JSONObject getByArticleId(final String articleId) throws RepositoryException { public JSONObject getByArticleId(final String articleId) throws RepositoryException {
final Query query = new Query(); final Query query = new Query();
query.addFilter(Article.ARTICLE + "_" + Keys.OBJECT_ID, FilterOperator.EQUAL, articleId); query.addFilter(Article.ARTICLE + "_" + Keys.OBJECT_ID, FilterOperator.EQUAL, articleId);
final JSONObject result = get(query); final JSONObject result = get(query);
final JSONArray array = result.optJSONArray(Keys.RESULTS); final JSONArray array = result.optJSONArray(Keys.RESULTS);
if (0 == array.length()) { if (0 == array.length()) {
return null; return null;
} }
return array.optJSONObject(0); return array.optJSONObject(0);
} }
/** /**
* Gets the {@link ArchiveDateArticleRepositoryImpl} singleton. * Gets the {@link ArchiveDateArticleRepositoryImpl} singleton.
* *
* @return the singleton * @return the singleton
*/ */
public static ArchiveDateArticleRepositoryImpl getInstance() { public static ArchiveDateArticleRepositoryImpl getInstance() {
return SINGLETON; return SINGLETON;
} }
/** /**
* Private constructor. * Private constructor.
* *
* @param name the specified name * @param name the specified name
*/ */
private ArchiveDateArticleRepositoryImpl(final String name) { private ArchiveDateArticleRepositoryImpl(final String name) {
super(name); super(name);
} }
} }
/* /*
* Copyright (c) 2009, 2010, 2011, 2012, B3log Team * Copyright (c) 2009, 2010, 2011, 2012, B3log Team
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.b3log.solo.repository.impl; package org.b3log.solo.repository.impl;
import java.text.ParseException; import java.text.ParseException;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.b3log.latke.Keys; import org.b3log.latke.Keys;
import org.b3log.latke.repository.AbstractRepository; import org.b3log.latke.repository.AbstractRepository;
import org.b3log.latke.repository.FilterOperator; import org.b3log.latke.repository.FilterOperator;
import org.b3log.latke.repository.Query; import org.b3log.latke.repository.Query;
import org.b3log.latke.repository.RepositoryException; import org.b3log.latke.repository.RepositoryException;
import org.b3log.latke.repository.SortDirection; import org.b3log.latke.repository.SortDirection;
import org.b3log.latke.util.CollectionUtils; import org.b3log.latke.util.CollectionUtils;
import org.b3log.solo.model.ArchiveDate; import org.b3log.solo.model.ArchiveDate;
import org.b3log.solo.repository.ArchiveDateRepository; import org.b3log.solo.repository.ArchiveDateRepository;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
/** /**
* Archive date repository. * Archive date repository.
* *
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a> * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.6, Dec 31, 2011 * @version 1.0.0.6, Dec 31, 2011
* @since 0.3.1 * @since 0.3.1
*/ */
public final class ArchiveDateRepositoryImpl extends AbstractRepository implements ArchiveDateRepository { public final class ArchiveDateRepositoryImpl extends AbstractRepository implements ArchiveDateRepository {
/** /**
* Logger. * Logger.
*/ */
private static final Logger LOGGER = Logger.getLogger(ArchiveDateRepositoryImpl.class.getName()); private static final Logger LOGGER = Logger.getLogger(ArchiveDateRepositoryImpl.class.getName());
/** /**
* Singleton. * Singleton.
*/ */
private static final ArchiveDateRepositoryImpl SINGLETON = new ArchiveDateRepositoryImpl(ArchiveDate.ARCHIVE_DATE); private static final ArchiveDateRepositoryImpl SINGLETON = new ArchiveDateRepositoryImpl(ArchiveDate.ARCHIVE_DATE);
@Override @Override
public JSONObject getByArchiveDate(final String archiveDate) throws RepositoryException { public JSONObject getByArchiveDate(final String archiveDate) throws RepositoryException {
long time = 0L; long time = 0L;
try { try {
time = ArchiveDate.DATE_FORMAT.parse(archiveDate).getTime(); time = ArchiveDate.DATE_FORMAT.parse(archiveDate).getTime();
} catch (final ParseException e) { } catch (final ParseException e) {
LOGGER.log(Level.SEVERE, "Can not parse archive date [" + archiveDate + "]", e); LOGGER.log(Level.SEVERE, "Can not parse archive date [" + archiveDate + "]", e);
throw new RepositoryException("Can not parse archive date [" + archiveDate + "]"); throw new RepositoryException("Can not parse archive date [" + archiveDate + "]");
} }
final Query query = new Query(); final Query query = new Query();
query.addFilter(ArchiveDate.ARCHIVE_TIME, FilterOperator.EQUAL, time).setPageCount(1); query.addFilter(ArchiveDate.ARCHIVE_TIME, FilterOperator.EQUAL, time).setPageCount(1);
final JSONObject result = get(query); final JSONObject result = get(query);
final JSONArray array = result.optJSONArray(Keys.RESULTS); final JSONArray array = result.optJSONArray(Keys.RESULTS);
if (0 == array.length()) { if (0 == array.length()) {
return null; return null;
} }
return array.optJSONObject(0); return array.optJSONObject(0);
} }
@Override @Override
public List<JSONObject> getArchiveDates() throws RepositoryException { public List<JSONObject> getArchiveDates() throws RepositoryException {
final org.b3log.latke.repository.Query query = final org.b3log.latke.repository.Query query =
new Query().addSort(ArchiveDate.ARCHIVE_TIME, SortDirection.DESCENDING).setPageCount(1); new Query().addSort(ArchiveDate.ARCHIVE_TIME, SortDirection.DESCENDING).setPageCount(1);
final JSONObject result = get(query); final JSONObject result = get(query);
final JSONArray archiveDates = result.optJSONArray(Keys.RESULTS); final JSONArray archiveDates = result.optJSONArray(Keys.RESULTS);
final List<JSONObject> ret = CollectionUtils.jsonArrayToList(archiveDates); final List<JSONObject> ret = CollectionUtils.jsonArrayToList(archiveDates);
removeForUnpublishedArticles(ret); removeForUnpublishedArticles(ret);
return ret; return ret;
} }
/** /**
* Removes archive dates of unpublished articles from the specified archive * Removes archive dates of unpublished articles from the specified archive
* dates. * dates.
* *
* @param archiveDates the specified archive dates * @param archiveDates the specified archive dates
* @throws RepositoryException repository exception * @throws RepositoryException repository exception
*/ */
private void removeForUnpublishedArticles(final List<JSONObject> archiveDates) throws RepositoryException { private void removeForUnpublishedArticles(final List<JSONObject> archiveDates) throws RepositoryException {
final Iterator<JSONObject> iterator = archiveDates.iterator(); final Iterator<JSONObject> iterator = archiveDates.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
final JSONObject archiveDate = iterator.next(); final JSONObject archiveDate = iterator.next();
if (0 == archiveDate.optInt(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT)) { if (0 == archiveDate.optInt(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT)) {
iterator.remove(); iterator.remove();
} }
} }
} }
/** /**
* Gets the {@link ArchiveDateRepositoryImpl} singleton. * Gets the {@link ArchiveDateRepositoryImpl} singleton.
* *
* @return the singleton * @return the singleton
*/ */
public static ArchiveDateRepositoryImpl getInstance() { public static ArchiveDateRepositoryImpl getInstance() {
return SINGLETON; return SINGLETON;
} }
/** /**
* Private constructor. * Private constructor.
* *
* @param name the specified name * @param name the specified name
*/ */
private ArchiveDateRepositoryImpl(final String name) { private ArchiveDateRepositoryImpl(final String name) {
super(name); super(name);
} }
} }
/* /*
* Copyright (c) 2009, 2010, 2011, 2012, B3log Team * Copyright (c) 2009, 2010, 2011, 2012, B3log Team
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.b3log.solo.repository.impl; package org.b3log.solo.repository.impl;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.b3log.latke.Keys; import org.b3log.latke.Keys;
import org.b3log.latke.repository.AbstractRepository; import org.b3log.latke.repository.AbstractRepository;
import org.b3log.latke.repository.FilterOperator; import org.b3log.latke.repository.FilterOperator;
import org.b3log.latke.repository.Query; import org.b3log.latke.repository.Query;
import org.b3log.latke.repository.RepositoryException; import org.b3log.latke.repository.RepositoryException;
import org.b3log.latke.repository.SortDirection; import org.b3log.latke.repository.SortDirection;
import org.b3log.solo.model.Link; import org.b3log.solo.model.Link;
import org.b3log.solo.repository.LinkRepository; import org.b3log.solo.repository.LinkRepository;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
/** /**
* Link repository. * Link repository.
* *
* @author <a href="mailto:DL88250@gmail.com">Liang Ding</a> * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
* @version 1.0.0.5, Nov 10, 2011 * @version 1.0.0.5, Nov 10, 2011
* @since 0.3.1 * @since 0.3.1
*/ */
public final class LinkRepositoryImpl extends AbstractRepository implements LinkRepository { public final class LinkRepositoryImpl extends AbstractRepository implements LinkRepository {
/** /**
* Logger. * Logger.
*/ */
private static final Logger LOGGER = Logger.getLogger(LinkRepositoryImpl.class.getName()); private static final Logger LOGGER = Logger.getLogger(LinkRepositoryImpl.class.getName());
/** /**
* Singleton. * Singleton.
*/ */
private static final LinkRepositoryImpl SINGLETON = new LinkRepositoryImpl(Link.LINK); private static final LinkRepositoryImpl SINGLETON = new LinkRepositoryImpl(Link.LINK);
@Override @Override
public JSONObject getByAddress(final String address) throws RepositoryException { public JSONObject getByAddress(final String address) throws RepositoryException {
final Query query = new Query().addFilter(Link.LINK_ADDRESS, FilterOperator.EQUAL, address). final Query query = new Query().addFilter(Link.LINK_ADDRESS, FilterOperator.EQUAL, address).
setPageCount(1); setPageCount(1);
final JSONObject result = get(query); final JSONObject result = get(query);
final JSONArray array = result.optJSONArray(Keys.RESULTS); final JSONArray array = result.optJSONArray(Keys.RESULTS);
if (0 == array.length()) { if (0 == array.length()) {
return null; return null;
} }
return array.optJSONObject(0); return array.optJSONObject(0);
} }
@Override @Override
public int getMaxOrder() throws RepositoryException { public int getMaxOrder() throws RepositoryException {
final Query query = new Query(); final Query query = new Query();
query.addSort(Link.LINK_ORDER, SortDirection.DESCENDING); query.addSort(Link.LINK_ORDER, SortDirection.DESCENDING);
final JSONObject result = get(query); final JSONObject result = get(query);
final JSONArray array = result.optJSONArray(Keys.RESULTS); final JSONArray array = result.optJSONArray(Keys.RESULTS);
if (0 == array.length()) { if (0 == array.length()) {
return -1; return -1;
} }
return array.optJSONObject(0).optInt(Link.LINK_ORDER); return array.optJSONObject(0).optInt(Link.LINK_ORDER);
} }
@Override @Override
public JSONObject getByOrder(final int order) throws RepositoryException { public JSONObject getByOrder(final int order) throws RepositoryException {
final Query query = new Query(); final Query query = new Query();
query.addFilter(Link.LINK_ORDER, FilterOperator.EQUAL, order); query.addFilter(Link.LINK_ORDER, FilterOperator.EQUAL, order);
final JSONObject result = get(query); final JSONObject result = get(query);
final JSONArray array = result.optJSONArray(Keys.RESULTS); final JSONArray array = result.optJSONArray(Keys.RESULTS);
if (0 == array.length()) { if (0 == array.length()) {
return null; return null;
} }
return array.optJSONObject(0); return array.optJSONObject(0);
} }
@Override @Override
public JSONObject getUpper(final String id) throws RepositoryException { public JSONObject getUpper(final String id) throws RepositoryException {
final JSONObject link = get(id); final JSONObject link = get(id);
if (null == link) { if (null == link) {
return null; return null;
} }
final Query query = new Query(); final Query query = new Query();
query.addFilter(Link.LINK_ORDER, FilterOperator.LESS_THAN, link.optInt(Link.LINK_ORDER)). query.addFilter(Link.LINK_ORDER, FilterOperator.LESS_THAN, link.optInt(Link.LINK_ORDER)).
addSort(Link.LINK_ORDER, SortDirection.DESCENDING); addSort(Link.LINK_ORDER, SortDirection.DESCENDING);
query.setCurrentPageNum(1); query.setCurrentPageNum(1);
query.setPageSize(1); query.setPageSize(1);
final JSONObject result = get(query); final JSONObject result = get(query);
final JSONArray array = result.optJSONArray(Keys.RESULTS); final JSONArray array = result.optJSONArray(Keys.RESULTS);
if (1 != array.length()) { if (1 != array.length()) {
return null; return null;
} }
return array.optJSONObject(0); return array.optJSONObject(0);
} }
@Override @Override
public JSONObject getUnder(final String id) throws RepositoryException { public JSONObject getUnder(final String id) throws RepositoryException {
final JSONObject link = get(id); final JSONObject link = get(id);
if (null == link) { if (null == link) {
return null; return null;
} }
final Query query = new Query(); final Query query = new Query();
query.addFilter(Link.LINK_ORDER, FilterOperator.GREATER_THAN, link.optInt(Link.LINK_ORDER)). query.addFilter(Link.LINK_ORDER, FilterOperator.GREATER_THAN, link.optInt(Link.LINK_ORDER)).
addSort(Link.LINK_ORDER, SortDirection.ASCENDING); addSort(Link.LINK_ORDER, SortDirection.ASCENDING);
query.setCurrentPageNum(1); query.setCurrentPageNum(1);
query.setPageSize(1); query.setPageSize(1);
final JSONObject result = get(query); final JSONObject result = get(query);
final JSONArray array = result.optJSONArray(Keys.RESULTS); final JSONArray array = result.optJSONArray(Keys.RESULTS);
if (1 != array.length()) { if (1 != array.length()) {
return null; return null;
} }
return array.optJSONObject(0); return array.optJSONObject(0);
} }
/** /**
* Gets the {@link LinkRepositoryImpl} singleton. * Gets the {@link LinkRepositoryImpl} singleton.
* *
* @return the singleton * @return the singleton
*/ */
public static LinkRepositoryImpl getInstance() { public static LinkRepositoryImpl getInstance() {
return SINGLETON; return SINGLETON;
} }
/** /**
* Private constructor. * Private constructor.
* *
* @param name the specified name * @param name the specified name
*/ */
private LinkRepositoryImpl(final String name) { private LinkRepositoryImpl(final String name) {
super(name); super(name);
} }
} }
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment