Search in sources :

Example 11 with BlogPostInfo

use of org.alfresco.service.cmr.blog.BlogPostInfo in project alfresco-remote-api by Alfresco.

the class BlogPostsPost method executeImpl.

@Override
protected Map<String, Object> executeImpl(SiteInfo site, NodeRef nodeRef, BlogPostInfo blog, WebScriptRequest req, JSONObject json, Status status, Cache cache) {
    Map<String, Object> model = new HashMap<String, Object>();
    // that the Container is a Tag Scope
    if (site == null && nodeRef != null) {
        ensureTagScope(nodeRef);
    }
    // Have the Blog Post created
    JsonParams jsonPostParams = parsePostParams(json);
    BlogPostInfo post = createBlogPost(jsonPostParams, site, nodeRef);
    Map<String, Object> blogPostData = BlogPostLibJs.getBlogPostData(post.getNodeRef(), services);
    model.put(ITEM, blogPostData);
    model.put(EXTERNAL_BLOG_CONFIG, BlogPostLibJs.hasExternalBlogConfiguration(nodeRef, services));
    boolean isDraft = blogPostData.get("isDraft") != null && ((Boolean) blogPostData.get("isDraft")).booleanValue();
    if (jsonPostParams.getSite() != null && jsonPostParams.getContainer() != null && jsonPostParams.getPage() != null && !isDraft) {
        addActivityEntry("created", post, site, req, json, post.getNodeRef());
    }
    return model;
}
Also used : HashMap(java.util.HashMap) JSONObject(org.json.simple.JSONObject) BlogPostInfo(org.alfresco.service.cmr.blog.BlogPostInfo)

Example 12 with BlogPostInfo

use of org.alfresco.service.cmr.blog.BlogPostInfo in project alfresco-repository by Alfresco.

the class BlogServiceImpl method findBlogPosts.

@Override
public PagingResults<BlogPostInfo> findBlogPosts(final NodeRef blogContainerNode, final RangedDateProperty dateRange, final String tag, final PagingRequest pagingReq) {
    StringBuilder luceneQuery = new StringBuilder();
    luceneQuery.append("+TYPE:\"").append(ContentModel.TYPE_CONTENT).append("\" ").append("+PARENT:\"").append(blogContainerNode.toString()).append("\" ");
    if (tag != null && !tag.trim().isEmpty()) {
        luceneQuery.append("+PATH:\"/cm:taggable/cm:").append(ISO9075.encode(tag)).append("/member\"");
    }
    if (dateRange != null) {
        luceneQuery.append(createDateRangeQuery(dateRange.getFromDate(), dateRange.getToDate(), dateRange.getDateProperty()));
    }
    SearchParameters sp = new SearchParameters();
    sp.addStore(blogContainerNode.getStoreRef());
    sp.setLanguage(SearchService.LANGUAGE_LUCENE);
    sp.setQuery(luceneQuery.toString());
    sp.addSort(ContentModel.PROP_PUBLISHED.toString(), false);
    sp.setMaxItems(pagingReq.getMaxItems() * MIN_NUMBER_OF_PAGES_FOR_THE_USER_TO_LOOP_THROUGH);
    sp.setSkipCount(pagingReq.getSkipCount());
    ResultSet luceneResults = null;
    PagingResults<BlogPostInfo> results = null;
    try {
        luceneResults = searchService.query(sp);
        final ResultSet finalLuceneResults = luceneResults;
        final List<NodeRef> nodeRefs = finalLuceneResults.getNodeRefs().subList(0, min(pagingReq.getMaxItems(), finalLuceneResults.length()));
        results = new PagingResults<BlogPostInfo>() {

            @Override
            public List<BlogPostInfo> getPage() {
                List<BlogPostInfo> blogPostInfos = new ArrayList<BlogPostInfo>(nodeRefs.size());
                for (NodeRef nodeRef : nodeRefs) {
                    String postName = (String) nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
                    blogPostInfos.add(new BlogPostInfoImpl(nodeRef, blogContainerNode, postName));
                }
                return blogPostInfos;
            }

            @Override
            public String getQueryExecutionId() {
                return null;
            }

            @Override
            public Pair<Integer, Integer> getTotalResultCount() {
                long totalResultCount = finalLuceneResults.getNumberFound();
                /*if (finalLuceneResults.hasMore()){
                        totalResultCount++;
                    }*/
                return new Pair<Integer, Integer>((int) totalResultCount, (int) totalResultCount);
            }

            @Override
            public boolean hasMoreItems() {
                return finalLuceneResults.length() > pagingReq.getMaxItems();
            }
        };
    } finally {
        if (luceneResults != null)
            luceneResults.close();
    }
    return results;
}
Also used : BlogPostInfo(org.alfresco.service.cmr.blog.BlogPostInfo) SearchParameters(org.alfresco.service.cmr.search.SearchParameters) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ResultSet(org.alfresco.service.cmr.search.ResultSet) List(java.util.List) ArrayList(java.util.ArrayList) Pair(org.alfresco.util.Pair)

Example 13 with BlogPostInfo

use of org.alfresco.service.cmr.blog.BlogPostInfo in project alfresco-repository by Alfresco.

the class BlogServiceImpl method createBlogPost.

@Override
public BlogPostInfo createBlogPost(NodeRef blogContainerNode, String blogTitle, String blogContent, boolean isDraft) {
    final String nodeName = getUniqueChildName(blogContainerNode, "post");
    // we simply create a new file inside the blog folder
    Map<QName, Serializable> nodeProps = new HashMap<QName, Serializable>();
    nodeProps.put(ContentModel.PROP_NAME, nodeName);
    nodeProps.put(ContentModel.PROP_TITLE, blogTitle);
    QName assocName = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, nodeName);
    ChildAssociationRef postNode = null;
    try {
        postNode = nodeService.createNode(blogContainerNode, ContentModel.ASSOC_CONTAINS, assocName, ContentModel.TYPE_CONTENT, nodeProps);
    } catch (DuplicateChildNodeNameException e) {
        // We have to retry the operation.
        throw new ConcurrencyFailureException("Blog post name already used: " + nodeName);
    }
    ContentWriter writer = contentService.getWriter(postNode.getChildRef(), ContentModel.PROP_CONTENT, true);
    // Blog posts are always HTML (based on the JavaScript this class replaces.)
    writer.setMimetype(MimetypeMap.MIMETYPE_HTML);
    writer.setEncoding("UTF-8");
    writer.putContent(blogContent);
    if (isDraft) {
        // Comment from the old JavaScript:
        // disable permission inheritance. The result is that only the creator will have access to the draft
        NodeRef draft = postNode.getChildRef();
        permissionService.setInheritParentPermissions(draft, false);
        // MNT-12082: give permissions to the post creator. He should be able to comment in his post's
        // forumFolder and commentsFolder, where owner is System user
        String creator = (String) nodeService.getProperty(draft, ContentModel.PROP_CREATOR);
        permissionService.setPermission(draft, creator, permissionService.getAllPermission(), true);
    } else {
        setOrUpdateReleasedAndUpdatedDates(postNode.getChildRef());
    }
    BlogPostInfo post = new BlogPostInfoImpl(postNode.getChildRef(), blogContainerNode, nodeName);
    post.setTitle(blogTitle);
    return post;
}
Also used : DuplicateChildNodeNameException(org.alfresco.service.cmr.repository.DuplicateChildNodeNameException) NodeRef(org.alfresco.service.cmr.repository.NodeRef) Serializable(java.io.Serializable) ContentWriter(org.alfresco.service.cmr.repository.ContentWriter) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) ChildAssociationRef(org.alfresco.service.cmr.repository.ChildAssociationRef) BlogPostInfo(org.alfresco.service.cmr.blog.BlogPostInfo)

Example 14 with BlogPostInfo

use of org.alfresco.service.cmr.blog.BlogPostInfo in project alfresco-repository by Alfresco.

the class BlogServiceImplTest method findBlogPostsByPublishedDate.

/**
 * This test method uses the eventually consistent find*() method and so may fail if Lucene is disabled.
 */
@Category(RedundantTests.class)
@Test
public void findBlogPostsByPublishedDate() throws Exception {
    final List<String> tags = Arrays.asList(new String[] { "hello", "goodbye" });
    // Going to set some specific published dates on these blog posts & query by date.
    final Calendar cal = Calendar.getInstance();
    cal.set(1971, 6, 15);
    final Date _1971 = cal.getTime();
    cal.set(1975, 0, 1);
    final Date _1975 = cal.getTime();
    cal.set(1980, 0, 1);
    final Date _1980 = cal.getTime();
    cal.set(1981, 0, 1);
    final Date _1981 = cal.getTime();
    cal.set(1985, 6, 15);
    final Date _1985 = cal.getTime();
    cal.set(1991, 6, 15);
    final Date _1991 = cal.getTime();
    final Map<Integer, NodeRef> blogPosts = TRANSACTION_HELPER.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Map<Integer, NodeRef>>() {

        @Override
        public Map<Integer, NodeRef> execute() throws Throwable {
            Map<Integer, NodeRef> result = new HashMap<Integer, NodeRef>();
            // Create some blog posts. They'll all be published 'now' of course...
            final BlogPostInfo blogPost1971 = BLOG_SERVICE.createBlogPost(BLOG_CONTAINER_NODE, "publishedPostWithTags1971", "Hello world", true);
            final BlogPostInfo blogPost1981 = BLOG_SERVICE.createBlogPost(BLOG_CONTAINER_NODE, "publishedPostWithTags1981", "Hello world", true);
            final BlogPostInfo blogPost1991 = BLOG_SERVICE.createBlogPost(BLOG_CONTAINER_NODE, "publishedPostWithTags1991", "Hello world", true);
            TAGGING_SERVICE.addTags(blogPost1971.getNodeRef(), tags);
            TAGGING_SERVICE.addTags(blogPost1981.getNodeRef(), tags);
            TAGGING_SERVICE.addTags(blogPost1991.getNodeRef(), tags);
            testNodesToTidy.add(blogPost1971.getNodeRef());
            testNodesToTidy.add(blogPost1981.getNodeRef());
            testNodesToTidy.add(blogPost1991.getNodeRef());
            // We need to 'cheat' and set the nodes' cm:published dates to specific values.
            NODE_SERVICE.setProperty(blogPost1971.getNodeRef(), ContentModel.PROP_PUBLISHED, _1971);
            NODE_SERVICE.setProperty(blogPost1981.getNodeRef(), ContentModel.PROP_PUBLISHED, _1981);
            NODE_SERVICE.setProperty(blogPost1991.getNodeRef(), ContentModel.PROP_PUBLISHED, _1991);
            result.put(1971, blogPost1971.getNodeRef());
            result.put(1981, blogPost1981.getNodeRef());
            result.put(1991, blogPost1991.getNodeRef());
            return result;
        }
    });
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        @SuppressWarnings("deprecation")
        @Override
        public Void execute() throws Throwable {
            // Quick sanity check: Did our cheating with the cm:created dates work?
            assertEquals("Incorrect published date", 71, ((Date) NODE_SERVICE.getProperty(blogPosts.get(1971), ContentModel.PROP_PUBLISHED)).getYear());
            PagingRequest pagingReq = new PagingRequest(0, 10, null);
            final RangedDateProperty publishedBefore1980 = new RangedDateProperty(null, _1980, ContentModel.PROP_PUBLISHED);
            final RangedDateProperty publishedAfter1980 = new RangedDateProperty(_1980, null, ContentModel.PROP_PUBLISHED);
            final RangedDateProperty publishedBetween1975And1985 = new RangedDateProperty(_1975, _1985, ContentModel.PROP_PUBLISHED);
            // Find all
            PagingResults<BlogPostInfo> pagedResults = BLOG_SERVICE.findBlogPosts(BLOG_CONTAINER_NODE, null, null, pagingReq);
            assertEquals("Wrong number of blog posts", 3, pagedResults.getPage().size());
            Set<NodeRef> recoveredBlogNodes = new HashSet<NodeRef>();
            for (BlogPostInfo bpi : pagedResults.getPage()) {
                recoveredBlogNodes.add(bpi.getNodeRef());
            }
            assertTrue("Missing expected BlogPost NodeRef 71", recoveredBlogNodes.contains(blogPosts.get(1971)));
            assertTrue("Missing expected BlogPost NodeRef 81", recoveredBlogNodes.contains(blogPosts.get(1981)));
            assertTrue("Missing expected BlogPost NodeRef 91", recoveredBlogNodes.contains(blogPosts.get(1991)));
            // Find posts before date
            pagedResults = BLOG_SERVICE.findBlogPosts(BLOG_CONTAINER_NODE, publishedBefore1980, null, pagingReq);
            assertEquals("Wrong blog post count", 1, pagedResults.getPage().size());
            NodeRef blogNode = pagedResults.getPage().get(0).getNodeRef();
            assertEquals("Incorrect NodeRef.", blogNode, blogPosts.get(1971));
            List<String> recoveredTags = TAGGING_SERVICE.getTags(blogNode);
            assertEquals("Incorrect tags.", tags, recoveredTags);
            // Find posts after date
            pagedResults = BLOG_SERVICE.findBlogPosts(BLOG_CONTAINER_NODE, publishedAfter1980, "hello", pagingReq);
            assertEquals("Wrong blog post count", 2, pagedResults.getPage().size());
            blogNode = pagedResults.getPage().get(0).getNodeRef();
            assertEquals("Incorrect NodeRef.", blogNode, blogPosts.get(1981));
            // Find posts between dates
            pagedResults = BLOG_SERVICE.findBlogPosts(BLOG_CONTAINER_NODE, publishedBetween1975And1985, "hello", pagingReq);
            assertEquals("Wrong blog post count", 1, pagedResults.getPage().size());
            blogNode = pagedResults.getPage().get(0).getNodeRef();
            assertEquals("Incorrect NodeRef.", blogNode, blogPosts.get(1981));
            return null;
        }
    });
}
Also used : PagingResults(org.alfresco.query.PagingResults) HashSet(java.util.HashSet) Set(java.util.Set) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) Calendar(java.util.Calendar) RangedDateProperty(org.alfresco.service.cmr.blog.BlogService.RangedDateProperty) BlogPostInfo(org.alfresco.service.cmr.blog.BlogPostInfo) Date(java.util.Date) PagingRequest(org.alfresco.query.PagingRequest) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) PropertyMap(org.alfresco.util.PropertyMap) Category(org.junit.experimental.categories.Category) BaseSpringTest(org.alfresco.util.BaseSpringTest) Test(org.junit.Test)

Example 15 with BlogPostInfo

use of org.alfresco.service.cmr.blog.BlogPostInfo in project alfresco-repository by Alfresco.

the class BlogServiceImplTest method getBlogPostsFilteredByDateRange.

@Test
public void getBlogPostsFilteredByDateRange() throws Exception {
    final int numberOfPosts = 31 + 31 + 29;
    final List<NodeRef> posts = TRANSACTION_HELPER.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<List<NodeRef>>() {

        @Override
        public List<NodeRef> execute() throws Throwable {
            List<NodeRef> results = new ArrayList<NodeRef>();
            for (int i = 0; i < numberOfPosts; i++) {
                BlogPostInfo newBlogPost = BLOG_SERVICE.createBlogPost(BLOG_CONTAINER_NODE, "date-specific-post" + i, "", false);
                testNodesToTidy.add(newBlogPost.getNodeRef());
                results.add(newBlogPost.getNodeRef());
            }
            return results;
        }
    });
    // Now go through and set their creation dates to specific points in the past.
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            // FROM 1st December 1999
            final Calendar current = Calendar.getInstance();
            current.set(1999, 11, 1, 11, 0);
            // should give us:
            // 31 posts in december 99
            // 31 posts in january 00
            // 29 posts in february 00
            Date currentDate = current.getTime();
            for (NodeRef nr : posts) {
                // We'll permanently turn off auditing on this node.
                // This should allow us to set the cm:created date without auditing overwriting our value.
                // These nodes get deleted after the test anyway.
                BEHAVIOUR_FILTER.disableBehaviour(nr, ContentModel.ASPECT_AUDITABLE);
                // Yes, cm:published will be before cm:created. But I don't think that matter.
                NODE_SERVICE.setProperty(nr, ContentModel.PROP_PUBLISHED, currentDate);
                current.add(Calendar.DATE, 1);
                currentDate = current.getTime();
            }
            return null;
        }
    });
    TRANSACTION_HELPER.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            PagingRequest pagingReq = new PagingRequest(0, 100, null);
            Calendar cal = Calendar.getInstance();
            cal.set(1999, 11, 1, 0, 0, 0);
            Date firstDec99 = cal.getTime();
            cal.set(2000, 0, 1, 0, 0, 0);
            Date firstJan00 = cal.getTime();
            cal.set(2000, 1, 1, 0, 0, 0);
            Date firstFeb00 = cal.getTime();
            cal.set(2000, 2, 1, 0, 0, 0);
            Date firstMar00 = cal.getTime();
            PagingResults<BlogPostInfo> pagedResults = BLOG_SERVICE.getPublished(BLOG_CONTAINER_NODE, firstDec99, firstJan00, null, pagingReq);
            assertEquals("Wrong number of user blog posts", 31, pagedResults.getPage().size());
            pagedResults = BLOG_SERVICE.getPublished(BLOG_CONTAINER_NODE, firstFeb00, firstMar00, null, pagingReq);
            assertEquals("Wrong number of user blog posts", 29, pagedResults.getPage().size());
            pagedResults = BLOG_SERVICE.getPublished(BLOG_CONTAINER_NODE, firstJan00, firstMar00, null, pagingReq);
            assertEquals("Wrong number of user blog posts", 31 + 29, pagedResults.getPage().size());
            return null;
        }
    });
}
Also used : PagingResults(org.alfresco.query.PagingResults) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) Calendar(java.util.Calendar) BlogPostInfo(org.alfresco.service.cmr.blog.BlogPostInfo) Date(java.util.Date) PagingRequest(org.alfresco.query.PagingRequest) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ArrayList(java.util.ArrayList) List(java.util.List) BaseSpringTest(org.alfresco.util.BaseSpringTest) Test(org.junit.Test)

Aggregations

BlogPostInfo (org.alfresco.service.cmr.blog.BlogPostInfo)16 ArrayList (java.util.ArrayList)10 NodeRef (org.alfresco.service.cmr.repository.NodeRef)10 PagingRequest (org.alfresco.query.PagingRequest)8 PagingResults (org.alfresco.query.PagingResults)8 List (java.util.List)7 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)7 BaseSpringTest (org.alfresco.util.BaseSpringTest)7 Test (org.junit.Test)7 HashMap (java.util.HashMap)5 Date (java.util.Date)4 JSONObject (org.json.simple.JSONObject)4 RangedDateProperty (org.alfresco.service.cmr.blog.BlogService.RangedDateProperty)3 Pair (org.alfresco.util.Pair)3 Category (org.junit.experimental.categories.Category)3 Calendar (java.util.Calendar)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 IOException (java.io.IOException)1