| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.wiki.workspacesmigrator.internal; |
| 21 |
|
|
| 22 |
|
import java.util.Iterator; |
| 23 |
|
import java.util.LinkedList; |
| 24 |
|
import java.util.List; |
| 25 |
|
|
| 26 |
|
import javax.inject.Inject; |
| 27 |
|
import javax.inject.Named; |
| 28 |
|
import javax.inject.Singleton; |
| 29 |
|
|
| 30 |
|
import org.apache.commons.lang3.StringUtils; |
| 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.WikiReference; |
| 35 |
|
import org.xwiki.wiki.descriptor.WikiDescriptorManager; |
| 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 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
@since |
| 51 |
|
@version |
| 52 |
|
|
| 53 |
|
@Component |
| 54 |
|
@Named("R530000WorkspacesMigration") |
| 55 |
|
@Singleton |
| |
|
| 84.4% |
Uncovered Elements: 14 (90) |
Complexity: 21 |
Complexity Density: 0.34 |
|
| 56 |
|
public class WorkspacesMigration extends AbstractHibernateDataMigration |
| 57 |
|
{ |
| 58 |
|
private static final String WORKSPACE_CLASS_SPACE = "WorkspaceManager"; |
| 59 |
|
|
| 60 |
|
private static final String WORKSPACE_CLASS_PAGE = "WorkspaceClass"; |
| 61 |
|
|
| 62 |
|
@Inject |
| 63 |
|
private WikiDescriptorManager wikiDescriptorManager; |
| 64 |
|
|
| 65 |
|
@Inject |
| 66 |
|
private DocumentRestorerFromAttachedXAR documentRestorerFromAttachedXAR; |
| 67 |
|
|
| 68 |
|
@Inject |
| 69 |
|
private SearchSuggestCustomConfigDeleter searchSuggestCustomConfigDeleter; |
| 70 |
|
|
| 71 |
|
@Inject |
| 72 |
|
private Logger logger; |
| 73 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 74 |
4 |
@Override... |
| 75 |
|
protected void hibernateMigrate() throws DataMigrationException, XWikiException |
| 76 |
|
{ |
| 77 |
|
|
| 78 |
4 |
String currentWikiId = wikiDescriptorManager.getCurrentWikiId(); |
| 79 |
|
|
| 80 |
|
|
| 81 |
4 |
deleteSearchSuggestCustomConfig(currentWikiId); |
| 82 |
|
|
| 83 |
|
|
| 84 |
4 |
if (isWorkspace(currentWikiId)) { |
| 85 |
|
|
| 86 |
3 |
restoreDeletedDocuments(currentWikiId); |
| 87 |
|
} |
| 88 |
|
} |
| 89 |
|
|
| |
|
| 81.8% |
Uncovered Elements: 2 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
| 90 |
4 |
private boolean isWorkspace(String wikiId) throws DataMigrationException, XWikiException... |
| 91 |
|
{ |
| 92 |
|
|
| 93 |
4 |
if (wikiId.equals(wikiDescriptorManager.getMainWikiId())) { |
| 94 |
0 |
return false; |
| 95 |
|
} |
| 96 |
|
|
| 97 |
|
|
| 98 |
4 |
XWikiContext context = getXWikiContext(); |
| 99 |
4 |
XWiki xwiki = context.getWiki(); |
| 100 |
|
|
| 101 |
|
|
| 102 |
4 |
DocumentReference oldWikiDescriptorReference = new DocumentReference(wikiDescriptorManager.getMainWikiId(), |
| 103 |
|
XWiki.SYSTEM_SPACE, String.format("XWikiServer%s", StringUtils.capitalize(wikiId))); |
| 104 |
4 |
XWikiDocument oldWikiDescriptor = xwiki.getDocument(oldWikiDescriptorReference, context); |
| 105 |
|
|
| 106 |
|
|
| 107 |
4 |
DocumentReference oldClassDocument = new DocumentReference(wikiDescriptorManager.getMainWikiId(), |
| 108 |
|
WORKSPACE_CLASS_SPACE, WORKSPACE_CLASS_PAGE); |
| 109 |
4 |
BaseObject oldObject = oldWikiDescriptor.getXObject(oldClassDocument); |
| 110 |
|
|
| 111 |
4 |
return (oldObject != null) || isWorkspaceTemplate(wikiId); |
| 112 |
|
} |
| 113 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 114 |
2 |
private boolean isWorkspaceTemplate(String wikiId)... |
| 115 |
|
{ |
| 116 |
|
|
| 117 |
2 |
XWikiContext context = getXWikiContext(); |
| 118 |
2 |
XWiki xwiki = context.getWiki(); |
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
2 |
return wikiId.equals("workspacetemplate") && xwiki.exists(new DocumentReference(wikiId, |
| 123 |
|
"XWiki", "ManageWorkspace"), context); |
| 124 |
|
} |
| 125 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 126 |
0 |
@Override... |
| 127 |
|
public String getDescription() |
| 128 |
|
{ |
| 129 |
0 |
return "http://jira.xwiki.org/browse/XWIKI-9738"; |
| 130 |
|
} |
| 131 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 132 |
0 |
@Override... |
| 133 |
|
public XWikiDBVersion getVersion() |
| 134 |
|
{ |
| 135 |
|
|
| 136 |
0 |
return new XWikiDBVersion(53000); |
| 137 |
|
} |
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
|
|
| 142 |
|
|
| 143 |
|
|
| 144 |
|
@param |
| 145 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 146 |
4 |
private void deleteSearchSuggestCustomConfig(String wikiId) throws XWikiException... |
| 147 |
|
{ |
| 148 |
4 |
searchSuggestCustomConfigDeleter.deleteSearchSuggestCustomConfig(wikiId); |
| 149 |
|
} |
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
| 153 |
|
|
| 154 |
|
|
| 155 |
|
|
| 156 |
|
|
| 157 |
|
|
| 158 |
|
@param |
| 159 |
|
|
| |
|
| 81.6% |
Uncovered Elements: 7 (38) |
Complexity: 7 |
Complexity Density: 0.27 |
|
| 160 |
3 |
private void restoreDeletedDocuments(String wikiId)... |
| 161 |
|
{ |
| 162 |
3 |
XWikiContext xcontext = getXWikiContext(); |
| 163 |
3 |
XWiki xwiki = xcontext.getWiki(); |
| 164 |
|
|
| 165 |
|
|
| 166 |
3 |
List<DocumentReference> documentsToRestore = new LinkedList<DocumentReference>(); |
| 167 |
3 |
documentsToRestore.add(new DocumentReference(wikiId, XWiki.SYSTEM_SPACE, "AdminRegistrationSheet")); |
| 168 |
3 |
documentsToRestore.add(new DocumentReference(wikiId, XWiki.SYSTEM_SPACE, "RegistrationConfig")); |
| 169 |
3 |
documentsToRestore.add(new DocumentReference(wikiId, XWiki.SYSTEM_SPACE, "RegistrationHelp")); |
| 170 |
3 |
documentsToRestore.add(new DocumentReference(wikiId, XWiki.SYSTEM_SPACE, "AdminUsersSheet")); |
| 171 |
|
|
| 172 |
|
|
| 173 |
3 |
Iterator<DocumentReference> itDocumentsToRestore = documentsToRestore.iterator(); |
| 174 |
15 |
while (itDocumentsToRestore.hasNext()) { |
| 175 |
12 |
DocumentReference docRef = itDocumentsToRestore.next(); |
| 176 |
12 |
if (xwiki.exists(docRef, xcontext)) { |
| 177 |
0 |
itDocumentsToRestore.remove(); |
| 178 |
|
} |
| 179 |
|
} |
| 180 |
|
|
| 181 |
|
|
| 182 |
3 |
if (documentsToRestore.isEmpty()) { |
| 183 |
0 |
return; |
| 184 |
|
} |
| 185 |
|
|
| 186 |
|
|
| 187 |
3 |
restoreDocumentsFromWorkspaceXar(documentsToRestore); |
| 188 |
|
|
| 189 |
|
|
| 190 |
3 |
if (documentsToRestore.isEmpty()) { |
| 191 |
0 |
return; |
| 192 |
|
} |
| 193 |
|
|
| 194 |
|
|
| 195 |
3 |
restoreDocumentFromMainWiki(documentsToRestore); |
| 196 |
|
|
| 197 |
|
|
| 198 |
3 |
if (!documentsToRestore.isEmpty()) { |
| 199 |
3 |
String documentsToRestoreAsString = new String(); |
| 200 |
3 |
int counter = 0; |
| 201 |
3 |
for (DocumentReference d : documentsToRestore) { |
| 202 |
11 |
if (counter++ > 0) { |
| 203 |
8 |
documentsToRestoreAsString += ", "; |
| 204 |
|
} |
| 205 |
11 |
documentsToRestoreAsString += d; |
| 206 |
|
} |
| 207 |
3 |
logger.warn("Failed to restore some documents: [{}]. You should import manually " |
| 208 |
|
+ "(1) xwiki-platform-administration-ui.xar and then (2) xwiki-platform-wiki-ui-wiki.xar into your" |
| 209 |
|
+ " wiki, to restore these documents.", documentsToRestoreAsString); |
| 210 |
|
} |
| 211 |
|
} |
| 212 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 213 |
3 |
private void restoreDocumentsFromWorkspaceXar(List<DocumentReference> documentsToRestore)... |
| 214 |
|
{ |
| 215 |
3 |
DocumentReference installDocumentReference = new DocumentReference(wikiDescriptorManager.getMainWikiId(), |
| 216 |
|
WORKSPACE_CLASS_SPACE, "Install"); |
| 217 |
3 |
try { |
| 218 |
3 |
documentRestorerFromAttachedXAR.restoreDocumentFromAttachedXAR(installDocumentReference, |
| 219 |
|
"workspace-template.xar", documentsToRestore); |
| 220 |
|
} catch (XWikiException e) { |
| 221 |
1 |
logger.error("Error while restoring documents from the Workspace XAR", e); |
| 222 |
|
} |
| 223 |
|
} |
| 224 |
|
|
| |
|
| 93.8% |
Uncovered Elements: 1 (16) |
Complexity: 4 |
Complexity Density: 0.33 |
|
| 225 |
3 |
private void restoreDocumentFromMainWiki(List<DocumentReference> documentsToRestore)... |
| 226 |
|
{ |
| 227 |
3 |
XWikiContext xcontext = getXWikiContext(); |
| 228 |
3 |
XWiki xwiki = xcontext.getWiki(); |
| 229 |
|
|
| 230 |
3 |
WikiReference mainWikiReference = new WikiReference(wikiDescriptorManager.getMainWikiId()); |
| 231 |
|
|
| 232 |
3 |
Iterator<DocumentReference> itDocumentsToRestore = documentsToRestore.iterator(); |
| 233 |
15 |
while (itDocumentsToRestore.hasNext()) { |
| 234 |
12 |
DocumentReference docRef = itDocumentsToRestore.next(); |
| 235 |
|
|
| 236 |
|
|
| 237 |
12 |
DocumentReference mainDocRef = docRef.setWikiReference(mainWikiReference); |
| 238 |
|
|
| 239 |
|
|
| 240 |
12 |
if (xwiki.exists(mainDocRef, xcontext)) { |
| 241 |
1 |
try { |
| 242 |
1 |
xwiki.copyDocument(mainDocRef, docRef, xcontext); |
| 243 |
1 |
itDocumentsToRestore.remove(); |
| 244 |
|
} catch (XWikiException e) { |
| 245 |
0 |
logger.error("Failed to copy [{}] to [{}].", mainDocRef, docRef, e); |
| 246 |
|
} |
| 247 |
|
} |
| 248 |
|
} |
| 249 |
|
} |
| 250 |
|
} |