Search in sources :

Example 26 with AuditLog

use of com.pratilipi.data.type.AuditLog in project pratilipi by Pratilipi.

the class PratilipiDataUtil method updatePratilipiIndex.

public static void updatePratilipiIndex(Long pratilipiId) throws UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    if (pratilipi.isOldContent() && pratilipi.getContentType() == PratilipiContentType.PRATILIPI) {
        BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
        BlobEntry blobEntry = blobAccessor.getBlob(CONTENT_FOLDER + "/" + pratilipiId);
        String index = null;
        Integer pageCount = 0;
        if (blobEntry != null) {
            String content = new String(blobEntry.getData(), Charset.forName("UTF-8"));
            PratilipiContentUtil pratilipiContentUtil = new PratilipiContentUtil(content);
            index = pratilipiContentUtil.generateIndex();
            pageCount = pratilipiContentUtil.getPageCount();
        }
        AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE, pratilipi);
        boolean isChanged = false;
        if ((pratilipi.getIndex() == null && index != null) || (pratilipi.getIndex() != null && index == null) || (pratilipi.getIndex() != null && index != null && !pratilipi.getIndex().equals(index))) {
            pratilipi.setIndex(index);
            isChanged = true;
        }
        if (pratilipi.getPageCount() != pageCount) {
            pratilipi.setPageCount(pageCount);
            isChanged = true;
        }
        if (isChanged)
            pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
    } else {
    // throw new InvalidArgumentException( "Index generation for " + pratilipi.getContentType() + " content type is not yet supported." );
    }
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) BlobEntry(com.pratilipi.data.type.BlobEntry) BlobAccessor(com.pratilipi.data.BlobAccessor) Pratilipi(com.pratilipi.data.type.Pratilipi) UserPratilipi(com.pratilipi.data.type.UserPratilipi) AuditLog(com.pratilipi.data.type.AuditLog)

Example 27 with AuditLog

use of com.pratilipi.data.type.AuditLog in project pratilipi by Pratilipi.

the class PratilipiDocUtil method saveContentPage.

public static PratilipiContentDoc saveContentPage(Long pratilipiId, String chapterId, Integer chapterNo, String chapterTitle, Integer pageNo, String html) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
    // Auth check
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    // Auth Service will be taking care of it - Move it to gamma and prod asap
    if (_shouldAuth()) {
        if (!PratilipiDataUtil.hasAccessToUpdatePratilipiContent(pratilipi))
            throw new InsufficientAccessException();
    }
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    // Doc
    // backfill chapterId
    PratilipiContentDoc pcDoc = _validateChapterIdInContent(pratilipiId);
    // if not present, get a new doc
    if (pcDoc == null)
        pcDoc = docAccessor.newPratilipiContentDoc();
    // Chapter
    Chapter chapter = null;
    // If chapterNo is not null => web case
    if (chapterNo != null) {
        // If chapter already present -> editChapter case -> update the chapter and set lastUpdated and lastUpdatedBy
        chapter = pcDoc.getChapter(chapterNo);
        // If chapter not present -> addChapter case -> add empty chapters (there are 10 chapters already, user is trying to add 15th chapter => so add 4 empty chapters in between)
        if (chapter == null) {
            /* Add Chapter */
            Long aChapterId = new Date().getTime();
            while ((chapter = pcDoc.getChapter(chapterNo)) == null) {
                Chapter ch = pcDoc.addChapter(null);
                ch.setId(generateChapterId(pratilipiId, aChapterId++));
                ch.setLastUpdated(UxModeFilter.getClientType());
            }
        }
    // If chapterId is not null => android case
    } else if (chapterId != null) {
        // If chapter already present -> editChapter case -> update the chapter and set lastUpdated and lastUpdatedBy
        chapter = pcDoc.getChapter(chapterId);
        // If chapter not present -> addChapter case -> add chapter at the end always
        if (chapter == null) {
            chapter = pcDoc.addChapter(chapterTitle);
            chapter.setId(chapterId);
        }
    }
    // Set lastUpdatedDate and lastUpdatedBy -> WEB / ANDROID
    chapter.setLastUpdated(UxModeFilter.getClientType());
    // Setting chapterTitle
    if (chapterTitle != null)
        chapter.setTitle(chapterTitle);
    // Page
    PratilipiContentDoc.Page page = chapter.getPage(pageNo);
    if (page == null)
        page = chapter.addPage(pageNo);
    // Magic
    _setPage(pratilipiId, page, html);
    // Save
    docAccessor.save(pratilipiId, pcDoc);
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE_CONTENT, null);
    auditLog.setEventDataNew(html);
    auditLog.setPrimaryContentId(pratilipiId);
    auditLog.setCreationDate(new Date());
    dataAccessor.createOrUpdateAuditLog(auditLog);
    return 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) Date(java.util.Date) AuditLog(com.pratilipi.data.type.AuditLog)

Example 28 with AuditLog

use of com.pratilipi.data.type.AuditLog in project pratilipi by Pratilipi.

the class PratilipiDocUtil method saveContentPageBatch.

public static PratilipiContentDoc saveContentPageBatch(Long pratilipiId, JsonObject chapters) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    // Auth Service will be taking care of it - Move it to gamma and prod asap
    if (_shouldAuth()) {
        if (!PratilipiDataUtil.hasAccessToUpdatePratilipiContent(pratilipi))
            throw new InsufficientAccessException();
    }
    DocAccessor docAccessor = DataAccessorFactory.getDocAccessor();
    // Doc
    PratilipiContentDoc pcDoc = docAccessor.getPratilipiContentDoc(pratilipiId);
    if (pcDoc == null)
        pcDoc = docAccessor.newPratilipiContentDoc();
    for (Map.Entry<String, JsonElement> chapterEntry : chapters.entrySet()) {
        String chapterId = chapterEntry.getKey();
        // chapterTitle
        String chapterTitle = null;
        if (chapterEntry.getValue().getAsJsonObject().has("chapterTitle"))
            chapterTitle = chapterEntry.getValue().getAsJsonObject().get("chapterTitle").getAsString();
        // content
        String content = null;
        if (chapterEntry.getValue().getAsJsonObject().has("content"))
            content = chapterEntry.getValue().getAsJsonObject().get("content").getAsString();
        // pageNo
        if (!chapterEntry.getValue().getAsJsonObject().has("pageNo"))
            throw new InvalidArgumentException(GenericRequest.ERR_PRATILIPI_PAGE_NO_REQUIRED);
        Integer pageNo = chapterEntry.getValue().getAsJsonObject().get("pageNo").getAsInt();
        // Chapter
        Chapter chapter = pcDoc.getChapter(chapterId);
        if (chapter == null) {
            chapter = pcDoc.addChapter(pcDoc.getChapterCount() + 1, chapterTitle);
            chapter.setId(chapterId);
        } else if (chapterTitle != null) {
            chapter.setTitle(chapterTitle);
        }
        chapter.setLastUpdated(UxModeFilter.getClientType());
        // Page
        PratilipiContentDoc.Page page = chapter.getPage(pageNo);
        if (page == null)
            page = chapter.addPage(pageNo);
        _setPage(pratilipiId, page, content);
    }
    // Save
    docAccessor.save(pratilipiId, pcDoc);
    // AuditLog
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE_CONTENT, null);
    auditLog.setEventDataNew(chapters);
    auditLog.setPrimaryContentId(pratilipiId);
    auditLog.setCreationDate(new Date());
    dataAccessor.createOrUpdateAuditLog(auditLog);
    return pcDoc;
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) DocAccessor(com.pratilipi.data.DocAccessor) Chapter(com.pratilipi.data.type.PratilipiContentDoc.Chapter) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) JsonElement(com.google.gson.JsonElement) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) PratilipiContentDoc(com.pratilipi.data.type.PratilipiContentDoc) Map(java.util.Map) HashMap(java.util.HashMap)

Example 29 with AuditLog

use of com.pratilipi.data.type.AuditLog in project pratilipi by Pratilipi.

the class TestApi method get.

@Get
public static GenericResponse get(GenericRequest request) throws UnexpectedServerException {
    String primaryContentId = 4920371378651136L + "";
    Query<AuditLogEntity> query = ObjectifyService.ofy().load().type(AuditLogEntity.class).filter("PRIMARY_CONTENT_ID", primaryContentId).filter("CREATION_DATE >=", new Date(new Date().getTime() - TimeUnit.DAYS.toMillis(1))).order("CREATION_DATE").chunk(100).limit(100);
    List<AuditLog> responseList = new ArrayList<AuditLog>();
    QueryResultIterator<AuditLogEntity> iterator = query.iterator();
    while (iterator.hasNext()) responseList.add(iterator.next());
    System.out.println(responseList.size());
    return new GenericResponse();
}
Also used : GenericResponse(com.pratilipi.api.shared.GenericResponse) ArrayList(java.util.ArrayList) Date(java.util.Date) AuditLog(com.pratilipi.data.type.AuditLog) AuditLogEntity(com.pratilipi.data.type.gae.AuditLogEntity) Get(com.pratilipi.api.annotation.Get)

Aggregations

AuditLog (com.pratilipi.data.type.AuditLog)29 DataAccessor (com.pratilipi.data.DataAccessor)28 Date (java.util.Date)19 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)17 UserPratilipi (com.pratilipi.data.type.UserPratilipi)15 Pratilipi (com.pratilipi.data.type.Pratilipi)10 User (com.pratilipi.data.type.User)8 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)7 DocAccessor (com.pratilipi.data.DocAccessor)5 BlobAccessor (com.pratilipi.data.BlobAccessor)4 JsonObject (com.google.gson.JsonObject)3 UserData (com.pratilipi.data.client.UserData)3 Comment (com.pratilipi.data.type.Comment)3 PratilipiContentDoc (com.pratilipi.data.type.PratilipiContentDoc)3 Chapter (com.pratilipi.data.type.PratilipiContentDoc.Chapter)3 HashMap (java.util.HashMap)3 Get (com.pratilipi.api.annotation.Get)2 GenericResponse (com.pratilipi.api.shared.GenericResponse)2 AccessToken (com.pratilipi.data.type.AccessToken)2 BlobEntry (com.pratilipi.data.type.BlobEntry)2