Search in sources :

Example 6 with Chapter

use of com.pratilipi.data.type.PratilipiContentDoc.Chapter in project pratilipi by Pratilipi.

the class PratilipiDocUtil method saveContentPageBatch.

public static void saveContentPageBatch(Long pratilipiId, JsonObject chapters) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    if (!PratilipiDataUtil.hasAccessToUpdatePratilipiContent(pratilipi))
        throw new InsufficientAccessException();
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    // Doc
    PratilipiContentDoc pcDoc = docAccessor.getPratilipiContentDoc(pratilipiId);
    if (pcDoc == null)
        pcDoc = docAccessor.newPratilipiContentDoc();
    for (Map.Entry<String, JsonElement> chapterEntry : chapters.entrySet()) {
        Integer chapterNo = Integer.parseInt(chapterEntry.getKey());
        // chapterTitle
        String chapterTitle = null;
        if (chapterEntry.getValue().getAsJsonObject().has("chapterTitle"))
            chapterTitle = chapterEntry.getValue().getAsJsonObject().get("chapterTitle").getAsString();
        // content
        String content = null;
        if (chapterEntry.getValue().getAsJsonObject().has("content"))
            content = chapterEntry.getValue().getAsJsonObject().get("content").getAsString();
        // pageNo
        if (!chapterEntry.getValue().getAsJsonObject().has("pageNo"))
            throw new InvalidArgumentException(GenericRequest.ERR_PRATILIPI_PAGE_NO_REQUIRED);
        Integer pageNo = chapterEntry.getValue().getAsJsonObject().get("pageNo").getAsInt();
        // Chapter
        Chapter chapter = pcDoc.getChapter(chapterNo);
        if (chapter == null)
            chapter = pcDoc.addChapter(chapterNo, chapterTitle);
        else if (chapterTitle != null)
            chapter.setTitle(chapterTitle);
        // Page
        PratilipiContentDoc.Page page = chapter.getPage(pageNo);
        if (page == null)
            page = chapter.addPage(pageNo);
        _setPage(pratilipiId, page, content);
    }
    // Save
    docAccessor.save(pratilipiId, pcDoc);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) Chapter(com.pratilipi.data.type.PratilipiContentDoc.Chapter) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) JsonElement(com.google.gson.JsonElement) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) PratilipiContentDoc(com.pratilipi.data.type.PratilipiContentDoc) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with Chapter

use of com.pratilipi.data.type.PratilipiContentDoc.Chapter in project pratilipi by Pratilipi.

the class PratilipiDocUtil method updateMeta.

public static boolean updateMeta(Long pratilipiId) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    PratilipiContentDoc pcDoc = docAccessor.getPratilipiContentDoc(pratilipiId);
    PratilipiMetaDoc pmDoc = docAccessor.getPratilipiMetaDoc(pratilipiId);
    if (pmDoc == null)
        pmDoc = docAccessor.newPratilipiMetaDoc();
    int wordCount = 0;
    int imageCount = 0;
    int chapterCount = 0;
    Map<String, Integer> wordCounts = new HashMap<>();
    for (PratilipiContentDoc.Chapter chapter : pcDoc.getChapterList()) {
        String content = chapter.getTitle() == null ? "" : chapter.getTitle();
        for (PratilipiContentDoc.Page page : chapter.getPageList()) {
            for (PratilipiContentDoc.Pagelet pagelet : page.getPageletList()) {
                if (pagelet.getType() == PratilipiContentDoc.PageletType.IMAGE)
                    imageCount++;
                else
                    content += " " + pagelet.getDataAsString();
            }
        }
        content = content.trim();
        if (content.length() > 1) {
            String[] words = content.replaceAll(nonKeywordsPattern, " ").split("[\\s]+");
            for (String word : words) {
                Integer count = wordCounts.get(word);
                count = count == null ? 1 : count++;
                wordCounts.put(word, count);
            }
            wordCount += words.length;
        }
        chapterCount++;
    }
    boolean isChanged = wordCounts.size() != pmDoc.getWordCounts().size();
    if (!isChanged) {
        for (Entry<String, Integer> entry : pmDoc.getWordCounts().entrySet()) {
            if (!entry.getValue().equals(wordCounts.get(entry.getKey()))) {
                isChanged = true;
                break;
            }
        }
    }
    if (isChanged) {
        // Update & Save Pratilipi Meta Doc
        pmDoc.setWordCounts(wordCounts);
        docAccessor.save(pratilipiId, pmDoc);
        // Update counts in Pratilipi Entity
        AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE, pratilipi);
        pratilipi.setWordCount(wordCount);
        pratilipi.setImageCount(imageCount);
        pratilipi.setChapterCount(chapterCount);
        pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
    }
    return isChanged;
}
Also used : HashMap(java.util.HashMap) DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) AuditLog(com.pratilipi.data.type.AuditLog) PratilipiMetaDoc(com.pratilipi.data.type.PratilipiMetaDoc) Chapter(com.pratilipi.data.type.PratilipiContentDoc.Chapter) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) PratilipiContentDoc(com.pratilipi.data.type.PratilipiContentDoc)

Aggregations

DataAccessor (com.pratilipi.data.DataAccessor)7 Pratilipi (com.pratilipi.data.type.Pratilipi)7 PratilipiContentDoc (com.pratilipi.data.type.PratilipiContentDoc)7 Chapter (com.pratilipi.data.type.PratilipiContentDoc.Chapter)7 DocAccessor (com.pratilipi.data.DocAccessor)6 UserPratilipi (com.pratilipi.data.type.UserPratilipi)6 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)4 HashMap (java.util.HashMap)3 JsonObject (com.google.gson.JsonObject)2 Gson (com.google.gson.Gson)1 JsonElement (com.google.gson.JsonElement)1 PratilipiContentIndexApi (com.pratilipi.api.impl.pratilipi.PratilipiContentIndexApi)1 PratilipiContentV1Api (com.pratilipi.api.impl.pratilipi.PratilipiContentV1Api)1 PratilipiContentV2Api (com.pratilipi.api.impl.pratilipi.PratilipiContentV2Api)1 PratilipiContentV3Api (com.pratilipi.api.impl.pratilipi.PratilipiContentV3Api)1 PratilipiV2Api (com.pratilipi.api.impl.pratilipi.PratilipiV2Api)1 UserPratilipiApi (com.pratilipi.api.impl.userpratilipi.UserPratilipiApi)1 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)1 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)1 BlobAccessor (com.pratilipi.data.BlobAccessor)1