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