| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.wiki.internal.descriptor.document; |
| 21 |
|
|
| 22 |
|
import java.util.ArrayList; |
| 23 |
|
import java.util.List; |
| 24 |
|
|
| 25 |
|
import javax.inject.Inject; |
| 26 |
|
import javax.inject.Named; |
| 27 |
|
import javax.inject.Provider; |
| 28 |
|
import javax.inject.Singleton; |
| 29 |
|
|
| 30 |
|
import org.apache.commons.lang3.StringUtils; |
| 31 |
|
import org.xwiki.component.annotation.Component; |
| 32 |
|
import org.xwiki.component.manager.ComponentManager; |
| 33 |
|
import org.xwiki.model.reference.DocumentReference; |
| 34 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
| 35 |
|
import org.xwiki.model.reference.WikiReference; |
| 36 |
|
import org.xwiki.query.Query; |
| 37 |
|
import org.xwiki.query.QueryException; |
| 38 |
|
import org.xwiki.query.QueryFilter; |
| 39 |
|
import org.xwiki.query.QueryManager; |
| 40 |
|
import org.xwiki.wiki.descriptor.WikiDescriptorManager; |
| 41 |
|
import org.xwiki.wiki.manager.WikiManagerException; |
| 42 |
|
|
| 43 |
|
import com.xpn.xwiki.XWiki; |
| 44 |
|
import com.xpn.xwiki.XWikiContext; |
| 45 |
|
import com.xpn.xwiki.XWikiException; |
| 46 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
@version |
| 51 |
|
@since |
| 52 |
|
|
| 53 |
|
@Component |
| 54 |
|
@Singleton |
| |
|
| 36.9% |
Uncovered Elements: 41 (65) |
Complexity: 18 |
Complexity Density: 0.39 |
|
| 55 |
|
public class DefaultWikiDescriptorDocumentHelper implements WikiDescriptorDocumentHelper |
| 56 |
|
{ |
| 57 |
|
@Inject |
| 58 |
|
private Provider<XWikiContext> xcontextProvider; |
| 59 |
|
|
| 60 |
|
@Inject |
| 61 |
|
private QueryManager queryManager; |
| 62 |
|
|
| 63 |
|
@Inject |
| 64 |
|
private Provider<WikiDescriptorManager> wikiDescriptorManagerProvider; |
| 65 |
|
|
| 66 |
|
@Inject |
| 67 |
|
@Named("current") |
| 68 |
|
private DocumentReferenceResolver<String> documentReferenceResolver; |
| 69 |
|
|
| 70 |
|
@Inject |
| 71 |
|
private ComponentManager componentManager; |
| 72 |
|
|
| |
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 73 |
36 |
@Override... |
| 74 |
|
public DocumentReference getDocumentReferenceFromId(String wikiId) |
| 75 |
|
{ |
| 76 |
36 |
if (wikiId == null) { |
| 77 |
0 |
throw new IllegalArgumentException("Wiki id cannot be null"); |
| 78 |
|
} |
| 79 |
|
|
| 80 |
36 |
WikiDescriptorManager wikiDescriptorManager = wikiDescriptorManagerProvider.get(); |
| 81 |
36 |
return new DocumentReference(wikiDescriptorManager.getMainWikiId(), |
| 82 |
|
XWiki.SYSTEM_SPACE, String.format("XWikiServer%s", StringUtils.capitalize(wikiId))); |
| 83 |
|
} |
| 84 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 85 |
70 |
@Override... |
| 86 |
|
public String getWikiIdFromDocumentReference(DocumentReference descriptorDocumentReference) |
| 87 |
|
{ |
| 88 |
70 |
String docName = descriptorDocumentReference.getName(); |
| 89 |
70 |
return StringUtils.removeStart(docName, "XWikiServer").toLowerCase(); |
| 90 |
|
} |
| 91 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 92 |
61 |
@Override... |
| 93 |
|
public String getWikiIdFromDocumentFullname(String descriptorDocumentFullname) |
| 94 |
|
{ |
| 95 |
61 |
return StringUtils.removeStart(descriptorDocumentFullname, "XWiki.XWikiServer").toLowerCase(); |
| 96 |
|
} |
| 97 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 98 |
35 |
@Override... |
| 99 |
|
public XWikiDocument getDocumentFromWikiId(String wikiId) throws WikiManagerException |
| 100 |
|
{ |
| 101 |
35 |
return getDocument(getDocumentReferenceFromId(wikiId)); |
| 102 |
|
} |
| 103 |
|
|
| |
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 5 |
Complexity Density: 0.38 |
|
| 104 |
0 |
@Override... |
| 105 |
|
public DocumentReference findXWikiServerClassDocumentReference(String wikiAlias) throws WikiManagerException |
| 106 |
|
{ |
| 107 |
0 |
if (wikiAlias == null) { |
| 108 |
0 |
throw new IllegalArgumentException("Wiki alias cannot be null"); |
| 109 |
|
} |
| 110 |
|
|
| 111 |
0 |
WikiDescriptorManager wikiDescriptorManager = wikiDescriptorManagerProvider.get(); |
| 112 |
|
|
| 113 |
0 |
DocumentReference result = null; |
| 114 |
|
|
| 115 |
0 |
try { |
| 116 |
0 |
Query query = this.queryManager.createQuery( |
| 117 |
|
"where doc.object(XWiki.XWikiServerClass).server = :wikiAlias and doc.name like 'XWikiServer%'", |
| 118 |
|
Query.XWQL); |
| 119 |
0 |
query.bindValue("wikiAlias", wikiAlias); |
| 120 |
0 |
query.setWiki(wikiDescriptorManager.getMainWikiId()); |
| 121 |
0 |
List<String> documentNames = query.execute(); |
| 122 |
|
|
| 123 |
|
|
| 124 |
0 |
if (documentNames != null && !documentNames.isEmpty()) { |
| 125 |
0 |
result = documentReferenceResolver.resolve(documentNames.get(0)); |
| 126 |
|
} |
| 127 |
|
} catch (QueryException e) { |
| 128 |
0 |
throw new WikiManagerException(String.format( |
| 129 |
|
"Failed to locate XWiki.XWikiServerClass document for wiki alias [%s]", wikiAlias), e); |
| 130 |
|
} |
| 131 |
|
|
| 132 |
0 |
return result; |
| 133 |
|
} |
| 134 |
|
|
| |
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 135 |
0 |
@Override... |
| 136 |
|
public XWikiDocument findXWikiServerClassDocument(String wikiAlias) throws WikiManagerException |
| 137 |
|
{ |
| 138 |
0 |
XWikiDocument document = null; |
| 139 |
|
|
| 140 |
0 |
DocumentReference documentReference = findXWikiServerClassDocumentReference(wikiAlias); |
| 141 |
0 |
if (documentReference != null) { |
| 142 |
0 |
document = getDocument(documentReference); |
| 143 |
|
} |
| 144 |
|
|
| 145 |
0 |
return document; |
| 146 |
|
} |
| 147 |
|
|
| |
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
| 148 |
0 |
@Override... |
| 149 |
|
public List<XWikiDocument> getAllXWikiServerClassDocument() throws WikiManagerException |
| 150 |
|
{ |
| 151 |
0 |
WikiDescriptorManager wikiDescriptorManager = wikiDescriptorManagerProvider.get(); |
| 152 |
|
|
| 153 |
0 |
List<XWikiDocument> result = new ArrayList<XWikiDocument>(); |
| 154 |
|
|
| 155 |
0 |
List<String> documentNames = getAllXWikiServerClassDocumentNames(); |
| 156 |
0 |
if (documentNames != null) { |
| 157 |
0 |
WikiReference mainWikiReference = new WikiReference(wikiDescriptorManager.getMainWikiId()); |
| 158 |
0 |
for (String documentName : documentNames) { |
| 159 |
0 |
result.add(getDocument(documentReferenceResolver.resolve(documentName, mainWikiReference))); |
| 160 |
|
} |
| 161 |
|
} |
| 162 |
|
|
| 163 |
0 |
return result; |
| 164 |
|
} |
| 165 |
|
|
| |
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
| 166 |
50 |
@Override... |
| 167 |
|
public List<String> getAllXWikiServerClassDocumentNames() throws WikiManagerException |
| 168 |
|
{ |
| 169 |
50 |
WikiDescriptorManager wikiDescriptorManager = wikiDescriptorManagerProvider.get(); |
| 170 |
|
|
| 171 |
50 |
try { |
| 172 |
50 |
Query query = this.queryManager.createQuery( |
| 173 |
|
"from doc.object(XWiki.XWikiServerClass) as descriptor where doc.name like 'XWikiServer%' " |
| 174 |
|
+ "and doc.fullName <> 'XWiki.XWikiServerClassTemplate'", |
| 175 |
|
Query.XWQL); |
| 176 |
50 |
query.setWiki(wikiDescriptorManager.getMainWikiId()); |
| 177 |
50 |
query.addFilter(componentManager.<QueryFilter>getInstance(QueryFilter.class, "unique")); |
| 178 |
50 |
return query.execute(); |
| 179 |
|
} catch (Exception e) { |
| 180 |
0 |
throw new WikiManagerException("Failed to locate XWiki.XWikiServerClass documents", e); |
| 181 |
|
} |
| 182 |
|
} |
| 183 |
|
|
| |
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 184 |
35 |
private XWikiDocument getDocument(DocumentReference reference) throws WikiManagerException... |
| 185 |
|
{ |
| 186 |
35 |
XWikiContext context = xcontextProvider.get(); |
| 187 |
35 |
com.xpn.xwiki.XWiki xwiki = context.getWiki(); |
| 188 |
35 |
try { |
| 189 |
35 |
return xwiki.getDocument(reference, context); |
| 190 |
|
} catch (XWikiException e) { |
| 191 |
0 |
throw new WikiManagerException(String.format( |
| 192 |
|
"Failed to get document [%s] containing a XWiki.XWikiServerClass object", reference), e); |
| 193 |
|
} |
| 194 |
|
} |
| 195 |
|
} |