001
014
015 package com.liferay.portlet.wiki.action;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.json.JSONFactoryUtil;
019 import com.liferay.portal.kernel.json.JSONObject;
020 import com.liferay.portal.kernel.language.LanguageUtil;
021 import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
022 import com.liferay.portal.kernel.repository.model.FileEntry;
023 import com.liferay.portal.kernel.servlet.ServletResponseConstants;
024 import com.liferay.portal.kernel.servlet.SessionErrors;
025 import com.liferay.portal.kernel.servlet.SessionMessages;
026 import com.liferay.portal.kernel.upload.UploadException;
027 import com.liferay.portal.kernel.upload.UploadPortletRequest;
028 import com.liferay.portal.kernel.util.Constants;
029 import com.liferay.portal.kernel.util.KeyValuePair;
030 import com.liferay.portal.kernel.util.ObjectValuePair;
031 import com.liferay.portal.kernel.util.ParamUtil;
032 import com.liferay.portal.kernel.util.StreamUtil;
033 import com.liferay.portal.kernel.util.StringUtil;
034 import com.liferay.portal.kernel.util.TempFileUtil;
035 import com.liferay.portal.kernel.util.Validator;
036 import com.liferay.portal.portletfilerepository.PortletFileRepositoryUtil;
037 import com.liferay.portal.security.auth.PrincipalException;
038 import com.liferay.portal.struts.ActionConstants;
039 import com.liferay.portal.theme.ThemeDisplay;
040 import com.liferay.portal.util.PortalUtil;
041 import com.liferay.portal.util.WebKeys;
042 import com.liferay.portlet.documentlibrary.DuplicateFileException;
043 import com.liferay.portlet.documentlibrary.FileNameException;
044 import com.liferay.portlet.documentlibrary.FileSizeException;
045 import com.liferay.portlet.documentlibrary.action.EditFileEntryAction;
046 import com.liferay.portlet.wiki.NoSuchNodeException;
047 import com.liferay.portlet.wiki.NoSuchPageException;
048 import com.liferay.portlet.wiki.model.WikiPage;
049 import com.liferay.portlet.wiki.service.WikiPageServiceUtil;
050 import com.liferay.portlet.wiki.util.WikiPageAttachmentsUtil;
051
052 import java.io.InputStream;
053
054 import java.util.ArrayList;
055 import java.util.HashMap;
056 import java.util.List;
057 import java.util.Map;
058
059 import javax.portlet.ActionRequest;
060 import javax.portlet.ActionResponse;
061 import javax.portlet.PortletConfig;
062 import javax.portlet.RenderRequest;
063 import javax.portlet.RenderResponse;
064
065 import javax.servlet.http.HttpServletResponse;
066
067 import org.apache.struts.action.ActionForm;
068 import org.apache.struts.action.ActionForward;
069 import org.apache.struts.action.ActionMapping;
070
071
074 public class EditPageAttachmentsAction extends EditFileEntryAction {
075
076 @Override
077 public void processAction(
078 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
079 ActionRequest actionRequest, ActionResponse actionResponse)
080 throws Exception {
081
082 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
083
084 try {
085 if (Validator.isNull(cmd)) {
086 UploadException uploadException =
087 (UploadException)actionRequest.getAttribute(
088 WebKeys.UPLOAD_EXCEPTION);
089
090 if (uploadException != null) {
091 if (uploadException.isExceededSizeLimit()) {
092 throw new FileSizeException(uploadException.getCause());
093 }
094
095 throw new PortalException(uploadException.getCause());
096 }
097 }
098 else if (cmd.equals(Constants.ADD)) {
099 addAttachment(actionRequest);
100 }
101 else if (cmd.equals(Constants.ADD_MULTIPLE)) {
102 addMultipleFileEntries(actionRequest, actionResponse);
103 }
104 else if (cmd.equals(Constants.ADD_TEMP)) {
105 addTempAttachment(actionRequest);
106 }
107 else if (cmd.equals(Constants.DELETE)) {
108 deleteAttachment(
109 (LiferayPortletConfig)portletConfig, actionRequest, false);
110 }
111 else if (cmd.equals(Constants.DELETE_TEMP)) {
112 deleteTempAttachment(actionRequest, actionResponse);
113 }
114 else if (cmd.equals(Constants.EMPTY_TRASH)) {
115 emptyTrash(actionRequest);
116 }
117 else if (cmd.equals(Constants.MOVE_FROM_TRASH)) {
118 restoreAttachmentFromTrash(actionRequest, actionResponse);
119 }
120 else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
121 deleteAttachment(
122 (LiferayPortletConfig)portletConfig, actionRequest, true);
123 }
124 else if (cmd.equals(Constants.RESTORE)) {
125 restoreAttachment(actionRequest);
126 }
127
128 if (cmd.equals(Constants.ADD_TEMP) ||
129 cmd.equals(Constants.DELETE_TEMP)) {
130
131 setForward(actionRequest, ActionConstants.COMMON_NULL);
132 }
133 else {
134 sendRedirect(actionRequest, actionResponse);
135 }
136 }
137 catch (Exception e) {
138 if (e instanceof NoSuchNodeException ||
139 e instanceof NoSuchPageException ||
140 e instanceof PrincipalException) {
141
142 SessionErrors.add(actionRequest, e.getClass());
143
144 setForward(actionRequest, "portlet.wiki.error");
145 }
146 else if (e instanceof DuplicateFileException ||
147 e instanceof FileNameException) {
148
149 SessionErrors.add(actionRequest, e.getClass());
150
151 HttpServletResponse response =
152 PortalUtil.getHttpServletResponse(actionResponse);
153
154 if (e instanceof DuplicateFileException) {
155 response.setStatus(
156 ServletResponseConstants.SC_DUPLICATE_FILE_EXCEPTION);
157 }
158 else {
159 response.setStatus(
160 ServletResponseConstants.SC_FILE_NAME_EXCEPTION);
161 }
162 }
163 else if (e instanceof FileSizeException) {
164 SessionErrors.add(actionRequest, e.getClass());
165 }
166 else {
167 throw e;
168 }
169 }
170 }
171
172 @Override
173 public ActionForward render(
174 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
175 RenderRequest renderRequest, RenderResponse renderResponse)
176 throws Exception {
177
178 try {
179 ActionUtil.getNode(renderRequest);
180 ActionUtil.getPage(renderRequest);
181 }
182 catch (Exception e) {
183 if (e instanceof NoSuchNodeException ||
184 e instanceof NoSuchPageException ||
185 e instanceof PrincipalException) {
186
187 SessionErrors.add(renderRequest, e.getClass());
188
189 return mapping.findForward("portlet.wiki.error");
190 }
191 else {
192 throw e;
193 }
194 }
195
196 return mapping.findForward(
197 getForward(renderRequest, "portlet.wiki.edit_page_attachment"));
198 }
199
200 protected void addAttachment(ActionRequest actionRequest) throws Exception {
201 UploadPortletRequest uploadPortletRequest =
202 PortalUtil.getUploadPortletRequest(actionRequest);
203
204 long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
205 String title = ParamUtil.getString(actionRequest, "title");
206
207 int numOfFiles = ParamUtil.getInteger(actionRequest, "numOfFiles");
208
209 List<ObjectValuePair<String, InputStream>> inputStreamOVPs =
210 new ArrayList<ObjectValuePair<String, InputStream>>();
211
212 try {
213 if (numOfFiles == 0) {
214 String fileName = uploadPortletRequest.getFileName("file");
215 InputStream inputStream = uploadPortletRequest.getFileAsStream(
216 "file");
217
218 if (inputStream != null) {
219 ObjectValuePair<String, InputStream> inputStreamOVP =
220 new ObjectValuePair<String, InputStream>(
221 fileName, inputStream);
222
223 inputStreamOVPs.add(inputStreamOVP);
224 }
225 }
226 else {
227 for (int i = 1; i <= numOfFiles; i++) {
228 String fileName = uploadPortletRequest.getFileName(
229 "file" + i);
230 InputStream inputStream =
231 uploadPortletRequest.getFileAsStream("file" + i);
232
233 if (inputStream == null) {
234 continue;
235 }
236
237 ObjectValuePair<String, InputStream> inputStreamOVP =
238 new ObjectValuePair<String, InputStream>(
239 fileName, inputStream);
240
241 inputStreamOVPs.add(inputStreamOVP);
242 }
243 }
244
245 WikiPageServiceUtil.addPageAttachments(
246 nodeId, title, inputStreamOVPs);
247 }
248 finally {
249 for (ObjectValuePair<String, InputStream> inputStreamOVP :
250 inputStreamOVPs) {
251
252 InputStream inputStream = inputStreamOVP.getValue();
253
254 StreamUtil.cleanUp(inputStream);
255 }
256 }
257 }
258
259 @Override
260 protected void addMultipleFileEntries(
261 ActionRequest actionRequest, ActionResponse actionResponse,
262 String selectedFileName, List<String> validFileNames,
263 List<KeyValuePair> invalidFileNameKVPs)
264 throws Exception {
265
266 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
267 WebKeys.THEME_DISPLAY);
268
269 long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
270 String title = ParamUtil.getString(actionRequest, "title");
271
272 FileEntry tempFileEntry = null;
273
274 try {
275 tempFileEntry = TempFileUtil.getTempFile(
276 themeDisplay.getScopeGroupId(), themeDisplay.getUserId(),
277 selectedFileName, _TEMP_FOLDER_NAME);
278
279 InputStream inputStream = tempFileEntry.getContentStream();
280 String mimeType = tempFileEntry.getMimeType();
281
282 WikiPageServiceUtil.addPageAttachment(
283 nodeId, title, selectedFileName, inputStream, mimeType);
284
285 validFileNames.add(selectedFileName);
286 }
287 catch (Exception e) {
288 String errorMessage = getAddMultipleFileEntriesErrorMessage(
289 themeDisplay, e);
290
291 KeyValuePair invalidFileNameKVP = new KeyValuePair(
292 selectedFileName, errorMessage);
293
294 invalidFileNameKVPs.add(invalidFileNameKVP);
295 }
296 finally {
297 if (tempFileEntry != null) {
298 TempFileUtil.deleteTempFile(tempFileEntry.getFileEntryId());
299 }
300 }
301 }
302
303 protected void addTempAttachment(ActionRequest actionRequest)
304 throws Exception {
305
306 UploadPortletRequest uploadPortletRequest =
307 PortalUtil.getUploadPortletRequest(actionRequest);
308
309 long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
310 String sourceFileName = uploadPortletRequest.getFileName("file");
311
312 InputStream inputStream = null;
313
314 try {
315 inputStream = uploadPortletRequest.getFileAsStream("file");
316
317 String mimeType = uploadPortletRequest.getContentType("file");
318
319 WikiPageServiceUtil.addTempPageAttachment(
320 nodeId, sourceFileName, _TEMP_FOLDER_NAME, inputStream,
321 mimeType);
322 }
323 finally {
324 StreamUtil.cleanUp(inputStream);
325 }
326 }
327
328 protected void deleteAttachment(
329 LiferayPortletConfig liferayPortletConfig,
330 ActionRequest actionRequest, boolean moveToTrash)
331 throws Exception {
332
333 long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
334 String title = ParamUtil.getString(actionRequest, "title");
335 String attachment = ParamUtil.getString(actionRequest, "fileName");
336
337 long dlFileEntryId = 0;
338
339 if (moveToTrash) {
340 dlFileEntryId = WikiPageServiceUtil.movePageAttachmentToTrash(
341 nodeId, title, attachment);
342 }
343 else {
344 WikiPageServiceUtil.deletePageAttachment(nodeId, title, attachment);
345 }
346
347 if (moveToTrash && (dlFileEntryId > 0)) {
348 Map<String, String[]> data = new HashMap<String, String[]>();
349
350 data.put(
351 "restoreEntryIds",
352 new String[] {String.valueOf(dlFileEntryId)});
353
354 SessionMessages.add(
355 actionRequest,
356 liferayPortletConfig.getPortletId() +
357 SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
358
359 SessionMessages.add(
360 actionRequest,
361 liferayPortletConfig.getPortletId() +
362 SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_SUCCESS_MESSAGE);
363 }
364 }
365
366 protected void deleteTempAttachment(
367 ActionRequest actionRequest, ActionResponse actionResponse)
368 throws Exception {
369
370 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
371 WebKeys.THEME_DISPLAY);
372
373 long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
374 String fileName = ParamUtil.getString(actionRequest, "fileName");
375
376 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
377
378 try {
379 WikiPageServiceUtil.deleteTempPageAttachment(
380 nodeId, fileName, _TEMP_FOLDER_NAME);
381
382 jsonObject.put("deleted", Boolean.TRUE);
383 }
384 catch (Exception e) {
385 String errorMessage = LanguageUtil.get(
386 themeDisplay.getLocale(),
387 "an-unexpected-error-occurred-while-deleting-the-file");
388
389 jsonObject.put("deleted", Boolean.FALSE);
390 jsonObject.put("errorMessage", errorMessage);
391 }
392
393 writeJSON(actionRequest, actionResponse, jsonObject);
394 }
395
396 protected void emptyTrash(ActionRequest actionRequest) throws Exception {
397 long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
398 String title = ParamUtil.getString(actionRequest, "title");
399
400 WikiPageServiceUtil.deleteTrashPageAttachments(nodeId, title);
401 }
402
403 protected void restoreAttachment(ActionRequest actionRequest)
404 throws Exception {
405
406 long[] restoreEntryIds = StringUtil.split(
407 ParamUtil.getString(actionRequest, "restoreEntryIds"), 0L);
408
409 for (long restoreEntryId : restoreEntryIds) {
410 FileEntry fileEntry = PortletFileRepositoryUtil.getPortletFileEntry(
411 restoreEntryId);
412
413 WikiPage page = WikiPageAttachmentsUtil.getPage(
414 fileEntry.getFileEntryId());
415
416 WikiPageServiceUtil.restorePageAttachmentFromTrash(
417 page.getNodeId(), page.getTitle(), fileEntry.getTitle());
418 }
419 }
420
421 protected void restoreAttachmentFromTrash(
422 ActionRequest actionRequest, ActionResponse actionResponse)
423 throws Exception {
424
425 long nodeId = ParamUtil.getLong(actionRequest, "nodeId");
426 String title = ParamUtil.getString(actionRequest, "title");
427 String fileName = ParamUtil.getString(actionRequest, "fileName");
428
429 JSONObject jsonObject =
430 com.liferay.portlet.trash.action.ActionUtil.checkEntry(
431 actionRequest);
432
433 writeJSON(actionRequest, actionResponse, jsonObject);
434
435 WikiPageServiceUtil.restorePageAttachmentFromTrash(
436 nodeId, title, fileName);
437 }
438
439 private static final String _TEMP_FOLDER_NAME =
440 EditPageAttachmentsAction.class.getName();
441
442 }