1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
|
50 |
|
|
51 |
|
@version |
52 |
|
@since |
53 |
|
|
54 |
|
@Component |
55 |
|
@Named("R530010WikiUserFromXEMMigration") |
56 |
|
@Singleton |
|
|
| 88% |
Uncovered Elements: 11 (92) |
Complexity: 21 |
Complexity Density: 0.3 |
|
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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
72 |
0 |
@Override... |
73 |
|
public String getDescription() |
74 |
|
{ |
75 |
0 |
return "http://jira.xwiki.org/browse/XWIKI-9516"; |
76 |
|
} |
77 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
78 |
6 |
@Override... |
79 |
|
public XWikiDBVersion getVersion() |
80 |
|
{ |
81 |
|
|
82 |
6 |
return new XWikiDBVersion(53010); |
83 |
|
} |
84 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
85 |
0 |
@Override... |
86 |
|
public boolean shouldExecute(XWikiDBVersion startupVersion) |
87 |
|
{ |
88 |
|
|
89 |
0 |
return !wikiDescriptorManager.getCurrentWikiId().equals(wikiDescriptorManager.getMainWikiId()); |
90 |
|
} |
91 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 3 |
Complexity Density: 0.3 |
|
92 |
3 |
@Override... |
93 |
|
protected void hibernateMigrate() throws DataMigrationException, XWikiException |
94 |
|
{ |
95 |
|
|
96 |
3 |
XWikiContext context = getXWikiContext(); |
97 |
3 |
XWiki xwiki = context.getWiki(); |
98 |
|
|
99 |
|
|
100 |
3 |
String currentWikiId = wikiDescriptorManager.getCurrentWikiId(); |
101 |
|
|
102 |
|
|
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 |
|
|
108 |
3 |
DocumentReference oldClassDocument = new DocumentReference(wikiDescriptorManager.getMainWikiId(), |
109 |
|
WORKSPACE_CLASS_SPACE, WORKSPACE_CLASS_PAGE); |
110 |
3 |
BaseObject oldObject = oldWikiDescriptor.getXObject(oldClassDocument); |
111 |
|
|
112 |
|
|
113 |
3 |
if (oldObject != null || isWorkspaceTemplate(currentWikiId)) { |
114 |
|
|
115 |
2 |
upgradeWorkspace(oldObject, currentWikiId, oldWikiDescriptor); |
116 |
|
} else { |
117 |
|
|
118 |
1 |
upgradeRegularSubwiki(currentWikiId); |
119 |
|
} |
120 |
|
} |
121 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
122 |
2 |
private boolean isWorkspaceTemplate(String wikiId)... |
123 |
|
{ |
124 |
|
|
125 |
2 |
XWikiContext context = getXWikiContext(); |
126 |
2 |
XWiki xwiki = context.getWiki(); |
127 |
|
|
128 |
|
|
129 |
|
|
130 |
2 |
return wikiId.equals("workspacetemplate") && xwiki.exists(new DocumentReference(wikiId, |
131 |
|
"XWiki", "ManageWorkspace"), context); |
132 |
|
} |
133 |
|
|
|
|
| 66.7% |
Uncovered Elements: 1 (3) |
Complexity: 2 |
Complexity Density: 0.67 |
|
134 |
3 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
145 |
1 |
private void upgradeRegularSubwiki(String wikiId) throws DataMigrationException, XWikiException... |
146 |
|
{ |
147 |
|
|
148 |
1 |
WikiUserConfiguration configuration = new WikiUserConfiguration(); |
149 |
1 |
configuration.setUserScope(UserScope.LOCAL_AND_GLOBAL); |
150 |
1 |
configuration.setMembershipType(MembershipType.INVITE); |
151 |
|
|
152 |
|
|
153 |
1 |
saveConfiguration(configuration, wikiId); |
154 |
|
} |
155 |
|
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
156 |
2 |
private void upgradeWorkspace(BaseObject oldObject, String wikiId, XWikiDocument oldWikiDescriptor)... |
157 |
|
throws DataMigrationException, XWikiException |
158 |
|
{ |
159 |
2 |
upgradeWorkspaceConfiguration(oldObject, wikiId, oldWikiDescriptor); |
160 |
|
|
161 |
|
|
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 |
|
|
170 |
2 |
if (oldObject != null) { |
171 |
1 |
deleteOldWorkspaceObject(oldObject, oldWikiDescriptor); |
172 |
|
} |
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
@param |
179 |
|
@param |
180 |
|
@param |
181 |
|
@throws |
182 |
|
@throws |
183 |
|
|
|
|
| 93.3% |
Uncovered Elements: 1 (15) |
Complexity: 3 |
Complexity Density: 0.23 |
|
184 |
2 |
private void upgradeWorkspaceConfiguration(BaseObject oldObject, String wikiId,... |
185 |
|
XWikiDocument oldWikiDescriptor) throws DataMigrationException, XWikiException |
186 |
|
{ |
187 |
|
|
188 |
2 |
XWikiContext context = getXWikiContext(); |
189 |
2 |
XWiki xwiki = context.getWiki(); |
190 |
|
|
191 |
|
|
192 |
2 |
WikiUserConfiguration configuration = new WikiUserConfiguration(); |
193 |
|
|
194 |
|
|
195 |
2 |
configuration.setUserScope(UserScope.GLOBAL_ONLY); |
196 |
|
|
197 |
|
|
198 |
2 |
if (oldObject != null) { |
199 |
|
|
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 |
|
|
206 |
0 |
membershipType = MembershipType.INVITE; |
207 |
|
} |
208 |
1 |
configuration.setMembershipType(membershipType); |
209 |
|
} else { |
210 |
|
|
211 |
1 |
configuration.setMembershipType(MembershipType.INVITE); |
212 |
|
} |
213 |
|
|
214 |
|
|
215 |
2 |
saveConfiguration(configuration, wikiId); |
216 |
|
} |
217 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
218 |
1 |
private void deleteOldWorkspaceObject(BaseObject oldObject, XWikiDocument oldWikiDescriptor)... |
219 |
|
throws DataMigrationException |
220 |
|
{ |
221 |
|
|
222 |
1 |
XWikiContext context = getXWikiContext(); |
223 |
1 |
XWiki xwiki = context.getWiki(); |
224 |
|
|
225 |
|
|
226 |
1 |
oldWikiDescriptor.removeXObject(oldObject); |
227 |
|
|
228 |
|
|
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 |
|
|
241 |
|
|
242 |
|
@param |
243 |
|
@throws |
244 |
|
|
|
|
| 88.9% |
Uncovered Elements: 3 (27) |
Complexity: 3 |
Complexity Density: 0.13 |
|
245 |
2 |
private void upgradeWorkspaceCandidacies(String wikiId) throws XWikiException... |
246 |
|
{ |
247 |
2 |
XWikiContext xcontext = getXWikiContext(); |
248 |
2 |
XWiki xwiki = xcontext.getWiki(); |
249 |
|
|
250 |
|
|
251 |
2 |
DocumentReference candidaciesDocumentReference = new DocumentReference(wikiId, XWiki.SYSTEM_SPACE, |
252 |
|
"XWikiAllGroup"); |
253 |
2 |
XWikiDocument candidaciesDocument = xwiki.getDocument(candidaciesDocumentReference, xcontext); |
254 |
|
|
255 |
|
|
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 |
|
|
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 |
|
|
288 |
1 |
candidaciesDocument.removeXObject(oldObject); |
289 |
|
} |
290 |
|
|
291 |
|
|
292 |
2 |
xwiki.saveDocument(candidaciesDocument, "Upgrade candidacies from the old Workspace Application to the " |
293 |
|
+ "new Wiki Application.", xcontext); |
294 |
|
} |
295 |
|
} |
296 |
|
} |