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