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