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.softwarecatalog.action;
022:
023: import com.liferay.portal.kernel.util.Constants;
024: import com.liferay.portal.kernel.util.GetterUtil;
025: import com.liferay.portal.kernel.util.ParamUtil;
026: import com.liferay.portal.kernel.util.Validator;
027: import com.liferay.portal.model.Image;
028: import com.liferay.portal.model.Layout;
029: import com.liferay.portal.security.auth.PrincipalException;
030: import com.liferay.portal.service.impl.ImageLocalUtil;
031: import com.liferay.portal.struts.PortletAction;
032: import com.liferay.portal.util.PortalUtil;
033: import com.liferay.portal.util.WebKeys;
034: import com.liferay.portlet.softwarecatalog.DuplicateProductEntryModuleIdException;
035: import com.liferay.portlet.softwarecatalog.NoSuchProductEntryException;
036: import com.liferay.portlet.softwarecatalog.ProductEntryAuthorException;
037: import com.liferay.portlet.softwarecatalog.ProductEntryLicenseException;
038: import com.liferay.portlet.softwarecatalog.ProductEntryNameException;
039: import com.liferay.portlet.softwarecatalog.ProductEntryPageURLException;
040: import com.liferay.portlet.softwarecatalog.ProductEntryScreenshotsException;
041: import com.liferay.portlet.softwarecatalog.ProductEntryShortDescriptionException;
042: import com.liferay.portlet.softwarecatalog.ProductEntryTypeException;
043: import com.liferay.portlet.softwarecatalog.model.SCProductScreenshot;
044: import com.liferay.portlet.softwarecatalog.service.SCProductEntryServiceUtil;
045: import com.liferay.portlet.softwarecatalog.service.SCProductScreenshotLocalServiceUtil;
046: import com.liferay.util.FileUtil;
047: import com.liferay.util.servlet.SessionErrors;
048: import com.liferay.util.servlet.UploadPortletRequest;
049:
050: import java.io.File;
051:
052: import java.util.ArrayList;
053: import java.util.Collections;
054: import java.util.Enumeration;
055: import java.util.Iterator;
056: import java.util.List;
057:
058: import javax.portlet.ActionRequest;
059: import javax.portlet.ActionResponse;
060: import javax.portlet.PortletConfig;
061: import javax.portlet.RenderRequest;
062: import javax.portlet.RenderResponse;
063:
064: import org.apache.struts.action.ActionForm;
065: import org.apache.struts.action.ActionForward;
066: import org.apache.struts.action.ActionMapping;
067:
068: /**
069: * <a href="EditProductEntryAction.java.html"><b><i>View Source</i></b></a>
070: *
071: * @author Jorge Ferrer
072: *
073: */
074: public class EditProductEntryAction extends PortletAction {
075:
076: public void processAction(ActionMapping mapping, ActionForm form,
077: PortletConfig config, ActionRequest req, ActionResponse res)
078: throws Exception {
079:
080: String cmd = ParamUtil.getString(req, Constants.CMD);
081:
082: try {
083: if (cmd.equals(Constants.ADD)
084: || cmd.equals(Constants.UPDATE)) {
085: updateProductEntry(req);
086: } else if (cmd.equals(Constants.DELETE)) {
087: deleteProductEntry(req);
088: }
089:
090: if (Validator.isNotNull(cmd)) {
091: sendRedirect(req, res);
092: }
093: } catch (Exception e) {
094: if (e instanceof NoSuchProductEntryException
095: || e instanceof PrincipalException) {
096:
097: SessionErrors.add(req, e.getClass().getName());
098:
099: setForward(req, "portlet.software_catalog.error");
100: } else if (e instanceof DuplicateProductEntryModuleIdException
101: || e instanceof ProductEntryAuthorException
102: || e instanceof ProductEntryNameException
103: || e instanceof ProductEntryLicenseException
104: || e instanceof ProductEntryPageURLException
105: || e instanceof ProductEntryScreenshotsException
106: || e instanceof ProductEntryShortDescriptionException
107: || e instanceof ProductEntryTypeException) {
108:
109: SessionErrors.add(req, e.getClass().getName());
110: } else {
111: throw e;
112: }
113: }
114: }
115:
116: public ActionForward render(ActionMapping mapping, ActionForm form,
117: PortletConfig config, RenderRequest req, RenderResponse res)
118: throws Exception {
119:
120: try {
121: ActionUtil.getProductEntry(req);
122: } catch (Exception e) {
123: if (e instanceof NoSuchProductEntryException
124: || e instanceof PrincipalException) {
125:
126: SessionErrors.add(req, e.getClass().getName());
127:
128: return mapping
129: .findForward("portlet.software_catalog.error");
130: } else {
131: throw e;
132: }
133: }
134:
135: return mapping.findForward(getForward(req,
136: "portlet.software_catalog.edit_product_entry"));
137: }
138:
139: protected void deleteProductEntry(ActionRequest req)
140: throws Exception {
141: long productEntryId = ParamUtil.getLong(req, "productEntryId");
142:
143: SCProductEntryServiceUtil.deleteProductEntry(productEntryId);
144: }
145:
146: protected List getFullImages(UploadPortletRequest uploadReq)
147: throws Exception {
148:
149: return getImages(uploadReq, "fullImage");
150: }
151:
152: protected List getImages(UploadPortletRequest uploadReq,
153: String imagePrefix) throws Exception {
154:
155: List images = new ArrayList();
156:
157: Iterator itr = getSortedParameterNames(uploadReq, imagePrefix)
158: .iterator();
159:
160: while (itr.hasNext()) {
161: String name = (String) itr.next();
162:
163: int priority = GetterUtil.getInteger(name.substring(
164: imagePrefix.length(), name.length()));
165:
166: File file = uploadReq.getFile(name);
167: byte[] bytes = FileUtil.getBytes(file);
168:
169: boolean preserveScreenshot = ParamUtil.getBoolean(
170: uploadReq, "preserveScreenshot" + priority);
171:
172: if (preserveScreenshot) {
173: SCProductScreenshot productScreenshot = getProductScreenshot(
174: uploadReq, priority);
175:
176: Image image = null;
177:
178: if (imagePrefix.equals("fullImage")) {
179: image = ImageLocalUtil.getImage(productScreenshot
180: .getFullImageId());
181: } else {
182: image = ImageLocalUtil.getImage(productScreenshot
183: .getThumbnailId());
184: }
185:
186: bytes = image.getTextObj();
187: }
188:
189: if ((bytes != null) && (bytes.length > 0)) {
190: images.add(bytes);
191: } else {
192: throw new ProductEntryScreenshotsException();
193: }
194: }
195:
196: return images;
197: }
198:
199: protected SCProductScreenshot getProductScreenshot(
200: UploadPortletRequest uploadReq, int priority)
201: throws Exception {
202:
203: long productEntryId = ParamUtil.getLong(uploadReq,
204: "productEntryId");
205:
206: try {
207: return SCProductScreenshotLocalServiceUtil
208: .getProductScreenshot(productEntryId, priority);
209: } catch (Exception e) {
210: throw new ProductEntryScreenshotsException();
211: }
212: }
213:
214: protected List getSortedParameterNames(
215: UploadPortletRequest uploadReq, String imagePrefix)
216: throws Exception {
217:
218: List parameterNames = new ArrayList();
219:
220: Enumeration enu = uploadReq.getParameterNames();
221:
222: while (enu.hasMoreElements()) {
223: String name = (String) enu.nextElement();
224:
225: if (name.startsWith(imagePrefix)) {
226: parameterNames.add(name);
227: }
228: }
229:
230: Collections.sort(parameterNames);
231:
232: return parameterNames;
233: }
234:
235: protected List getThumbnails(UploadPortletRequest uploadReq)
236: throws Exception {
237:
238: return getImages(uploadReq, "thumbnail");
239: }
240:
241: protected void updateProductEntry(ActionRequest req)
242: throws Exception {
243: UploadPortletRequest uploadReq = PortalUtil
244: .getUploadPortletRequest(req);
245:
246: Layout layout = (Layout) req.getAttribute(WebKeys.LAYOUT);
247:
248: long productEntryId = ParamUtil.getLong(req, "productEntryId");
249:
250: String name = ParamUtil.getString(req, "name");
251: String type = ParamUtil.getString(req, "type");
252: String tags = ParamUtil.getString(req, "tags");
253: String shortDescription = ParamUtil.getString(req,
254: "shortDescription");
255: String longDescription = ParamUtil.getString(req,
256: "longDescription");
257: String pageURL = ParamUtil.getString(req, "pageURL");
258: String author = ParamUtil.getString(req, "author");
259: String repoGroupId = ParamUtil.getString(req, "repoGroupId");
260: String repoArtifactId = ParamUtil.getString(req,
261: "repoArtifactId");
262:
263: long[] licenseIds = ParamUtil.getLongValues(req, "licenses");
264:
265: List thumbnails = getThumbnails(uploadReq);
266: List fullImages = getFullImages(uploadReq);
267:
268: String[] communityPermissions = req
269: .getParameterValues("communityPermissions");
270: String[] guestPermissions = req
271: .getParameterValues("guestPermissions");
272:
273: if (productEntryId <= 0) {
274:
275: // Add product entry
276:
277: SCProductEntryServiceUtil.addProductEntry(layout.getPlid(),
278: name, type, tags, shortDescription,
279: longDescription, pageURL, author, repoGroupId,
280: repoArtifactId, licenseIds, thumbnails, fullImages,
281: communityPermissions, guestPermissions);
282: } else {
283:
284: // Update product entry
285:
286: SCProductEntryServiceUtil.updateProductEntry(
287: productEntryId, name, type, tags, shortDescription,
288: longDescription, pageURL, author, repoGroupId,
289: repoArtifactId, licenseIds, thumbnails, fullImages);
290: }
291: }
292:
293: }
|