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