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

File WikiUserFromXEMMigration.java

 

Coverage histogram

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

Code metrics

10
71
11
1
296
185
21
0.3
6.45
11
1.91

Classes

Class Line # Actions
WikiUserFromXEMMigration 57 71 0% 21 11
0.880434888%
 

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.user.internal;
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.apache.commons.lang3.StringUtils;
29    import org.slf4j.Logger;
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.model.reference.LocalDocumentReference;
33    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
34    import org.xwiki.wiki.user.MembershipType;
35    import org.xwiki.wiki.user.UserScope;
36    import org.xwiki.wiki.user.WikiUserConfiguration;
37    import org.xwiki.wiki.user.WikiUserManagerException;
38   
39    import com.xpn.xwiki.XWiki;
40    import com.xpn.xwiki.XWikiContext;
41    import com.xpn.xwiki.XWikiException;
42    import com.xpn.xwiki.doc.XWikiDocument;
43    import com.xpn.xwiki.objects.BaseObject;
44    import com.xpn.xwiki.store.migration.DataMigrationException;
45    import com.xpn.xwiki.store.migration.XWikiDBVersion;
46    import com.xpn.xwiki.store.migration.hibernate.AbstractHibernateDataMigration;
47   
48    /**
49    * Migrator to add a WikiUserConfiguration object on every existing wiki.
50    *
51    * @version $Id: e221274ddea1494b8f063e37ad0627ba1f62e55c $
52    * @since 5.3RC1
53    */
54    @Component
55    @Named("R530010WikiUserFromXEMMigration")
56    @Singleton
 
57    public class WikiUserFromXEMMigration extends AbstractHibernateDataMigration
58    {
59    private static final String WORKSPACE_CLASS_SPACE = "WorkspaceManager";
60   
61    private static final String WORKSPACE_CLASS_PAGE = "WorkspaceClass";
62   
63    @Inject
64    private WikiUserConfigurationHelper wikiUserConfigurationHelper;
65   
66    @Inject
67    private WikiDescriptorManager wikiDescriptorManager;
68   
69    @Inject
70    private Logger logger;
71   
 
72  0 toggle @Override
73    public String getDescription()
74    {
75  0 return "http://jira.xwiki.org/browse/XWIKI-9516";
76    }
77   
 
78  6 toggle @Override
79    public XWikiDBVersion getVersion()
80    {
81    // XWiki 5.3, migration, to execute after xwiki-platform-wiki-workspaces-migrator.WorkspacesMigration.
82  6 return new XWikiDBVersion(53010);
83    }
84   
 
85  0 toggle @Override
86    public boolean shouldExecute(XWikiDBVersion startupVersion)
87    {
88    // We migrate only subwikis
89  0 return !wikiDescriptorManager.getCurrentWikiId().equals(wikiDescriptorManager.getMainWikiId());
90    }
91   
 
92  3 toggle @Override
93    protected void hibernateMigrate() throws DataMigrationException, XWikiException
94    {
95    // Context, XWiki
96  3 XWikiContext context = getXWikiContext();
97  3 XWiki xwiki = context.getWiki();
98   
99    // Current wiki
100  3 String currentWikiId = wikiDescriptorManager.getCurrentWikiId();
101   
102    // Get the old wiki descriptor
103  3 DocumentReference oldWikiDescriptorReference = new DocumentReference(wikiDescriptorManager.getMainWikiId(),
104    XWiki.SYSTEM_SPACE, String.format("XWikiServer%s", StringUtils.capitalize(currentWikiId)));
105  3 XWikiDocument oldWikiDescriptor = xwiki.getDocument(oldWikiDescriptorReference, context);
106   
107    // Try to get the old workspace object
108  3 DocumentReference oldClassDocument = new DocumentReference(wikiDescriptorManager.getMainWikiId(),
109    WORKSPACE_CLASS_SPACE, WORKSPACE_CLASS_PAGE);
110  3 BaseObject oldObject = oldWikiDescriptor.getXObject(oldClassDocument);
111   
112    // Upgrade depending of the type
113  3 if (oldObject != null || isWorkspaceTemplate(currentWikiId)) {
114    // It's a workspace
115  2 upgradeWorkspace(oldObject, currentWikiId, oldWikiDescriptor);
116    } else {
117    // It's a regular subwiki
118  1 upgradeRegularSubwiki(currentWikiId);
119    }
120    }
121   
 
122  2 toggle private boolean isWorkspaceTemplate(String wikiId)
123    {
124    // Context, XWiki
125  2 XWikiContext context = getXWikiContext();
126  2 XWiki xwiki = context.getWiki();
127   
128    // In the first version of the Workspace Application, workspacetemplate did not have the workspace object.
129    // We test for the existence of XWiki.ManageWorkspace just to be sure that the workspacetemplate is a workspace.
130  2 return wikiId.equals("workspacetemplate") && xwiki.exists(new DocumentReference(wikiId,
131    "XWiki", "ManageWorkspace"), context);
132    }
133   
 
134  3 toggle private void saveConfiguration(WikiUserConfiguration configuration, String wikiId)
135    throws DataMigrationException, XWikiException
136    {
137  3 try {
138  3 wikiUserConfigurationHelper.saveConfiguration(configuration, wikiId);
139    } catch (WikiUserManagerException e) {
140  0 throw new DataMigrationException(String.format(
141    "Failed to save the new wiki user configuration page for wiki [%s].", wikiId), e);
142    }
143    }
144   
 
145  1 toggle private void upgradeRegularSubwiki(String wikiId) throws DataMigrationException, XWikiException
146    {
147    // Create the new configuration
148  1 WikiUserConfiguration configuration = new WikiUserConfiguration();
149  1 configuration.setUserScope(UserScope.LOCAL_AND_GLOBAL);
150  1 configuration.setMembershipType(MembershipType.INVITE);
151   
152    // Save the new configuration
153  1 saveConfiguration(configuration, wikiId);
154    }
155   
 
156  2 toggle private void upgradeWorkspace(BaseObject oldObject, String wikiId, XWikiDocument oldWikiDescriptor)
157    throws DataMigrationException, XWikiException
158    {
159  2 upgradeWorkspaceConfiguration(oldObject, wikiId, oldWikiDescriptor);
160   
161    // We also need to upgrade workspaces candidacies
162  2 try {
163  2 upgradeWorkspaceCandidacies(wikiId);
164    } catch (XWikiException e) {
165  0 throw new DataMigrationException("Unable to upgrade candidacies from the old Workspace Application to "
166    + "the new Wiki Application.");
167    }
168   
169    // Finally, the migration is done, we can delete the old workspace object.
170  2 if (oldObject != null) {
171  1 deleteOldWorkspaceObject(oldObject, oldWikiDescriptor);
172    }
173    }
174   
175    /**
176    * Convert the old WorkspaceManager.WorkspaceClass objects to the new configuration format.
177    *
178    * @param oldObject old workspace object
179    * @param wikiId id of the wiki to upgrade
180    * @param oldWikiDescriptor document that holds the old object
181    * @throws DataMigrationException if problems occur
182    * @throws XWikiException if problems occur
183    */
 
184  2 toggle private void upgradeWorkspaceConfiguration(BaseObject oldObject, String wikiId,
185    XWikiDocument oldWikiDescriptor) throws DataMigrationException, XWikiException
186    {
187    // Context, XWiki
188  2 XWikiContext context = getXWikiContext();
189  2 XWiki xwiki = context.getWiki();
190   
191    // Create the new configuration
192  2 WikiUserConfiguration configuration = new WikiUserConfiguration();
193   
194    // No local users
195  2 configuration.setUserScope(UserScope.GLOBAL_ONLY);
196   
197    // Set the membershipType value
198  2 if (oldObject != null) {
199    // Get the membershipType value
200  1 String membershipTypeValue = oldObject.getStringValue("membershipType");
201  1 MembershipType membershipType;
202  1 try {
203  1 membershipType = MembershipType.valueOf(membershipTypeValue.toUpperCase());
204    } catch (Exception e) {
205    // Default value
206  0 membershipType = MembershipType.INVITE;
207    }
208  1 configuration.setMembershipType(membershipType);
209    } else {
210    // If there is no workspace object, we put a default value.
211  1 configuration.setMembershipType(MembershipType.INVITE);
212    }
213   
214    // Save the new configuration
215  2 saveConfiguration(configuration, wikiId);
216    }
217   
 
218  1 toggle private void deleteOldWorkspaceObject(BaseObject oldObject, XWikiDocument oldWikiDescriptor)
219    throws DataMigrationException
220    {
221    // Context, XWiki
222  1 XWikiContext context = getXWikiContext();
223  1 XWiki xwiki = context.getWiki();
224   
225    // Delete the old object
226  1 oldWikiDescriptor.removeXObject(oldObject);
227   
228    // Save the document
229  1 try {
230  1 xwiki.saveDocument(oldWikiDescriptor, "[UPGRADE] Remove the old WorkspaceManager.WorkspaceClass object.",
231    context);
232    } catch (XWikiException e) {
233  0 throw new DataMigrationException(String.format(
234    "Failed to save the document [%s] to remove the WorkspaceManager.WorkspaceClass object.",
235    oldWikiDescriptor.getDocumentReference().toString()), e);
236    }
237    }
238   
239    /**
240    * Convert the old WorkspaceManager.WorkspaceCandidateMemberClass objects to the new candidacies format.
241    *
242    * @param wikiId id of the wiki to upgrade
243    * @throws XWikiException if problems occur
244    */
 
245  2 toggle private void upgradeWorkspaceCandidacies(String wikiId) throws XWikiException
246    {
247  2 XWikiContext xcontext = getXWikiContext();
248  2 XWiki xwiki = xcontext.getWiki();
249   
250    // We need to get the document that holds the candidacies
251  2 DocumentReference candidaciesDocumentReference = new DocumentReference(wikiId, XWiki.SYSTEM_SPACE,
252    "XWikiAllGroup");
253  2 XWikiDocument candidaciesDocument = xwiki.getDocument(candidaciesDocumentReference, xcontext);
254   
255    // We need to get all the old candidacies
256  2 DocumentReference oldCandidateClassReference = new DocumentReference(wikiId, XWiki.SYSTEM_SPACE,
257    "WorkspaceCandidateMemberClass");
258  2 List<BaseObject> candidacyObjects = candidaciesDocument.getXObjects(oldCandidateClassReference);
259  2 if (candidacyObjects != null) {
260  2 LocalDocumentReference newCandidateClassReference = WikiCandidateMemberClassInitializer.REFERENCE;
261   
262  2 for (BaseObject oldObject : candidacyObjects) {
263  1 if (oldObject == null) {
264  0 continue;
265    }
266    // Transform the candidacy to the new class
267  1 BaseObject newObject = candidaciesDocument.newXObject(newCandidateClassReference, xcontext);
268  1 newObject.setStringValue(WikiCandidateMemberClassInitializer.FIELD_TYPE,
269    oldObject.getStringValue("type"));
270  1 newObject.setStringValue(WikiCandidateMemberClassInitializer.FIELD_STATUS,
271    oldObject.getStringValue("status"));
272  1 newObject.setStringValue(WikiCandidateMemberClassInitializer.FIELD_USER,
273    oldObject.getStringValue("userName"));
274  1 newObject.setLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_USER_COMMENT,
275    oldObject.getLargeStringValue("userComment"));
276  1 newObject.setStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN,
277    oldObject.getStringValue("reviewer"));
278  1 newObject.setLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN_COMMENT,
279    oldObject.getLargeStringValue("reviewerComment"));
280  1 newObject.setLargeStringValue(WikiCandidateMemberClassInitializer.FIELD_ADMIN_PRIVATE_COMMENT,
281    oldObject.getLargeStringValue("reviewerPrivateComment"));
282  1 newObject.setDateValue(WikiCandidateMemberClassInitializer.FIELD_DATE_OF_CREATION,
283    oldObject.getDateValue("date"));
284  1 newObject.setDateValue(WikiCandidateMemberClassInitializer.FIELD_DATE_OF_CLOSURE,
285    oldObject.getDateValue("resolutionDate"));
286   
287    // Remove the old object
288  1 candidaciesDocument.removeXObject(oldObject);
289    }
290   
291    // Save
292  2 xwiki.saveDocument(candidaciesDocument, "Upgrade candidacies from the old Workspace Application to the "
293    + "new Wiki Application.", xcontext);
294    }
295    }
296    }