Search in sources :

Example 36 with Pratilipi

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

the class PratilipiDataUtil method updateFacebookScrape.

public static void updateFacebookScrape(List<Long> pratilipiIdList) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    for (Long pratilipiId : pratilipiIdList) {
        Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
        if (pratilipi.getState() != PratilipiState.PUBLISHED)
            continue;
        Page page = dataAccessor.getPage(PageType.PRATILIPI, pratilipiId);
        String uri = page.getUriAlias() == null ? page.getUri() : page.getUriAlias();
        FacebookApi.postScrapeRequest("http://" + pratilipi.getLanguage().getHostName() + uri);
    }
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) Page(com.pratilipi.data.type.Page) Pratilipi(com.pratilipi.data.type.Pratilipi) UserPratilipi(com.pratilipi.data.type.UserPratilipi)

Example 37 with Pratilipi

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

the class PratilipiDataUtil method getPratilipiContent.

public static String getPratilipiContent(long pratilipiId, Integer chapterNo) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    if (!hasAccessToReadPratilipiContent(pratilipi))
        throw new InsufficientAccessException();
    BlobEntry blobEntry = DataAccessorFactory.getBlobAccessor().getBlob(CONTENT_FOLDER + "/" + pratilipiId);
    if (blobEntry == null)
        return null;
    String contentHtml = new String(blobEntry.getData(), Charset.forName("UTF-8"));
    PratilipiContentUtil pratilipiContentUtil = new PratilipiContentUtil(contentHtml);
    return pratilipiContentUtil.getContent(chapterNo);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) BlobEntry(com.pratilipi.data.type.BlobEntry) Pratilipi(com.pratilipi.data.type.Pratilipi) UserPratilipi(com.pratilipi.data.type.UserPratilipi) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException)

Example 38 with Pratilipi

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

the class PratilipiDataUtil method updatePratilipiIndex.

public static void updatePratilipiIndex(Long pratilipiId) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    if (pratilipi.isOldContent() && pratilipi.getContentType() == PratilipiContentType.PRATILIPI) {
        BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
        BlobEntry blobEntry = blobAccessor.getBlob(CONTENT_FOLDER + "/" + pratilipiId);
        String index = null;
        Integer pageCount = 0;
        if (blobEntry != null) {
            String content = new String(blobEntry.getData(), Charset.forName("UTF-8"));
            PratilipiContentUtil pratilipiContentUtil = new PratilipiContentUtil(content);
            index = pratilipiContentUtil.generateIndex();
            pageCount = pratilipiContentUtil.getPageCount();
        }
        AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE, pratilipi);
        boolean isChanged = false;
        if ((pratilipi.getIndex() == null && index != null) || (pratilipi.getIndex() != null && index == null) || (pratilipi.getIndex() != null && index != null && !pratilipi.getIndex().equals(index))) {
            pratilipi.setIndex(index);
            isChanged = true;
        }
        if (pratilipi.getPageCount() != pageCount) {
            pratilipi.setPageCount(pageCount);
            isChanged = true;
        }
        if (isChanged)
            pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
    } else {
    // throw new InvalidArgumentException( "Index generation for " + pratilipi.getContentType() + " content type is not yet supported." );
    }
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) BlobEntry(com.pratilipi.data.type.BlobEntry) BlobAccessor(com.pratilipi.data.BlobAccessor) Pratilipi(com.pratilipi.data.type.Pratilipi) UserPratilipi(com.pratilipi.data.type.UserPratilipi) AuditLog(com.pratilipi.data.type.AuditLog)

Example 39 with Pratilipi

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

the class PratilipiDataUtil method updatePratilipiSearchIndex.

public static void updatePratilipiSearchIndex(Long pratilipiId, Long authorId) throws InvalidArgumentException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    List<Pratilipi> pratilipiList = null;
    if (authorId != null) {
        PratilipiFilter pratilipiFilter = new PratilipiFilter();
        pratilipiFilter.setAuthorId(authorId);
        pratilipiList = dataAccessor.getPratilipiList(pratilipiFilter, null, null).getDataList();
    } else if (pratilipiId != null) {
        pratilipiList = new ArrayList<>(1);
        pratilipiList.add(dataAccessor.getPratilipi(pratilipiId));
    } else {
        logger.log(Level.SEVERE, "Neither AuthorId, nor PratilipiId is provided !");
        throw new InvalidArgumentException("Neither AuthorId, nor PratilipiId is provided !");
    }
    _updatePratilipiSearchIndex(pratilipiList);
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) DataAccessor(com.pratilipi.data.DataAccessor) ArrayList(java.util.ArrayList) Pratilipi(com.pratilipi.data.type.Pratilipi) UserPratilipi(com.pratilipi.data.type.UserPratilipi)

Example 40 with Pratilipi

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

the class PratilipiDocUtil method saveContentPage.

public static PratilipiContentDoc saveContentPage(Long pratilipiId, String chapterId, Integer chapterNo, String chapterTitle, Integer pageNo, String html) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
    // Auth check
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    // Auth Service will be taking care of it - Move it to gamma and prod asap
    if (_shouldAuth()) {
        if (!PratilipiDataUtil.hasAccessToUpdatePratilipiContent(pratilipi))
            throw new InsufficientAccessException();
    }
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    // Doc
    // backfill chapterId
    PratilipiContentDoc pcDoc = _validateChapterIdInContent(pratilipiId);
    // if not present, get a new doc
    if (pcDoc == null)
        pcDoc = docAccessor.newPratilipiContentDoc();
    // Chapter
    Chapter chapter = null;
    // If chapterNo is not null => web case
    if (chapterNo != null) {
        // If chapter already present -> editChapter case -> update the chapter and set lastUpdated and lastUpdatedBy
        chapter = pcDoc.getChapter(chapterNo);
        // If chapter not present -> addChapter case -> add empty chapters (there are 10 chapters already, user is trying to add 15th chapter => so add 4 empty chapters in between)
        if (chapter == null) {
            /* Add Chapter */
            Long aChapterId = new Date().getTime();
            while ((chapter = pcDoc.getChapter(chapterNo)) == null) {
                Chapter ch = pcDoc.addChapter(null);
                ch.setId(generateChapterId(pratilipiId, aChapterId++));
                ch.setLastUpdated(UxModeFilter.getClientType());
            }
        }
    // If chapterId is not null => android case
    } else if (chapterId != null) {
        // If chapter already present -> editChapter case -> update the chapter and set lastUpdated and lastUpdatedBy
        chapter = pcDoc.getChapter(chapterId);
        // If chapter not present -> addChapter case -> add chapter at the end always
        if (chapter == null) {
            chapter = pcDoc.addChapter(chapterTitle);
            chapter.setId(chapterId);
        }
    }
    // Set lastUpdatedDate and lastUpdatedBy -> WEB / ANDROID
    chapter.setLastUpdated(UxModeFilter.getClientType());
    // Setting chapterTitle
    if (chapterTitle != null)
        chapter.setTitle(chapterTitle);
    // Page
    PratilipiContentDoc.Page page = chapter.getPage(pageNo);
    if (page == null)
        page = chapter.addPage(pageNo);
    // Magic
    _setPage(pratilipiId, page, html);
    // Save
    docAccessor.save(pratilipiId, pcDoc);
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE_CONTENT, null);
    auditLog.setEventDataNew(html);
    auditLog.setPrimaryContentId(pratilipiId);
    auditLog.setCreationDate(new Date());
    dataAccessor.createOrUpdateAuditLog(auditLog);
    return pcDoc;
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) Chapter(com.pratilipi.data.type.PratilipiContentDoc.Chapter) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) PratilipiContentDoc(com.pratilipi.data.type.PratilipiContentDoc) Date(java.util.Date) AuditLog(com.pratilipi.data.type.AuditLog)

Aggregations

Pratilipi (com.pratilipi.data.type.Pratilipi)45 DataAccessor (com.pratilipi.data.DataAccessor)36 UserPratilipi (com.pratilipi.data.type.UserPratilipi)33 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)17 PratilipiContentDoc (com.pratilipi.data.type.PratilipiContentDoc)14 DocAccessor (com.pratilipi.data.DocAccessor)13 AuditLog (com.pratilipi.data.type.AuditLog)10 Author (com.pratilipi.data.type.Author)9 Page (com.pratilipi.data.type.Page)9 Chapter (com.pratilipi.data.type.PratilipiContentDoc.Chapter)9 Date (java.util.Date)9 BlobEntry (com.pratilipi.data.type.BlobEntry)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)6 BlobAccessor (com.pratilipi.data.BlobAccessor)6 Get (com.pratilipi.api.annotation.Get)5 GenericResponse (com.pratilipi.api.shared.GenericResponse)5 PratilipiData (com.pratilipi.data.client.PratilipiData)5 PratilipiEntity (com.pratilipi.data.type.gae.PratilipiEntity)4