1
22
23 package com.liferay.portlet.softwarecatalog.action;
24
25 import com.liferay.portal.kernel.util.Constants;
26 import com.liferay.portal.kernel.util.GetterUtil;
27 import com.liferay.portal.kernel.util.ParamUtil;
28 import com.liferay.portal.kernel.util.Validator;
29 import com.liferay.portal.model.Image;
30 import com.liferay.portal.model.Layout;
31 import com.liferay.portal.security.auth.PrincipalException;
32 import com.liferay.portal.service.impl.ImageLocalUtil;
33 import com.liferay.portal.struts.PortletAction;
34 import com.liferay.portal.util.PortalUtil;
35 import com.liferay.portal.util.WebKeys;
36 import com.liferay.portlet.softwarecatalog.NoSuchProductEntryException;
37 import com.liferay.portlet.softwarecatalog.ProductEntryAuthorException;
38 import com.liferay.portlet.softwarecatalog.ProductEntryLicenseException;
39 import com.liferay.portlet.softwarecatalog.ProductEntryNameException;
40 import com.liferay.portlet.softwarecatalog.ProductEntryPageURLException;
41 import com.liferay.portlet.softwarecatalog.ProductEntryScreenshotsException;
42 import com.liferay.portlet.softwarecatalog.ProductEntryShortDescriptionException;
43 import com.liferay.portlet.softwarecatalog.ProductEntryTypeException;
44 import com.liferay.portlet.softwarecatalog.model.SCProductScreenshot;
45 import com.liferay.portlet.softwarecatalog.service.SCProductEntryServiceUtil;
46 import com.liferay.portlet.softwarecatalog.service.SCProductScreenshotLocalServiceUtil;
47 import com.liferay.util.FileUtil;
48 import com.liferay.util.servlet.SessionErrors;
49 import com.liferay.util.servlet.UploadPortletRequest;
50
51 import java.io.File;
52
53 import java.util.ArrayList;
54 import java.util.Collections;
55 import java.util.Enumeration;
56 import java.util.Iterator;
57 import java.util.List;
58
59 import javax.portlet.ActionRequest;
60 import javax.portlet.ActionResponse;
61 import javax.portlet.PortletConfig;
62 import javax.portlet.RenderRequest;
63 import javax.portlet.RenderResponse;
64
65 import org.apache.struts.action.ActionForm;
66 import org.apache.struts.action.ActionForward;
67 import org.apache.struts.action.ActionMapping;
68
69
75 public class EditProductEntryAction extends PortletAction {
76
77 public void processAction(
78 ActionMapping mapping, ActionForm form, PortletConfig config,
79 ActionRequest req, ActionResponse res)
80 throws Exception {
81
82 String cmd = ParamUtil.getString(req, Constants.CMD);
83
84 try {
85 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
86 updateProductEntry(req);
87 }
88 else if (cmd.equals(Constants.DELETE)) {
89 deleteProductEntry(req);
90 }
91
92 if (Validator.isNotNull(cmd)) {
93 sendRedirect(req, res);
94 }
95 }
96 catch (Exception e) {
97 if (e instanceof NoSuchProductEntryException ||
98 e instanceof PrincipalException) {
99
100 SessionErrors.add(req, e.getClass().getName());
101
102 setForward(req, "portlet.software_catalog.error");
103 }
104 else if (e instanceof ProductEntryAuthorException ||
105 e instanceof ProductEntryNameException ||
106 e instanceof ProductEntryLicenseException ||
107 e instanceof ProductEntryPageURLException ||
108 e instanceof ProductEntryScreenshotsException ||
109 e instanceof ProductEntryShortDescriptionException ||
110 e instanceof ProductEntryTypeException) {
111
112 SessionErrors.add(req, e.getClass().getName());
113 }
114 else {
115 throw e;
116 }
117 }
118 }
119
120 public ActionForward render(
121 ActionMapping mapping, ActionForm form, PortletConfig config,
122 RenderRequest req, RenderResponse res)
123 throws Exception {
124
125 try {
126 ActionUtil.getProductEntry(req);
127 }
128 catch (Exception e) {
129 if (e instanceof NoSuchProductEntryException ||
130 e instanceof PrincipalException) {
131
132 SessionErrors.add(req, e.getClass().getName());
133
134 return mapping.findForward("portlet.software_catalog.error");
135 }
136 else {
137 throw e;
138 }
139 }
140
141 return mapping.findForward(
142 getForward(req, "portlet.software_catalog.edit_product_entry"));
143 }
144
145 protected void deleteProductEntry(ActionRequest req) throws Exception {
146 long productEntryId = ParamUtil.getLong(req, "productEntryId");
147
148 SCProductEntryServiceUtil.deleteProductEntry(productEntryId);
149 }
150
151 protected List getFullImages(UploadPortletRequest uploadReq)
152 throws Exception {
153
154 return getImages(uploadReq, "fullImage");
155 }
156
157 protected List getImages(UploadPortletRequest uploadReq, String imagePrefix)
158 throws Exception {
159
160 List images = new ArrayList();
161
162 Iterator itr = getSortedParameterNames(
163 uploadReq, imagePrefix).iterator();
164
165 while (itr.hasNext()) {
166 String name = (String)itr.next();
167
168 int priority = GetterUtil.getInteger(
169 name.substring(imagePrefix.length(), name.length()));
170
171 File file = uploadReq.getFile(name);
172 byte[] bytes = FileUtil.getBytes(file);
173
174 boolean preserveScreenshot = ParamUtil.getBoolean(
175 uploadReq, "preserveScreenshot" + priority);
176
177 if (preserveScreenshot) {
178 SCProductScreenshot productScreenshot = getProductScreenshot(
179 uploadReq, priority);
180
181 Image image = null;
182
183 if (imagePrefix.equals("fullImage")) {
184 image = ImageLocalUtil.getImage(
185 productScreenshot.getFullImageId());
186 }
187 else {
188 image = ImageLocalUtil.getImage(
189 productScreenshot.getThumbnailId());
190 }
191
192 bytes = image.getTextObj();
193 }
194
195 if ((bytes != null) && (bytes.length > 0)) {
196 images.add(bytes);
197 }
198 else {
199 throw new ProductEntryScreenshotsException();
200 }
201 }
202
203 return images;
204 }
205
206 protected SCProductScreenshot getProductScreenshot(
207 UploadPortletRequest uploadReq, int priority)
208 throws Exception {
209
210 long productEntryId = ParamUtil.getLong(uploadReq, "productEntryId");
211
212 try {
213 return SCProductScreenshotLocalServiceUtil.getProductScreenshot(
214 productEntryId, priority);
215 }
216 catch (Exception e) {
217 throw new ProductEntryScreenshotsException();
218 }
219 }
220
221 protected List getSortedParameterNames(
222 UploadPortletRequest uploadReq, String imagePrefix)
223 throws Exception {
224
225 List parameterNames = new ArrayList();
226
227 Enumeration enu = uploadReq.getParameterNames();
228
229 while (enu.hasMoreElements()) {
230 String name = (String)enu.nextElement();
231
232 if (name.startsWith(imagePrefix)) {
233 parameterNames.add(name);
234 }
235 }
236
237 Collections.sort(parameterNames);
238
239 return parameterNames;
240 }
241
242 protected List getThumbnails(UploadPortletRequest uploadReq)
243 throws Exception {
244
245 return getImages(uploadReq, "thumbnail");
246 }
247
248 protected void updateProductEntry(ActionRequest req) throws Exception {
249 UploadPortletRequest uploadReq =
250 PortalUtil.getUploadPortletRequest(req);
251
252 Layout layout = (Layout)req.getAttribute(WebKeys.LAYOUT);
253
254 long productEntryId = ParamUtil.getLong(req, "productEntryId");
255
256 String name = ParamUtil.getString(req, "name");
257 String type = ParamUtil.getString(req, "type");
258 String tags = ParamUtil.getString(req, "tags");
259 String shortDescription = ParamUtil.getString(req, "shortDescription");
260 String longDescription = ParamUtil.getString(req, "longDescription");
261 String pageURL = ParamUtil.getString(req, "pageURL");
262 String author = ParamUtil.getString(req, "author");
263 String repoGroupId = ParamUtil.getString(req, "repoGroupId");
264 String repoArtifactId = ParamUtil.getString(req, "repoArtifactId");
265
266 long[] licenseIds = ParamUtil.getLongValues(req, "licenses");
267
268 List thumbnails = getThumbnails(uploadReq);
269 List fullImages = getFullImages(uploadReq);
270
271 String[] communityPermissions = req.getParameterValues(
272 "communityPermissions");
273 String[] guestPermissions = req.getParameterValues(
274 "guestPermissions");
275
276 if (productEntryId <= 0) {
277
278
280 SCProductEntryServiceUtil.addProductEntry(
281 layout.getPlid(), name, type, tags, shortDescription,
282 longDescription, pageURL, author, repoGroupId, repoArtifactId,
283 licenseIds, thumbnails, fullImages, communityPermissions,
284 guestPermissions);
285 }
286 else {
287
288
290 SCProductEntryServiceUtil.updateProductEntry(
291 productEntryId, name, type, tags, shortDescription,
292 longDescription, pageURL, author, repoGroupId, repoArtifactId,
293 licenseIds, thumbnails, fullImages);
294 }
295 }
296
297 }