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