use of com.pratilipi.data.client.PratilipiData in project pratilipi by Pratilipi.
the class PratilipiDataUtil method updatePratilipiTags.
public static boolean updatePratilipiTags(long pratilipiId, PratilipiType pratilipiType, List<Long> tagIds, List<String> suggestedTags) throws InsufficientAccessException, UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
if (pratilipi == null)
return false;
AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE, pratilipi);
PratilipiData pratilipiData = createPratilipiData(pratilipi);
AccessToken accessToken = AccessTokenFilter.getAccessToken();
logger.log(Level.INFO, "User Id : " + accessToken.getUserId());
if (!hasAccessToUpdatePratilipiData(pratilipi, pratilipiData)) {
throw new InsufficientAccessException();
}
if (pratilipiType != null)
pratilipi.setType(pratilipiType);
if (tagIds != null)
pratilipi.setTagIds(tagIds);
if (suggestedTags != null)
pratilipi.setSuggestedTags(suggestedTags);
dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
return true;
}
use of com.pratilipi.data.client.PratilipiData in project pratilipi by Pratilipi.
the class PratilipiDataUtil method createPratilipiDataList.
public static List<PratilipiData> createPratilipiDataList(List<Long> pratilipiIdList, boolean includeAuthorData, boolean includeMetaData) throws UnexpectedServerException {
DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
List<Pratilipi> pratilipiList = dataAccessor.getPratilipiList(pratilipiIdList);
Map<Long, Page> pratilipiPages = dataAccessor.getPages(PageType.PRATILIPI, pratilipiIdList);
Map<Long, AuthorData> authorIdToDataMap = null;
if (includeAuthorData) {
List<Long> authorIdList = new LinkedList<>();
for (Pratilipi pratilipi : pratilipiList) if (pratilipi.getAuthorId() != null && !authorIdList.contains(pratilipi.getAuthorId()))
authorIdList.add(pratilipi.getAuthorId());
List<AuthorData> authorDataList = AuthorDataUtil.createAuthorDataList(authorIdList, false);
authorIdToDataMap = new HashMap<>(authorDataList.size());
for (AuthorData authorData : authorDataList) authorIdToDataMap.put(authorData.getId(), authorData);
}
List<PratilipiData> pratilipiDataList = new ArrayList<>(pratilipiList.size());
for (Pratilipi pratilipi : pratilipiList) {
PratilipiData pratilipiData = createPratilipiData(pratilipi, pratilipiPages.get(pratilipi.getId()), null, includeMetaData);
if (includeAuthorData && pratilipi.getAuthorId() != null)
pratilipiData.setAuthor(authorIdToDataMap.get(pratilipi.getAuthorId()));
pratilipiData.setRelevance(calculateRelevance(pratilipi, dataAccessor.getAuthor(pratilipi.getAuthorId())));
pratilipiDataList.add(pratilipiData);
}
// Fetching UserPratilipi list from DataStore
List<UserPratilipi> userPratilipiList = dataAccessor.getUserPratilipiList(AccessTokenFilter.getAccessToken().getUserId(), pratilipiIdList);
// Setting isAddedToLib flag for for each PratilipiData in the list
for (int i = 0; i < userPratilipiList.size(); i++) {
UserPratilipi userPratilipi = userPratilipiList.get(i);
pratilipiDataList.get(i).setAddedToLib(userPratilipi != null && userPratilipi.isAddedToLib());
}
return pratilipiDataList;
}
use of com.pratilipi.data.client.PratilipiData in project pratilipi by Pratilipi.
the class PratilipiListV1Api method get.
@Get
public Response get(GetRequest request) throws InsufficientAccessException, UnexpectedServerException {
PratilipiFilter pratilipiFilter = new PratilipiFilter();
pratilipiFilter.setAuthorId(request.authorId);
pratilipiFilter.setLanguage(request.language);
pratilipiFilter.setType(request.type);
pratilipiFilter.setListName(request.listName);
pratilipiFilter.setState(request.state);
pratilipiFilter.setOrderByLastUpdate(request.orderByLastUpdated);
DataListCursorTuple<PratilipiData> pratilipiListCursorTuple = PratilipiDataUtil.getPratilipiDataList(request.searchQuery, request.eventId, pratilipiFilter, request.cursor, request.offset, request.resultCount == null ? 20 : request.resultCount);
if (UxModeFilter.isAndroidApp()) {
DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
for (PratilipiData pratilipiData : pratilipiListCursorTuple.getDataList()) {
PratilipiContentDoc pcDoc = docAccessor.getPratilipiContentDoc(pratilipiData.getId());
if (pcDoc == null)
continue;
pratilipiData.setIndex(pcDoc.getIndex());
}
}
Response response = new Response(pratilipiListCursorTuple.getDataList(), pratilipiListCursorTuple.getCursor(), pratilipiListCursorTuple.getNumberFound());
if (UxModeFilter.isAndroidApp() && request.eventId != null && request.cursor == null) {
Event event = DataAccessorFactory.getDataAccessor().getEvent(request.eventId);
response.setBannerUrl(EventDataUtil.createEventBannerUrl(event));
response.setDescription(event.getDescription());
}
return response;
}
use of com.pratilipi.data.client.PratilipiData in project pratilipi by Pratilipi.
the class PratilipiListV2Api method get.
@Get
public Response get(GetRequest request) throws InsufficientAccessException, UnexpectedServerException {
PratilipiFilter pratilipiFilter = new PratilipiFilter();
pratilipiFilter.setAuthorId(request.authorId);
pratilipiFilter.setLanguage(request.language);
pratilipiFilter.setType(request.type);
pratilipiFilter.setListName(request.listName);
pratilipiFilter.setState(request.state);
pratilipiFilter.setOrderByLastUpdate(request.orderByLastUpdated);
DataListCursorTuple<PratilipiData> pratilipiListCursorTuple = PratilipiDataUtil.getPratilipiDataList(request.searchQuery, request.eventId, pratilipiFilter, request.cursor, request.offset, request.resultCount == null ? 20 : request.resultCount);
// Preparing & returning response object.
Response response = new Response(pratilipiListCursorTuple.getDataList(), pratilipiListCursorTuple.getCursor(), pratilipiListCursorTuple.getNumberFound());
if (UxModeFilter.isAndroidApp() && request.eventId != null && request.cursor == null) {
Event event = DataAccessorFactory.getDataAccessor().getEvent(request.eventId);
response.setBannerUrl(EventDataUtil.createEventBannerUrl(event));
response.setDescription(event.getDescription());
}
return response;
}
use of com.pratilipi.data.client.PratilipiData 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;
}
Aggregations