001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.documentlibrary.action;
022:
023: import com.liferay.documentlibrary.DuplicateFileException;
024: import com.liferay.documentlibrary.FileNameException;
025: import com.liferay.documentlibrary.FileSizeException;
026: import com.liferay.documentlibrary.SourceFileNameException;
027: import com.liferay.lock.DuplicateLockException;
028: import com.liferay.portal.kernel.portlet.LiferayWindowState;
029: import com.liferay.portal.kernel.security.permission.ActionKeys;
030: import com.liferay.portal.kernel.util.Constants;
031: import com.liferay.portal.kernel.util.ParamUtil;
032: import com.liferay.portal.kernel.util.PropertiesUtil;
033: import com.liferay.portal.kernel.util.StringUtil;
034: import com.liferay.portal.security.auth.PrincipalException;
035: import com.liferay.portal.struts.PortletAction;
036: import com.liferay.portal.theme.ThemeDisplay;
037: import com.liferay.portal.util.PortalUtil;
038: import com.liferay.portal.util.WebKeys;
039: import com.liferay.portlet.documentlibrary.DuplicateFolderNameException;
040: import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
041: import com.liferay.portlet.documentlibrary.NoSuchFolderException;
042: import com.liferay.portlet.documentlibrary.form.FileEntryForm;
043: import com.liferay.portlet.documentlibrary.model.DLFileEntry;
044: import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
045: import com.liferay.portlet.documentlibrary.service.DLFileEntryServiceUtil;
046: import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
047: import com.liferay.portlet.documentlibrary.service.permission.DLFolderPermission;
048: import com.liferay.portlet.taggedcontent.util.AssetPublisherUtil;
049: import com.liferay.portlet.tags.TagsEntryException;
050: import com.liferay.util.servlet.SessionErrors;
051: import com.liferay.util.servlet.UploadPortletRequest;
052:
053: import java.io.File;
054:
055: import javax.portlet.ActionRequest;
056: import javax.portlet.ActionResponse;
057: import javax.portlet.PortletConfig;
058: import javax.portlet.RenderRequest;
059: import javax.portlet.RenderResponse;
060:
061: import org.apache.struts.action.ActionForm;
062: import org.apache.struts.action.ActionForward;
063: import org.apache.struts.action.ActionMapping;
064:
065: /**
066: * <a href="EditFileEntryAction.java.html"><b><i>View Source</i></b></a>
067: *
068: * @author Brian Wing Shun Chan
069: * @author Alexander Chow
070: *
071: */
072: public class EditFileEntryAction extends PortletAction {
073:
074: public void processAction(ActionMapping mapping, ActionForm form,
075: PortletConfig config, ActionRequest req, ActionResponse res)
076: throws Exception {
077:
078: FileEntryForm fileEntryForm = (FileEntryForm) form;
079:
080: String cmd = ParamUtil.getString(req, Constants.CMD);
081:
082: try {
083: if (cmd.equals(Constants.ADD)
084: || cmd.equals(Constants.UPDATE)) {
085: updateFileEntry(fileEntryForm, req, res);
086: } else if (cmd.equals(Constants.DELETE)) {
087: deleteFileEntry(req);
088:
089: sendRedirect(req, res);
090: } else if (cmd.equals(Constants.LOCK)) {
091: lockFileEntry(req);
092: } else if (cmd.equals(Constants.UNLOCK)) {
093: unlockFileEntry(req);
094: }
095: } catch (Exception e) {
096: if (e instanceof DuplicateLockException
097: || e instanceof NoSuchFileEntryException
098: || e instanceof PrincipalException) {
099:
100: if (e instanceof DuplicateLockException) {
101: DuplicateLockException dle = (DuplicateLockException) e;
102:
103: SessionErrors.add(req, dle.getClass().getName(),
104: dle.getLock());
105: } else {
106: SessionErrors.add(req, e.getClass().getName());
107: }
108:
109: setForward(req, "portlet.document_library.error");
110: } else if (e instanceof DuplicateFileException
111: || e instanceof DuplicateFolderNameException
112: || e instanceof FileNameException
113: || e instanceof FileSizeException
114: || e instanceof NoSuchFolderException
115: || e instanceof SourceFileNameException) {
116:
117: SessionErrors.add(req, e.getClass().getName());
118: } else if (e instanceof TagsEntryException) {
119: SessionErrors.add(req, e.getClass().getName(), e);
120: } else {
121: throw e;
122: }
123: }
124: }
125:
126: public ActionForward render(ActionMapping mapping, ActionForm form,
127: PortletConfig config, RenderRequest req, RenderResponse res)
128: throws Exception {
129:
130: try {
131: ActionUtil.getFileEntry(req);
132: } catch (Exception e) {
133: if (e instanceof NoSuchFileEntryException
134: || e instanceof PrincipalException) {
135:
136: SessionErrors.add(req, e.getClass().getName());
137:
138: return mapping
139: .findForward("portlet.document_library.error");
140: } else {
141: throw e;
142: }
143: }
144:
145: String forward = "portlet.document_library.edit_file_entry";
146:
147: if (req.getWindowState().equals(LiferayWindowState.POP_UP)) {
148: forward = "portlet.document_library.edit_file_entry_form";
149: }
150:
151: return mapping.findForward(getForward(req, forward));
152: }
153:
154: protected void deleteFileEntry(ActionRequest req) throws Exception {
155: long folderId = ParamUtil.getLong(req, "folderId");
156: String name = ParamUtil.getString(req, "name");
157: double version = ParamUtil.getDouble(req, "version");
158:
159: DLFileEntryServiceUtil.deleteFileEntry(folderId, name, version);
160: }
161:
162: protected void lockFileEntry(ActionRequest req) throws Exception {
163: long folderId = ParamUtil.getLong(req, "folderId");
164: String name = ParamUtil.getString(req, "name");
165:
166: DLFileEntryServiceUtil.lockFileEntry(folderId, name);
167: }
168:
169: protected void unlockFileEntry(ActionRequest req) throws Exception {
170: long folderId = ParamUtil.getLong(req, "folderId");
171: String name = ParamUtil.getString(req, "name");
172:
173: DLFileEntryServiceUtil.unlockFileEntry(folderId, name);
174: }
175:
176: protected void updateFileEntry(FileEntryForm fileEntryForm,
177: ActionRequest req, ActionResponse res) throws Exception {
178:
179: UploadPortletRequest uploadReq = PortalUtil
180: .getUploadPortletRequest(req);
181:
182: String cmd = ParamUtil.getString(uploadReq, Constants.CMD);
183:
184: ThemeDisplay themeDisplay = (ThemeDisplay) req
185: .getAttribute(WebKeys.THEME_DISPLAY);
186:
187: long folderId = ParamUtil.getLong(uploadReq, "folderId");
188: long newFolderId = ParamUtil.getLong(uploadReq, "newFolderId");
189: String name = ParamUtil.getString(uploadReq, "name");
190: String sourceFileName = uploadReq.getFileName("file");
191:
192: String title = ParamUtil.getString(uploadReq, "title");
193: String description = ParamUtil.getString(uploadReq,
194: "description");
195:
196: String[] tagsEntries = StringUtil.split(ParamUtil.getString(
197: uploadReq, "tagsEntries"));
198:
199: String extraSettings = PropertiesUtil.toString(fileEntryForm
200: .getExtraSettingsProperties());
201:
202: File file = uploadReq.getFile("file");
203:
204: String[] communityPermissions = req
205: .getParameterValues("communityPermissions");
206: String[] guestPermissions = req
207: .getParameterValues("guestPermissions");
208:
209: if (cmd.equals(Constants.ADD)) {
210:
211: // Add file entry
212:
213: DLFolderPermission.check(themeDisplay
214: .getPermissionChecker(), folderId,
215: ActionKeys.ADD_DOCUMENT);
216:
217: DLFileEntry entry = DLFileEntryLocalServiceUtil
218: .addFileEntry(themeDisplay.getUserId(), folderId,
219: sourceFileName, title, description,
220: tagsEntries, extraSettings, file,
221: communityPermissions, guestPermissions);
222:
223: AssetPublisherUtil.addAndStoreSelection(req,
224: DLFileEntry.class.getName(),
225: entry.getFileEntryId(), -1);
226: } else {
227:
228: // Update file entry
229:
230: DLFileEntryPermission.check(themeDisplay
231: .getPermissionChecker(), folderId, name,
232: ActionKeys.UPDATE);
233:
234: DLFileEntryLocalServiceUtil.updateFileEntry(themeDisplay
235: .getUserId(), folderId, newFolderId, name,
236: sourceFileName, title, description, tagsEntries,
237: extraSettings, file);
238: }
239:
240: AssetPublisherUtil.addRecentFolderId(req, DLFileEntry.class
241: .getName(), folderId);
242: }
243:
244: }
|