Search in sources :

Example 41 with InsufficientAccessException

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

the class BlogPostDataUtil method saveBlogPostData.

public static BlogPostData saveBlogPostData(BlogPostData blogPostData) throws InvalidArgumentException, InsufficientAccessException {
    _validateBlogPostDataForSave(blogPostData);
    boolean isNew = blogPostData.getId() == null;
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    BlogPost blogPost = isNew ? dataAccessor.newBlogPost() : dataAccessor.getBlogPost(blogPostData.getId());
    if (isNew && !hasAccessToAddBlogPostData(blogPostData))
        throw new InsufficientAccessException();
    if (!isNew && !hasAccessToUpdateBlogPostData(blogPost, blogPostData))
        throw new InsufficientAccessException();
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), isNew ? AccessType.BLOG_POST_ADD : AccessType.BLOG_POST_UPDATE, blogPost);
    if (// Changing blog id is not allowed
    isNew && blogPostData.hasBlogId())
        blogPost.setBlogId(blogPostData.getBlogId());
    if (blogPostData.hasTitle())
        blogPost.setTitle(blogPostData.getTitle());
    if (blogPostData.hasTitleEn())
        blogPost.setTitleEn(blogPostData.getTitleEn());
    if (blogPostData.hasContent())
        blogPost.setContent(blogPostData.getContent());
    if (blogPostData.hasLanguage())
        blogPost.setLanguage(blogPostData.getLanguage());
    if (blogPostData.hasState())
        blogPost.setState(blogPostData.getState());
    if (isNew) {
        blogPost.setCreatedBy(AccessTokenFilter.getAccessToken().getUserId());
        blogPost.setCreationDate(new Date());
    } else if (blogPost.getCreatedBy() == null) {
        blogPost.setCreatedBy(AccessTokenFilter.getAccessToken().getUserId());
    }
    blogPost.setLastUpdated(new Date());
    blogPost = dataAccessor.createOrUpdateBlogPost(blogPost, auditLog);
    _updateBlogPostPageUrl(blogPost);
    return createBlogPostData(blogPost);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) BlogPost(com.pratilipi.data.type.BlogPost) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Example 42 with InsufficientAccessException

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

the class EventDataUtil method saveEventData.

public static EventData saveEventData(EventData eventData) throws InvalidArgumentException, InsufficientAccessException {
    _validateEventDataForSave(eventData);
    boolean isNew = eventData.getId() == null;
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Event event = isNew ? dataAccessor.newEvent() : dataAccessor.getEvent(eventData.getId());
    if (isNew && !hasAccessToAddEventData(eventData))
        throw new InsufficientAccessException();
    if (!isNew && !hasAccessToUpdateEventData(event, eventData))
        throw new InsufficientAccessException();
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), isNew ? AccessType.EVENT_ADD : AccessType.EVENT_UPDATE, event);
    if (eventData.hasName())
        event.setName(eventData.getName());
    if (eventData.hasNameEn())
        event.setNameEn(eventData.getNameEn());
    if (eventData.hasLanguage())
        event.setLanguage(eventData.getLanguage());
    if (eventData.hasDescription())
        event.setDescription(eventData.getDescription());
    if (eventData.hasPratilipiUrlList()) {
        if (eventData.getPratilipiUrlList() == null) {
            eventData.setPratilipiIdList(null);
        } else {
            List<Long> pratilipiIdList = new LinkedList<>();
            Map<String, Page> map = dataAccessor.getPages(eventData.getPratilipiUrlList());
            for (String pratilipiUrl : eventData.getPratilipiUrlList()) {
                Page page = map.get(pratilipiUrl);
                if (page != null && page.getType() == PageType.PRATILIPI)
                    pratilipiIdList.add(page.getPrimaryContentId());
            }
            event.setPratilipiIdList(pratilipiIdList);
        }
    }
    if (isNew)
        event.setCreationDate(new Date());
    event.setLastUpdated(new Date());
    if (isNew) {
        event = dataAccessor.createOrUpdateEvent(event, auditLog);
        dataAccessor.createOrUpdatePage(_updateEventPageUrl(event));
    } else {
        event = dataAccessor.createOrUpdateEvent(event, _updateEventPageUrl(event), auditLog);
    }
    return createEventData(event, true);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) Event(com.pratilipi.data.type.Event) Page(com.pratilipi.data.type.Page) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog) LinkedList(java.util.LinkedList) Date(java.util.Date)

Example 43 with InsufficientAccessException

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

the class AuthorDataUtil method getAuthorDataList.

public static DataListCursorTuple<AuthorData> getAuthorDataList(String searchQuery, AuthorFilter authorFilter, String cursor, Integer resultCount) throws InsufficientAccessException {
    if (!hasAccessToListAuthorData(authorFilter.getLanguage()))
        throw new InsufficientAccessException();
    // Processing search query
    if (searchQuery != null)
        searchQuery = searchQuery.toLowerCase().trim().replaceAll(",|\\sor\\s", " ").replaceAll("[\\s]+", " OR ");
    DataListCursorTuple<Long> authorIdListCursorTuple = DataAccessorFactory.getSearchAccessor().searchAuthor(searchQuery, authorFilter, cursor, null, resultCount);
    List<AuthorData> authorDataList = createAuthorDataList(authorIdListCursorTuple.getDataList(), true);
    return new DataListCursorTuple<AuthorData>(authorDataList, authorIdListCursorTuple.getCursor());
}
Also used : DataListCursorTuple(com.pratilipi.data.DataListCursorTuple) AuthorData(com.pratilipi.data.client.AuthorData) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException)

Example 44 with InsufficientAccessException

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

the class AuthorDataUtil method saveAuthorImage.

public static String saveAuthorImage(Long authorId, BlobEntry blobEntry) throws InvalidArgumentException, InsufficientAccessException, UnexpectedServerException {
    if (blobEntry.getData() == null || blobEntry.getData().length == 0)
        throw new InvalidArgumentException("Image data is missing.");
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Author author = dataAccessor.getAuthor(authorId);
    if (!hasAccessToUpdateAuthorData(author, null))
        throw new InsufficientAccessException();
    String profileImageName = new Date().getTime() + "";
    BlobAccessor blobAccessor = DataAccessorFactory.getBlobAccessor();
    blobEntry.setName("author/" + authorId + "/images/profile/" + profileImageName);
    blobAccessor.createOrUpdateBlob(blobEntry);
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.AUTHOR_UPDATE, author);
    author.setProfileImage(profileImageName);
    author.setLastUpdated(new Date());
    author = dataAccessor.createOrUpdateAuthor(author, auditLog);
    return createAuthorProfileImageUrl(author);
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) DataAccessor(com.pratilipi.data.DataAccessor) BlobAccessor(com.pratilipi.data.BlobAccessor) Author(com.pratilipi.data.type.Author) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) Date(java.util.Date) AuditLog(com.pratilipi.data.type.AuditLog)

Example 45 with InsufficientAccessException

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

the class AuthorDataUtil method removeAuthorImage.

public static void removeAuthorImage(Long authorId, boolean coverImage, boolean profileImage) throws InsufficientAccessException {
    if (!coverImage && !profileImage)
        return;
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    Author author = dataAccessor.getAuthor(authorId);
    if (!hasAccessToUpdateAuthorData(author, null))
        throw new InsufficientAccessException();
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.AUTHOR_UPDATE, author);
    if (coverImage)
        author.setCoverImage(null);
    if (profileImage)
        author.setProfileImage(null);
    author.setLastUpdated(new Date());
    author = dataAccessor.createOrUpdateAuthor(author, auditLog);
}
Also used : DataAccessor(com.pratilipi.data.DataAccessor) Author(com.pratilipi.data.type.Author) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

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