001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.action;
016    
017    import com.liferay.portal.DuplicateLockException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.portlet.LiferayWindowState;
021    import com.liferay.portal.kernel.repository.model.FileEntry;
022    import com.liferay.portal.kernel.repository.model.Folder;
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.util.ArrayUtil;
027    import com.liferay.portal.kernel.util.Constants;
028    import com.liferay.portal.kernel.util.ParamUtil;
029    import com.liferay.portal.kernel.util.StringPool;
030    import com.liferay.portal.kernel.util.StringUtil;
031    import com.liferay.portal.kernel.util.Validator;
032    import com.liferay.portal.security.auth.PrincipalException;
033    import com.liferay.portal.service.ServiceContext;
034    import com.liferay.portal.service.ServiceContextFactory;
035    import com.liferay.portal.struts.PortletAction;
036    import com.liferay.portal.util.PortalUtil;
037    import com.liferay.portlet.asset.AssetCategoryException;
038    import com.liferay.portlet.asset.AssetTagException;
039    import com.liferay.portlet.documentlibrary.DuplicateFileException;
040    import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
041    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
042    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
043    import com.liferay.portlet.documentlibrary.SourceFileNameException;
044    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
045    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
046    import com.liferay.portlet.documentlibrary.model.DLFolder;
047    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
048    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
049    import com.liferay.portlet.trash.util.TrashUtil;
050    
051    import java.util.HashMap;
052    import java.util.Map;
053    
054    import javax.portlet.ActionRequest;
055    import javax.portlet.ActionResponse;
056    import javax.portlet.PortletConfig;
057    import javax.portlet.RenderRequest;
058    import javax.portlet.RenderResponse;
059    import javax.portlet.WindowState;
060    
061    import javax.servlet.http.HttpServletResponse;
062    
063    import org.apache.struts.action.ActionForm;
064    import org.apache.struts.action.ActionForward;
065    import org.apache.struts.action.ActionMapping;
066    
067    /**
068     * @author Brian Wing Shun Chan
069     * @author Sergio Gonz??lez
070     * @author Manuel de la Pe??a
071     * @author Levente Hud??k
072     */
073    public class EditEntryAction extends PortletAction {
074    
075            @Override
076            public void processAction(
077                            ActionMapping actionMapping, ActionForm actionForm,
078                            PortletConfig portletConfig, ActionRequest actionRequest,
079                            ActionResponse actionResponse)
080                    throws Exception {
081    
082                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
083    
084                    try {
085                            if (cmd.equals(Constants.CANCEL_CHECKOUT)) {
086                                    cancelCheckedOutEntries(actionRequest);
087                            }
088                            else if (cmd.equals(Constants.CHECKIN)) {
089                                    checkInEntries(actionRequest);
090                            }
091                            else if (cmd.equals(Constants.CHECKOUT)) {
092                                    checkOutEntries(actionRequest);
093                            }
094                            else if (cmd.equals(Constants.DELETE)) {
095                                    deleteEntries(actionRequest, false);
096                            }
097                            else if (cmd.equals(Constants.MOVE)) {
098                                    moveEntries(actionRequest);
099                            }
100                            else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
101                                    deleteEntries(actionRequest, true);
102                            }
103                            else if (cmd.equals(Constants.RESTORE)) {
104                                    restoreEntries(actionRequest);
105                            }
106    
107                            WindowState windowState = actionRequest.getWindowState();
108    
109                            if (!windowState.equals(LiferayWindowState.POP_UP)) {
110                                    sendRedirect(actionRequest, actionResponse);
111                            }
112                            else {
113                                    String redirect = PortalUtil.escapeRedirect(
114                                            ParamUtil.getString(actionRequest, "redirect"));
115    
116                                    if (Validator.isNotNull(redirect)) {
117                                            actionResponse.sendRedirect(redirect);
118                                    }
119                            }
120                    }
121                    catch (Exception e) {
122                            if (e instanceof DuplicateLockException ||
123                                    e instanceof NoSuchFileEntryException ||
124                                    e instanceof NoSuchFolderException ||
125                                    e instanceof PrincipalException) {
126    
127                                    if (e instanceof DuplicateLockException) {
128                                            DuplicateLockException dle = (DuplicateLockException)e;
129    
130                                            SessionErrors.add(
131                                                    actionRequest, dle.getClass(), dle.getLock());
132                                    }
133                                    else {
134                                            SessionErrors.add(actionRequest, e.getClass());
135                                    }
136    
137                                    setForward(actionRequest, "portlet.document_library.error");
138                            }
139                            else if (e instanceof DuplicateFileException ||
140                                             e instanceof DuplicateFolderNameException ||
141                                             e instanceof SourceFileNameException) {
142    
143                                    if (e instanceof DuplicateFileException) {
144                                            HttpServletResponse response =
145                                                    PortalUtil.getHttpServletResponse(actionResponse);
146    
147                                            response.setStatus(
148                                                    ServletResponseConstants.SC_DUPLICATE_FILE_EXCEPTION);
149                                    }
150    
151                                    SessionErrors.add(actionRequest, e.getClass());
152                            }
153                            else if (e instanceof AssetCategoryException ||
154                                             e instanceof AssetTagException) {
155    
156                                    SessionErrors.add(actionRequest, e.getClass(), e);
157                            }
158                            else {
159                                    throw e;
160                            }
161                    }
162            }
163    
164            @Override
165            public ActionForward render(
166                            ActionMapping actionMapping, ActionForm actionForm,
167                            PortletConfig portletConfig, RenderRequest renderRequest,
168                            RenderResponse renderResponse)
169                    throws Exception {
170    
171                    try {
172                            ActionUtil.getFileEntries(renderRequest);
173                            ActionUtil.getFileShortcuts(renderRequest);
174                            ActionUtil.getFolders(renderRequest);
175                    }
176                    catch (Exception e) {
177                            if (e instanceof NoSuchFileEntryException ||
178                                    e instanceof PrincipalException) {
179    
180                                    SessionErrors.add(renderRequest, e.getClass());
181    
182                                    return actionMapping.findForward(
183                                            "portlet.document_library.error");
184                            }
185                            else {
186                                    throw e;
187                            }
188                    }
189    
190                    String forward = "portlet.document_library.edit_entry";
191    
192                    return actionMapping.findForward(getForward(renderRequest, forward));
193            }
194    
195            protected void cancelCheckedOutEntries(ActionRequest actionRequest)
196                    throws Exception {
197    
198                    long[] fileEntryIds = StringUtil.split(
199                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
200    
201                    for (long fileEntryId : fileEntryIds) {
202                            DLAppServiceUtil.cancelCheckOut(fileEntryId);
203                    }
204    
205                    long[] fileShortcutIds = StringUtil.split(
206                            ParamUtil.getString(actionRequest, "fileShortcutIds"), 0L);
207    
208                    for (long fileShortcutId : fileShortcutIds) {
209                            DLFileShortcut fileShortcut = DLAppLocalServiceUtil.getFileShortcut(
210                                    fileShortcutId);
211    
212                            DLAppServiceUtil.cancelCheckOut(fileShortcut.getToFileEntryId());
213                    }
214            }
215    
216            protected void checkInEntries(ActionRequest actionRequest)
217                    throws Exception {
218    
219                    long[] fileEntryIds = StringUtil.split(
220                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
221    
222                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
223                            actionRequest);
224    
225                    for (long fileEntryId : fileEntryIds) {
226                            DLAppServiceUtil.checkInFileEntry(
227                                    fileEntryId, false, StringPool.BLANK, serviceContext);
228                    }
229    
230                    long[] fileShortcutIds = StringUtil.split(
231                            ParamUtil.getString(actionRequest, "fileShortcutIds"), 0L);
232    
233                    for (long fileShortcutId : fileShortcutIds) {
234                            DLFileShortcut fileShortcut = DLAppLocalServiceUtil.getFileShortcut(
235                                    fileShortcutId);
236    
237                            DLAppServiceUtil.checkInFileEntry(
238                                    fileShortcut.getToFileEntryId(), false, StringPool.BLANK,
239                                    serviceContext);
240                    }
241            }
242    
243            protected void checkOutEntries(ActionRequest actionRequest)
244                    throws Exception {
245    
246                    long[] fileEntryIds = StringUtil.split(
247                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
248    
249                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
250                            actionRequest);
251    
252                    for (long fileEntryId : fileEntryIds) {
253                            DLAppServiceUtil.checkOutFileEntry(fileEntryId, serviceContext);
254                    }
255    
256                    long[] fileShortcutIds = StringUtil.split(
257                            ParamUtil.getString(actionRequest, "fileShortcutIds"), 0L);
258    
259                    for (long fileShortcutId : fileShortcutIds) {
260                            DLFileShortcut fileShortcut = DLAppLocalServiceUtil.getFileShortcut(
261                                    fileShortcutId);
262    
263                            DLAppServiceUtil.checkOutFileEntry(
264                                    fileShortcut.getToFileEntryId(), serviceContext);
265                    }
266            }
267    
268            protected void deleteEntries(
269                            ActionRequest actionRequest, boolean moveToTrash)
270                    throws Exception {
271    
272                    String deleteEntryClassName = null;
273                    String deleteEntryTitle = null;
274    
275                    long[] deleteFolderIds = StringUtil.split(
276                            ParamUtil.getString(actionRequest, "folderIds"), 0L);
277    
278                    for (int i = 0; i < deleteFolderIds.length; i++) {
279                            long deleteFolderId = deleteFolderIds[i];
280    
281                            if (moveToTrash) {
282                                    Folder folder = DLAppServiceUtil.moveFolderToTrash(
283                                            deleteFolderId);
284    
285                                    if (i == 0) {
286                                            deleteEntryClassName = DLFolder.class.getName();
287                                            deleteEntryTitle = TrashUtil.getOriginalTitle(
288                                                    folder.getName());
289                                    }
290                            }
291                            else {
292                                    DLAppServiceUtil.deleteFolder(deleteFolderId);
293                            }
294                    }
295    
296                    // Delete file shortcuts before file entries. See LPS-21348.
297    
298                    long[] deleteFileShortcutIds = StringUtil.split(
299                            ParamUtil.getString(actionRequest, "fileShortcutIds"), 0L);
300    
301                    for (int i = 0; i < deleteFileShortcutIds.length; i++) {
302                            long deleteFileShortcutId = deleteFileShortcutIds[i];
303    
304                            if (moveToTrash) {
305                                    DLFileShortcut fileShortcut =
306                                            DLAppServiceUtil.moveFileShortcutToTrash(
307                                                    deleteFileShortcutId);
308    
309                                    if (i == 0) {
310                                            deleteEntryClassName = DLFileShortcut.class.getName();
311    
312                                            deleteEntryTitle = fileShortcut.getToTitle();
313                                    }
314                            }
315                            else {
316                                    DLAppServiceUtil.deleteFileShortcut(deleteFileShortcutId);
317                            }
318                    }
319    
320                    long[] deleteFileEntryIds = StringUtil.split(
321                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
322    
323                    for (int i = 0; i < deleteFileEntryIds.length; i++) {
324                            long deleteFileEntryId = deleteFileEntryIds[i];
325    
326                            if (moveToTrash) {
327                                    FileEntry fileEntry = DLAppServiceUtil.moveFileEntryToTrash(
328                                            deleteFileEntryId);
329    
330                                    if (i == 0) {
331                                            deleteEntryClassName = DLFileEntry.class.getName();
332                                            deleteEntryTitle = TrashUtil.getOriginalTitle(
333                                                    fileEntry.getTitle());
334                                    }
335                            }
336                            else {
337                                    DLAppServiceUtil.deleteFileEntry(deleteFileEntryId);
338                            }
339                    }
340    
341                    if (moveToTrash &&
342                            ((deleteFileEntryIds.length > 0) ||
343                             (deleteFileShortcutIds.length > 0) ||
344                             (deleteFolderIds.length > 0))) {
345    
346                            Map<String, String[]> data = new HashMap<String, String[]>();
347    
348                            if (Validator.isNotNull(deleteEntryClassName)) {
349                                    data.put(
350                                            "deleteEntryClassName",
351                                            new String[] {deleteEntryClassName});
352                            }
353    
354                            if (Validator.isNotNull(deleteEntryTitle)) {
355                                    data.put("deleteEntryTitle", new String[] {deleteEntryTitle});
356                            }
357    
358                            data.put(
359                                    "restoreFileEntryIds",
360                                    ArrayUtil.toStringArray(deleteFileEntryIds));
361                            data.put(
362                                    "restoreFileShortcutIds",
363                                    ArrayUtil.toStringArray(deleteFileShortcutIds));
364                            data.put(
365                                    "restoreFolderIds", ArrayUtil.toStringArray(deleteFolderIds));
366    
367                            SessionMessages.add(
368                                    actionRequest,
369                                    PortalUtil.getPortletId(actionRequest) +
370                                            SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
371    
372                            hideDefaultSuccessMessage(actionRequest);
373                    }
374            }
375    
376            protected void moveEntries(ActionRequest actionRequest) throws Exception {
377                    long newFolderId = ParamUtil.getLong(actionRequest, "newFolderId");
378    
379                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
380                            DLFileEntry.class.getName(), actionRequest);
381    
382                    long[] folderIds = StringUtil.split(
383                            ParamUtil.getString(actionRequest, "folderIds"), 0L);
384    
385                    for (long folderId : folderIds) {
386                            DLAppServiceUtil.moveFolder(folderId, newFolderId, serviceContext);
387                    }
388    
389                    long[] fileEntryIds = StringUtil.split(
390                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
391    
392                    for (long fileEntryId : fileEntryIds) {
393                            DLAppServiceUtil.moveFileEntry(
394                                    fileEntryId, newFolderId, serviceContext);
395                    }
396    
397                    long[] fileShortcutIds = StringUtil.split(
398                            ParamUtil.getString(actionRequest, "fileShortcutIds"), 0L);
399    
400                    for (long fileShortcutId : fileShortcutIds) {
401                            if (fileShortcutId == 0) {
402                                    continue;
403                            }
404    
405                            DLFileShortcut fileShortcut = DLAppServiceUtil.getFileShortcut(
406                                    fileShortcutId);
407    
408                            DLAppServiceUtil.updateFileShortcut(
409                                    fileShortcutId, newFolderId, fileShortcut.getToFileEntryId(),
410                                    serviceContext);
411                    }
412            }
413    
414            protected void restoreEntries(ActionRequest actionRequest)
415                    throws PortalException, SystemException {
416    
417                    long[] restoreFolderIds = StringUtil.split(
418                            ParamUtil.getString(actionRequest, "restoreFolderIds"), 0L);
419    
420                    for (long restoreFolderId : restoreFolderIds) {
421                            DLAppServiceUtil.restoreFolderFromTrash(restoreFolderId);
422                    }
423    
424                    long[] restoreFileEntryIds = StringUtil.split(
425                            ParamUtil.getString(actionRequest, "restoreFileEntryIds"), 0L);
426    
427                    for (long restoreFileEntryId : restoreFileEntryIds) {
428                            DLAppServiceUtil.restoreFileEntryFromTrash(restoreFileEntryId);
429                    }
430    
431                    long[] restoreFileShortcutIds = StringUtil.split(
432                            ParamUtil.getString(actionRequest, "restoreFileShortcutIds"), 0L);
433    
434                    for (long restoreFileShortcutId : restoreFileShortcutIds) {
435                            DLAppServiceUtil.restoreFileShortcutFromTrash(
436                                    restoreFileShortcutId);
437                    }
438            }
439    
440    }