1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.sheet.internal; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.Collections; |
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.bridge.DocumentAccessBridge; |
33 |
|
import org.xwiki.bridge.DocumentModelBridge; |
34 |
|
import org.xwiki.component.annotation.Component; |
35 |
|
import org.xwiki.context.Execution; |
36 |
|
import org.xwiki.model.reference.DocumentReference; |
37 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
38 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
39 |
|
import org.xwiki.sheet.SheetBinder; |
40 |
|
import org.xwiki.sheet.SheetManager; |
41 |
|
|
42 |
|
|
43 |
|
@link |
44 |
|
|
45 |
|
@version |
46 |
|
@since |
47 |
|
|
48 |
|
@Component |
49 |
|
@Singleton |
|
|
| 93.9% |
Uncovered Elements: 3 (49) |
Complexity: 12 |
Complexity Density: 0.36 |
|
50 |
|
public class DefaultSheetManager implements SheetManager |
51 |
|
{ |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
private static final String SHEET_CLASS = "XWiki.SheetDescriptorClass"; |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
@Inject |
61 |
|
private Logger logger; |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
@Inject |
67 |
|
private Execution execution; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
@Inject |
73 |
|
private DocumentReferenceResolver<String> documentReferenceResolver; |
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
@Inject |
79 |
|
private EntityReferenceSerializer<String> defaultEntityReferenceSerializer; |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
@Inject |
85 |
|
private DocumentAccessBridge documentAccessBridge; |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
@Inject |
91 |
|
private ModelBridge modelBridge; |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
@Inject |
97 |
|
@Named("document") |
98 |
|
private SheetBinder documentSheetBinder; |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
@Inject |
104 |
|
@Named("class") |
105 |
|
private SheetBinder classSheetBinder; |
106 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 5 |
Complexity Density: 0.42 |
|
107 |
25986 |
@Override... |
108 |
|
public List<DocumentReference> getSheets(DocumentModelBridge document, String action) |
109 |
|
{ |
110 |
25983 |
DocumentReference documentReference = document.getDocumentReference(); |
111 |
|
|
112 |
|
|
113 |
|
|
114 |
25980 |
String sheetStringRef = (String) execution.getContext().getProperty("sheet"); |
115 |
25982 |
if (sheetStringRef != null && documentReference.equals(documentAccessBridge.getCurrentDocumentReference())) { |
116 |
287 |
DocumentReference sheetReference = documentReferenceResolver.resolve(sheetStringRef, documentReference); |
117 |
287 |
if (matchSheet(sheetReference, action)) { |
118 |
142 |
return Collections.singletonList(sheetReference); |
119 |
|
} |
120 |
|
} |
121 |
|
|
122 |
|
|
123 |
25842 |
List<DocumentReference> sheets = getDocumentSheets(document, action); |
124 |
25837 |
if (sheets.isEmpty()) { |
125 |
|
|
126 |
|
|
127 |
25613 |
sheets = new ArrayList<DocumentReference>(); |
128 |
25611 |
for (DocumentReference classReference : modelBridge.getXObjectClassReferences(document)) { |
129 |
6002 |
sheets.addAll(getClassSheets(classReference, action)); |
130 |
|
} |
131 |
|
} |
132 |
|
|
133 |
25840 |
return sheets; |
134 |
|
} |
135 |
|
|
136 |
|
|
137 |
|
@param |
138 |
|
@param |
139 |
|
@return |
140 |
|
|
141 |
|
|
142 |
|
|
|
|
| 76.9% |
Uncovered Elements: 3 (13) |
Complexity: 3 |
Complexity Density: 0.27 |
|
143 |
6001 |
private List<DocumentReference> getClassSheets(DocumentReference classReference, String action)... |
144 |
|
{ |
145 |
6001 |
DocumentModelBridge classDocument; |
146 |
6001 |
try { |
147 |
6002 |
classDocument = documentAccessBridge.getDocument(classReference); |
148 |
|
} catch (Exception e) { |
149 |
0 |
String classStringReference = defaultEntityReferenceSerializer.serialize(classReference); |
150 |
0 |
logger.warn("Failed to get class sheets for [{}]. Reason: [{}]", classStringReference, e.getMessage()); |
151 |
0 |
return Collections.emptyList(); |
152 |
|
} |
153 |
6004 |
List<DocumentReference> sheetReferences = new ArrayList<DocumentReference>(); |
154 |
6004 |
for (DocumentReference sheetReference : classSheetBinder.getSheets(classDocument)) { |
155 |
1765 |
if (matchSheet(sheetReference, action)) { |
156 |
1662 |
sheetReferences.add(sheetReference); |
157 |
|
} |
158 |
|
} |
159 |
6004 |
return sheetReferences; |
160 |
|
} |
161 |
|
|
162 |
|
|
163 |
|
@param |
164 |
|
@param |
165 |
|
@return |
166 |
|
|
167 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
168 |
25842 |
private List<DocumentReference> getDocumentSheets(DocumentModelBridge document, String action)... |
169 |
|
{ |
170 |
25840 |
List<DocumentReference> sheets = new ArrayList<DocumentReference>(); |
171 |
25836 |
for (DocumentReference sheetReference : documentSheetBinder.getSheets(document)) { |
172 |
525 |
if (matchSheet(sheetReference, action)) { |
173 |
223 |
sheets.add(sheetReference); |
174 |
|
} |
175 |
|
} |
176 |
25837 |
return sheets; |
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
|
@param |
181 |
|
@param |
182 |
|
@return |
183 |
|
|
184 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
185 |
2575 |
private boolean matchSheet(DocumentReference sheetReference, String expectedAction)... |
186 |
|
{ |
187 |
2577 |
if (!documentAccessBridge.exists(sheetReference)) { |
188 |
510 |
return false; |
189 |
|
} |
190 |
|
|
191 |
2067 |
DocumentReference sheetClassReference = documentReferenceResolver.resolve(SHEET_CLASS, sheetReference); |
192 |
2067 |
String actualAction = (String) documentAccessBridge.getProperty(sheetReference, sheetClassReference, "action"); |
193 |
|
|
194 |
|
|
195 |
2067 |
return StringUtils.isEmpty(expectedAction) || StringUtils.isEmpty(actualAction) |
196 |
|
|| actualAction.equals(expectedAction); |
197 |
|
} |
198 |
|
} |