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 javax.script.ScriptContext; |
23 |
|
|
24 |
|
import org.apache.commons.lang3.StringUtils; |
25 |
|
import org.apache.commons.lang3.math.NumberUtils; |
26 |
|
import org.slf4j.Logger; |
27 |
|
import org.slf4j.LoggerFactory; |
28 |
|
import org.xwiki.rendering.syntax.Syntax; |
29 |
|
|
30 |
|
import com.xpn.xwiki.XWikiContext; |
31 |
|
import com.xpn.xwiki.XWikiException; |
32 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
33 |
|
import com.xpn.xwiki.doc.XWikiLock; |
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
@version |
39 |
|
|
|
|
| 72.9% |
Uncovered Elements: 32 (118) |
Complexity: 33 |
Complexity Density: 0.43 |
|
40 |
|
public class EditAction extends XWikiAction |
41 |
|
{ |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(EditAction.class); |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
50 |
58 |
public EditAction()... |
51 |
|
{ |
52 |
58 |
this.waitForXWikiInitialization = false; |
53 |
|
} |
54 |
|
|
|
|
| 45.5% |
Uncovered Elements: 6 (11) |
Complexity: 3 |
Complexity Density: 0.33 |
|
55 |
126 |
@Override... |
56 |
|
public String render(XWikiContext context) throws XWikiException |
57 |
|
{ |
58 |
126 |
try { |
59 |
126 |
XWikiDocument editedDocument = prepareEditedDocument(context); |
60 |
126 |
maybeLockDocument(editedDocument, context); |
61 |
|
} catch (XWikiException e) { |
62 |
0 |
if (e.getCode() == XWikiException.ERROR_XWIKI_APP_DOCUMENT_NOT_EMPTY) { |
63 |
0 |
context.put("exception", e); |
64 |
0 |
return "docalreadyexists"; |
65 |
|
} else { |
66 |
0 |
throw e; |
67 |
|
} |
68 |
|
} |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
126 |
context.put("display", "edit"); |
75 |
126 |
return "edit"; |
76 |
|
} |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
@param |
83 |
|
@return |
84 |
|
@throws |
85 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 2 |
Complexity Density: 0.18 |
|
86 |
129 |
protected XWikiDocument prepareEditedDocument(XWikiContext context) throws XWikiException... |
87 |
|
{ |
88 |
|
|
89 |
129 |
XWikiDocument editedDocument = getEditedDocument(context); |
90 |
129 |
EditForm editForm = (EditForm) context.getForm(); |
91 |
|
|
92 |
|
|
93 |
129 |
editedDocument.readFromTemplate(editForm, context); |
94 |
|
|
95 |
|
|
96 |
129 |
updateDocumentTitleAndContentFromRequest(editedDocument, context); |
97 |
129 |
editedDocument.readObjectsFromForm(editForm, context); |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
129 |
if (editedDocument.isNew()) { |
102 |
72 |
editedDocument.setCreatorReference(context.getUserReference()); |
103 |
72 |
editedDocument.setAuthorReference(context.getUserReference()); |
104 |
72 |
editedDocument.setContentAuthorReference(context.getUserReference()); |
105 |
|
} |
106 |
|
|
107 |
|
|
108 |
129 |
putDocumentOnContext(editedDocument, context); |
109 |
|
|
110 |
129 |
return editedDocument; |
111 |
|
} |
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
@param |
124 |
|
@return |
125 |
|
@throws |
126 |
|
|
|
|
| 69.4% |
Uncovered Elements: 15 (49) |
Complexity: 14 |
Complexity Density: 0.42 |
|
127 |
129 |
private XWikiDocument getEditedDocument(XWikiContext context) throws XWikiException... |
128 |
|
{ |
129 |
129 |
XWikiDocument doc = context.getDoc(); |
130 |
129 |
boolean hasTranslation = doc != context.get("tdoc"); |
131 |
|
|
132 |
|
|
133 |
|
|
134 |
129 |
doc = doc.clone(); |
135 |
129 |
context.put("doc", doc); |
136 |
|
|
137 |
129 |
EditForm editForm = (EditForm) context.getForm(); |
138 |
129 |
doc.readDocMetaFromForm(editForm, context); |
139 |
|
|
140 |
129 |
String language = context.getWiki().getLanguagePreference(context); |
141 |
129 |
if (doc.isNew() && doc.getDefaultLanguage().equals("")) { |
142 |
72 |
doc.setDefaultLanguage(language); |
143 |
|
} |
144 |
|
|
145 |
129 |
String languageToEdit = StringUtils.isEmpty(editForm.getLanguage()) ? language : editForm.getLanguage(); |
146 |
|
|
147 |
|
|
148 |
129 |
if (languageToEdit == null || languageToEdit.equals("default")) { |
149 |
0 |
languageToEdit = ""; |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
129 |
if (doc.isNew() || doc.getDefaultLanguage().equals(languageToEdit)) { |
154 |
113 |
languageToEdit = ""; |
155 |
|
} |
156 |
|
|
157 |
|
|
158 |
129 |
if (!hasTranslation && StringUtils.isEmpty(editForm.getLanguage())) { |
159 |
126 |
languageToEdit = ""; |
160 |
|
} |
161 |
|
|
162 |
|
|
163 |
129 |
XWikiDocument tdoc; |
164 |
129 |
if (languageToEdit.equals("")) { |
165 |
|
|
166 |
129 |
tdoc = doc; |
167 |
129 |
if (doc.isNew()) { |
168 |
72 |
doc.setDefaultLanguage(language); |
169 |
72 |
doc.setLanguage(""); |
170 |
|
} |
171 |
0 |
} else if (!hasTranslation && context.getWiki().isMultiLingual(context)) { |
172 |
|
|
173 |
0 |
tdoc = new XWikiDocument(doc.getDocumentReference()); |
174 |
0 |
tdoc.setLanguage(languageToEdit); |
175 |
0 |
tdoc.setDefaultLocale(doc.getDefaultLocale()); |
176 |
|
|
177 |
|
|
178 |
|
|
179 |
0 |
tdoc.setTitle(doc.getTitle()); |
180 |
0 |
tdoc.setContent(doc.getContent()); |
181 |
0 |
tdoc.setSyntax(doc.getSyntax()); |
182 |
0 |
tdoc.setAuthorReference(context.getUserReference()); |
183 |
0 |
tdoc.setStore(doc.getStore()); |
184 |
|
} else { |
185 |
|
|
186 |
|
|
187 |
0 |
tdoc = ((XWikiDocument) context.get("tdoc")).clone(); |
188 |
|
} |
189 |
|
|
190 |
129 |
return tdoc; |
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
@param |
198 |
|
@param |
199 |
|
@throws |
200 |
|
|
|
|
| 63% |
Uncovered Elements: 10 (27) |
Complexity: 7 |
Complexity Density: 0.47 |
|
201 |
129 |
private void updateDocumentTitleAndContentFromRequest(XWikiDocument document, XWikiContext context)... |
202 |
|
throws XWikiException |
203 |
|
{ |
204 |
|
|
205 |
129 |
boolean sectionEditingEnabled = context.getWiki().hasSectionEdit(context); |
206 |
129 |
int sectionNumber = sectionEditingEnabled ? NumberUtils.toInt(context.getRequest().getParameter("section")) : 0; |
207 |
129 |
getCurrentScriptContext().setAttribute("sectionNumber", sectionNumber, ScriptContext.ENGINE_SCOPE); |
208 |
|
|
209 |
|
|
210 |
129 |
EditForm editForm = (EditForm) context.getForm(); |
211 |
129 |
if (editForm.getContent() != null) { |
212 |
1 |
document.setContent(editForm.getContent()); |
213 |
128 |
} else if (sectionNumber > 0) { |
214 |
0 |
document.setContent(document.getContentOfSection(sectionNumber)); |
215 |
|
} |
216 |
|
|
217 |
|
|
218 |
129 |
if (editForm.getTitle() != null) { |
219 |
31 |
document.setTitle(editForm.getTitle()); |
220 |
98 |
} else if (sectionNumber > 0) { |
221 |
|
|
222 |
|
|
223 |
|
|
224 |
0 |
String sectionTitle = document.getDocumentSection(1).getSectionTitle(); |
225 |
0 |
if (StringUtils.isNotBlank(sectionTitle)) { |
226 |
|
|
227 |
0 |
String sectionPlainTitle = document.getRenderedContent(sectionTitle, document.getSyntax().toIdString(), |
228 |
|
Syntax.PLAIN_1_0.toIdString(), context); |
229 |
0 |
document.setTitle(localizePlainOrKey("core.editors.content.titleField.sectionEditingFormat", |
230 |
|
document.getRenderedTitle(Syntax.PLAIN_1_0, context), sectionNumber, sectionPlainTitle)); |
231 |
|
} |
232 |
|
} |
233 |
|
} |
234 |
|
|
235 |
|
|
236 |
|
|
237 |
|
|
238 |
|
@param |
239 |
|
@param |
240 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
241 |
129 |
private void putDocumentOnContext(XWikiDocument document, XWikiContext context)... |
242 |
|
{ |
243 |
129 |
context.put("tdoc", document); |
244 |
|
|
245 |
|
|
246 |
129 |
context.put("cdoc", context.get("tdoc")); |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
|
253 |
|
@param |
254 |
|
@param |
255 |
|
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 5 |
Complexity Density: 0.83 |
|
256 |
126 |
private void maybeLockDocument(XWikiDocument document, XWikiContext context)... |
257 |
|
{ |
258 |
126 |
try { |
259 |
126 |
XWikiLock lock = document.getLock(context); |
260 |
126 |
EditForm editForm = (EditForm) context.getForm(); |
261 |
126 |
if (lock == null || lock.getUserName().equals(context.getUser()) || editForm.isLockForce()) { |
262 |
124 |
document.setLock(context.getUser(), context); |
263 |
|
} |
264 |
|
} catch (Exception e) { |
265 |
|
|
266 |
0 |
LOGGER.error("Exception while setting up lock", e); |
267 |
|
} |
268 |
|
} |
269 |
|
} |