1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.wiki.template.internal.migration

File WikiTemplateMigration.java

 

Coverage histogram

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

Code metrics

0
24
4
1
137
83
6
0.25
6
4
1.5

Classes

Class Line # Actions
WikiTemplateMigration 55 24 0% 6 4
0.8571428785.7%
 

Contributing tests

This file is covered by 3 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.wiki.template.internal.migration;
21   
22    import java.util.List;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.model.reference.DocumentReference;
30    import org.xwiki.model.reference.DocumentReferenceResolver;
31    import org.xwiki.query.Query;
32    import org.xwiki.query.QueryException;
33    import org.xwiki.query.QueryManager;
34    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
35    import org.xwiki.wiki.template.internal.WikiTemplateClassDocumentInitializer;
36   
37    import com.xpn.xwiki.XWiki;
38    import com.xpn.xwiki.XWikiContext;
39    import com.xpn.xwiki.XWikiException;
40    import com.xpn.xwiki.doc.XWikiDocument;
41    import com.xpn.xwiki.objects.BaseObject;
42    import com.xpn.xwiki.store.migration.DataMigrationException;
43    import com.xpn.xwiki.store.migration.XWikiDBVersion;
44    import com.xpn.xwiki.store.migration.hibernate.AbstractHibernateDataMigration;
45   
46    /**
47    * Migrator to create a WikiManager.WikiTemplateClass on every descriptor.
48    *
49    * @since 5.4RC1
50    * @version $Id: b9bb84ace19484e2b0ca03896022b02c87803d60 $
51    */
52    @Component
53    @Named("R54000WikiTemplateMigration")
54    @Singleton
 
55    public class WikiTemplateMigration extends AbstractHibernateDataMigration
56    {
57    private static final String OLD_TEMPLATE_PROPERTY = "iswikitemplate";
58   
59    @Inject
60    private WikiDescriptorManager wikiDescriptorManager;
61   
62    @Inject
63    private QueryManager queryManager;
64   
65    @Inject
66    private DocumentReferenceResolver<String> documentReferenceResolver;
67   
 
68  0 toggle @Override
69    public String getDescription()
70    {
71  0 return "http://jira.xwiki.org/browse/XWIKI-9934";
72    }
73   
 
74  3 toggle @Override
75    public XWikiDBVersion getVersion()
76    {
77  3 return new XWikiDBVersion(54000);
78    }
79   
 
80  2 toggle @Override
81    public boolean shouldExecute(XWikiDBVersion startupVersion)
82    {
83    // We migrate main wiki
84  2 return wikiDescriptorManager.getCurrentWikiId().equals(wikiDescriptorManager.getMainWikiId());
85    }
86   
 
87  1 toggle @Override
88    protected void hibernateMigrate() throws DataMigrationException, XWikiException
89    {
90    // XWiki objects
91  1 XWikiContext context = getXWikiContext();
92  1 XWiki xwiki = context.getWiki();
93   
94    // WikiManager.WikiTemplateClass reference
95  1 DocumentReference templateClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(),
96    WikiTemplateClassDocumentInitializer.DOCUMENT_SPACE,
97    WikiTemplateClassDocumentInitializer.DOCUMENT_NAME);
98   
99    // XWiki.XWikiServerClass reference
100  1 DocumentReference descriptorClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(),
101    XWiki.SYSTEM_SPACE, "XWikiServerClass");
102   
103    // Superadmin reference
104  1 DocumentReference superAdmin = new DocumentReference(wikiDescriptorManager.getMainWikiId(),
105    XWiki.SYSTEM_SPACE, "superadmin");
106   
107  1 try {
108    // Get all the descriptor documents
109  1 String statement = "select distinct doc.fullName "
110    + "from Document doc, doc.object(XWiki.XWikiServerClass) as obj";
111  1 Query query = queryManager.createQuery(statement, Query.XWQL);
112  1 List<String> results = query.execute();
113  1 for (String wikiPage : results) {
114  4 XWikiDocument document = xwiki.getDocument(documentReferenceResolver.resolve(wikiPage), context);
115    // Get the "iswikitemplate" value
116  4 BaseObject descriptorObject = document.getXObject(descriptorClassReference);
117  4 int isTemplate = descriptorObject.getIntValue(OLD_TEMPLATE_PROPERTY, 0);
118    // We remove the deprecated property from the descriptor
119  4 descriptorObject.removeField(OLD_TEMPLATE_PROPERTY);
120    // Add the new WikiManager.WikiTemplateClass object
121  4 BaseObject object = document.getXObject(templateClassReference, true, context);
122    // The new object might already exists and have a template property already set
123  4 isTemplate = object.getIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate);
124    // Set the (new) value
125  4 object.setIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate);
126    // The document must have an author
127  4 document.setAuthorReference(superAdmin);
128    // Save the document
129  4 xwiki.saveDocument(document, "[UPGRADE] Upgrade the template section.", context);
130    }
131    } catch (QueryException e) {
132  0 throw new DataMigrationException("Failed to get the list of all existing descriptors.", e);
133    } catch (XWikiException e) {
134  0 throw new DataMigrationException("Failed to upgrade a wiki descriptor.", e);
135    }
136    }
137    }