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

File WikiDescriptorMigrator.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

4
27
5
1
145
103
9
0.33
5.4
5
1.8

Classes

Class Line # Actions
WikiDescriptorMigrator 59 27 0% 9 3
0.916666791.7%
 

Contributing tests

This file is covered by 5 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.internal.descriptor.migrator;
21   
22    import java.util.List;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Provider;
27    import javax.inject.Singleton;
28   
29    import org.apache.commons.lang.StringUtils;
30    import org.apache.commons.lang.exception.ExceptionUtils;
31    import org.slf4j.Logger;
32    import org.xwiki.component.annotation.Component;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.model.reference.DocumentReferenceResolver;
35    import org.xwiki.query.Query;
36    import org.xwiki.query.QueryException;
37    import org.xwiki.query.QueryManager;
38    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
39    import org.xwiki.wiki.internal.descriptor.document.XWikiServerClassDocumentInitializer;
40   
41    import com.xpn.xwiki.XWiki;
42    import com.xpn.xwiki.XWikiContext;
43    import com.xpn.xwiki.XWikiException;
44    import com.xpn.xwiki.doc.XWikiDocument;
45    import com.xpn.xwiki.objects.BaseObject;
46    import com.xpn.xwiki.store.migration.DataMigrationException;
47    import com.xpn.xwiki.store.migration.XWikiDBVersion;
48    import com.xpn.xwiki.store.migration.hibernate.AbstractHibernateDataMigration;
49   
50    /**
51    * This migrator is designed to add missing properties to existing descriptors.
52    *
53    * @since 5.4.3
54    * @version $Id: cdec4a213f4e66f52eab2c9ee2c6d6703f60ed59 $
55    */
56    @Component
57    @Named("R54300WikiDescriptorMigration")
58    @Singleton
 
59    public class WikiDescriptorMigrator extends AbstractHibernateDataMigration
60    {
61    @Inject
62    private QueryManager queryManager;
63   
64    @Inject
65    private WikiDescriptorManager wikiDescriptorManager;
66   
67    @Inject
68    private Provider<XWikiContext> xcontextProvider;
69   
70    @Inject
71    private DocumentReferenceResolver<String> documentReferenceResolver;
72   
73    @Inject
74    private Logger logger;
75   
 
76  0 toggle @Override
77    public String getDescription()
78    {
79  0 return "http://jira.xwiki.org/browse/XWIKI-10091";
80    }
81   
 
82  152 toggle @Override
83    public XWikiDBVersion getVersion()
84    {
85  152 return new XWikiDBVersion(54300);
86    }
87   
 
88  2 toggle @Override
89    public boolean shouldExecute(XWikiDBVersion startupVersion)
90    {
91    // Should execute only on the main wiki
92  2 return wikiDescriptorManager.getCurrentWikiId().equals(wikiDescriptorManager.getMainWikiId());
93    }
94   
 
95  3 toggle @Override
96    protected void hibernateMigrate() throws DataMigrationException, XWikiException
97    {
98  3 String hql = "SELECT DISTINCT doc.fullName FROM XWikiDocument doc, BaseObject obj WHERE doc.fullName = obj.name"
99    + " AND obj.className = :className AND doc.fullName <> :template AND doc.fullName NOT IN "
100    + "(SELECT DISTINCT doc2.fullName FROM XWikiDocument doc2, BaseObject obj2, StringProperty propPrettyName"
101    + " WHERE doc2.fullName = obj2.name AND obj2.className = :className"
102    + " AND propPrettyName.id = obj2.id AND propPrettyName.name = :propertyName)";
103   
104  3 try {
105  3 Query query = queryManager.createQuery(hql, Query.HQL);
106  2 query.bindValue("className", String.format("%s.%s", XWiki.SYSTEM_SPACE,
107    XWikiServerClassDocumentInitializer.DOCUMENT_NAME));
108  2 query.bindValue("propertyName", XWikiServerClassDocumentInitializer.FIELD_WIKIPRETTYNAME);
109  2 query.bindValue("template", "XWiki.XWikiServerClassTemplate");
110  2 List<String> results = query.execute();
111  2 for (String result : results) {
112  2 fixDocument(result);
113    }
114    } catch (QueryException e) {
115  1 logger.error("Failed to perform a query on the main wiki.", e);
116    }
117    }
118   
 
119  2 toggle private void fixDocument(String documentName)
120    {
121  2 XWikiContext context = xcontextProvider.get();
122  2 XWiki xwiki = context.getWiki();
123   
124  2 DocumentReference documentReference = documentReferenceResolver.resolve(documentName);
125  2 try {
126  2 XWikiDocument document = xwiki.getDocument(documentReference, context);
127  1 List<BaseObject> objects = document.getXObjects(XWikiServerClassDocumentInitializer.SERVER_CLASS);
128  1 for (BaseObject obj : objects) {
129  2 if (obj == null) {
130  1 continue;
131    }
132  1 String value = obj.getStringValue(XWikiServerClassDocumentInitializer.FIELD_WIKIPRETTYNAME);
133  1 if (StringUtils.isBlank(value)) {
134  1 obj.setStringValue(XWikiServerClassDocumentInitializer.FIELD_WIKIPRETTYNAME,
135    StringUtils.capitalize(StringUtils.removeStart(documentReference.getName(), "XWikiServer")));
136    }
137    }
138  1 xwiki.saveDocument(document, "[UPGRADE] Set a default pretty name.", context);
139    } catch (XWikiException e) {
140  1 logger.warn("Failed to get or save the wiki descriptor document [{}]. You will not see the"
141    + " corresponding wiki in the Wiki Index unless you give it a Pretty Name manually. {}",
142    documentName, ExceptionUtils.getRootCauseMessage(e));
143    }
144    }
145    }