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