Search in sources :

Example 6 with PratilipiData

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;
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) PratilipiData(com.pratilipi.data.client.PratilipiData) AccessToken(com.pratilipi.data.type.AccessToken) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog)

Example 7 with PratilipiData

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;
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) ArrayList(java.util.ArrayList) Page(com.pratilipi.data.type.Page) LinkedList(java.util.LinkedList) UserPratilipi(com.pratilipi.data.type.UserPratilipi) PratilipiData(com.pratilipi.data.client.PratilipiData) AuthorData(com.pratilipi.data.client.AuthorData) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi)

Example 8 with PratilipiData

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;
}
Also used : GenericResponse(com.pratilipi.api.shared.GenericResponse) PratilipiFilter(com.pratilipi.common.util.PratilipiFilter) PratilipiData(com.pratilipi.data.client.PratilipiData) DocAccessor(com.pratilipi.data.DocAccessor) Event(com.pratilipi.data.type.Event) PratilipiContentDoc(com.pratilipi.data.type.PratilipiContentDoc) Get(com.pratilipi.api.annotation.Get)

Example 9 with PratilipiData

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;
}
Also used : PratilipiFilter(com.pratilipi.common.util.PratilipiFilter) PratilipiData(com.pratilipi.data.client.PratilipiData) Event(com.pratilipi.data.type.Event) Get(com.pratilipi.api.annotation.Get)

Example 10 with PratilipiData

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;
}
Also used : HashMap(java.util.HashMap) DataAccessor(com.pratilipi.data.DataAccessor) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) PratilipiContentV2Api(com.pratilipi.api.impl.pratilipi.PratilipiContentV2Api) PratilipiV2Api(com.pratilipi.api.impl.pratilipi.PratilipiV2Api) PratilipiContentDoc(com.pratilipi.data.type.PratilipiContentDoc) Chapter(com.pratilipi.data.type.PratilipiContentDoc.Chapter) PratilipiContentV3Api(com.pratilipi.api.impl.pratilipi.PratilipiContentV3Api) HttpServletResponse(javax.servlet.http.HttpServletResponse) PratilipiContentIndexApi(com.pratilipi.api.impl.pratilipi.PratilipiContentIndexApi) PratilipiContentV1Api(com.pratilipi.api.impl.pratilipi.PratilipiContentV1Api) UserPratilipiApi(com.pratilipi.api.impl.userpratilipi.UserPratilipiApi) PratilipiData(com.pratilipi.data.client.PratilipiData) JsonObject(com.google.gson.JsonObject) Pratilipi(com.pratilipi.data.type.Pratilipi)

Aggregations

PratilipiData (com.pratilipi.data.client.PratilipiData)20 DataAccessor (com.pratilipi.data.DataAccessor)15 HashMap (java.util.HashMap)9 Get (com.pratilipi.api.annotation.Get)5 UserPratilipiData (com.pratilipi.data.client.UserPratilipiData)5 Pratilipi (com.pratilipi.data.type.Pratilipi)5 LinkedList (java.util.LinkedList)5 Gson (com.google.gson.Gson)4 GenericResponse (com.pratilipi.api.shared.GenericResponse)4 PratilipiFilter (com.pratilipi.common.util.PratilipiFilter)4 PratilipiContentDoc (com.pratilipi.data.type.PratilipiContentDoc)4 JsonObject (com.google.gson.JsonObject)3 DocAccessor (com.pratilipi.data.DocAccessor)3 UserData (com.pratilipi.data.client.UserData)3 Author (com.pratilipi.data.type.Author)3 UserPratilipi (com.pratilipi.data.type.UserPratilipi)3 ArrayList (java.util.ArrayList)3 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)2 CommentData (com.pratilipi.data.client.CommentData)2 Event (com.pratilipi.data.type.Event)2