Search in sources :

Example 61 with DataAccessor

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

the class PratilipiDataUtil method createPratilipiData.

@SuppressWarnings("unused")
public static PratilipiData createPratilipiData(Pratilipi pratilipi, Page pratilipiPage, Author author, boolean includeMetaData) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    if (// will still be null for DELETED Pratilipis
    pratilipiPage == null)
        pratilipiPage = dataAccessor.getPage(PageType.PRATILIPI, pratilipi.getId());
    // dataAccessor.getPage( PageType.READ, pratilipi.getId() ); TODO: Uncomment this once data in DataStore is fixed.
    Page readPage = null;
    // dataAccessor.getPage( PageType.WRITE, pratilipi.getId() ); TODO: Uncomment this once data in DataStore is fixed.
    Page writePage = null;
    PratilipiData pratilipiData = new PratilipiData();
    pratilipiData.setId(pratilipi.getId());
    pratilipiData.setTitle(pratilipi.getTitle());
    pratilipiData.setTitleEn(pratilipi.getTitleEn());
    pratilipiData.setLanguage(pratilipi.getLanguage());
    pratilipiData.setAuthorId(pratilipi.getAuthorId());
    pratilipiData.setAuthor(AuthorDataUtil.createAuthorData(author));
    pratilipiData.setSummary(HtmlUtil.toPlainText(pratilipi.getSummary()));
    pratilipiData.setPageUrl(pratilipiPage == null ? null : (pratilipiPage.getUriAlias() == null ? pratilipiPage.getUri() : pratilipiPage.getUriAlias()));
    pratilipiData.setCoverImageUrl(createPratilipiCoverUrl(pratilipi));
    pratilipiData.setReadPageUrl(readPage == null || readPage.getUriAlias() == null ? PageType.READ.getUrlPrefix() + pratilipi.getId() : readPage.getUriAlias());
    if (writePage != null && writePage.getUriAlias() != null) {
        pratilipiData.setWritePageUrl(writePage.getUriAlias());
    } else {
        if (pratilipi.isOldContent())
            pratilipiData.setWritePageUrl("/write?id=" + pratilipi.getId());
        else
            pratilipiData.setWritePageUrl(PageType.WRITE.getUrlPrefix() + pratilipi.getId());
    }
    pratilipiData.setOldContent(pratilipi.isOldContent());
    pratilipiData.setType(pratilipi.getType());
    pratilipiData.setContentType(pratilipi.getContentType());
    pratilipiData.setState(pratilipi.getState());
    pratilipiData.setListingDate(pratilipi.getListingDate());
    if (includeMetaData)
        pratilipiData.setLastUpdated(pratilipi.getLastUpdated());
    pratilipiData.setIndex(pratilipi.getIndex());
    pratilipiData.setPageCount(pratilipi.getPageCount());
    pratilipiData.setReviewCount(pratilipi.getReviewCount());
    pratilipiData.setRatingCount(pratilipi.getRatingCount());
    pratilipiData.setAverageRating(pratilipi.getRatingCount() == 0L ? 0F : (float) ((double) pratilipi.getTotalRating() / pratilipi.getRatingCount()));
    pratilipiData.setRelevance(calculateRelevance(pratilipi, author));
    pratilipiData.setReadCount(pratilipi.getReadCountOffset() + pratilipi.getReadCount());
    pratilipiData.setFbLikeShareCount(pratilipi.getFbLikeShareCountOffset() + pratilipi.getFbLikeShareCount());
    pratilipiData.setWordCount(pratilipi.getWordCount());
    pratilipiData.setTags(TagDataUtil.createTagDataList(pratilipi.getTagIds()));
    pratilipiData.setSuggestedTags(pratilipi.getSuggestedTags());
    pratilipiData.setAccessToUpdate(hasAccessToUpdatePratilipiData(pratilipi, null));
    return pratilipiData;
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) PratilipiData(com.pratilipi.data.client.PratilipiData) Page(com.pratilipi.data.type.Page)

Example 62 with DataAccessor

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

the class PratilipiDataUtil method updatePratilipiSearchIndex.

public static void updatePratilipiSearchIndex(List<Long> pratilipiIdList) throws InvalidArgumentException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    _updatePratilipiSearchIndex(dataAccessor.getPratilipiList(pratilipiIdList));
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor)

Example 63 with DataAccessor

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

the class PratilipiDataUtil method updatePratilipiContent.

public static int updatePratilipiContent(long pratilipiId, int pageNo, PratilipiContentType contentType, Object pageContent, boolean insertNew) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    if (!hasAccessToUpdatePratilipiContent(pratilipi))
        throw new InsufficientAccessException();
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE, pratilipi);
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
    if (contentType == PratilipiContentType.PRATILIPI) {
        BlobEntry blobEntry = blobAccessor.getBlob(CONTENT_FOLDER + "/" + pratilipiId);
        if (blobEntry == null) {
            blobEntry = blobAccessor.newBlob(CONTENT_FOLDER + "/" + pratilipiId);
            blobEntry.setData("&nbsp".getBytes(Charset.forName("UTF-8")));
            blobEntry.setMimeType("text/html");
        }
        String content = new String(blobEntry.getData(), Charset.forName("UTF-8"));
        PratilipiContentUtil pratilipiContentUtil = new PratilipiContentUtil(content);
        content = pratilipiContentUtil.updateContent(pageNo, (String) pageContent, insertNew);
        int pageCount = pratilipiContentUtil.getPageCount();
        if (content.isEmpty()) {
            content = "&nbsp";
            pageCount = 1;
        }
        blobEntry.setData(content.getBytes(Charset.forName("UTF-8")));
        blobAccessor.createOrUpdateBlob(blobEntry);
        pratilipi.setPageCount(pageCount);
        if (insertNew)
            auditLog.setEventComment("Added new page " + pageNo + " in Pratilpi content.");
        else if (!((String) pageContent).isEmpty())
            auditLog.setEventComment("Updated page " + pageNo + " in Pratilpi content.");
        else
            auditLog.setEventComment("Deleted page " + pageNo + " in Pratilpi content.");
    } else if (contentType == PratilipiContentType.IMAGE) {
        BlobEntry blobEntry = (BlobEntry) pageContent;
        blobEntry.setName(IMAGE_CONTENT_FOLDER + "/" + pratilipiId + "/" + pageNo);
        blobAccessor.createOrUpdateBlob(blobEntry);
        if (pageNo > (int) pratilipi.getPageCount())
            pratilipi.setPageCount(pageNo);
        auditLog.setEventComment("Uploaded page " + pageNo + " in Image content.");
    } else {
        throw new InvalidArgumentException(contentType + " content type is not yet supported.");
    }
    pratilipi.setLastUpdated(new Date());
    pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
    return pratilipi.getPageCount();
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) DataAccessor(com.pratilipi.data.DataAccessor) BlobEntry(com.pratilipi.data.type.BlobEntry) PratilipiContentUtil(com.pratilipi.common.util.PratilipiContentUtil) BlobAccessor(com.pratilipi.data.BlobAccessor) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Example 64 with DataAccessor

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

the class PratilipiDataUtil method getPratilipiDataList.

public static DataListCursorTuple<PratilipiData> getPratilipiDataList(String searchQuery, Long eventId, PratilipiFilter pratilipiFilter, String cursor, Integer offset, Integer resultCount) throws InsufficientAccessException, UnexpectedServerException {
    if (!hasAccessToListPratilipiData(pratilipiFilter))
        throw new InsufficientAccessException();
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    // Processing search query
    if (searchQuery != null)
        searchQuery = searchQuery.toLowerCase().trim().replaceAll("\"|\\\\|\\(|\\)|:|,|\\sor\\s", " ").trim().replaceAll("[\\s]+", " OR ");
    // Fetching Pratilipi id list from DataStore/SearchIndex/Event
    DataListCursorTuple<Long> pratilipiIdListCursorTuple = null;
    if (eventId != null) {
        List<Long> pratilipiIdList = dataAccessor.getEvent(eventId).getPratilipiIdList();
        // move read content to end of the list.
        List<Long> contentsReadList = UserPratilipiDataUtil.getContentsReadList(AccessTokenFilter.getAccessToken().getUserId());
        // move all read contents to bottom of the list.
        if (contentsReadList != null) {
            // Find all read contents in the list
            contentsReadList.retainAll(pratilipiIdList);
            // Remove already read content from the list
            pratilipiIdList.removeAll(contentsReadList);
            // Append read content at end of the list
            pratilipiIdList.addAll(contentsReadList);
        }
        offset = (cursor == null ? 0 : Integer.parseInt(cursor)) + (offset == null || offset < 0 ? 0 : offset);
        offset = Math.min(offset, pratilipiIdList.size());
        resultCount = resultCount == null || resultCount > pratilipiIdList.size() - offset ? pratilipiIdList.size() - offset : resultCount;
        pratilipiIdListCursorTuple = new DataListCursorTuple<Long>(pratilipiIdList.subList(offset, offset + resultCount), offset + resultCount + "", (long) pratilipiIdList.size());
    } else if (pratilipiFilter.getListName() == null && pratilipiFilter.getState() == PratilipiState.PUBLISHED) {
        pratilipiIdListCursorTuple = DataAccessorFactory.getSearchAccessor().searchPratilipi(searchQuery, pratilipiFilter, cursor, offset, resultCount);
    } else {
        pratilipiIdListCursorTuple = dataAccessor.getPratilipiIdList(pratilipiFilter, cursor, offset, resultCount);
    }
    // Creating PratilipiData list from Pratilipi id list
    List<PratilipiData> pratilipiDataList = createPratilipiDataList(pratilipiIdListCursorTuple.getDataList(), pratilipiFilter.getAuthorId() == null, false);
    // Creating response object
    return new DataListCursorTuple<PratilipiData>(pratilipiDataList, pratilipiIdListCursorTuple.getCursor(), pratilipiIdListCursorTuple.getNumberFound());
}
Also used : DataListCursorTuple(com.pratilipi.data.DataListCursorTuple) DataAccessor(com.pratilipi.data.DataAccessor) PratilipiData(com.pratilipi.data.client.PratilipiData) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException)

Example 65 with DataAccessor

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

the class PratilipiDataUtil method _updatePratilipiSearchIndex.

private static void _updatePratilipiSearchIndex(List<Pratilipi> pratilipiList) throws InvalidArgumentException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    SearchAccessor searchAccessor = DataAccessorFactory.getSearchAccessor();
    List<PratilipiData> pratilipiDataList = new ArrayList<>(pratilipiList.size());
    for (Pratilipi pratilipi : pratilipiList) {
        if (pratilipi.getState() == PratilipiState.PUBLISHED) {
            Author author = dataAccessor.getAuthor(pratilipi.getAuthorId());
            pratilipiDataList.add(createPratilipiData(pratilipi, author, true));
        } else {
            searchAccessor.deletePratilipiDataIndex(pratilipi.getId());
        }
    }
    if (pratilipiDataList.size() > 0) {
        Map<PratilipiData, String> pratilipiDataKeywordsMap = new HashMap<>();
        for (PratilipiData pratilipiData : pratilipiDataList) pratilipiDataKeywordsMap.put(pratilipiData, getPratilipiKeywords(pratilipiData.getId()));
        searchAccessor.indexPratilipiDataList(pratilipiDataKeywordsMap);
    }
}
Also used : SearchAccessor(com.pratilipi.data.SearchAccessor) HashMap(java.util.HashMap) DataAccessor(com.pratilipi.data.DataAccessor) PratilipiData(com.pratilipi.data.client.PratilipiData) ArrayList(java.util.ArrayList) Author(com.pratilipi.data.type.Author) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi)

Aggregations

DataAccessor (com.pratilipi.data.DataAccessor)162 Date (java.util.Date)53 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)37 Pratilipi (com.pratilipi.data.type.Pratilipi)34 Author (com.pratilipi.data.type.Author)33 AuditLog (com.pratilipi.data.type.AuditLog)32 UserPratilipi (com.pratilipi.data.type.UserPratilipi)32 ArrayList (java.util.ArrayList)30 Page (com.pratilipi.data.type.Page)28 HashMap (java.util.HashMap)25 GenericResponse (com.pratilipi.api.shared.GenericResponse)24 DocAccessor (com.pratilipi.data.DocAccessor)22 User (com.pratilipi.data.type.User)22 Get (com.pratilipi.api.annotation.Get)20 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)19 PratilipiData (com.pratilipi.data.client.PratilipiData)15 UserData (com.pratilipi.data.client.UserData)14 BlobAccessor (com.pratilipi.data.BlobAccessor)13 Email (com.pratilipi.data.type.Email)13 JsonObject (com.google.gson.JsonObject)12