use of com.pratilipi.common.util.PratilipiContentUtil 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.common.util.PratilipiContentUtil 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);
}
use of com.pratilipi.common.util.PratilipiContentUtil 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." );
}
}
Aggregations