001    /**
002     * Copyright (c) 2000-present 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.portlet.LiferayWindowState;
019    import com.liferay.portal.kernel.repository.model.FileEntry;
020    import com.liferay.portal.kernel.repository.model.Folder;
021    import com.liferay.portal.kernel.servlet.ServletResponseConstants;
022    import com.liferay.portal.kernel.servlet.SessionErrors;
023    import com.liferay.portal.kernel.util.Constants;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.model.TrashedModel;
029    import com.liferay.portal.security.auth.PrincipalException;
030    import com.liferay.portal.service.ServiceContext;
031    import com.liferay.portal.service.ServiceContextFactory;
032    import com.liferay.portal.struts.PortletAction;
033    import com.liferay.portal.util.PortalUtil;
034    import com.liferay.portlet.asset.AssetCategoryException;
035    import com.liferay.portlet.asset.AssetTagException;
036    import com.liferay.portlet.documentlibrary.DuplicateFileException;
037    import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
038    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
039    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
040    import com.liferay.portlet.documentlibrary.SourceFileNameException;
041    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
042    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
043    import com.liferay.portlet.documentlibrary.model.DLFolder;
044    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
045    import com.liferay.portlet.trash.util.TrashUtil;
046    
047    import java.util.ArrayList;
048    import java.util.List;
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 actionMapping, ActionForm actionForm,
074                            PortletConfig portletConfig, ActionRequest actionRequest,
075                            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(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(actionRequest, true);
098                            }
099    
100                            WindowState windowState = actionRequest.getWindowState();
101    
102                            if (!windowState.equals(LiferayWindowState.POP_UP)) {
103                                    sendRedirect(actionRequest, actionResponse);
104                            }
105                            else {
106                                    String redirect = PortalUtil.escapeRedirect(
107                                            ParamUtil.getString(actionRequest, "redirect"));
108    
109                                    if (Validator.isNotNull(redirect)) {
110                                            actionResponse.sendRedirect(redirect);
111                                    }
112                            }
113                    }
114                    catch (Exception e) {
115                            if (e instanceof DuplicateLockException ||
116                                    e instanceof NoSuchFileEntryException ||
117                                    e instanceof NoSuchFolderException ||
118                                    e instanceof PrincipalException) {
119    
120                                    if (e instanceof DuplicateLockException) {
121                                            DuplicateLockException dle = (DuplicateLockException)e;
122    
123                                            SessionErrors.add(
124                                                    actionRequest, dle.getClass(), dle.getLock());
125                                    }
126                                    else {
127                                            SessionErrors.add(actionRequest, e.getClass());
128                                    }
129    
130                                    setForward(actionRequest, "portlet.document_library.error");
131                            }
132                            else if (e instanceof DuplicateFileException ||
133                                             e instanceof DuplicateFolderNameException ||
134                                             e instanceof SourceFileNameException) {
135    
136                                    if (e instanceof DuplicateFileException) {
137                                            HttpServletResponse response =
138                                                    PortalUtil.getHttpServletResponse(actionResponse);
139    
140                                            response.setStatus(
141                                                    ServletResponseConstants.SC_DUPLICATE_FILE_EXCEPTION);
142                                    }
143    
144                                    SessionErrors.add(actionRequest, e.getClass());
145                            }
146                            else if (e instanceof AssetCategoryException ||
147                                             e instanceof AssetTagException) {
148    
149                                    SessionErrors.add(actionRequest, e.getClass(), e);
150                            }
151                            else {
152                                    throw e;
153                            }
154                    }
155            }
156    
157            @Override
158            public ActionForward render(
159                            ActionMapping actionMapping, ActionForm actionForm,
160                            PortletConfig portletConfig, RenderRequest renderRequest,
161                            RenderResponse renderResponse)
162                    throws Exception {
163    
164                    try {
165                            ActionUtil.getFileEntries(renderRequest);
166                            ActionUtil.getFileShortcuts(renderRequest);
167                            ActionUtil.getFolders(renderRequest);
168                    }
169                    catch (Exception e) {
170                            if (e instanceof NoSuchFileEntryException ||
171                                    e instanceof PrincipalException) {
172    
173                                    SessionErrors.add(renderRequest, e.getClass());
174    
175                                    return actionMapping.findForward(
176                                            "portlet.document_library.error");
177                            }
178                            else {
179                                    throw e;
180                            }
181                    }
182    
183                    String forward = "portlet.document_library.edit_entry";
184    
185                    return actionMapping.findForward(getForward(renderRequest, forward));
186            }
187    
188            protected void cancelCheckedOutEntries(ActionRequest actionRequest)
189                    throws Exception {
190    
191                    long[] fileEntryIds = StringUtil.split(
192                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
193    
194                    for (long fileEntryId : fileEntryIds) {
195                            DLAppServiceUtil.cancelCheckOut(fileEntryId);
196                    }
197            }
198    
199            protected void checkInEntries(ActionRequest actionRequest)
200                    throws Exception {
201    
202                    long[] fileEntryIds = StringUtil.split(
203                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
204    
205                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
206                            actionRequest);
207    
208                    for (long fileEntryId : fileEntryIds) {
209                            DLAppServiceUtil.checkInFileEntry(
210                                    fileEntryId, false, StringPool.BLANK, serviceContext);
211                    }
212            }
213    
214            protected void checkOutEntries(ActionRequest actionRequest)
215                    throws Exception {
216    
217                    long[] fileEntryIds = StringUtil.split(
218                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
219    
220                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
221                            actionRequest);
222    
223                    for (long fileEntryId : fileEntryIds) {
224                            DLAppServiceUtil.checkOutFileEntry(fileEntryId, serviceContext);
225                    }
226            }
227    
228            protected void deleteEntries(
229                            ActionRequest actionRequest, boolean moveToTrash)
230                    throws Exception {
231    
232                    long[] deleteFolderIds = StringUtil.split(
233                            ParamUtil.getString(actionRequest, "folderIds"), 0L);
234    
235                    List<TrashedModel> trashedModels = new ArrayList<TrashedModel>();
236    
237                    for (int i = 0; i < deleteFolderIds.length; i++) {
238                            long deleteFolderId = deleteFolderIds[i];
239    
240                            if (moveToTrash) {
241                                    Folder folder = DLAppServiceUtil.moveFolderToTrash(
242                                            deleteFolderId);
243    
244                                    if (folder.getModel() instanceof DLFolder) {
245                                            trashedModels.add((DLFolder)folder.getModel());
246                                    }
247                            }
248                            else {
249                                    DLAppServiceUtil.deleteFolder(deleteFolderId);
250                            }
251                    }
252    
253                    // Delete file shortcuts before file entries. See LPS-21348.
254    
255                    long[] deleteFileShortcutIds = StringUtil.split(
256                            ParamUtil.getString(actionRequest, "fileShortcutIds"), 0L);
257    
258                    for (int i = 0; i < deleteFileShortcutIds.length; i++) {
259                            long deleteFileShortcutId = deleteFileShortcutIds[i];
260    
261                            if (moveToTrash) {
262                                    DLFileShortcut fileShortcut =
263                                            DLAppServiceUtil.moveFileShortcutToTrash(
264                                                    deleteFileShortcutId);
265    
266                                    trashedModels.add(fileShortcut);
267                            }
268                            else {
269                                    DLAppServiceUtil.deleteFileShortcut(deleteFileShortcutId);
270                            }
271                    }
272    
273                    long[] deleteFileEntryIds = StringUtil.split(
274                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
275    
276                    for (long deleteFileEntryId : deleteFileEntryIds) {
277                            if (moveToTrash) {
278                                    FileEntry fileEntry = DLAppServiceUtil.moveFileEntryToTrash(
279                                            deleteFileEntryId);
280    
281                                    if (fileEntry.getModel() instanceof DLFileEntry) {
282                                            trashedModels.add((DLFileEntry)fileEntry.getModel());
283                                    }
284                            }
285                            else {
286                                    DLAppServiceUtil.deleteFileEntry(deleteFileEntryId);
287                            }
288                    }
289    
290                    if (moveToTrash && !trashedModels.isEmpty()) {
291                            TrashUtil.addTrashSessionMessages(actionRequest, trashedModels);
292    
293                            hideDefaultSuccessMessage(actionRequest);
294                    }
295            }
296    
297            protected void moveEntries(ActionRequest actionRequest) throws Exception {
298                    long newFolderId = ParamUtil.getLong(actionRequest, "newFolderId");
299    
300                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
301                            DLFileEntry.class.getName(), actionRequest);
302    
303                    long[] folderIds = StringUtil.split(
304                            ParamUtil.getString(actionRequest, "folderIds"), 0L);
305    
306                    for (long folderId : folderIds) {
307                            DLAppServiceUtil.moveFolder(folderId, newFolderId, serviceContext);
308                    }
309    
310                    long[] fileEntryIds = StringUtil.split(
311                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
312    
313                    for (long fileEntryId : fileEntryIds) {
314                            DLAppServiceUtil.moveFileEntry(
315                                    fileEntryId, newFolderId, serviceContext);
316                    }
317    
318                    long[] fileShortcutIds = StringUtil.split(
319                            ParamUtil.getString(actionRequest, "fileShortcutIds"), 0L);
320    
321                    for (long fileShortcutId : fileShortcutIds) {
322                            if (fileShortcutId == 0) {
323                                    continue;
324                            }
325    
326                            DLFileShortcut fileShortcut = DLAppServiceUtil.getFileShortcut(
327                                    fileShortcutId);
328    
329                            DLAppServiceUtil.updateFileShortcut(
330                                    fileShortcutId, newFolderId, fileShortcut.getToFileEntryId(),
331                                    serviceContext);
332                    }
333            }
334    
335    }