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