use of com.pratilipi.data.type.Pratilipi 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.Pratilipi in project pratilipi by Pratilipi.
the class PratilipiV1Api method get.
@Get
public Response get(GetRequest request) throws UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Pratilipi pratilipi = dataAccessor.getPratilipi(request.pratilipiId);
Author author = pratilipi.getAuthorId() == null ? null : dataAccessor.getAuthor(pratilipi.getAuthorId());
PratilipiData pratilipiData = PratilipiDataUtil.createPratilipiData(pratilipi, author);
if (UxModeFilter.isAndroidApp()) {
DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
PratilipiContentDoc pcDoc = docAccessor.getPratilipiContentDoc(request.pratilipiId);
pratilipiData.setIndex(pcDoc == null ? null : pcDoc.getIndex());
}
return new Response(pratilipiData);
}
use of com.pratilipi.data.type.Pratilipi in project pratilipi by Pratilipi.
the class DataStoreBackupUtil method csvPratilipi.
public static void csvPratilipi() throws UnexpectedServerException {
BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessorBackup();
DateFormat csvDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
csvDateFormat.setTimeZone(TimeZone.getTimeZone("Asia/Kolkata"));
String CSV_HEADER = "PratilipiId,AuthorId," + "Title,TitleEN,Language,HasSummary," + "Type,ContentType,State,HasCover,ListingDate," + "ReviewCount, RatingCount, TotalRating, ReadCount";
String CSV_SEPARATOR = ",";
String LINE_SEPARATOR = "\n";
StringBuilder csv = new StringBuilder(CSV_HEADER + LINE_SEPARATOR);
int count = 0;
QueryResultIterator<PratilipiEntity> itr = ObjectifyService.ofy().cache(false).load().type(PratilipiEntity.class).chunk(1000).iterator();
while (itr.hasNext()) {
Pratilipi pratilipi = itr.next();
csv.append("'" + pratilipi.getId()).append(CSV_SEPARATOR).append(pratilipi.getAuthorId() == null ? "" : "'" + pratilipi.getAuthorId()).append(CSV_SEPARATOR).append(pratilipi.getTitle() == null ? "" : "\"" + pratilipi.getTitle().replace("\"", "\"\"") + "\"").append(CSV_SEPARATOR).append(pratilipi.getTitleEn() == null ? "" : "\"" + pratilipi.getTitleEn().replace("\"", "\"\"") + "\"").append(CSV_SEPARATOR).append(pratilipi.getLanguage()).append(CSV_SEPARATOR).append(pratilipi.getSummary() != null && pratilipi.getSummary().trim().length() != 0).append(CSV_SEPARATOR).append(pratilipi.getType()).append(CSV_SEPARATOR).append(pratilipi.getContentType()).append(CSV_SEPARATOR).append(pratilipi.getState()).append(CSV_SEPARATOR).append(pratilipi.getCoverImage() != null).append(CSV_SEPARATOR).append(csvDateFormat.format(pratilipi.getListingDate())).append(CSV_SEPARATOR).append(pratilipi.getReviewCount()).append(CSV_SEPARATOR).append(pratilipi.getRatingCount()).append(CSV_SEPARATOR).append(pratilipi.getTotalRating()).append(CSV_SEPARATOR).append(pratilipi.getReadCount()).append(LINE_SEPARATOR);
count++;
if (count % 1000 == 0)
System.out.println(count + " ...");
}
System.out.println(count + " ...");
BlobEntry authorCsvEntry = blobAccessor.newBlob("datastore/pratilipi.csv", csv.toString().getBytes(Charset.forName("UTF-8")), "text/csv");
blobAccessor.createOrUpdateBlob(authorCsvEntry);
}
use of com.pratilipi.data.type.Pratilipi in project pratilipi by Pratilipi.
the class PratilipiV2Api method get.
@Get
public Response get(GetRequest request) throws UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Pratilipi pratilipi = dataAccessor.getPratilipi(request.pratilipiId);
Author author = pratilipi.getAuthorId() == null ? null : dataAccessor.getAuthor(pratilipi.getAuthorId());
return new Response(PratilipiDataUtil.createPratilipiData(pratilipi, author));
}
use of com.pratilipi.data.type.Pratilipi in project pratilipi by Pratilipi.
the class DataAccessorMockImpl method createOrUpdatePratilipi.
@Override
public Pratilipi createOrUpdatePratilipi(Pratilipi pratilipi, AuditLog auditLog) {
if (pratilipi.getId() != null)
return pratilipi;
long pratilipiId = 0L;
for (Pratilipi aPratilipi : PRATILIPI_TABLE) if (pratilipiId <= aPratilipi.getId())
pratilipiId = aPratilipi.getId() + 1;
((PratilipiEntity) pratilipi).setId(pratilipiId);
PRATILIPI_TABLE.add(pratilipi);
return pratilipi;
}
Aggregations