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