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