use of com.pratilipi.data.type.PratilipiContentDoc.Chapter in project pratilipi by Pratilipi.
the class PratilipiDocUtil method getContent_v3.
public static Object getContent_v3(Long pratilipiId, Integer chapterNo, Integer pageNo) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
if (!PratilipiDataUtil.hasAccessToReadPratilipiContent(pratilipi))
throw new InsufficientAccessException();
DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
PratilipiContentDoc pcDoc = docAccessor.getPratilipiContentDoc(pratilipiId);
if (pcDoc == null)
return null;
else if (chapterNo == null)
return _processContent_v3(pratilipi, pcDoc);
Chapter chapter = pcDoc.getChapter(chapterNo);
if (chapter == null)
return null;
else if (pageNo == null)
return _processContent_v3(pratilipi, chapter);
PratilipiContentDoc.Page page = chapter.getPage(pageNo);
if (page == null)
return null;
else
return _processContent_v3(pratilipi, chapter.getPage(pageNo));
}
use of com.pratilipi.data.type.PratilipiContentDoc.Chapter in project pratilipi by Pratilipi.
the class PratilipiDocUtil method updatePratilipiContent.
public static void updatePratilipiContent(Long pratilipiId) throws UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
PratilipiContentDoc pcDoc = DataAccessorFactory.getDocAccessor().newPratilipiContentDoc();
Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
if (!pratilipi.isOldContent()) {
return;
} else if (pratilipi.getContentType() == PratilipiContentType.PRATILIPI) {
BlobEntry blobEntry = blobAccessor.getBlob("pratilipi-content/pratilipi/" + pratilipiId);
if (blobEntry == null)
return;
String contentHtml = new String(blobEntry.getData(), Charset.forName("UTF-8"));
List<Object[]> pageletList = _createPageletList(pratilipi, Jsoup.parse(contentHtml).body());
if (pageletList.size() > 0) {
PratilipiContentDoc.Chapter chapter = null;
if (pageletList.get(0)[0] != PratilipiContentDoc.PageletType.HEAD)
chapter = pcDoc.addChapter(pratilipi.getTitle() == null ? pratilipi.getTitleEn() : pratilipi.getTitle());
for (Object[] pagelet : pageletList) {
if (pagelet[0] == PratilipiContentDoc.PageletType.HEAD) {
chapter = pcDoc.addChapter((String) pagelet[1]);
} else {
PratilipiContentDoc.Page page = chapter.getPage(1);
if (page == null)
page = chapter.addPage();
page.addPagelet((PratilipiContentDoc.PageletType) pagelet[0], pagelet[1], (PratilipiContentDoc.AlignmentType) pagelet[2]);
}
}
}
} else if (pratilipi.getContentType() == PratilipiContentType.IMAGE) {
for (int i = 1; i <= pratilipi.getPageCount(); i++) {
BlobEntry blobEntry = blobAccessor.getBlob("pratilipi/" + pratilipiId + "/images/" + i);
if (pratilipi.getId() == 5639838220943360L && i <= 5)
// Skipping first 5 pages as per Shally's request
continue;
else if (pratilipi.getId() == 5749258686824448L && i <= 4)
// Skipping first 4 pages as per Shally's request
continue;
else if (pratilipi.getId() == 5486454792781824L && i <= 1)
// Skipping first page as per Shally's request
continue;
else if (blobEntry == null && pratilipi.getId() == 5768181499035648L)
// Skipping missing pages as per Dileepan's request
continue;
JsonObject imgData = new JsonObject();
imgData.addProperty("name", i + "");
imgData.addProperty("height", ImageUtil.getHeight(blobEntry));
imgData.addProperty("width", ImageUtil.getWidth(blobEntry));
pcDoc.addChapter(null).addPage().addPagelet(PratilipiContentDoc.PageletType.IMAGE, imgData);
}
} else {
throw new UnexpectedServerException("ContentType " + pratilipi.getContentType() + " is not supported !");
}
docAccessor.save(pratilipiId, pcDoc);
}
use of com.pratilipi.data.type.PratilipiContentDoc.Chapter in project pratilipi by Pratilipi.
the class PratilipiDocUtil method getContent.
@Deprecated
public static Object getContent(Long pratilipiId, Integer chapterNo, Integer pageNo) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
if (!PratilipiDataUtil.hasAccessToReadPratilipiContent(pratilipi))
throw new InsufficientAccessException();
DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
PratilipiContentDoc pcDoc = docAccessor.getPratilipiContentDoc(pratilipiId);
if (pcDoc == null)
return null;
else if (chapterNo == null)
return _processContent(pratilipi, pcDoc);
Chapter chapter = pcDoc.getChapter(chapterNo);
if (chapter == null)
return null;
else if (pageNo == null)
return _processContent(pratilipi, chapter);
PratilipiContentDoc.Page page = chapter.getPage(pageNo);
if (page == null)
return null;
else
return _processContent(pratilipi, chapter.getPage(pageNo));
}
use of com.pratilipi.data.type.PratilipiContentDoc.Chapter in project pratilipi by Pratilipi.
the class PratilipiSite method createDataModelForReadPage.
public Map<String, Object> createDataModelForReadPage(Long pratilipiId, Integer chapterNo, String version, Language language, boolean basicMode) throws InvalidArgumentException, UnexpectedServerException, InsufficientAccessException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
PratilipiData pratilipiData = PratilipiDataUtil.createPratilipiData(pratilipi);
PratilipiV2Api.Response pratilipiResponse = new PratilipiV2Api.Response(pratilipiData);
UserPratilipiApi.GetRequest userPratilipiRequest = new UserPratilipiApi.GetRequest();
userPratilipiRequest.setPratilipiId(pratilipiId);
UserPratilipiApi.Response userPratilipiResponse = ApiRegistry.getApi(UserPratilipiApi.class).getUserPratilipi(userPratilipiRequest);
String indexJson = null;
Integer pageCount = null;
Object content = null;
// In case, version is not specified in URL
if (version == null) {
if (SystemProperty.STAGE.equals(SystemProperty.STAGE_PROD) && pratilipi.isOldContent() && pratilipi.getContentType() == PratilipiContentType.PRATILIPI) {
version = "1";
} else {
version = "3";
}
}
// Index and pageCount for all contents
if (version.equals("1")) {
indexJson = pratilipi.getIndex();
pageCount = pratilipi.getPageCount() > 0 ? pratilipi.getPageCount() : 1;
} else if (version.equals("2") || version.equals("3")) {
// Json format
PratilipiContentIndexApi.GetRequest indexReq = new PratilipiContentIndexApi.GetRequest();
indexReq.setPratilipiId(pratilipiId);
PratilipiContentIndexApi.Response indexRes = ApiRegistry.getApi(PratilipiContentIndexApi.class).getIndex(indexReq);
indexJson = indexRes.getIndex().toString();
pageCount = indexRes.getIndex().size() > 0 ? indexRes.getIndex().size() : 1;
}
if (pratilipi.getContentType() == PratilipiContentType.PRATILIPI) {
if (chapterNo < 1)
chapterNo = 1;
if (chapterNo > pageCount)
chapterNo = pageCount;
if (version.equals("1")) {
PratilipiContentV1Api.GetRequest req = new PratilipiContentV1Api.GetRequest();
req.setPratilipiId(pratilipiId);
req.setChapterNo(chapterNo);
PratilipiContentV1Api.GetResponse res = (PratilipiContentV1Api.GetResponse) ApiRegistry.getApi(PratilipiContentV1Api.class).get(req);
content = res.getContent();
} else if (version.equals("2")) {
PratilipiContentV2Api.GetRequest req = new PratilipiContentV2Api.GetRequest();
req.setPratilipiId(pratilipiId);
req.setChapterNo(chapterNo);
PratilipiContentV2Api.GetResponse res = (PratilipiContentV2Api.GetResponse) ApiRegistry.getApi(PratilipiContentV2Api.class).get(req);
content = res.getContent();
if (res.getChapterTitle() != null)
content = "<h1>" + res.getChapterTitle() + "</h1>" + content;
} else if (version.equals("3")) {
PratilipiContentV3Api.GetRequest req = new PratilipiContentV3Api.GetRequest();
req.setPratilipiId(pratilipiId);
req.setChapterNo(chapterNo);
PratilipiContentV3Api.GetResponse res = (PratilipiContentV3Api.GetResponse) ApiRegistry.getApi(PratilipiContentV3Api.class).get(req);
content = res.getContent();
if (res.getChapterTitle() != null)
content = "<h1>" + res.getChapterTitle() + "</h1>" + content;
}
} else if (pratilipi.getContentType() == PratilipiContentType.IMAGE) {
PratilipiContentV2Api.GetRequest req = new PratilipiContentV2Api.GetRequest();
req.setPratilipiId(pratilipiId);
PratilipiContentV2Api.GetResponse res = (PratilipiContentV2Api.GetResponse) ApiRegistry.getApi(PratilipiContentV2Api.class).get(req);
PratilipiContentDoc pcDoc = (PratilipiContentDoc) res.getContent();
// Image books can have more than one page in a chapter
pageCount = 0;
for (Chapter chapter : pcDoc.getChapterList()) pageCount += chapter.getPageCount();
if (chapterNo < 1)
chapterNo = 1;
if (chapterNo > pageCount)
chapterNo = pageCount;
if (basicMode) {
JsonObject jsonObject = null;
int c = 0;
for (int i = 1; i <= pcDoc.getChapterCount(); i++) {
for (int j = 1; j <= pcDoc.getChapter(i).getPageCount(); j++) {
if (++c == chapterNo) {
jsonObject = pcDoc.getChapter(i).getPage(j).getPageletList().get(0).getData();
break;
}
}
}
content = PratilipiDocUtil._processImagePagelet(pratilipiId, jsonObject);
} else {
content = new Gson().toJson(res.getContent());
}
}
Gson gson = new Gson();
Map<String, Object> dataModel = new HashMap<String, Object>();
dataModel.put("title", SEOTitleUtil.getReadPageTitle(pratilipiId, language));
dataModel.put("pageNo", chapterNo);
dataModel.put("pageCount", pageCount);
dataModel.put("content", content);
dataModel.put("version", version);
dataModel.put("contentType", pratilipi.getContentType());
if (basicMode) {
dataModel.put("pratilipi", pratilipiResponse);
dataModel.put("userpratilipi", userPratilipiResponse);
dataModel.put("indexList", gson.fromJson(indexJson, Object.class));
} else {
dataModel.put("pratilipiJson", gson.toJson(pratilipiResponse));
dataModel.put("userpratilipiJson", gson.toJson(userPratilipiResponse));
dataModel.put("indexJson", gson.toJson(indexJson));
}
return dataModel;
}
use of com.pratilipi.data.type.PratilipiContentDoc.Chapter in project pratilipi by Pratilipi.
the class PratilipiDocUtil method saveContentPage.
public static void saveContentPage(Long pratilipiId, Integer chapterNo, String chapterTitle, Integer pageNo, String html) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
if (!PratilipiDataUtil.hasAccessToUpdatePratilipiContent(pratilipi))
throw new InsufficientAccessException();
DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
// Doc
PratilipiContentDoc pcDoc = docAccessor.getPratilipiContentDoc(pratilipiId);
if (pcDoc == null)
pcDoc = docAccessor.newPratilipiContentDoc();
// Chapter
Chapter chapter = pcDoc.getChapter(chapterNo);
if (chapter == null)
chapter = pcDoc.addChapter(chapterNo, chapterTitle);
else
chapter.setTitle(chapterTitle);
// Page
PratilipiContentDoc.Page page = chapter.getPage(pageNo);
if (page == null)
page = chapter.addPage(pageNo);
_setPage(pratilipiId, page, html);
// Save
docAccessor.save(pratilipiId, pcDoc);
}
Aggregations