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.imagegallery.action;
022:
023: import com.liferay.portal.kernel.portlet.LiferayWindowState;
024: import com.liferay.portal.kernel.util.Constants;
025: import com.liferay.portal.kernel.util.GetterUtil;
026: import com.liferay.portal.kernel.util.ParamUtil;
027: import com.liferay.portal.kernel.util.StringUtil;
028: import com.liferay.portal.kernel.util.Validator;
029: import com.liferay.portal.security.auth.PrincipalException;
030: import com.liferay.portal.struts.PortletAction;
031: import com.liferay.portal.util.PortalUtil;
032: import com.liferay.portlet.imagegallery.ImageNameException;
033: import com.liferay.portlet.imagegallery.ImageSizeException;
034: import com.liferay.portlet.imagegallery.NoSuchFolderException;
035: import com.liferay.portlet.imagegallery.NoSuchImageException;
036: import com.liferay.portlet.imagegallery.model.IGImage;
037: import com.liferay.portlet.imagegallery.service.IGImageServiceUtil;
038: import com.liferay.portlet.taggedcontent.util.AssetPublisherUtil;
039: import com.liferay.portlet.tags.TagsEntryException;
040: import com.liferay.util.FileUtil;
041: import com.liferay.util.servlet.SessionErrors;
042: import com.liferay.util.servlet.UploadPortletRequest;
043:
044: import java.io.File;
045:
046: import javax.portlet.ActionRequest;
047: import javax.portlet.ActionResponse;
048: import javax.portlet.PortletConfig;
049: import javax.portlet.RenderRequest;
050: import javax.portlet.RenderResponse;
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: * <a href="EditImageAction.java.html"><b><i>View Source</i></b></a>
058: *
059: * @author Brian Wing Shun Chan
060: *
061: */
062: public class EditImageAction extends PortletAction {
063:
064: public void processAction(ActionMapping mapping, ActionForm form,
065: PortletConfig config, ActionRequest req, ActionResponse res)
066: throws Exception {
067:
068: String cmd = ParamUtil.getString(req, Constants.CMD);
069:
070: try {
071: if (cmd.equals(Constants.ADD)
072: || cmd.equals(Constants.UPDATE)) {
073: updateImage(req);
074: } else if (cmd.equals(Constants.DELETE)) {
075: deleteImage(req);
076:
077: sendRedirect(req, res);
078: }
079: } catch (Exception e) {
080: if (e instanceof NoSuchImageException
081: || e instanceof PrincipalException) {
082:
083: SessionErrors.add(req, e.getClass().getName());
084:
085: setForward(req, "portlet.image_gallery.error");
086: } else if (e instanceof ImageNameException
087: || e instanceof ImageSizeException
088: || e instanceof NoSuchFolderException) {
089:
090: SessionErrors.add(req, e.getClass().getName());
091: } else if (e instanceof TagsEntryException) {
092: SessionErrors.add(req, e.getClass().getName(), e);
093: } else {
094: throw e;
095: }
096: }
097: }
098:
099: public ActionForward render(ActionMapping mapping, ActionForm form,
100: PortletConfig config, RenderRequest req, RenderResponse res)
101: throws Exception {
102:
103: try {
104: ActionUtil.getImage(req);
105: } catch (Exception e) {
106: if (e instanceof NoSuchImageException
107: || e instanceof PrincipalException) {
108:
109: SessionErrors.add(req, e.getClass().getName());
110:
111: return mapping
112: .findForward("portlet.image_gallery.error");
113: } else {
114: throw e;
115: }
116: }
117:
118: String forward = "portlet.image_gallery.edit_image";
119:
120: if (req.getWindowState().equals(LiferayWindowState.POP_UP)) {
121: forward = "portlet.image_gallery.edit_image_form";
122: }
123:
124: return mapping.findForward(getForward(req, forward));
125: }
126:
127: protected void deleteImage(ActionRequest req) throws Exception {
128: long imageId = ParamUtil.getLong(req, "imageId");
129:
130: IGImageServiceUtil.deleteImage(imageId);
131: }
132:
133: protected String getContentType(UploadPortletRequest uploadReq,
134: File file) {
135: String contentType = GetterUtil.getString(uploadReq
136: .getContentType("file"));
137:
138: if (contentType.equals("application/octet-stream")) {
139: String ext = GetterUtil.getString(
140: FileUtil.getExtension(file.getName()))
141: .toLowerCase();
142:
143: if (Validator.isNotNull(ext)) {
144: if (ext.equals("jpg")) {
145: ext = "jpeg";
146: } else if (ext.equals("tif")) {
147: ext = "tiff";
148: }
149:
150: contentType = "image/" + ext;
151: }
152: }
153:
154: return contentType;
155: }
156:
157: protected void updateImage(ActionRequest req) throws Exception {
158: UploadPortletRequest uploadReq = PortalUtil
159: .getUploadPortletRequest(req);
160:
161: long imageId = ParamUtil.getLong(uploadReq, "imageId");
162:
163: long folderId = ParamUtil.getLong(uploadReq, "folderId");
164: String description = ParamUtil.getString(uploadReq,
165: "description");
166:
167: if (Validator.isNull(description)) {
168: description = uploadReq.getFileName("file");
169: }
170:
171: File file = uploadReq.getFile("file");
172: String contentType = getContentType(uploadReq, file);
173:
174: if (contentType.equals("application/octet-stream")) {
175: String ext = GetterUtil.getString(
176: FileUtil.getExtension(file.getName()))
177: .toLowerCase();
178:
179: if (Validator.isNotNull(ext)) {
180: if (ext.equals("jpg")) {
181: ext = "jpeg";
182: } else if (ext.equals("tif")) {
183: ext = "tiff";
184: }
185:
186: contentType = "image/" + ext;
187: }
188: }
189:
190: String[] tagsEntries = StringUtil.split(ParamUtil.getString(
191: uploadReq, "tagsEntries"));
192:
193: String[] communityPermissions = req
194: .getParameterValues("communityPermissions");
195: String[] guestPermissions = req
196: .getParameterValues("guestPermissions");
197:
198: if (imageId <= 0) {
199:
200: // Add image
201:
202: IGImage image = IGImageServiceUtil.addImage(folderId,
203: description, file, contentType, tagsEntries,
204: communityPermissions, guestPermissions);
205:
206: AssetPublisherUtil.addAndStoreSelection(req, IGImage.class
207: .getName(), image.getImageId(), -1);
208: } else {
209:
210: // Update image
211:
212: String fileName = uploadReq.getFileName("file");
213:
214: if (Validator.isNull(fileName)) {
215: file = null;
216: }
217:
218: IGImageServiceUtil.updateImage(imageId, folderId,
219: description, file, contentType, tagsEntries);
220: }
221:
222: AssetPublisherUtil.addRecentFolderId(req, IGImage.class
223: .getName(), folderId);
224: }
225:
226: }
|