1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.script.sheet; |
21 |
|
|
22 |
|
import java.lang.reflect.Field; |
23 |
|
import java.lang.reflect.Method; |
24 |
|
import java.util.ArrayList; |
25 |
|
import java.util.List; |
26 |
|
|
27 |
|
import javax.inject.Inject; |
28 |
|
import javax.inject.Named; |
29 |
|
import javax.inject.Singleton; |
30 |
|
|
31 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
32 |
|
import org.xwiki.bridge.DocumentModelBridge; |
33 |
|
import org.xwiki.component.annotation.Component; |
34 |
|
import org.xwiki.model.reference.DocumentReference; |
35 |
|
import org.xwiki.script.service.ScriptService; |
36 |
|
import org.xwiki.sheet.SheetBinder; |
37 |
|
import org.xwiki.sheet.SheetManager; |
38 |
|
|
39 |
|
import com.xpn.xwiki.api.Document; |
40 |
|
|
41 |
|
|
42 |
|
@link@link |
43 |
|
|
44 |
|
@version |
45 |
|
@since |
46 |
|
|
47 |
|
@Component |
48 |
|
@Named("sheet") |
49 |
|
@Singleton |
|
|
| 79.5% |
Uncovered Elements: 8 (39) |
Complexity: 14 |
Complexity Density: 0.54 |
|
50 |
|
public class SheetScriptService implements ScriptService |
51 |
|
{ |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
@Inject |
56 |
|
private SheetManager sheetManager; |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
@Inject |
62 |
|
@Named("class") |
63 |
|
private SheetBinder classSheetBinder; |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
@Inject |
69 |
|
@Named("document") |
70 |
|
private SheetBinder documentSheetBinder; |
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
@Inject |
76 |
|
private DocumentAccessBridge documentAccessBridge; |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
@param |
82 |
|
@param |
83 |
|
@return |
84 |
|
|
85 |
|
|
86 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
87 |
50 |
public List<DocumentReference> getSheets(Document document, String action)... |
88 |
|
{ |
89 |
50 |
return filterViewable(this.sheetManager.getSheets(getReadOnlyDocument(document), action)); |
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
@param |
96 |
|
@return |
97 |
|
|
98 |
|
|
99 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
100 |
12 |
public List<DocumentReference> getClassSheets(Document classDocument)... |
101 |
|
{ |
102 |
12 |
return filterViewable(this.classSheetBinder.getSheets(getReadOnlyDocument(classDocument))); |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
@param |
109 |
|
@param |
110 |
|
@return |
111 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
112 |
2 |
public boolean bindClassSheet(Document classDocument, DocumentReference sheetReference)... |
113 |
|
{ |
114 |
2 |
return this.classSheetBinder.bind(getModifiableDocument(classDocument), sheetReference); |
115 |
|
} |
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
@param |
122 |
|
@param |
123 |
|
@return |
124 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
125 |
0 |
public boolean unbindClassSheet(Document classDocument, DocumentReference sheetReference)... |
126 |
|
{ |
127 |
0 |
return this.classSheetBinder.unbind(getModifiableDocument(classDocument), sheetReference); |
128 |
|
} |
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
@param |
134 |
|
@return |
135 |
|
|
136 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
137 |
12 |
public List<DocumentReference> getDocumentSheets(Document document)... |
138 |
|
{ |
139 |
12 |
return filterViewable(this.documentSheetBinder.getSheets(getReadOnlyDocument(document))); |
140 |
|
} |
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
@param |
146 |
|
@param |
147 |
|
@return |
148 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
149 |
0 |
public boolean bindDocumentSheet(Document document, DocumentReference sheetReference)... |
150 |
|
{ |
151 |
0 |
return this.documentSheetBinder.bind(getModifiableDocument(document), sheetReference); |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
@param |
158 |
|
@param |
159 |
|
@return |
160 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
161 |
0 |
public boolean unbindDocumentSheet(Document document, DocumentReference sheetReference)... |
162 |
|
{ |
163 |
0 |
return this.documentSheetBinder.unbind(getModifiableDocument(document), sheetReference); |
164 |
|
} |
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
@param |
170 |
|
@return |
171 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
172 |
1 |
public List<DocumentReference> getDocuments(DocumentReference sheetReference)... |
173 |
|
{ |
174 |
1 |
List<DocumentReference> documents = new ArrayList<DocumentReference>(); |
175 |
1 |
documents.addAll(this.documentSheetBinder.getDocuments(sheetReference)); |
176 |
1 |
documents.addAll(this.classSheetBinder.getDocuments(sheetReference)); |
177 |
1 |
return filterViewable(documents); |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
|
@param |
182 |
|
@return |
183 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
184 |
75 |
private List<DocumentReference> filterViewable(List<DocumentReference> documentReferences)... |
185 |
|
{ |
186 |
75 |
List<DocumentReference> viewable = new ArrayList<DocumentReference>(); |
187 |
75 |
for (DocumentReference documentReference : documentReferences) { |
188 |
46 |
if (this.documentAccessBridge.isDocumentViewable(documentReference)) { |
189 |
44 |
viewable.add(documentReference); |
190 |
|
} |
191 |
|
} |
192 |
75 |
return viewable; |
193 |
|
} |
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
@param@link |
199 |
|
@return@link |
200 |
|
|
201 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
202 |
74 |
private DocumentModelBridge getReadOnlyDocument(Document document)... |
203 |
|
{ |
204 |
74 |
try { |
205 |
|
|
206 |
|
|
207 |
74 |
Field docField = Document.class.getDeclaredField("doc"); |
208 |
74 |
docField.setAccessible(true); |
209 |
74 |
return (DocumentModelBridge) docField.get(document); |
210 |
|
} catch (Exception e) { |
211 |
0 |
throw new RuntimeException("Failed to access the XWikiDocument instance wrapped by the document API.", e); |
212 |
|
} |
213 |
|
} |
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
@param@link |
219 |
|
@return@link |
220 |
|
|
221 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
222 |
2 |
private DocumentModelBridge getModifiableDocument(Document document)... |
223 |
|
{ |
224 |
2 |
try { |
225 |
|
|
226 |
|
|
227 |
2 |
Method getDocMethod = Document.class.getDeclaredMethod("getDoc", new Class[] {}); |
228 |
2 |
getDocMethod.setAccessible(true); |
229 |
2 |
return (DocumentModelBridge) getDocMethod.invoke(document); |
230 |
|
} catch (Exception e) { |
231 |
0 |
throw new RuntimeException( |
232 |
|
"Failed to get the modifiable XWikiDocument instance wrapped by the document API.", e); |
233 |
|
} |
234 |
|
} |
235 |
|
} |