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.DLAppServiceUtil;
048    import com.liferay.portlet.trash.util.TrashUtil;
049    
050    import java.util.HashMap;
051    import java.util.Map;
052    
053    import javax.portlet.ActionRequest;
054    import javax.portlet.ActionResponse;
055    import javax.portlet.PortletConfig;
056    import javax.portlet.RenderRequest;
057    import javax.portlet.RenderResponse;
058    import javax.portlet.WindowState;
059    
060    import javax.servlet.http.HttpServletResponse;
061    
062    import org.apache.struts.action.ActionForm;
063    import org.apache.struts.action.ActionForward;
064    import org.apache.struts.action.ActionMapping;
065    
066    /**
067     * @author Brian Wing Shun Chan
068     * @author Sergio Gonz??lez
069     * @author Manuel de la Pe??a
070     * @author Levente Hud??k
071     */
072    public class EditEntryAction extends PortletAction {
073    
074            @Override
075            public void processAction(
076                            ActionMapping actionMapping, ActionForm actionForm,
077                            PortletConfig portletConfig, ActionRequest actionRequest,
078                            ActionResponse actionResponse)
079                    throws Exception {
080    
081                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
082    
083                    try {
084                            if (cmd.equals(Constants.CANCEL_CHECKOUT)) {
085                                    cancelCheckedOutEntries(actionRequest);
086                            }
087                            else if (cmd.equals(Constants.CHECKIN)) {
088                                    checkInEntries(actionRequest);
089                            }
090                            else if (cmd.equals(Constants.CHECKOUT)) {
091                                    checkOutEntries(actionRequest);
092                            }
093                            else if (cmd.equals(Constants.DELETE)) {
094                                    deleteEntries(actionRequest, false);
095                            }
096                            else if (cmd.equals(Constants.MOVE)) {
097                                    moveEntries(actionRequest);
098                            }
099                            else if (cmd.equals(Constants.MOVE_TO_TRASH)) {
100                                    deleteEntries(actionRequest, true);
101                            }
102                            else if (cmd.equals(Constants.RESTORE)) {
103                                    restoreEntries(actionRequest);
104                            }
105    
106                            WindowState windowState = actionRequest.getWindowState();
107    
108                            if (!windowState.equals(LiferayWindowState.POP_UP)) {
109                                    sendRedirect(actionRequest, actionResponse);
110                            }
111                            else {
112                                    String redirect = PortalUtil.escapeRedirect(
113                                            ParamUtil.getString(actionRequest, "redirect"));
114    
115                                    if (Validator.isNotNull(redirect)) {
116                                            actionResponse.sendRedirect(redirect);
117                                    }
118                            }
119                    }
120                    catch (Exception e) {
121                            if (e instanceof DuplicateLockException ||
122                                    e instanceof NoSuchFileEntryException ||
123                                    e instanceof NoSuchFolderException ||
124                                    e instanceof PrincipalException) {
125    
126                                    if (e instanceof DuplicateLockException) {
127                                            DuplicateLockException dle = (DuplicateLockException)e;
128    
129                                            SessionErrors.add(
130                                                    actionRequest, dle.getClass(), dle.getLock());
131                                    }
132                                    else {
133                                            SessionErrors.add(actionRequest, e.getClass());
134                                    }
135    
136                                    setForward(actionRequest, "portlet.document_library.error");
137                            }
138                            else if (e instanceof DuplicateFileException ||
139                                             e instanceof DuplicateFolderNameException ||
140                                             e instanceof SourceFileNameException) {
141    
142                                    if (e instanceof DuplicateFileException) {
143                                            HttpServletResponse response =
144                                                    PortalUtil.getHttpServletResponse(actionResponse);
145    
146                                            response.setStatus(
147                                                    ServletResponseConstants.SC_DUPLICATE_FILE_EXCEPTION);
148                                    }
149    
150                                    SessionErrors.add(actionRequest, e.getClass());
151                            }
152                            else if (e instanceof AssetCategoryException ||
153                                             e instanceof AssetTagException) {
154    
155                                    SessionErrors.add(actionRequest, e.getClass(), e);
156                            }
157                            else {
158                                    throw e;
159                            }
160                    }
161            }
162    
163            @Override
164            public ActionForward render(
165                            ActionMapping actionMapping, ActionForm actionForm,
166                            PortletConfig portletConfig, RenderRequest renderRequest,
167                            RenderResponse renderResponse)
168                    throws Exception {
169    
170                    try {
171                            ActionUtil.getFileEntries(renderRequest);
172                            ActionUtil.getFileShortcuts(renderRequest);
173                            ActionUtil.getFolders(renderRequest);
174                    }
175                    catch (Exception e) {
176                            if (e instanceof NoSuchFileEntryException ||
177                                    e instanceof PrincipalException) {
178    
179                                    SessionErrors.add(renderRequest, e.getClass());
180    
181                                    return actionMapping.findForward(
182                                            "portlet.document_library.error");
183                            }
184                            else {
185                                    throw e;
186                            }
187                    }
188    
189                    String forward = "portlet.document_library.edit_entry";
190    
191                    return actionMapping.findForward(getForward(renderRequest, forward));
192            }
193    
194            protected void cancelCheckedOutEntries(ActionRequest actionRequest)
195                    throws Exception {
196    
197                    long[] fileEntryIds = StringUtil.split(
198                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
199    
200                    for (long fileEntryId : fileEntryIds) {
201                            DLAppServiceUtil.cancelCheckOut(fileEntryId);
202                    }
203            }
204    
205            protected void checkInEntries(ActionRequest actionRequest)
206                    throws Exception {
207    
208                    long[] fileEntryIds = StringUtil.split(
209                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
210    
211                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
212                            actionRequest);
213    
214                    for (long fileEntryId : fileEntryIds) {
215                            DLAppServiceUtil.checkInFileEntry(
216                                    fileEntryId, false, StringPool.BLANK, serviceContext);
217                    }
218            }
219    
220            protected void checkOutEntries(ActionRequest actionRequest)
221                    throws Exception {
222    
223                    long[] fileEntryIds = StringUtil.split(
224                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
225    
226                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
227                            actionRequest);
228    
229                    for (long fileEntryId : fileEntryIds) {
230                            DLAppServiceUtil.checkOutFileEntry(fileEntryId, serviceContext);
231                    }
232            }
233    
234            protected void deleteEntries(
235                            ActionRequest actionRequest, boolean moveToTrash)
236                    throws Exception {
237    
238                    String deleteEntryClassName = null;
239                    String deleteEntryTitle = null;
240    
241                    long[] deleteFolderIds = StringUtil.split(
242                            ParamUtil.getString(actionRequest, "folderIds"), 0L);
243    
244                    for (int i = 0; i < deleteFolderIds.length; i++) {
245                            long deleteFolderId = deleteFolderIds[i];
246    
247                            if (moveToTrash) {
248                                    Folder folder = DLAppServiceUtil.moveFolderToTrash(
249                                            deleteFolderId);
250    
251                                    if (i == 0) {
252                                            deleteEntryClassName = DLFolder.class.getName();
253                                            deleteEntryTitle = TrashUtil.getOriginalTitle(
254                                                    folder.getName());
255                                    }
256                            }
257                            else {
258                                    DLAppServiceUtil.deleteFolder(deleteFolderId);
259                            }
260                    }
261    
262                    // Delete file shortcuts before file entries. See LPS-21348.
263    
264                    long[] deleteFileShortcutIds = StringUtil.split(
265                            ParamUtil.getString(actionRequest, "fileShortcutIds"), 0L);
266    
267                    for (int i = 0; i < deleteFileShortcutIds.length; i++) {
268                            long deleteFileShortcutId = deleteFileShortcutIds[i];
269    
270                            if (moveToTrash) {
271                                    DLFileShortcut fileShortcut =
272                                            DLAppServiceUtil.moveFileShortcutToTrash(
273                                                    deleteFileShortcutId);
274    
275                                    if (i == 0) {
276                                            deleteEntryClassName = DLFileShortcut.class.getName();
277    
278                                            deleteEntryTitle = fileShortcut.getToTitle();
279                                    }
280                            }
281                            else {
282                                    DLAppServiceUtil.deleteFileShortcut(deleteFileShortcutId);
283                            }
284                    }
285    
286                    long[] deleteFileEntryIds = StringUtil.split(
287                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
288    
289                    for (int i = 0; i < deleteFileEntryIds.length; i++) {
290                            long deleteFileEntryId = deleteFileEntryIds[i];
291    
292                            if (moveToTrash) {
293                                    FileEntry fileEntry = DLAppServiceUtil.moveFileEntryToTrash(
294                                            deleteFileEntryId);
295    
296                                    if (i == 0) {
297                                            deleteEntryClassName = DLFileEntry.class.getName();
298                                            deleteEntryTitle = TrashUtil.getOriginalTitle(
299                                                    fileEntry.getTitle());
300                                    }
301                            }
302                            else {
303                                    DLAppServiceUtil.deleteFileEntry(deleteFileEntryId);
304                            }
305                    }
306    
307                    if (moveToTrash &&
308                            ((deleteFileEntryIds.length > 0) ||
309                             (deleteFileShortcutIds.length > 0) ||
310                             (deleteFolderIds.length > 0))) {
311    
312                            Map<String, String[]> data = new HashMap<String, String[]>();
313    
314                            if (Validator.isNotNull(deleteEntryClassName)) {
315                                    data.put(
316                                            "deleteEntryClassName",
317                                            new String[] {deleteEntryClassName});
318                            }
319    
320                            if (Validator.isNotNull(deleteEntryTitle)) {
321                                    data.put("deleteEntryTitle", new String[] {deleteEntryTitle});
322                            }
323    
324                            data.put(
325                                    "restoreFileEntryIds",
326                                    ArrayUtil.toStringArray(deleteFileEntryIds));
327                            data.put(
328                                    "restoreFileShortcutIds",
329                                    ArrayUtil.toStringArray(deleteFileShortcutIds));
330                            data.put(
331                                    "restoreFolderIds", ArrayUtil.toStringArray(deleteFolderIds));
332    
333                            SessionMessages.add(
334                                    actionRequest,
335                                    PortalUtil.getPortletId(actionRequest) +
336                                            SessionMessages.KEY_SUFFIX_DELETE_SUCCESS_DATA, data);
337    
338                            hideDefaultSuccessMessage(actionRequest);
339                    }
340            }
341    
342            protected void moveEntries(ActionRequest actionRequest) throws Exception {
343                    long newFolderId = ParamUtil.getLong(actionRequest, "newFolderId");
344    
345                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
346                            DLFileEntry.class.getName(), actionRequest);
347    
348                    long[] folderIds = StringUtil.split(
349                            ParamUtil.getString(actionRequest, "folderIds"), 0L);
350    
351                    for (long folderId : folderIds) {
352                            DLAppServiceUtil.moveFolder(folderId, newFolderId, serviceContext);
353                    }
354    
355                    long[] fileEntryIds = StringUtil.split(
356                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
357    
358                    for (long fileEntryId : fileEntryIds) {
359                            DLAppServiceUtil.moveFileEntry(
360                                    fileEntryId, newFolderId, serviceContext);
361                    }
362    
363                    long[] fileShortcutIds = StringUtil.split(
364                            ParamUtil.getString(actionRequest, "fileShortcutIds"), 0L);
365    
366                    for (long fileShortcutId : fileShortcutIds) {
367                            if (fileShortcutId == 0) {
368                                    continue;
369                            }
370    
371                            DLFileShortcut fileShortcut = DLAppServiceUtil.getFileShortcut(
372                                    fileShortcutId);
373    
374                            DLAppServiceUtil.updateFileShortcut(
375                                    fileShortcutId, newFolderId, fileShortcut.getToFileEntryId(),
376                                    serviceContext);
377                    }
378            }
379    
380            protected void restoreEntries(ActionRequest actionRequest)
381                    throws PortalException, SystemException {
382    
383                    long[] restoreFolderIds = StringUtil.split(
384                            ParamUtil.getString(actionRequest, "restoreFolderIds"), 0L);
385    
386                    for (long restoreFolderId : restoreFolderIds) {
387                            DLAppServiceUtil.restoreFolderFromTrash(restoreFolderId);
388                    }
389    
390                    long[] restoreFileEntryIds = StringUtil.split(
391                            ParamUtil.getString(actionRequest, "restoreFileEntryIds"), 0L);
392    
393                    for (long restoreFileEntryId : restoreFileEntryIds) {
394                            DLAppServiceUtil.restoreFileEntryFromTrash(restoreFileEntryId);
395                    }
396    
397                    long[] restoreFileShortcutIds = StringUtil.split(
398                            ParamUtil.getString(actionRequest, "restoreFileShortcutIds"), 0L);
399    
400                    for (long restoreFileShortcutId : restoreFileShortcutIds) {
401                            DLAppServiceUtil.restoreFileShortcutFromTrash(
402                                    restoreFileShortcutId);
403                    }
404            }
405    
406    }