001
014
015 package com.liferay.portlet.documentlibrary.action;
016
017 import com.liferay.documentlibrary.DuplicateFileException;
018 import com.liferay.documentlibrary.FileNameException;
019 import com.liferay.documentlibrary.FileSizeException;
020 import com.liferay.documentlibrary.SourceFileNameException;
021 import com.liferay.portal.DuplicateLockException;
022 import com.liferay.portal.kernel.servlet.ServletResponseConstants;
023 import com.liferay.portal.kernel.servlet.SessionErrors;
024 import com.liferay.portal.kernel.upload.UploadPortletRequest;
025 import com.liferay.portal.kernel.util.Constants;
026 import com.liferay.portal.kernel.util.ParamUtil;
027 import com.liferay.portal.kernel.util.PropertiesParamUtil;
028 import com.liferay.portal.kernel.util.UnicodeProperties;
029 import com.liferay.portal.kernel.util.Validator;
030 import com.liferay.portal.kernel.workflow.WorkflowConstants;
031 import com.liferay.portal.security.auth.PrincipalException;
032 import com.liferay.portal.service.ServiceContext;
033 import com.liferay.portal.service.ServiceContextFactory;
034 import com.liferay.portal.struts.PortletAction;
035 import com.liferay.portal.theme.ThemeDisplay;
036 import com.liferay.portal.util.PortalUtil;
037 import com.liferay.portal.util.WebKeys;
038 import com.liferay.portlet.asset.AssetTagException;
039 import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
040 import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
041 import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
042 import com.liferay.portlet.documentlibrary.NoSuchFolderException;
043 import com.liferay.portlet.documentlibrary.model.DLFileEntry;
044 import com.liferay.portlet.documentlibrary.model.DLFileVersion;
045 import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
046 import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
047
048 import java.io.File;
049
050 import javax.portlet.ActionRequest;
051 import javax.portlet.ActionResponse;
052 import javax.portlet.PortletConfig;
053 import javax.portlet.RenderRequest;
054 import javax.portlet.RenderResponse;
055
056 import javax.servlet.http.HttpServletResponse;
057
058 import org.apache.struts.action.ActionForm;
059 import org.apache.struts.action.ActionForward;
060 import org.apache.struts.action.ActionMapping;
061
062
066 public class EditFileEntryAction extends PortletAction {
067
068 public void processAction(
069 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
070 ActionRequest actionRequest, ActionResponse actionResponse)
071 throws Exception {
072
073 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
074
075 try {
076 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
077 updateFileEntry(actionRequest, actionResponse);
078 }
079 else if (cmd.equals(Constants.DELETE)) {
080 deleteFileEntry(actionRequest);
081 }
082 else if (cmd.equals(Constants.LOCK)) {
083 lockFileEntry(actionRequest);
084 }
085 else if (cmd.equals(Constants.MOVE)) {
086 moveFileEntry(actionRequest);
087 }
088 else if (cmd.equals(Constants.REVERT)) {
089 revertFileEntry(actionRequest);
090 }
091 else if (cmd.equals(Constants.UNLOCK)) {
092 unlockFileEntry(actionRequest);
093 }
094
095 sendRedirect(actionRequest, actionResponse);
096 }
097 catch (Exception e) {
098 if (e instanceof DuplicateLockException ||
099 e instanceof NoSuchFileEntryException ||
100 e instanceof PrincipalException) {
101
102 if (e instanceof DuplicateLockException) {
103 DuplicateLockException dle = (DuplicateLockException)e;
104
105 SessionErrors.add(
106 actionRequest, dle.getClass().getName(), dle.getLock());
107 }
108 else {
109 SessionErrors.add(actionRequest, e.getClass().getName());
110 }
111
112 setForward(actionRequest, "portlet.document_library.error");
113 }
114 else if (e instanceof DuplicateFileException ||
115 e instanceof DuplicateFolderNameException ||
116 e instanceof FileNameException ||
117 e instanceof FileSizeException ||
118 e instanceof NoSuchFolderException ||
119 e instanceof SourceFileNameException) {
120
121 if (e instanceof DuplicateFileException) {
122 HttpServletResponse response =
123 PortalUtil.getHttpServletResponse(actionResponse);
124
125 response.setStatus(
126 ServletResponseConstants.SC_DUPLICATE_FILE_EXCEPTION);
127 }
128 else if (e instanceof FileNameException) {
129 HttpServletResponse response =
130 PortalUtil.getHttpServletResponse(actionResponse);
131
132 response.setStatus(
133 ServletResponseConstants.SC_FILE_NAME_EXCEPTION);
134 }
135 else if (e instanceof FileSizeException) {
136 HttpServletResponse response =
137 PortalUtil.getHttpServletResponse(actionResponse);
138
139 response.setStatus(
140 ServletResponseConstants.SC_FILE_SIZE_EXCEPTION);
141 }
142
143 SessionErrors.add(actionRequest, e.getClass().getName());
144 }
145 else if (e instanceof AssetTagException) {
146 SessionErrors.add(actionRequest, e.getClass().getName(), e);
147 }
148 else {
149 throw e;
150 }
151 }
152 }
153
154 public ActionForward render(
155 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
156 RenderRequest renderRequest, RenderResponse renderResponse)
157 throws Exception {
158
159 try {
160 ActionUtil.getFileEntry(renderRequest);
161 }
162 catch (Exception e) {
163 if (e instanceof NoSuchFileEntryException ||
164 e instanceof PrincipalException) {
165
166 SessionErrors.add(renderRequest, e.getClass().getName());
167
168 return mapping.findForward("portlet.document_library.error");
169 }
170 else {
171 throw e;
172 }
173 }
174
175 String forward = "portlet.document_library.edit_file_entry";
176
177 return mapping.findForward(getForward(renderRequest, forward));
178 }
179
180 protected void deleteFileEntry(ActionRequest actionRequest)
181 throws Exception {
182
183 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
184 WebKeys.THEME_DISPLAY);
185
186 long groupId = themeDisplay.getScopeGroupId();
187 long folderId = ParamUtil.getLong(actionRequest, "folderId");
188 String name = ParamUtil.getString(actionRequest, "name");
189 String version = ParamUtil.getString(actionRequest, "version");
190
191 DLFileEntryServiceUtil.deleteFileEntry(
192 groupId, folderId, name, version);
193 }
194
195 protected void lockFileEntry(ActionRequest actionRequest) throws Exception {
196 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
197 WebKeys.THEME_DISPLAY);
198
199 long groupId = themeDisplay.getScopeGroupId();
200 long folderId = ParamUtil.getLong(actionRequest, "folderId");
201 String name = ParamUtil.getString(actionRequest, "name");
202
203 DLFileEntryServiceUtil.lockFileEntry(groupId, folderId, name);
204 }
205
206 protected void moveFileEntry(ActionRequest actionRequest) throws Exception {
207 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
208 WebKeys.THEME_DISPLAY);
209
210 long groupId = themeDisplay.getScopeGroupId();
211 long folderId = ParamUtil.getLong(actionRequest, "folderId");
212 long newFolderId = ParamUtil.getLong(actionRequest, "newFolderId");
213 String name = ParamUtil.getString(actionRequest, "name");
214
215 ServiceContext serviceContext = ServiceContextFactory.getInstance(
216 DLFileEntry.class.getName(), actionRequest);
217
218 DLFileEntryServiceUtil.moveFileEntry(
219 groupId, folderId, newFolderId, name, serviceContext);
220 }
221
222 protected void revertFileEntry(ActionRequest actionRequest)
223 throws Exception {
224
225 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
226 WebKeys.THEME_DISPLAY);
227
228 long groupId = themeDisplay.getScopeGroupId();
229 long folderId = ParamUtil.getLong(actionRequest, "folderId");
230 String name = ParamUtil.getString(actionRequest, "name");
231
232 DLFileVersion fileVersion =
233 DLFileVersionLocalServiceUtil.getLatestFileVersion(
234 groupId, folderId, name);
235
236 if (fileVersion.getStatus() != WorkflowConstants.STATUS_DRAFT) {
237 return;
238 }
239
240 DLFileEntryServiceUtil.deleteFileEntry(
241 groupId, folderId, name, fileVersion.getVersion());
242 }
243
244 protected void unlockFileEntry(ActionRequest actionRequest)
245 throws Exception {
246
247 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
248 WebKeys.THEME_DISPLAY);
249
250 long groupId = themeDisplay.getScopeGroupId();
251 long folderId = ParamUtil.getLong(actionRequest, "folderId");
252 String name = ParamUtil.getString(actionRequest, "name");
253
254 DLFileEntryServiceUtil.unlockFileEntry(groupId, folderId, name);
255 }
256
257 protected void updateFileEntry(
258 ActionRequest actionRequest, ActionResponse actionResponse)
259 throws Exception {
260
261 UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
262 actionRequest);
263
264 String cmd = ParamUtil.getString(uploadRequest, Constants.CMD);
265
266 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
267 WebKeys.THEME_DISPLAY);
268
269 long groupId = themeDisplay.getScopeGroupId();
270 long folderId = ParamUtil.getLong(uploadRequest, "folderId");
271 String name = ParamUtil.getString(uploadRequest, "name");
272 String sourceFileName = uploadRequest.getFileName("file");
273
274 String title = ParamUtil.getString(uploadRequest, "title");
275 String description = ParamUtil.getString(uploadRequest, "description");
276 String changeLog = ParamUtil.getString(
277 uploadRequest, "changeLog");
278 boolean majorVersion = ParamUtil.getBoolean(
279 uploadRequest, "majorVersion");
280
281 UnicodeProperties extraSettingsProperties =
282 PropertiesParamUtil.getProperties(
283 actionRequest, "extraSettingsProperties--");
284
285 String extraSettings = extraSettingsProperties.toString();
286
287 File file = uploadRequest.getFile("file");
288
289 if (Validator.isNotNull(sourceFileName) && !file.exists()) {
290 file.createNewFile();
291 }
292
293 ServiceContext serviceContext = ServiceContextFactory.getInstance(
294 DLFileEntry.class.getName(), actionRequest);
295
296 if (cmd.equals(Constants.ADD)) {
297
298
299
300 DLFileEntry fileEntry = DLFileEntryServiceUtil.addFileEntry(
301 groupId, folderId, sourceFileName, title, description,
302 changeLog, extraSettings, file, serviceContext);
303
304 AssetPublisherUtil.addAndStoreSelection(
305 actionRequest, DLFileEntry.class.getName(),
306 fileEntry.getFileEntryId(), -1);
307 }
308 else {
309
310
311
312 DLFileEntryServiceUtil.updateFileEntry(
313 groupId, folderId, name, sourceFileName, title, description,
314 changeLog, majorVersion, extraSettings, file,
315 serviceContext);
316 }
317
318 AssetPublisherUtil.addRecentFolderId(
319 actionRequest, DLFileEntry.class.getName(), folderId);
320 }
321
322 }