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.InvalidFolderException;
042    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
043    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
044    import com.liferay.portlet.documentlibrary.SourceFileNameException;
045    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
046    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
047    import com.liferay.portlet.documentlibrary.model.DLFolder;
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                                             e instanceof InvalidFolderException) {
156    
157                                    SessionErrors.add(actionRequest, e.getClass(), e);
158                            }
159                            else {
160                                    throw e;
161                            }
162                    }
163            }
164    
165            @Override
166            public ActionForward render(
167                            ActionMapping actionMapping, ActionForm actionForm,
168                            PortletConfig portletConfig, RenderRequest renderRequest,
169                            RenderResponse renderResponse)
170                    throws Exception {
171    
172                    try {
173                            ActionUtil.getFileEntries(renderRequest);
174                            ActionUtil.getFileShortcuts(renderRequest);
175                            ActionUtil.getFolders(renderRequest);
176                    }
177                    catch (Exception e) {
178                            if (e instanceof NoSuchFileEntryException ||
179                                    e instanceof PrincipalException) {
180    
181                                    SessionErrors.add(renderRequest, e.getClass());
182    
183                                    return actionMapping.findForward(
184                                            "portlet.document_library.error");
185                            }
186                            else {
187                                    throw e;
188                            }
189                    }
190    
191                    String forward = "portlet.document_library.edit_entry";
192    
193                    return actionMapping.findForward(getForward(renderRequest, forward));
194            }
195    
196            protected void cancelCheckedOutEntries(ActionRequest actionRequest)
197                    throws Exception {
198    
199                    long[] fileEntryIds = StringUtil.split(
200                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
201    
202                    for (long fileEntryId : fileEntryIds) {
203                            DLAppServiceUtil.cancelCheckOut(fileEntryId);
204                    }
205            }
206    
207            protected void checkInEntries(ActionRequest actionRequest)
208                    throws Exception {
209    
210                    long[] fileEntryIds = StringUtil.split(
211                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
212    
213                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
214                            actionRequest);
215    
216                    for (long fileEntryId : fileEntryIds) {
217                            DLAppServiceUtil.checkInFileEntry(
218                                    fileEntryId, false, StringPool.BLANK, serviceContext);
219                    }
220            }
221    
222            protected void checkOutEntries(ActionRequest actionRequest)
223                    throws Exception {
224    
225                    long[] fileEntryIds = StringUtil.split(
226                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
227    
228                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
229                            actionRequest);
230    
231                    for (long fileEntryId : fileEntryIds) {
232                            DLAppServiceUtil.checkOutFileEntry(fileEntryId, serviceContext);
233                    }
234            }
235    
236            protected void deleteEntries(
237                            ActionRequest actionRequest, boolean moveToTrash)
238                    throws Exception {
239    
240                    String deleteEntryClassName = null;
241                    String deleteEntryTitle = null;
242    
243                    long[] deleteFolderIds = StringUtil.split(
244                            ParamUtil.getString(actionRequest, "folderIds"), 0L);
245    
246                    for (int i = 0; i < deleteFolderIds.length; i++) {
247                            long deleteFolderId = deleteFolderIds[i];
248    
249                            if (moveToTrash) {
250                                    Folder folder = DLAppServiceUtil.moveFolderToTrash(
251                                            deleteFolderId);
252    
253                                    if (i == 0) {
254                                            deleteEntryClassName = DLFolder.class.getName();
255                                            deleteEntryTitle = TrashUtil.getOriginalTitle(
256                                                    folder.getName());
257                                    }
258                            }
259                            else {
260                                    DLAppServiceUtil.deleteFolder(deleteFolderId);
261                            }
262                    }
263    
264                    // Delete file shortcuts before file entries. See LPS-21348.
265    
266                    long[] deleteFileShortcutIds = StringUtil.split(
267                            ParamUtil.getString(actionRequest, "fileShortcutIds"), 0L);
268    
269                    for (int i = 0; i < deleteFileShortcutIds.length; i++) {
270                            long deleteFileShortcutId = deleteFileShortcutIds[i];
271    
272                            if (moveToTrash) {
273                                    DLFileShortcut fileShortcut =
274                                            DLAppServiceUtil.moveFileShortcutToTrash(
275                                                    deleteFileShortcutId);
276    
277                                    if (i == 0) {
278                                            deleteEntryClassName = DLFileShortcut.class.getName();
279    
280                                            deleteEntryTitle = fileShortcut.getToTitle();
281                                    }
282                            }
283                            else {
284                                    DLAppServiceUtil.deleteFileShortcut(deleteFileShortcutId);
285                            }
286                    }
287    
288                    long[] deleteFileEntryIds = StringUtil.split(
289                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
290    
291                    for (int i = 0; i < deleteFileEntryIds.length; i++) {
292                            long deleteFileEntryId = deleteFileEntryIds[i];
293    
294                            if (moveToTrash) {
295                                    FileEntry fileEntry = DLAppServiceUtil.moveFileEntryToTrash(
296                                            deleteFileEntryId);
297    
298                                    if (i == 0) {
299                                            deleteEntryClassName = DLFileEntry.class.getName();
300                                            deleteEntryTitle = TrashUtil.getOriginalTitle(
301                                                    fileEntry.getTitle());
302                                    }
303                            }
304                            else {
305                                    DLAppServiceUtil.deleteFileEntry(deleteFileEntryId);
306                            }
307                    }
308    
309                    if (moveToTrash &&
310                            ((deleteFileEntryIds.length > 0) ||
311                             (deleteFileShortcutIds.length > 0) ||
312                             (deleteFolderIds.length > 0))) {
313    
314                            Map<String, String[]> data = new HashMap<String, String[]>();
315    
316                            if (Validator.isNotNull(deleteEntryClassName)) {
317                                    data.put(
318                                            "deleteEntryClassName",
319                                            new String[] {deleteEntryClassName});
320                            }
321    
322                            if (Validator.isNotNull(deleteEntryTitle)) {
323                                    data.put("deleteEntryTitle", new String[] {deleteEntryTitle});
324                            }
325    
326                            data.put(
327                                    "restoreFileEntryIds",
328                                    ArrayUtil.toStringArray(deleteFileEntryIds));
329                            data.put(
330                                    "restoreFileShortcutIds",
331                                    ArrayUtil.toStringArray(deleteFileShortcutIds));
332                            data.put(
333                                    "restoreFolderIds", ArrayUtil.toStringArray(deleteFolderIds));
334    
335                            SessionMessages.add(
336                                    actionRequest,
337                                    PortalUtil.getPortletId(actionRequest) +
338                                            SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
339    
340                            hideDefaultSuccessMessage(actionRequest);
341                    }
342            }
343    
344            protected void moveEntries(ActionRequest actionRequest) throws Exception {
345                    long newFolderId = ParamUtil.getLong(actionRequest, "newFolderId");
346    
347                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
348                            DLFileEntry.class.getName(), actionRequest);
349    
350                    long[] folderIds = StringUtil.split(
351                            ParamUtil.getString(actionRequest, "folderIds"), 0L);
352    
353                    for (long folderId : folderIds) {
354                            DLAppServiceUtil.moveFolder(folderId, newFolderId, serviceContext);
355                    }
356    
357                    long[] fileEntryIds = StringUtil.split(
358                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
359    
360                    for (long fileEntryId : fileEntryIds) {
361                            DLAppServiceUtil.moveFileEntry(
362                                    fileEntryId, newFolderId, serviceContext);
363                    }
364    
365                    long[] fileShortcutIds = StringUtil.split(
366                            ParamUtil.getString(actionRequest, "fileShortcutIds"), 0L);
367    
368                    for (long fileShortcutId : fileShortcutIds) {
369                            if (fileShortcutId == 0) {
370                                    continue;
371                            }
372    
373                            DLFileShortcut fileShortcut = DLAppServiceUtil.getFileShortcut(
374                                    fileShortcutId);
375    
376                            DLAppServiceUtil.updateFileShortcut(
377                                    fileShortcutId, newFolderId, fileShortcut.getToFileEntryId(),
378                                    serviceContext);
379                    }
380            }
381    
382            protected void restoreEntries(ActionRequest actionRequest)
383                    throws PortalException, SystemException {
384    
385                    long[] restoreFolderIds = StringUtil.split(
386                            ParamUtil.getString(actionRequest, "restoreFolderIds"), 0L);
387    
388                    for (long restoreFolderId : restoreFolderIds) {
389                            DLAppServiceUtil.restoreFolderFromTrash(restoreFolderId);
390                    }
391    
392                    long[] restoreFileEntryIds = StringUtil.split(
393                            ParamUtil.getString(actionRequest, "restoreFileEntryIds"), 0L);
394    
395                    for (long restoreFileEntryId : restoreFileEntryIds) {
396                            DLAppServiceUtil.restoreFileEntryFromTrash(restoreFileEntryId);
397                    }
398    
399                    long[] restoreFileShortcutIds = StringUtil.split(
400                            ParamUtil.getString(actionRequest, "restoreFileShortcutIds"), 0L);
401    
402                    for (long restoreFileShortcutId : restoreFileShortcutIds) {
403                            DLAppServiceUtil.restoreFileShortcutFromTrash(
404                                    restoreFileShortcutId);
405                    }
406            }
407    
408    }