Search in sources :

Example 26 with DocAccessor

use of com.pratilipi.data.DocAccessor 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)

Example 27 with DocAccessor

use of com.pratilipi.data.DocAccessor in project pratilipi by Pratilipi.

the class BatchProcessDataUtil method _execStateGetUserIdsByAuthorFilter.

private static void _execStateGetUserIdsByAuthorFilter(BatchProcess batchProcess) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    BatchProcessDoc processDoc = docAccessor.getBatchProcessDoc(batchProcess.getId());
    AuthorFilter authorFilter = processDoc.getData(BatchProcessState.GET_USER_IDS_BY_AUTHOR_FILTER.getInputName(), AuthorFilter.class);
    Set<Long> userIdSet = new HashSet<>();
    List<Author> tempAuthorList = null;
    String cursor = null;
    int resultCount = 200;
    do {
        DataListCursorTuple<Author> authorListCusorTuple = dataAccessor.getAuthorList(authorFilter, cursor, resultCount);
        tempAuthorList = authorListCusorTuple.getDataList();
        for (Author author : tempAuthorList) if (author.getUserId() != null)
            userIdSet.add(author.getUserId());
        cursor = authorListCusorTuple.getCursor();
    } while (tempAuthorList.size() >= resultCount);
    processDoc.setData(BatchProcessState.GET_USER_IDS_BY_AUTHOR_FILTER.getOutputName(), userIdSet);
    // Saving Doc
    docAccessor.save(batchProcess.getId(), processDoc);
    batchProcess.setStateCompleted(BatchProcessState.GET_USER_IDS_BY_AUTHOR_FILTER);
}
Also used : BatchProcessDoc(com.pratilipi.data.type.BatchProcessDoc) DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) AuthorFilter(com.pratilipi.common.util.AuthorFilter) Author(com.pratilipi.data.type.Author) HashSet(java.util.HashSet)

Example 28 with DocAccessor

use of com.pratilipi.data.DocAccessor in project pratilipi by Pratilipi.

the class InitV1Api method get.

@Get
public Response get(GetRequest request) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    List<Response.Section> sectionList = new LinkedList<>();
    List<Long> pratilipiIdMasterList = new LinkedList<>();
    for (String listName : dataAccessor.getHomeSectionList(request.language)) {
        String title = dataAccessor.getPratilipiListTitle(listName, request.language);
        if (title == null)
            continue;
        if (title.indexOf('|') != -1)
            title = title.substring(0, title.indexOf('|')).trim();
        PratilipiFilter pratilipiFilter = new PratilipiFilter();
        pratilipiFilter.setLanguage(request.language);
        pratilipiFilter.setListName(listName);
        pratilipiFilter.setState(PratilipiState.PUBLISHED);
        List<Long> pratilipiIdList = dataAccessor.getPratilipiIdList(pratilipiFilter, null, null, 6).getDataList();
        if (pratilipiIdList.size() < 6)
            continue;
        sectionList.add(new Response.Section(title, "/" + listName));
        pratilipiIdMasterList.addAll(pratilipiIdList);
    }
    List<PratilipiData> pratilipiDataMasterList = PratilipiDataUtil.createPratilipiDataList(pratilipiIdMasterList, true);
    if (UxModeFilter.isAndroidApp()) {
        DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
        for (PratilipiData pratilipiData : pratilipiDataMasterList) {
            PratilipiContentDoc pcDoc = docAccessor.getPratilipiContentDoc(pratilipiData.getId());
            if (pcDoc == null)
                continue;
            pratilipiData.setIndex(pcDoc.getIndex());
        }
    }
    for (int i = 0; i < sectionList.size(); i++) sectionList.get(i).setPratilipiList(pratilipiDataMasterList.subList(i * 6, i * 6 + 6));
    return new Response(sectionList);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) LinkedList(java.util.LinkedList) GenericResponse(com.pratilipi.api.shared.GenericResponse) PratilipiFilter(com.pratilipi.common.util.PratilipiFilter) PratilipiData(com.pratilipi.data.client.PratilipiData) PratilipiContentDoc(com.pratilipi.data.type.PratilipiContentDoc) Get(com.pratilipi.api.annotation.Get)

Aggregations

DocAccessor (com.pratilipi.data.DocAccessor)28 DataAccessor (com.pratilipi.data.DataAccessor)22 Pratilipi (com.pratilipi.data.type.Pratilipi)12 PratilipiContentDoc (com.pratilipi.data.type.PratilipiContentDoc)12 UserPratilipi (com.pratilipi.data.type.UserPratilipi)12 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)7 Chapter (com.pratilipi.data.type.PratilipiContentDoc.Chapter)6 ArrayList (java.util.ArrayList)6 PratilipiReviewsDoc (com.pratilipi.data.type.PratilipiReviewsDoc)5 BatchProcessDoc (com.pratilipi.data.type.BatchProcessDoc)4 UserPratilipiDoc (com.pratilipi.data.type.UserPratilipiDoc)4 JsonElement (com.google.gson.JsonElement)3 JsonObject (com.google.gson.JsonObject)3 Get (com.pratilipi.api.annotation.Get)3 GenericResponse (com.pratilipi.api.shared.GenericResponse)3 DataListCursorTuple (com.pratilipi.data.DataListCursorTuple)3 PratilipiData (com.pratilipi.data.client.PratilipiData)3 AuditLog (com.pratilipi.data.type.AuditLog)3 Author (com.pratilipi.data.type.Author)3 Page (com.pratilipi.data.type.Page)3