001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(), dle.getLock());
111                                    }
112                                    else {
113                                            SessionErrors.add(actionRequest, e.getClass());
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());
132                            }
133                            else if (e instanceof AssetCategoryException ||
134                                             e instanceof AssetTagException) {
135    
136                                    SessionErrors.add(actionRequest, e.getClass(), 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());
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[] fileEntryIds = StringUtil.split(
177                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
178    
179                    for (long fileEntryId : fileEntryIds) {
180                            DLAppServiceUtil.cancelCheckOut(fileEntryId);
181                    }
182            }
183    
184            protected void checkInEntries(ActionRequest actionRequest)
185                    throws Exception {
186    
187                    long[] fileEntryIds = StringUtil.split(
188                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
189    
190                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
191                            actionRequest);
192    
193                    for (long fileEntryId : fileEntryIds) {
194                            DLAppServiceUtil.checkInFileEntry(
195                                    fileEntryId, false, StringPool.BLANK, serviceContext);
196                    }
197            }
198    
199            protected void checkOutEntries(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.checkOutFileEntry(fileEntryId, serviceContext);
210                    }
211            }
212    
213            protected void deleteEntries(ActionRequest actionRequest) throws Exception {
214                    long[] deleteFolderIds = StringUtil.split(
215                            ParamUtil.getString(actionRequest, "folderIds"), 0L);
216    
217                    for (long deleteFolderId : deleteFolderIds) {
218                            DLAppServiceUtil.deleteFolder(deleteFolderId);
219                    }
220    
221                    // Delete file shortcuts before file entries. See LPS-21348.
222    
223                    long[] deleteFileShortcutIds = StringUtil.split(
224                            ParamUtil.getString(actionRequest, "fileShortcutIds"), 0L);
225    
226                    for (long deleteFileShortcutId : deleteFileShortcutIds) {
227                            DLAppServiceUtil.deleteFileShortcut(deleteFileShortcutId);
228                    }
229    
230                    long[] deleteFileEntryIds = StringUtil.split(
231                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
232    
233                    for (long deleteFileEntryId : deleteFileEntryIds) {
234                            DLAppServiceUtil.deleteFileEntry(deleteFileEntryId);
235                    }
236            }
237    
238            protected void moveEntries(ActionRequest actionRequest) throws Exception {
239                    long newFolderId = ParamUtil.getLong(actionRequest, "newFolderId");
240    
241                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
242                            DLFileEntry.class.getName(), actionRequest);
243    
244                    long[] folderIds = StringUtil.split(
245                            ParamUtil.getString(actionRequest, "folderIds"), 0L);
246    
247                    for (long folderId : folderIds) {
248                            DLAppServiceUtil.moveFolder(folderId, newFolderId, serviceContext);
249                    }
250    
251                    long[] fileEntryIds = StringUtil.split(
252                            ParamUtil.getString(actionRequest, "fileEntryIds"), 0L);
253    
254                    for (long fileEntryId : fileEntryIds) {
255                            DLAppServiceUtil.moveFileEntry(
256                                    fileEntryId, newFolderId, serviceContext);
257                    }
258    
259                    long[] fileShortcutIds = StringUtil.split(
260                            ParamUtil.getString(actionRequest, "fileShortcutIds"), 0L);
261    
262                    for (long fileShortcutId : fileShortcutIds) {
263                            if (fileShortcutId == 0) {
264                                    continue;
265                            }
266    
267                            DLFileShortcut fileShortcut = DLAppServiceUtil.getFileShortcut(
268                                    fileShortcutId);
269    
270                            DLAppServiceUtil.updateFileShortcut(
271                                    fileShortcutId, newFolderId, fileShortcut.getToFileEntryId(),
272                                    serviceContext);
273                    }
274            }
275    
276    }