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