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;
}
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));
}
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(" ".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 = " ";
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();
}
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());
}
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);
}
}
Aggregations