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.portlet.LiferayWindowState;
019    import com.liferay.portal.kernel.servlet.ServletResponseConstants;
020    import com.liferay.portal.kernel.servlet.SessionErrors;
021    import com.liferay.portal.kernel.util.Constants;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.service.ServiceContextFactory;
029    import com.liferay.portal.struts.PortletAction;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portlet.asset.AssetCategoryException;
032    import com.liferay.portlet.asset.AssetTagException;
033    import com.liferay.portlet.documentlibrary.DuplicateFileException;
034    import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
035    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
036    import com.liferay.portlet.documentlibrary.NoSuchFolderException;
037    import com.liferay.portlet.documentlibrary.SourceFileNameException;
038    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
039    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
040    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
041    
042    import javax.portlet.ActionRequest;
043    import javax.portlet.ActionResponse;
044    import javax.portlet.PortletConfig;
045    import javax.portlet.RenderRequest;
046    import javax.portlet.RenderResponse;
047    import javax.portlet.WindowState;
048    
049    import javax.servlet.http.HttpServletResponse;
050    
051    import org.apache.struts.action.ActionForm;
052    import org.apache.struts.action.ActionForward;
053    import org.apache.struts.action.ActionMapping;
054    
055    /**
056     * @author Brian Wing Shun Chan
057     * @author Sergio González
058     */
059    public class EditEntryAction extends PortletAction {
060    
061            @Override
062            public void processAction(
063                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
064                            ActionRequest actionRequest, ActionResponse actionResponse)
065                    throws Exception {
066    
067                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
068    
069                    try {
070                            if (cmd.equals(Constants.DELETE)) {
071                                    deleteEntries(actionRequest);
072                            }
073                            else if (cmd.equals(Constants.CANCEL_CHECKOUT)) {
074                                    cancelCheckedOutEntries(actionRequest);
075                            }
076                            else if (cmd.equals(Constants.CHECKIN)) {
077                                    checkInEntries(actionRequest);
078                            }
079                            else if (cmd.equals(Constants.CHECKOUT)) {
080                                    checkOutEntries(actionRequest);
081                            }
082                            else if (cmd.equals(Constants.MOVE)) {
083                                    moveEntries(actionRequest);
084                            }
085    
086                            WindowState windowState = actionRequest.getWindowState();
087    
088                            if (!windowState.equals(LiferayWindowState.POP_UP)) {
089                                    sendRedirect(actionRequest, actionResponse);
090                            }
091                            else {
092                                    String redirect = PortalUtil.escapeRedirect(
093                                            ParamUtil.getString(actionRequest, "redirect"));
094    
095                                    if (Validator.isNotNull(redirect)) {
096                                            actionResponse.sendRedirect(redirect);
097                                    }
098                            }
099                    }
100                    catch (Exception e) {
101                            if (e instanceof DuplicateLockException ||
102                                    e instanceof NoSuchFileEntryException ||
103                                    e instanceof NoSuchFolderException ||
104                                    e instanceof PrincipalException) {
105    
106                                    if (e instanceof DuplicateLockException) {
107                                            DuplicateLockException dle = (DuplicateLockException)e;
108    
109                                            SessionErrors.add(
110                                                    actionRequest, dle.getClass().getName(), dle.getLock());
111                                    }
112                                    else {
113                                            SessionErrors.add(actionRequest, e.getClass().getName());
114                                    }
115    
116                                    setForward(actionRequest, "portlet.document_library.error");
117                            }
118                            else if (e instanceof DuplicateFileException ||
119                                             e instanceof DuplicateFolderNameException ||
120                                             e instanceof NoSuchFolderException ||
121                                             e instanceof SourceFileNameException) {
122    
123                                    if (e instanceof DuplicateFileException) {
124                                            HttpServletResponse response =
125                                                    PortalUtil.getHttpServletResponse(actionResponse);
126    
127                                            response.setStatus(
128                                                    ServletResponseConstants.SC_DUPLICATE_FILE_EXCEPTION);
129                                    }
130    
131                                    SessionErrors.add(actionRequest, e.getClass().getName());
132                            }
133                            else if (e instanceof AssetCategoryException ||
134                                             e instanceof AssetTagException) {
135    
136                                    SessionErrors.add(actionRequest, e.getClass().getName(), e);
137                            }
138                            else {
139                                    throw e;
140                            }
141                    }
142            }
143    
144            @Override
145            public ActionForward render(
146                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
147                            RenderRequest renderRequest, RenderResponse renderResponse)
148                    throws Exception {
149    
150                    try {
151                            ActionUtil.getFileEntries(renderRequest);
152                            ActionUtil.getFileShortcuts(renderRequest);
153                            ActionUtil.getFolders(renderRequest);
154                    }
155                    catch (Exception e) {
156                            if (e instanceof NoSuchFileEntryException ||
157                                    e instanceof PrincipalException) {
158    
159                                    SessionErrors.add(renderRequest, e.getClass().getName());
160    
161                                    return mapping.findForward("portlet.document_library.error");
162                            }
163                            else {
164                                    throw e;
165                            }
166                    }
167    
168                    String forward = "portlet.document_library.edit_entry";
169    
170                    return mapping.findForward(getForward(renderRequest, forward));
171            }
172    
173            protected void cancelCheckedOutEntries(ActionRequest actionRequest)
174                    throws Exception {
175    
176                    long repositoryId = ParamUtil.getLong(actionRequest, "repositoryId");
177    
178                    long[] folderIds = StringUtil.split(
179                            ParamUtil.getString(actionRequest, "folderIds"), 0L);
180    
181                    for (long folderId : folderIds) {
182                            DLAppServiceUtil.lockFolder(repositoryId, folderId);
183                    }
184    
185                    long[] fileEntryIds = StringUtil.split(
186                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
187    
188                    for (long fileEntryId : fileEntryIds) {
189                            DLAppServiceUtil.cancelCheckOut(fileEntryId);
190                    }
191            }
192    
193            protected void checkInEntries(ActionRequest actionRequest)
194                    throws Exception {
195    
196                    long repositoryId = ParamUtil.getLong(actionRequest, "repositoryId");
197    
198                    long[] folderIds = StringUtil.split(
199                            ParamUtil.getString(actionRequest, "folderIds"), 0L);
200    
201                    for (long folderId : folderIds) {
202                            DLAppServiceUtil.unlockFolder(repositoryId, folderId, null);
203                    }
204    
205                    long[] fileEntryIds = StringUtil.split(
206                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
207    
208                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
209                            actionRequest);
210    
211                    for (long fileEntryId : fileEntryIds) {
212                            DLAppServiceUtil.checkInFileEntry(
213                                    fileEntryId, false, StringPool.BLANK, serviceContext);
214                    }
215            }
216    
217            protected void checkOutEntries(ActionRequest actionRequest)
218                    throws Exception {
219    
220                    long repositoryId = ParamUtil.getLong(actionRequest, "repositoryId");
221    
222                    long[] folderIds = StringUtil.split(
223                            ParamUtil.getString(actionRequest, "folderIds"), 0L);
224    
225                    for (long folderId : folderIds) {
226                            DLAppServiceUtil.lockFolder(repositoryId, folderId);
227                    }
228    
229                    long[] fileEntryIds = StringUtil.split(
230                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
231    
232                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
233                            actionRequest);
234    
235                    for (long fileEntryId : fileEntryIds) {
236                            DLAppServiceUtil.checkOutFileEntry(fileEntryId, serviceContext);
237                    }
238            }
239    
240            protected void deleteEntries(ActionRequest actionRequest)
241                    throws Exception {
242    
243                    long[] deleteFolderIds = StringUtil.split(
244                            ParamUtil.getString(actionRequest, "folderIds"), 0L);
245    
246                    for (long deleteFolderId : deleteFolderIds) {
247                            DLAppServiceUtil.deleteFolder(deleteFolderId);
248                    }
249    
250                    // Delete file shortcuts before file entries. See LPS-21348.
251    
252                    long[] deleteFileShortcutIds = StringUtil.split(
253                            ParamUtil.getString(actionRequest, "fileShortcutIds"), 0L);
254    
255                    for (long deleteFileShortcutId : deleteFileShortcutIds) {
256                            DLAppServiceUtil.deleteFileShortcut(deleteFileShortcutId);
257                    }
258    
259                    long[] deleteFileEntryIds = StringUtil.split(
260                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
261    
262                    for (long deleteFileEntryId : deleteFileEntryIds) {
263                            DLAppServiceUtil.deleteFileEntry(deleteFileEntryId);
264                    }
265            }
266    
267            protected void moveEntries(ActionRequest actionRequest)
268                    throws Exception {
269    
270                    long newFolderId = ParamUtil.getLong(actionRequest, "newFolderId");
271    
272                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
273                            DLFileEntry.class.getName(), actionRequest);
274    
275                    long[] folderIds = StringUtil.split(
276                            ParamUtil.getString(actionRequest, "folderIds"), 0L);
277    
278                    for (long folderId : folderIds) {
279                            DLAppServiceUtil.moveFolder(folderId, newFolderId, serviceContext);
280                    }
281    
282                    long[] fileEntryIds = StringUtil.split(
283                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
284    
285                    for (long fileEntryId : fileEntryIds) {
286                            DLAppServiceUtil.moveFileEntry(
287                                    fileEntryId, newFolderId, serviceContext);
288                    }
289    
290                    long[] fileShortcutIds = StringUtil.split(
291                            ParamUtil.getString(actionRequest, "fileShortcutIds"), 0L);
292    
293                    for (long fileShortcutId : fileShortcutIds) {
294                            if (fileShortcutId == 0) {
295                                    continue;
296                            }
297    
298                            DLFileShortcut fileShortcut = DLAppServiceUtil.getFileShortcut(
299                                    fileShortcutId);
300    
301                            DLAppServiceUtil.updateFileShortcut(
302                                    fileShortcutId, newFolderId, fileShortcut.getToFileEntryId(),
303                                    serviceContext);
304                    }
305            }
306    
307    }