Search in sources :

Example 1 with Chapter

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));
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) Chapter(com.pratilipi.data.type.PratilipiContentDoc.Chapter) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) PratilipiContentDoc(com.pratilipi.data.type.PratilipiContentDoc)

Example 2 with Chapter

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);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) BlobEntry(com.pratilipi.data.type.BlobEntry) Chapter(com.pratilipi.data.type.PratilipiContentDoc.Chapter) JsonObject(com.google.gson.JsonObject) Page(com.pratilipi.data.type.Page) AlignmentType(com.pratilipi.data.type.PratilipiContentDoc.AlignmentType) UnexpectedServerException(com.pratilipi.common.exception.UnexpectedServerException) PageletType(com.pratilipi.data.type.PratilipiContentDoc.PageletType) BlobAccessor(com.pratilipi.data.BlobAccessor) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) JsonObject(com.google.gson.JsonObject) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) PratilipiContentDoc(com.pratilipi.data.type.PratilipiContentDoc)

Example 3 with Chapter

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));
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) Chapter(com.pratilipi.data.type.PratilipiContentDoc.Chapter) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) PratilipiContentDoc(com.pratilipi.data.type.PratilipiContentDoc)

Example 4 with Chapter

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;
}
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)

Example 5 with Chapter

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);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) Chapter(com.pratilipi.data.type.PratilipiContentDoc.Chapter) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) PratilipiContentDoc(com.pratilipi.data.type.PratilipiContentDoc)

Aggregations

DataAccessor (com.pratilipi.data.DataAccessor)7 Pratilipi (com.pratilipi.data.type.Pratilipi)7 PratilipiContentDoc (com.pratilipi.data.type.PratilipiContentDoc)7 Chapter (com.pratilipi.data.type.PratilipiContentDoc.Chapter)7 DocAccessor (com.pratilipi.data.DocAccessor)6 UserPratilipi (com.pratilipi.data.type.UserPratilipi)6 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)4 HashMap (java.util.HashMap)3 JsonObject (com.google.gson.JsonObject)2 Gson (com.google.gson.Gson)1 JsonElement (com.google.gson.JsonElement)1 PratilipiContentIndexApi (com.pratilipi.api.impl.pratilipi.PratilipiContentIndexApi)1 PratilipiContentV1Api (com.pratilipi.api.impl.pratilipi.PratilipiContentV1Api)1 PratilipiContentV2Api (com.pratilipi.api.impl.pratilipi.PratilipiContentV2Api)1 PratilipiContentV3Api (com.pratilipi.api.impl.pratilipi.PratilipiContentV3Api)1 PratilipiV2Api (com.pratilipi.api.impl.pratilipi.PratilipiV2Api)1 UserPratilipiApi (com.pratilipi.api.impl.userpratilipi.UserPratilipiApi)1 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)1 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)1 BlobAccessor (com.pratilipi.data.BlobAccessor)1