Search in sources :

Example 61 with Query

use of org.b3log.latke.repository.Query in project symphony by b3log.

the class ShortLinkQueryService method linkArticle.

/**
 * Processes article short link (article id).
 *
 * @param content the specified content
 * @return processed content
 */
public String linkArticle(final String content) {
    Stopwatchs.start("Link article");
    StringBuffer contentBuilder = new StringBuffer();
    try {
        Matcher matcher = ARTICLE_PATTERN_FULL.matcher(content);
        final String[] codeBlocks = StringUtils.substringsBetween(content, "```", "```");
        String codes = "";
        if (null != codeBlocks) {
            codes = String.join("", codeBlocks);
        }
        try {
            while (matcher.find()) {
                final String url = StringUtils.trim(matcher.group());
                if (StringUtils.containsIgnoreCase(codes, url)) {
                    continue;
                }
                String linkId;
                if (StringUtils.contains(url, "?")) {
                    linkId = StringUtils.substringBetween(matcher.group(), "/article/", "?");
                } else {
                    linkId = StringUtils.substringAfter(matcher.group(), "/article/");
                }
                final Query query = new Query().addProjection(Article.ARTICLE_TITLE, String.class).setFilter(new PropertyFilter(Keys.OBJECT_ID, FilterOperator.EQUAL, linkId));
                final JSONArray results = articleRepository.get(query).optJSONArray(Keys.RESULTS);
                if (0 == results.length()) {
                    continue;
                }
                final JSONObject linkArticle = results.optJSONObject(0);
                final String linkTitle = linkArticle.optString(Article.ARTICLE_TITLE);
                final String link = " [" + linkTitle + "](" + Latkes.getServePath() + "/article/" + linkId + ") ";
                matcher.appendReplacement(contentBuilder, link);
            }
            matcher.appendTail(contentBuilder);
        } catch (final RepositoryException e) {
            LOGGER.log(Level.ERROR, "Generates article link error", e);
        }
        matcher = ARTICLE_PATTERN_SIMPLE.matcher(contentBuilder.toString());
        contentBuilder = new StringBuffer();
        try {
            while (matcher.find()) {
                final String linkId = StringUtils.substringBetween(matcher.group(), "[", "]");
                final Query query = new Query().addProjection(Article.ARTICLE_TITLE, String.class).setFilter(new PropertyFilter(Keys.OBJECT_ID, FilterOperator.EQUAL, linkId));
                final JSONArray results = articleRepository.get(query).optJSONArray(Keys.RESULTS);
                if (0 == results.length()) {
                    continue;
                }
                final JSONObject linkArticle = results.optJSONObject(0);
                final String linkTitle = linkArticle.optString(Article.ARTICLE_TITLE);
                final String link = " [" + linkTitle + "](" + Latkes.getServePath() + "/article/" + linkId + ") ";
                matcher.appendReplacement(contentBuilder, link);
            }
            matcher.appendTail(contentBuilder);
        } catch (final RepositoryException e) {
            LOGGER.log(Level.ERROR, "Generates article link error", e);
        }
        return contentBuilder.toString();
    } finally {
        Stopwatchs.end();
    }
}
Also used : Query(org.b3log.latke.repository.Query) JSONObject(org.json.JSONObject) Matcher(java.util.regex.Matcher) JSONArray(org.json.JSONArray) PropertyFilter(org.b3log.latke.repository.PropertyFilter) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 62 with Query

use of org.b3log.latke.repository.Query in project symphony by b3log.

the class TagMgmtService method removeUnusedTags.

/**
 * Removes unused tags.
 */
@Transactional
public synchronized void removeUnusedTags() {
    LOGGER.info("Starting remove unused tags....");
    int removedCnt = 0;
    try {
        final JSONArray tags = tagRepository.get(new Query()).optJSONArray(Keys.RESULTS);
        for (int i = 0; i < tags.length(); i++) {
            final JSONObject tag = tags.optJSONObject(i);
            final String tagId = tag.optString(Keys.OBJECT_ID);
            if (// article ref cnt
            0 == tag.optInt(Tag.TAG_REFERENCE_CNT) && 0 == domainTagRepository.getByTagId(tagId, 1, Integer.MAX_VALUE).optJSONArray(Keys.RESULTS).length() && // tagUserLinkRefCnt
            0 == tagUserLinkRepository.countTagLink(tagId)) {
                final JSONArray userTagRels = userTagRepository.getByTagId(tagId, 1, Integer.MAX_VALUE).optJSONArray(Keys.RESULTS);
                if (1 == userTagRels.length() && Tag.TAG_TYPE_C_CREATOR == userTagRels.optJSONObject(0).optInt(Common.TYPE)) {
                    // Just the tag's creator but not use it now
                    tagRepository.remove(tagId);
                    removedCnt++;
                    LOGGER.info("Removed a unused tag [title=" + tag.optString(Tag.TAG_TITLE) + "]");
                }
            }
        }
        final JSONObject tagCntOption = optionRepository.get(Option.ID_C_STATISTIC_TAG_COUNT);
        final int tagCnt = tagCntOption.optInt(Option.OPTION_VALUE);
        tagCntOption.put(Option.OPTION_VALUE, tagCnt - removedCnt);
        optionRepository.update(Option.ID_C_STATISTIC_TAG_COUNT, tagCntOption);
        LOGGER.info("Removed [" + removedCnt + "] unused tags");
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Removes unused tags failed", e);
    }
}
Also used : Query(org.b3log.latke.repository.Query) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ServiceException(org.b3log.latke.service.ServiceException) RepositoryException(org.b3log.latke.repository.RepositoryException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Transactional(org.b3log.latke.repository.annotation.Transactional)

Example 63 with Query

use of org.b3log.latke.repository.Query in project symphony by b3log.

the class PostExportService method exportPosts.

/**
 * Exports all posts of a user's specified with the given user id.
 *
 * @param userId the given user id
 * @return download URL, returns {@code "-1"} if in sufficient balance, returns {@code null} if other exceptions
 */
public String exportPosts(final String userId) {
    final int pointDataExport = Symphonys.getInt("pointDataExport");
    try {
        final JSONObject user = userRepository.get(userId);
        final int balance = user.optInt(UserExt.USER_POINT);
        if (balance - pointDataExport < 0) {
            return "-1";
        }
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Checks user failed", e);
        return null;
    }
    final JSONArray posts = new JSONArray();
    Query query = new Query().setFilter(new PropertyFilter(Article.ARTICLE_AUTHOR_ID, FilterOperator.EQUAL, userId)).addProjection(Keys.OBJECT_ID, String.class).addProjection(Article.ARTICLE_TITLE, String.class).addProjection(Article.ARTICLE_TAGS, String.class).addProjection(Article.ARTICLE_CONTENT, String.class).addProjection(Article.ARTICLE_CREATE_TIME, Long.class);
    try {
        final JSONArray articles = articleRepository.get(query).optJSONArray(Keys.RESULTS);
        for (int i = 0; i < articles.length(); i++) {
            final JSONObject article = articles.getJSONObject(i);
            final JSONObject post = new JSONObject();
            post.put("id", article.optString(Keys.OBJECT_ID));
            final JSONObject content = new JSONObject();
            content.put("title", article.optString(Article.ARTICLE_TITLE));
            content.put("tags", article.optString(Article.ARTICLE_TAGS));
            content.put("body", article.optString(Article.ARTICLE_CONTENT));
            post.put("content", content.toString());
            post.put("created", Article.ARTICLE_CREATE_TIME);
            post.put("type", "article");
            posts.put(post);
        }
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Export articles failed", e);
        return null;
    }
    query = new Query().setFilter(new PropertyFilter(Comment.COMMENT_AUTHOR_ID, FilterOperator.EQUAL, userId)).addProjection(Keys.OBJECT_ID, String.class).addProjection(Comment.COMMENT_CONTENT, String.class).addProjection(Comment.COMMENT_CREATE_TIME, Long.class);
    try {
        final JSONArray comments = commentRepository.get(query).optJSONArray(Keys.RESULTS);
        for (int i = 0; i < comments.length(); i++) {
            final JSONObject comment = comments.getJSONObject(i);
            final JSONObject post = new JSONObject();
            post.put("id", comment.optString(Keys.OBJECT_ID));
            final JSONObject content = new JSONObject();
            content.put("title", "");
            content.put("tags", "");
            content.put("body", comment.optString(Comment.COMMENT_CONTENT));
            post.put("content", content.toString());
            post.put("created", Comment.COMMENT_CREATE_TIME);
            post.put("type", "comment");
            posts.put(post);
        }
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Export comments failed", e);
        return null;
    }
    LOGGER.info("Exporting posts [size=" + posts.length() + "]");
    final boolean succ = null != pointtransferMgmtService.transfer(userId, Pointtransfer.ID_C_SYS, Pointtransfer.TRANSFER_TYPE_C_DATA_EXPORT, Pointtransfer.TRANSFER_SUM_C_DATA_EXPORT, String.valueOf(posts.length()), System.currentTimeMillis());
    if (!succ) {
        return null;
    }
    final String uuid = UUID.randomUUID().toString().replaceAll("-", "");
    String fileKey = "export/" + userId + "/" + uuid + ".zip";
    final String tmpDir = System.getProperty("java.io.tmpdir");
    String localFilePath = tmpDir + "/" + uuid + ".json";
    LOGGER.info(localFilePath);
    final File localFile = new File(localFilePath);
    try {
        final byte[] data = posts.toString(2).getBytes("UTF-8");
        OutputStream output = new FileOutputStream(localFile);
        IOUtils.write(data, output);
        IOUtils.closeQuietly(output);
        final File zipFile = ZipUtil.zip(localFile);
        final FileInputStream inputStream = new FileInputStream(zipFile);
        final byte[] zipData = IOUtils.toByteArray(inputStream);
        if (Symphonys.getBoolean("qiniu.enabled")) {
            final Auth auth = Auth.create(Symphonys.get("qiniu.accessKey"), Symphonys.get("qiniu.secretKey"));
            final UploadManager uploadManager = new UploadManager(new Configuration());
            uploadManager.put(zipData, fileKey, auth.uploadToken(Symphonys.get("qiniu.bucket")), null, "application/zip", false);
            return Symphonys.get("qiniu.domain") + "/" + fileKey;
        } else {
            final String filePath = Symphonys.get("upload.dir") + fileKey;
            FileUtils.copyFile(zipFile, new File(filePath));
            return Latkes.getServePath() + "/upload/" + fileKey;
        }
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Uploading exprted data failed", e);
        return null;
    }
}
Also used : Query(org.b3log.latke.repository.Query) Configuration(com.qiniu.storage.Configuration) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) JSONArray(org.json.JSONArray) RepositoryException(org.b3log.latke.repository.RepositoryException) RepositoryException(org.b3log.latke.repository.RepositoryException) FileInputStream(java.io.FileInputStream) JSONObject(org.json.JSONObject) FileOutputStream(java.io.FileOutputStream) Auth(com.qiniu.util.Auth) PropertyFilter(org.b3log.latke.repository.PropertyFilter) File(java.io.File) UploadManager(com.qiniu.storage.UploadManager)

Example 64 with Query

use of org.b3log.latke.repository.Query in project symphony by b3log.

the class LinkRepository method getLink.

/**
 * Gets a link with the specified address.
 *
 * @param addr the specified address
 * @return a link, returns {@code null} if not found
 */
public JSONObject getLink(final String addr) {
    final Query query = new Query();
    query.setFilter(new PropertyFilter(Link.LINK_ADDR, FilterOperator.EQUAL, addr)).setPageCount(1).setPageSize(1).setCurrentPageNum(1);
    try {
        final JSONObject result = get(query);
        final JSONArray links = result.optJSONArray(Keys.RESULTS);
        if (0 == links.length()) {
            return null;
        }
        return links.optJSONObject(0);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Gets link by address [" + addr + "]", e);
        return null;
    }
}
Also used : Query(org.b3log.latke.repository.Query) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) PropertyFilter(org.b3log.latke.repository.PropertyFilter)

Aggregations

Query (org.b3log.latke.repository.Query)64 JSONObject (org.json.JSONObject)63 JSONArray (org.json.JSONArray)55 PropertyFilter (org.b3log.latke.repository.PropertyFilter)35 RepositoryException (org.b3log.latke.repository.RepositoryException)15 ServiceException (org.b3log.latke.service.ServiceException)10 Test (org.testng.annotations.Test)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 PrintWriter (java.io.PrintWriter)6 StringWriter (java.io.StringWriter)6 Date (java.util.Date)6 ServletContext (javax.servlet.ServletContext)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 JSONException (org.json.JSONException)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 URL (org.b3log.solo.model.sitemap.URL)4 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)3 Matcher (java.util.regex.Matcher)2 CompositeFilter (org.b3log.latke.repository.CompositeFilter)2