1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.web; |
21 |
|
|
22 |
|
import java.util.Arrays; |
23 |
|
import java.util.List; |
24 |
|
import java.util.Locale; |
25 |
|
|
26 |
|
import javax.script.ScriptContext; |
27 |
|
import javax.servlet.http.HttpServletResponse; |
28 |
|
|
29 |
|
import org.apache.commons.lang3.StringUtils; |
30 |
|
import org.xwiki.job.Job; |
31 |
|
import org.xwiki.localization.LocaleUtils; |
32 |
|
import org.xwiki.model.EntityType; |
33 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
34 |
|
import org.xwiki.model.reference.EntityReference; |
35 |
|
import org.xwiki.refactoring.job.CreateRequest; |
36 |
|
import org.xwiki.refactoring.script.RefactoringScriptService; |
37 |
|
import org.xwiki.script.service.ScriptService; |
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.doc.XWikiLock; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
@version |
51 |
|
|
|
|
| 59.3% |
Uncovered Elements: 61 (150) |
Complexity: 40 |
Complexity Density: 0.41 |
|
52 |
|
public class SaveAction extends PreviewAction |
53 |
|
{ |
54 |
|
|
55 |
|
public static final String ACTION_NAME = "save"; |
56 |
|
|
57 |
|
protected static final String ASYNC_PARAM = "async"; |
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
private static final EntityReference REDIRECT_CLASS = |
64 |
|
new EntityReference("RedirectClass", EntityType.DOCUMENT, new EntityReference("XWiki", EntityType.SPACE)); |
65 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
66 |
39 |
public SaveAction()... |
67 |
|
{ |
68 |
39 |
this.waitForXWikiInitialization = true; |
69 |
|
} |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
@param@link |
75 |
|
@return |
76 |
|
|
77 |
|
@throws |
78 |
|
|
79 |
|
|
|
|
| 55.6% |
Uncovered Elements: 44 (99) |
Complexity: 26 |
Complexity Density: 0.39 |
|
80 |
138 |
public boolean save(XWikiContext context) throws XWikiException... |
81 |
|
{ |
82 |
138 |
XWiki xwiki = context.getWiki(); |
83 |
138 |
XWikiRequest request = context.getRequest(); |
84 |
138 |
XWikiDocument doc = context.getDoc(); |
85 |
138 |
EditForm form = (EditForm) context.getForm(); |
86 |
|
|
87 |
|
|
88 |
138 |
int sectionNumber = 0; |
89 |
138 |
if (request.getParameter("section") != null && xwiki.hasSectionEdit(context)) { |
90 |
0 |
sectionNumber = Integer.parseInt(request.getParameter("section")); |
91 |
|
} |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
138 |
doc = doc.clone(); |
97 |
|
|
98 |
138 |
String language = form.getLanguage(); |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
138 |
XWikiDocument tdoc; |
103 |
|
|
104 |
138 |
if (doc.isNew() || (language == null) || (language.equals("")) || (language.equals("default")) |
105 |
|
|| (language.equals(doc.getDefaultLanguage()))) { |
106 |
|
|
107 |
|
|
108 |
138 |
tdoc = doc; |
109 |
|
} else { |
110 |
0 |
tdoc = doc.getTranslatedDocument(language, context); |
111 |
0 |
if ((tdoc == doc) && xwiki.isMultiLingual(context)) { |
112 |
|
|
113 |
0 |
tdoc = new XWikiDocument(doc.getDocumentReference()); |
114 |
0 |
tdoc.setLanguage(language); |
115 |
0 |
tdoc.setStore(doc.getStore()); |
116 |
0 |
} else if (tdoc != doc) { |
117 |
|
|
118 |
|
|
119 |
0 |
tdoc = tdoc.clone(); |
120 |
|
} |
121 |
|
} |
122 |
|
|
123 |
138 |
if (doc.isNew()) { |
124 |
85 |
doc.setLocale(Locale.ROOT); |
125 |
85 |
if (doc.getDefaultLocale() == Locale.ROOT) { |
126 |
85 |
doc.setDefaultLocale( |
127 |
|
LocaleUtils.toLocale(context.getWiki().getLanguagePreference(context), Locale.ROOT)); |
128 |
|
} |
129 |
|
} |
130 |
|
|
131 |
138 |
try { |
132 |
138 |
tdoc.readFromTemplate(form.getTemplate(), context); |
133 |
|
} catch (XWikiException e) { |
134 |
0 |
if (e.getCode() == XWikiException.ERROR_XWIKI_APP_DOCUMENT_NOT_EMPTY) { |
135 |
0 |
context.put("exception", e); |
136 |
0 |
return true; |
137 |
|
} |
138 |
|
} |
139 |
|
|
140 |
138 |
if (sectionNumber != 0) { |
141 |
0 |
XWikiDocument sectionDoc = tdoc.clone(); |
142 |
0 |
sectionDoc.readFromForm(form, context); |
143 |
0 |
String sectionContent = sectionDoc.getContent() + "\n"; |
144 |
0 |
String content = tdoc.updateDocumentSection(sectionNumber, sectionContent); |
145 |
0 |
tdoc.setContent(content); |
146 |
0 |
tdoc.setComment(sectionDoc.getComment()); |
147 |
0 |
tdoc.setMinorEdit(sectionDoc.isMinorEdit()); |
148 |
|
} else { |
149 |
138 |
tdoc.readFromForm(form, context); |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
138 |
String username = context.getUser(); |
154 |
138 |
tdoc.setAuthor(username); |
155 |
138 |
if (tdoc.isNew()) { |
156 |
85 |
tdoc.setCreator(username); |
157 |
|
} |
158 |
|
|
159 |
|
|
160 |
138 |
tdoc.setMetaDataDirty(true); |
161 |
|
|
162 |
|
|
163 |
138 |
if ("1".equals(request.getParameter("xvalidate"))) { |
164 |
0 |
boolean validationResult = tdoc.validate(context); |
165 |
|
|
166 |
0 |
if (validationResult == false) { |
167 |
|
|
168 |
0 |
context.put("display", "edit"); |
169 |
|
|
170 |
0 |
context.setAction(tdoc.getDefaultEditMode(context)); |
171 |
|
|
172 |
0 |
context.put("doc", doc); |
173 |
0 |
context.put("cdoc", tdoc); |
174 |
0 |
context.put("tdoc", tdoc); |
175 |
|
|
176 |
0 |
getCurrentScriptContext().setAttribute("editor", "inline", ScriptContext.ENGINE_SCOPE); |
177 |
|
|
178 |
0 |
return true; |
179 |
|
} |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
|
|
184 |
138 |
if (tdoc.getXObject(REDIRECT_CLASS) != null && request.getParameter("XWiki.RedirectClass_0_location") == null) { |
185 |
0 |
tdoc.removeXObjects(REDIRECT_CLASS); |
186 |
|
} |
187 |
|
|
188 |
|
|
189 |
|
|
190 |
138 |
xwiki.saveDocument(tdoc, tdoc.getComment(), tdoc.isMinorEdit(), context); |
191 |
|
|
192 |
138 |
Job createJob = startCreateJob(tdoc.getDocumentReference(), form); |
193 |
138 |
if (createJob != null) { |
194 |
17 |
if (isAsync(request)) { |
195 |
14 |
if (Utils.isAjaxRequest(context)) { |
196 |
|
|
197 |
14 |
sendRedirect(context.getResponse(), String.format("%s/rest/jobstatus/%s?media=json", |
198 |
|
context.getRequest().getContextPath(), serializeJobId(createJob.getRequest().getId()))); |
199 |
|
} |
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
} else { |
204 |
|
|
205 |
3 |
try { |
206 |
3 |
createJob.join(); |
207 |
|
} catch (InterruptedException e) { |
208 |
0 |
throw new XWikiException(String.format( |
209 |
|
"Interrupted while waiting for template [%s] to be processed when creating the document [%s]", |
210 |
|
form.getTemplate(), tdoc.getDocumentReference()), e); |
211 |
|
} |
212 |
|
} |
213 |
|
} else { |
214 |
|
|
215 |
121 |
XWikiLock lock = tdoc.getLock(context); |
216 |
121 |
if (lock != null) { |
217 |
46 |
tdoc.removeLock(context); |
218 |
|
} |
219 |
|
} |
220 |
|
|
221 |
138 |
return false; |
222 |
|
} |
223 |
|
|
|
|
| 71.4% |
Uncovered Elements: 4 (14) |
Complexity: 4 |
Complexity Density: 0.5 |
|
224 |
123 |
@Override... |
225 |
|
public boolean action(XWikiContext context) throws XWikiException |
226 |
|
{ |
227 |
|
|
228 |
123 |
if (!csrfTokenCheck(context)) { |
229 |
0 |
return false; |
230 |
|
} |
231 |
|
|
232 |
123 |
if (save(context)) { |
233 |
0 |
return true; |
234 |
|
} |
235 |
|
|
236 |
123 |
if (Utils.isAjaxRequest(context)) { |
237 |
14 |
context.getResponse().setStatus(HttpServletResponse.SC_NO_CONTENT); |
238 |
|
} else { |
239 |
109 |
sendRedirect(context.getResponse(), Utils.getRedirect("view", context)); |
240 |
|
} |
241 |
123 |
return false; |
242 |
|
} |
243 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 4 |
Complexity Density: 0.67 |
|
244 |
0 |
@Override... |
245 |
|
public String render(XWikiContext context) throws XWikiException |
246 |
|
{ |
247 |
0 |
XWikiException e = (XWikiException) context.get("exception"); |
248 |
0 |
if ((e != null) && (e.getCode() == XWikiException.ERROR_XWIKI_APP_DOCUMENT_NOT_EMPTY)) { |
249 |
0 |
return "docalreadyexists"; |
250 |
|
} |
251 |
|
|
252 |
0 |
if ("edit".equals(context.get("display"))) { |
253 |
|
|
254 |
|
|
255 |
|
|
256 |
0 |
return context.getAction(); |
257 |
|
} |
258 |
|
|
259 |
0 |
return "exception"; |
260 |
|
} |
261 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
262 |
17 |
private boolean isAsync(XWikiRequest request)... |
263 |
|
{ |
264 |
17 |
return "true".equals(request.get(ASYNC_PARAM)); |
265 |
|
} |
266 |
|
|
|
|
| 88.2% |
Uncovered Elements: 2 (17) |
Complexity: 3 |
Complexity Density: 0.23 |
|
267 |
138 |
private Job startCreateJob(EntityReference entityReference, EditForm editForm) throws XWikiException... |
268 |
|
{ |
269 |
138 |
if (StringUtils.isBlank(editForm.getTemplate())) { |
270 |
|
|
271 |
121 |
return null; |
272 |
|
} |
273 |
|
|
274 |
|
|
275 |
|
|
276 |
|
|
277 |
17 |
RefactoringScriptService refactoring = |
278 |
|
(RefactoringScriptService) Utils.getComponent(ScriptService.class, "refactoring"); |
279 |
|
|
280 |
17 |
CreateRequest request = refactoring.createCreateRequest(Arrays.asList(entityReference)); |
281 |
|
|
282 |
17 |
request.setEntityReferences(Arrays.asList(entityReference)); |
283 |
|
|
284 |
17 |
DocumentReferenceResolver<String> resolver = |
285 |
|
Utils.getComponent(DocumentReferenceResolver.TYPE_STRING, "currentmixed"); |
286 |
17 |
EntityReference templateReference = resolver.resolve(editForm.getTemplate()); |
287 |
17 |
request.setTemplateReference(templateReference); |
288 |
|
|
289 |
|
|
290 |
17 |
request.setSkippedEntities(Arrays.asList(entityReference)); |
291 |
|
|
292 |
17 |
Job createJob = refactoring.create(request); |
293 |
17 |
if (createJob != null) { |
294 |
17 |
return createJob; |
295 |
|
} else { |
296 |
0 |
throw new XWikiException(String.format("Failed to schedule the create job for [%s]", entityReference), |
297 |
|
refactoring.getLastError()); |
298 |
|
} |
299 |
|
} |
300 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
301 |
14 |
private String serializeJobId(List<String> jobId)... |
302 |
|
{ |
303 |
14 |
return StringUtils.join(jobId, "/"); |
304 |
|
} |
305 |
|
} |