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.File;
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 public void processAction(
068 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
069 ActionRequest actionRequest, ActionResponse actionResponse)
070 throws Exception {
071
072 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
073
074 try {
075 if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
076 updateProductEntry(actionRequest);
077 }
078 else if (cmd.equals(Constants.DELETE)) {
079 deleteProductEntry(actionRequest);
080 }
081
082 if (Validator.isNotNull(cmd)) {
083 sendRedirect(actionRequest, actionResponse);
084 }
085 }
086 catch (Exception e) {
087 if (e instanceof NoSuchProductEntryException ||
088 e instanceof PrincipalException) {
089
090 SessionErrors.add(actionRequest, e.getClass().getName());
091
092 setForward(actionRequest, "portlet.software_catalog.error");
093 }
094 else if (e instanceof DuplicateProductEntryModuleIdException ||
095 e instanceof ProductEntryAuthorException ||
096 e instanceof ProductEntryNameException ||
097 e instanceof ProductEntryLicenseException ||
098 e instanceof ProductEntryPageURLException ||
099 e instanceof ProductEntryScreenshotsException ||
100 e instanceof ProductEntryShortDescriptionException ||
101 e instanceof ProductEntryTypeException) {
102
103 SessionErrors.add(actionRequest, e.getClass().getName());
104 }
105 else {
106 throw e;
107 }
108 }
109 }
110
111 public ActionForward render(
112 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
113 RenderRequest renderRequest, RenderResponse renderResponse)
114 throws Exception {
115
116 try {
117 ActionUtil.getProductEntry(renderRequest);
118 }
119 catch (Exception e) {
120 if (e instanceof NoSuchProductEntryException ||
121 e instanceof PrincipalException) {
122
123 SessionErrors.add(renderRequest, e.getClass().getName());
124
125 return mapping.findForward("portlet.software_catalog.error");
126 }
127 else {
128 throw e;
129 }
130 }
131
132 return mapping.findForward(getForward(
133 renderRequest, "portlet.software_catalog.edit_product_entry"));
134 }
135
136 protected void deleteProductEntry(ActionRequest actionRequest)
137 throws Exception {
138
139 long productEntryId = ParamUtil.getLong(
140 actionRequest, "productEntryId");
141
142 SCProductEntryServiceUtil.deleteProductEntry(productEntryId);
143 }
144
145 protected List<byte[]> getFullImages(UploadPortletRequest uploadRequest)
146 throws Exception {
147
148 return getImages(uploadRequest, "fullImage");
149 }
150
151 protected List<byte[]> getImages(
152 UploadPortletRequest uploadRequest, String imagePrefix)
153 throws Exception {
154
155 List<byte[]> images = new ArrayList<byte[]>();
156
157 for (String name :
158 getSortedParameterNames(uploadRequest, imagePrefix)) {
159
160 int priority = GetterUtil.getInteger(
161 name.substring(imagePrefix.length(), name.length()));
162
163 File file = uploadRequest.getFile(name);
164 byte[] bytes = FileUtil.getBytes(file);
165
166 boolean preserveScreenshot = ParamUtil.getBoolean(
167 uploadRequest, "preserveScreenshot" + priority);
168
169 if (preserveScreenshot) {
170 SCProductScreenshot productScreenshot = getProductScreenshot(
171 uploadRequest, priority);
172
173 Image image = null;
174
175 if (imagePrefix.equals("fullImage")) {
176 image = ImageLocalServiceUtil.getImage(
177 productScreenshot.getFullImageId());
178 }
179 else {
180 image = ImageLocalServiceUtil.getImage(
181 productScreenshot.getThumbnailId());
182 }
183
184 bytes = image.getTextObj();
185 }
186
187 if ((bytes != null) && (bytes.length > 0)) {
188 images.add(bytes);
189 }
190 else {
191 throw new ProductEntryScreenshotsException();
192 }
193 }
194
195 return images;
196 }
197
198 protected SCProductScreenshot getProductScreenshot(
199 UploadPortletRequest uploadRequest, int priority)
200 throws Exception {
201
202 long productEntryId = ParamUtil.getLong(
203 uploadRequest, "productEntryId");
204
205 try {
206 return SCProductScreenshotLocalServiceUtil.getProductScreenshot(
207 productEntryId, priority);
208 }
209 catch (Exception e) {
210 throw new ProductEntryScreenshotsException();
211 }
212 }
213
214 protected List<String> getSortedParameterNames(
215 UploadPortletRequest uploadRequest, String imagePrefix)
216 throws Exception {
217
218 List<String> parameterNames = new ArrayList<String>();
219
220 Enumeration<String> enu = uploadRequest.getParameterNames();
221
222 while (enu.hasMoreElements()) {
223 String name = enu.nextElement();
224
225 if (name.startsWith(imagePrefix)) {
226 parameterNames.add(name);
227 }
228 }
229
230 return ListUtil.sort(parameterNames);
231 }
232
233 protected List<byte[]> getThumbnails(UploadPortletRequest uploadRequest)
234 throws Exception {
235
236 return getImages(uploadRequest, "thumbnail");
237 }
238
239 protected void updateProductEntry(ActionRequest actionRequest)
240 throws Exception {
241
242 UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
243 actionRequest);
244
245 long productEntryId = ParamUtil.getLong(
246 actionRequest, "productEntryId");
247
248 String name = ParamUtil.getString(actionRequest, "name");
249 String type = ParamUtil.getString(actionRequest, "type");
250 String tags = ParamUtil.getString(actionRequest, "tags");
251 String shortDescription = ParamUtil.getString(
252 actionRequest, "shortDescription");
253 String longDescription = ParamUtil.getString(
254 actionRequest, "longDescription");
255 String pageURL = ParamUtil.getString(actionRequest, "pageURL");
256 String author = ParamUtil.getString(actionRequest, "author");
257 String repoGroupId = ParamUtil.getString(actionRequest, "repoGroupId");
258 String repoArtifactId = ParamUtil.getString(
259 actionRequest, "repoArtifactId");
260
261 long[] licenseIds = ParamUtil.getLongValues(actionRequest, "licenses");
262
263 List<byte[]> thumbnails = getThumbnails(uploadRequest);
264 List<byte[]> fullImages = getFullImages(uploadRequest);
265
266 ServiceContext serviceContext = ServiceContextFactory.getInstance(
267 SCProductEntry.class.getName(), actionRequest);
268
269 if (productEntryId <= 0) {
270
271
272
273 SCProductEntryServiceUtil.addProductEntry(
274 name, type, tags, shortDescription, longDescription, pageURL,
275 author, repoGroupId, repoArtifactId, licenseIds, thumbnails,
276 fullImages, serviceContext);
277 }
278 else {
279
280
281
282 SCProductEntryServiceUtil.updateProductEntry(
283 productEntryId, name, type, tags, shortDescription,
284 longDescription, pageURL, author, repoGroupId, repoArtifactId,
285 licenseIds, thumbnails, fullImages);
286 }
287 }
288
289 }