Search in sources :

Example 6 with DataAccessor

use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.

the class PratilipiDataUtil method _updatePratilipiPageUrl.

public static Page _updatePratilipiPageUrl(Pratilipi pratilipi) {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Page pratilipiPage = dataAccessor.getPage(PageType.PRATILIPI, pratilipi.getId());
    if (pratilipi.getState() == PratilipiState.DELETED) {
        if (pratilipiPage != null)
            dataAccessor.deletePage(pratilipiPage);
        return null;
    }
    boolean isNew = pratilipiPage == null;
    if (isNew) {
        pratilipiPage = dataAccessor.newPage();
        pratilipiPage.setType(PageType.PRATILIPI);
        pratilipiPage.setUri(PageType.PRATILIPI.getUrlPrefix() + pratilipi.getId());
        pratilipiPage.setPrimaryContentId(pratilipi.getId());
        pratilipiPage.setCreationDate(new Date());
    }
    String uriPrifix = null;
    if (pratilipi.getAuthorId() != null) {
        Page authorPage = dataAccessor.getPage(PageType.AUTHOR, pratilipi.getAuthorId());
        if (authorPage.getUriAlias() != null)
            uriPrifix = authorPage.getUriAlias() + "/";
    }
    if (uriPrifix == null) {
        if (isNew) {
        // Do NOT return.
        } else if (pratilipiPage.getUriAlias() == null) {
            // Do Nothing.
            return null;
        } else {
            logger.log(Level.INFO, "Clearing Pratilipi Page Url: '" + pratilipiPage.getUriAlias() + "' -> 'null'");
            pratilipiPage.setUriAlias(null);
        }
    } else if (pratilipiPage.getUriAlias() == null) {
        String uriAlias = UriAliasUtil.generateUriAlias(pratilipiPage.getUriAlias(), uriPrifix, pratilipi.getTitleEn() == null ? pratilipi.getTitle() : pratilipi.getTitleEn());
        if (isNew && uriAlias == null) {
        // Do NOT return.
        //			} else if( uriAlias == pratilipiPage.getUriAlias()
        //					|| ( uriAlias != null && uriAlias.equals( pratilipiPage.getUriAlias() ) )
        //					|| ( pratilipiPage.getUriAlias() != null && pratilipiPage.getUriAlias().equals( uriAlias ) ) ) {
        //				return null; // Do Nothing.
        } else {
            logger.log(Level.INFO, "Updating Pratilipi Page Url: '" + pratilipiPage.getUriAlias() + "' -> '" + uriAlias + "'");
            pratilipiPage.setUriAlias(uriAlias);
        }
    } else {
        // Do Nothing.
        return null;
    }
    return pratilipiPage;
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) Page(com.pratilipi.data.type.Page) Date(java.util.Date)

Example 7 with DataAccessor

use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.

the class PratilipiDataUtil method savePratilipiData.

public static PratilipiData savePratilipiData(PratilipiData pratilipiData) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
    _validatePratilipiDataForSave(pratilipiData);
    boolean isNew = pratilipiData.getId() == null;
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = isNew ? dataAccessor.newPratilipi() : dataAccessor.getPratilipi(pratilipiData.getId());
    if (isNew && !hasAccessToAddPratilipiData(pratilipiData))
        throw new InsufficientAccessException();
    if (!isNew && !hasAccessToUpdatePratilipiData(pratilipi, pratilipiData))
        throw new InsufficientAccessException();
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), isNew ? AccessType.PRATILIPI_ADD : AccessType.PRATILIPI_UPDATE, pratilipi);
    if (pratilipiData.hasTitle())
        pratilipi.setTitle(pratilipiData.getTitle());
    if (pratilipiData.hasTitleEn())
        pratilipi.setTitleEn(pratilipiData.getTitleEn());
    if (pratilipiData.hasLanguage())
        pratilipi.setLanguage(pratilipiData.getLanguage());
    // Do NOT update Author for existing content pieces.
    if (isNew && pratilipiData.hasAuthorId())
        pratilipi.setAuthorId(pratilipiData.getAuthorId());
    if (pratilipiData.hasSummary())
        pratilipi.setSummary(pratilipiData.getSummary());
    if (pratilipiData.hasType())
        pratilipi.setType(pratilipiData.getType());
    if (pratilipiData.hasContentType())
        pratilipi.setContentType(pratilipiData.getContentType());
    else if (isNew)
        pratilipi.setContentType(PratilipiContentType.PRATILIPI);
    if (pratilipiData.hasState())
        pratilipi.setState(pratilipiData.getState());
    else if (isNew)
        pratilipi.setState(PratilipiState.DRAFTED);
    if (pratilipiData.hasOldContentFlag())
        pratilipi.setOldContent(pratilipiData.isOldContent());
    if (isNew)
        pratilipi.setListingDate(new Date());
    pratilipi.setLastUpdated(new Date());
    if (isNew) {
        pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
        dataAccessor.createOrUpdatePage(_updatePratilipiPageUrl(pratilipi));
    } else {
        pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, _updatePratilipiPageUrl(pratilipi), auditLog);
    }
    return createPratilipiData(pratilipi, dataAccessor.getAuthor(pratilipi.getAuthorId()), hasAccessToReadPratilipiMetaData(pratilipi));
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) 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 8 with DataAccessor

use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.

the class PratilipiDataUtil method hasAccessToListPratilipiData.

public static boolean hasAccessToListPratilipiData(PratilipiFilter pratilipiFilter) {
    if (pratilipiFilter.getState() == PratilipiState.PUBLISHED)
        return true;
    // Case 2: User with PRATILIPI_LIST access can list Pratilipis in any State.
    AccessToken accessToken = AccessTokenFilter.getAccessToken();
    if (UserAccessUtil.hasUserAccess(accessToken.getUserId(), pratilipiFilter.getLanguage(), AccessType.PRATILIPI_LIST))
        return true;
    if (pratilipiFilter.getState() == PratilipiState.DELETED)
        return false;
    if (pratilipiFilter.getAuthorId() == null)
        return false;
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Author author = dataAccessor.getAuthor(pratilipiFilter.getAuthorId());
    if (author == null)
        return false;
    return accessToken.getUserId().equals(author.getUserId());
}
Also used : AccessToken(com.pratilipi.data.type.AccessToken) DataAccessor(com.pratilipi.data.DataAccessor) Author(com.pratilipi.data.type.Author)

Example 9 with DataAccessor

use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.

the class PratilipiDataUtil method savePratilipiCover.

public static String savePratilipiCover(Long pratilipiId, BlobEntry blobEntry) throws InsufficientAccessException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Pratilipi pratilipi = dataAccessor.getPratilipi(pratilipiId);
    if (!hasAccessToUpdatePratilipiData(pratilipi, null))
        throw new InsufficientAccessException();
    String coverImageName = new Date().getTime() + "";
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
    blobEntry.setName("pratilipi/" + pratilipiId + "/images/" + coverImageName);
    blobAccessor.createOrUpdateBlob(blobEntry);
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.PRATILIPI_UPDATE, pratilipi);
    pratilipi.setCoverImage(coverImageName);
    pratilipi.setLastUpdated(new Date());
    pratilipi = dataAccessor.createOrUpdatePratilipi(pratilipi, auditLog);
    return createPratilipiCoverUrl(pratilipi);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) BlobAccessor(com.pratilipi.data.BlobAccessor) UserPratilipi(com.pratilipi.data.type.UserPratilipi) Pratilipi(com.pratilipi.data.type.Pratilipi) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) Date(java.util.Date) AuditLog(com.pratilipi.data.type.AuditLog)

Example 10 with DataAccessor

use of com.pratilipi.data.DataAccessor in project pratilipi by Pratilipi.

the class CommentDataUtil method saveCommentData.

public static CommentData saveCommentData(CommentData commentData) throws InvalidArgumentException, InsufficientAccessException {
    _validateCommentDataForSave(commentData);
    boolean isNew = commentData.getId() == null;
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Comment comment = isNew ? dataAccessor.newComment() : dataAccessor.getComment(commentData.getId());
    if (isNew && !hasAccessToAddCommentData(commentData))
        throw new InsufficientAccessException();
    if (!isNew && !hasAccessToUpdateCommentData(comment))
        throw new InsufficientAccessException();
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), isNew ? AccessType.COMMENT_ADD : AccessType.COMMENT_UPDATE, comment);
    if (isNew) {
        comment.setUserId(commentData.getUserId());
        comment.setParentType(commentData.getParentType());
        comment.setParentId(commentData.getParentId());
        if (commentData.getParentType() == CommentParentType.REVIEW) {
            UserPratilipi userPratilipi = dataAccessor.getUserPratilipi(commentData.getParentId());
            comment.setReferenceType(ReferenceType.PRATILIPI);
            comment.setReferenceId(userPratilipi.getPratilipiId());
        }
        comment.setCreationDate(new Date());
    } else {
        comment.setLastUpdated(new Date());
    }
    if (commentData.hasContent())
        comment.setContent(commentData.getContent());
    if (commentData.hasState())
        comment.setState(commentData.getState());
    comment = dataAccessor.createOrUpdateComment(comment, auditLog);
    User user = dataAccessor.getUser(commentData.getUserId());
    UserData userData = UserDataUtil.createUserData(user);
    commentData = createCommentData(comment);
    commentData.setUser(userData);
    return commentData;
}
Also used : Comment(com.pratilipi.data.type.Comment) UserPratilipi(com.pratilipi.data.type.UserPratilipi) User(com.pratilipi.data.type.User) UserData(com.pratilipi.data.client.UserData) DataAccessor(com.pratilipi.data.DataAccessor) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Aggregations

DataAccessor (com.pratilipi.data.DataAccessor)162 Date (java.util.Date)53 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)37 Pratilipi (com.pratilipi.data.type.Pratilipi)34 Author (com.pratilipi.data.type.Author)33 AuditLog (com.pratilipi.data.type.AuditLog)32 UserPratilipi (com.pratilipi.data.type.UserPratilipi)32 ArrayList (java.util.ArrayList)30 Page (com.pratilipi.data.type.Page)28 HashMap (java.util.HashMap)25 GenericResponse (com.pratilipi.api.shared.GenericResponse)24 DocAccessor (com.pratilipi.data.DocAccessor)22 User (com.pratilipi.data.type.User)22 Get (com.pratilipi.api.annotation.Get)20 InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)19 PratilipiData (com.pratilipi.data.client.PratilipiData)15 UserData (com.pratilipi.data.client.UserData)14 BlobAccessor (com.pratilipi.data.BlobAccessor)13 Email (com.pratilipi.data.type.Email)13 JsonObject (com.google.gson.JsonObject)12