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

File BlogUpgradeEventListener.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

8
20
5
1
139
79
12
0.6
4
5
2.4

Classes

Class Line # Actions
BlogUpgradeEventListener 54 20 0% 12 3
0.9090909490.9%
 

Contributing tests

This file is covered by 7 tests. .

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 java.util.Collection;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.slf4j.Logger;
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.extension.InstalledExtension;
31    import org.xwiki.extension.event.ExtensionUpgradedEvent;
32    import org.xwiki.extension.version.Version;
33    import org.xwiki.extension.version.VersionConstraint;
34    import org.xwiki.extension.version.internal.DefaultVersionConstraint;
35    import org.xwiki.model.reference.WikiReference;
36    import org.xwiki.observation.AbstractEventListener;
37    import org.xwiki.observation.event.Event;
38    import org.xwiki.platform.blog.BlogVisibilityMigration;
39    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
40    import org.xwiki.wiki.manager.WikiManagerException;
41   
42    /**
43    * React to the upgrade of the blog application by starting the blog post visibility migration.
44    *
45    * @version $Id: 23ea977f9905b11a9f23e48de749a5248764cd47 $
46    *
47    * @since 9.0RC1
48    * @since 8.4.3
49    * @since 7.4.6
50    */
51    @Component
52    @Singleton
53    @Named(BlogUpgradeEventListener.NAME)
 
54    public class BlogUpgradeEventListener extends AbstractEventListener
55    {
56    /**
57    * Name of the listener.
58    */
59    public static final String NAME = "Blog Upgrade Listener";
60   
61    /**
62    * ID of the Blog Application.
63    */
64    private static final String EXTENSION_ID = "org.xwiki.platform:xwiki-platform-blog-ui";
65   
66    /**
67    * The visibility is synchronized since 7.4.6, 8.4.3 and 9.0RC1, so we do the migration only if the previous
68    * version was anterior, ie matches the following constraint.
69    */
70    private static final VersionConstraint VERSION_CONSTRAINT = new DefaultVersionConstraint("(,7.4.6),[8.0,8.4.3)");
71   
72    @Inject
73    private BlogVisibilityMigration blogVisibilityMigration;
74   
75    @Inject
76    private WikiDescriptorManager wikiDescriptorManager;
77   
78    @Inject
79    private Logger logger;
80   
81    /**
82    * Construct a BlogUpgradeEventListener.
83    */
 
84  8 toggle public BlogUpgradeEventListener()
85    {
86  8 super(NAME, new ExtensionUpgradedEvent(EXTENSION_ID));
87    }
88   
 
89  7 toggle @Override
90    public void onEvent(Event event, Object installedExtension, Object previousExtensions)
91    {
92  7 ExtensionUpgradedEvent extensionUpgradedEvent = (ExtensionUpgradedEvent) event;
93   
94  7 Version previousVersion = getPreviousVersion((Collection<InstalledExtension>) previousExtensions);
95   
96  7 if (previousVersion != null && VERSION_CONSTRAINT.containsVersion(previousVersion)) {
97  3 String namespace = extensionUpgradedEvent.getNamespace();
98  3 if (namespace == null) {
99    // When the namespace is null, it means the application is installed on the root namespace, ie. on
100    // the farm.
101  1 migrateAllWikis();
102  2 } else if (namespace.startsWith("wiki:")) {
103  2 migrateWiki(new WikiReference(namespace.substring(5)));
104    }
105    }
106    }
107   
 
108  1 toggle private void migrateAllWikis()
109    {
110  1 try {
111  1 for (String wikiId : wikiDescriptorManager.getAllIds()) {
112  2 migrateWiki(new WikiReference(wikiId));
113    }
114    } catch (WikiManagerException e) {
115  0 logger.warn("Failed to migrate the visibility of non published blog posts.", e);
116    }
117    }
118   
 
119  4 toggle private void migrateWiki(WikiReference wikiReference)
120    {
121  4 try {
122  4 blogVisibilityMigration.execute(wikiReference);
123    } catch (Exception e) {
124  0 logger.warn("Failed to migrate the visibility of non published blog posts on the wiki [{}].",
125    wikiReference.getName(), e);
126    }
127    }
128   
 
129  7 toggle private Version getPreviousVersion(Collection<InstalledExtension> previousExtensions)
130    {
131  7 for (InstalledExtension extension : previousExtensions) {
132  12 if (extension.getId().getId().equals(EXTENSION_ID)) {
133  6 return extension.getId().getVersion();
134    }
135    }
136    // Should never happen
137  1 return null;
138    }
139    }