Search in sources :

Example 11 with InsufficientAccessException

use of com.pratilipi.common.exception.InsufficientAccessException in project pratilipi by Pratilipi.

the class PratilipiDataUtil method updatePratilipiContent.

public static int updatePratilipiContent(long pratilipiId, int pageNo, PratilipiContentType contentType, Object pageContent, boolean insertNew) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    if (!hasAccessToUpdatePratilipiContent(pratilipi))
        throw new InsufficientAccessException();
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE, pratilipi);
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
    if (contentType == PratilipiContentType.PRATILIPI) {
        BlobEntry blobEntry = blobAccessor.getBlob(CONTENT_FOLDER + "/" + pratilipiId);
        if (blobEntry == null) {
            blobEntry = blobAccessor.newBlob(CONTENT_FOLDER + "/" + pratilipiId);
            blobEntry.setData("&nbsp".getBytes(Charset.forName("UTF-8")));
            blobEntry.setMimeType("text/html");
        }
        String content = new String(blobEntry.getData(), Charset.forName("UTF-8"));
        PratilipiContentUtil pratilipiContentUtil = new PratilipiContentUtil(content);
        content = pratilipiContentUtil.updateContent(pageNo, (String) pageContent, insertNew);
        int pageCount = pratilipiContentUtil.getPageCount();
        if (content.isEmpty()) {
            content = "&nbsp";
            pageCount = 1;
        }
        blobEntry.setData(content.getBytes(Charset.forName("UTF-8")));
        blobAccessor.createOrUpdateBlob(blobEntry);
        pratilipi.setPageCount(pageCount);
        if (insertNew)
            auditLog.setEventComment("Added new page " + pageNo + " in Pratilpi content.");
        else if (!((String) pageContent).isEmpty())
            auditLog.setEventComment("Updated page " + pageNo + " in Pratilpi content.");
        else
            auditLog.setEventComment("Deleted page " + pageNo + " in Pratilpi content.");
    } else if (contentType == PratilipiContentType.IMAGE) {
        BlobEntry blobEntry = (BlobEntry) pageContent;
        blobEntry.setName(IMAGE_CONTENT_FOLDER + "/" + pratilipiId + "/" + pageNo);
        blobAccessor.createOrUpdateBlob(blobEntry);
        if (pageNo > (int) pratilipi.getPageCount())
            pratilipi.setPageCount(pageNo);
        auditLog.setEventComment("Uploaded page " + pageNo + " in Image content.");
    } else {
        throw new InvalidArgumentException(contentType + " content type is not yet supported.");
    }
    pratilipi.setLastUpdated(new Date());
    pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
    return pratilipi.getPageCount();
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) DataAccessor(com.pratilipi.data.DataAccessor) BlobEntry(com.pratilipi.data.type.BlobEntry) PratilipiContentUtil(com.pratilipi.common.util.PratilipiContentUtil) BlobAccessor(com.pratilipi.data.BlobAccessor) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Example 12 with InsufficientAccessException

use of com.pratilipi.common.exception.InsufficientAccessException in project pratilipi by Pratilipi.

the class PratilipiDataUtil method getPratilipiDataList.

public static DataListCursorTuple<PratilipiData> getPratilipiDataList(String searchQuery, Long eventId, PratilipiFilter pratilipiFilter, String cursor, Integer offset, Integer resultCount) throws InsufficientAccessException, UnexpectedServerException {
    if (!hasAccessToListPratilipiData(pratilipiFilter))
        throw new InsufficientAccessException();
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    // Processing search query
    if (searchQuery != null)
        searchQuery = searchQuery.toLowerCase().trim().replaceAll("\"|\\\\|\\(|\\)|:|,|\\sor\\s", " ").trim().replaceAll("[\\s]+", " OR ");
    // Fetching Pratilipi id list from DataStore/SearchIndex/Event
    DataListCursorTuple<Long> pratilipiIdListCursorTuple = null;
    if (eventId != null) {
        List<Long> pratilipiIdList = dataAccessor.getEvent(eventId).getPratilipiIdList();
        // move read content to end of the list.
        List<Long> contentsReadList = UserPratilipiDataUtil.getContentsReadList(AccessTokenFilter.getAccessToken().getUserId());
        // move all read contents to bottom of the list.
        if (contentsReadList != null) {
            // Find all read contents in the list
            contentsReadList.retainAll(pratilipiIdList);
            // Remove already read content from the list
            pratilipiIdList.removeAll(contentsReadList);
            // Append read content at end of the list
            pratilipiIdList.addAll(contentsReadList);
        }
        offset = (cursor == null ? 0 : Integer.parseInt(cursor)) + (offset == null || offset < 0 ? 0 : offset);
        offset = Math.min(offset, pratilipiIdList.size());
        resultCount = resultCount == null || resultCount > pratilipiIdList.size() - offset ? pratilipiIdList.size() - offset : resultCount;
        pratilipiIdListCursorTuple = new DataListCursorTuple<Long>(pratilipiIdList.subList(offset, offset + resultCount), offset + resultCount + "", (long) pratilipiIdList.size());
    } else if (pratilipiFilter.getListName() == null && pratilipiFilter.getState() == PratilipiState.PUBLISHED) {
        pratilipiIdListCursorTuple = DataAccessorFactory.getSearchAccessor().searchPratilipi(searchQuery, pratilipiFilter, cursor, offset, resultCount);
    } else {
        pratilipiIdListCursorTuple = dataAccessor.getPratilipiIdList(pratilipiFilter, cursor, offset, resultCount);
    }
    // Creating PratilipiData list from Pratilipi id list
    List<PratilipiData> pratilipiDataList = createPratilipiDataList(pratilipiIdListCursorTuple.getDataList(), pratilipiFilter.getAuthorId() == null, false);
    // Creating response object
    return new DataListCursorTuple<PratilipiData>(pratilipiDataList, pratilipiIdListCursorTuple.getCursor(), pratilipiIdListCursorTuple.getNumberFound());
}
Also used : DataListCursorTuple(com.pratilipi.data.DataListCursorTuple) DataAccessor(com.pratilipi.data.DataAccessor) PratilipiData(com.pratilipi.data.client.PratilipiData) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException)

Example 13 with InsufficientAccessException

use of com.pratilipi.common.exception.InsufficientAccessException 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 14 with InsufficientAccessException

use of com.pratilipi.common.exception.InsufficientAccessException 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 15 with InsufficientAccessException

use of com.pratilipi.common.exception.InsufficientAccessException in project pratilipi by Pratilipi.

the class PratilipiDocUtil method addContentChapter.

public static JsonArray addContentChapter(Long pratilipiId, Integer chapterNo) throws InsufficientAccessException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    if (!PratilipiDataUtil.hasAccessToUpdatePratilipiContent(pratilipi))
        throw new InsufficientAccessException();
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    PratilipiContentDoc pcDoc = docAccessor.getPratilipiContentDoc(pratilipiId);
    if (pcDoc == null)
        pcDoc = docAccessor.newPratilipiContentDoc();
    pcDoc.addChapter(chapterNo, null);
    docAccessor.save(pratilipiId, pcDoc);
    return pcDoc.getIndex();
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) 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

InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)48 DataAccessor (com.pratilipi.data.DataAccessor)37 Date (java.util.Date)21 AuditLog (com.pratilipi.data.type.AuditLog)19 UserPratilipi (com.pratilipi.data.type.UserPratilipi)18 Pratilipi (com.pratilipi.data.type.Pratilipi)14 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)11 BlobAccessor (com.pratilipi.data.BlobAccessor)7 DocAccessor (com.pratilipi.data.DocAccessor)7 PratilipiContentDoc (com.pratilipi.data.type.PratilipiContentDoc)7 JsonObject (com.google.gson.JsonObject)5 GenericResponse (com.pratilipi.api.shared.GenericResponse)5 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)5 AccessToken (com.pratilipi.data.type.AccessToken)5 Author (com.pratilipi.data.type.Author)5 Gson (com.google.gson.Gson)4 UserData (com.pratilipi.data.client.UserData)4 Chapter (com.pratilipi.data.type.PratilipiContentDoc.Chapter)4 User (com.pratilipi.data.type.User)4 HashMap (java.util.HashMap)4