1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.platform.blog.internal

File DefaultBlogVisibilityMigration.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

0
12
1
1
99
55
2
0.17
12
1
2

Classes

Class Line # Actions
DefaultBlogVisibilityMigration 51 12 0% 2 13
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.platform.blog.internal;
21   
22    import javax.inject.Inject;
23    import javax.inject.Provider;
24    import javax.inject.Singleton;
25   
26    import org.slf4j.Logger;
27    import org.xwiki.component.annotation.Component;
28    import org.xwiki.model.reference.DocumentReference;
29    import org.xwiki.model.reference.DocumentReferenceResolver;
30    import org.xwiki.model.reference.WikiReference;
31    import org.xwiki.platform.blog.BlogVisibilityMigration;
32    import org.xwiki.platform.blog.BlogVisibilityUpdater;
33    import org.xwiki.query.Query;
34    import org.xwiki.query.QueryManager;
35   
36    import com.xpn.xwiki.XWiki;
37    import com.xpn.xwiki.XWikiContext;
38    import com.xpn.xwiki.doc.XWikiDocument;
39   
40    /**
41    * Default implementation of {@link BlogVisibilityMigration}.
42    *
43    * @version $Id: 5f5fac66926a8558cb73aba275e07a642a9ece91 $
44    *
45    * @since 9.0RC1
46    * @since 8.4.3
47    * @since 7.4.6
48    */
49    @Component
50    @Singleton
 
51    public class DefaultBlogVisibilityMigration implements BlogVisibilityMigration
52    {
53    @Inject
54    private QueryManager queryManager;
55   
56    @Inject
57    private DocumentReferenceResolver<String> referenceResolver;
58   
59    @Inject
60    private Provider<XWikiContext> contextProvider;
61   
62    @Inject
63    private BlogVisibilityUpdater blogVisibilityUpdater;
64   
65    @Inject
66    private Logger logger;
67   
 
68  0 toggle @Override
69    public void execute(WikiReference wikiReference) throws Exception
70    {
71  0 try {
72  0 XWikiContext context = contextProvider.get();
73  0 XWiki xwiki = context.getWiki();
74   
75    // Detect the blog posts where the visibility is not what it should be
76  0 final String xwql =
77    "from doc.object(Blog.BlogPostClass) obj where "
78    + "((obj.published = 0 or obj.hidden = 1) and doc.hidden <> 1)"
79    + " or "
80    + "(obj.published = 1 and obj.hidden = 0 and doc.hidden <> 0)";
81   
82  0 Query query = queryManager.createQuery(xwql, Query.XWQL).setWiki(wikiReference.getName());
83  0 for (String docName : query.<String>execute()) {
84  0 DocumentReference documentReference = referenceResolver.resolve(docName, wikiReference);
85  0 XWikiDocument document = xwiki.getDocument(documentReference, context);
86    // The following line is not necessary since the BlogDocumentSavingListener will do the same
87    // but I think it's more clean to not rely on this and to do it anyway.
88  0 blogVisibilityUpdater.synchronizeHiddenMetadata(document);
89    // The saving is the necessary thing here.
90  0 xwiki.saveDocument(document, "Change the page's visibility according to the blog post.", context);
91    }
92   
93  0 logger.info("Migration of blog posts' visibility has been successfully executed on the wiki [{}].",
94    wikiReference.getName());
95    } catch (Exception e) {
96  0 throw new Exception("Failed to migrate the blog posts' visibility.", e);
97    }
98    }
99    }