Search in sources :

Example 1 with Pratilipi

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;
}
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 2 with Pratilipi

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

Example 3 with Pratilipi

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);
}
Also used : PratilipiEntity(com.pratilipi.data.type.gae.PratilipiEntity) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) BlobEntry(com.pratilipi.data.type.BlobEntry) BlobAccessor(com.pratilipi.data.BlobAccessor) Pratilipi(com.pratilipi.data.type.Pratilipi) SimpleDateFormat(java.text.SimpleDateFormat)

Example 4 with Pratilipi

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));
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) Author(com.pratilipi.data.type.Author) Pratilipi(com.pratilipi.data.type.Pratilipi) Get(com.pratilipi.api.annotation.Get)

Example 5 with Pratilipi

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;
}
Also used : PratilipiEntity(com.pratilipi.data.type.gae.PratilipiEntity) UserPratilipiEntity(com.pratilipi.data.type.gae.UserPratilipiEntity) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi)

Aggregations

Pratilipi (com.pratilipi.data.type.Pratilipi)45 DataAccessor (com.pratilipi.data.DataAccessor)36 UserPratilipi (com.pratilipi.data.type.UserPratilipi)33 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)17 PratilipiContentDoc (com.pratilipi.data.type.PratilipiContentDoc)14 DocAccessor (com.pratilipi.data.DocAccessor)13 AuditLog (com.pratilipi.data.type.AuditLog)10 Author (com.pratilipi.data.type.Author)9 Page (com.pratilipi.data.type.Page)9 Chapter (com.pratilipi.data.type.PratilipiContentDoc.Chapter)9 Date (java.util.Date)9 BlobEntry (com.pratilipi.data.type.BlobEntry)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)6 BlobAccessor (com.pratilipi.data.BlobAccessor)6 Get (com.pratilipi.api.annotation.Get)5 GenericResponse (com.pratilipi.api.shared.GenericResponse)5 PratilipiData (com.pratilipi.data.client.PratilipiData)5 PratilipiEntity (com.pratilipi.data.type.gae.PratilipiEntity)4