1 5 package com.dotmarketing.portlets.product.action; 6 7 import java.net.URLDecoder ; 8 import java.util.ArrayList ; 9 import java.util.Enumeration ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 13 import javax.portlet.ActionRequest; 14 import javax.portlet.ActionResponse; 15 import javax.portlet.PortletConfig; 16 import javax.servlet.http.HttpServletRequest ; 17 18 import org.apache.commons.beanutils.BeanUtils; 19 import org.apache.struts.Globals; 20 import org.apache.struts.action.ActionErrors; 21 import org.apache.struts.action.ActionForm; 22 import org.apache.struts.action.ActionMapping; 23 24 import com.dotmarketing.beans.Identifier; 25 import com.dotmarketing.db.DotHibernate; 26 import com.dotmarketing.factories.IdentifierFactory; 27 import com.dotmarketing.factories.InodeFactory; 28 import com.dotmarketing.portal.struts.DotPortletAction; 29 import com.dotmarketing.portlets.categories.model.Category; 30 import com.dotmarketing.portlets.files.model.File; 31 import com.dotmarketing.util.Logger; 32 import com.dotmarketing.util.UtilMethods; 33 import com.dotmarketing.util.WebKeys; 34 import com.liferay.portal.model.User; 35 import com.liferay.portal.struts.ActionException; 36 import com.liferay.portal.util.Constants; 37 import com.liferay.portlet.ActionRequestImpl; 38 import com.liferay.util.servlet.SessionMessages; 39 import com.dotmarketing.portlets.product.factories.ProductFactory; 40 import com.dotmarketing.portlets.product.model.Product; 41 import com.dotmarketing.portlets.product.struts.ProductForm; 42 43 44 48 public class EditProductAction extends DotPortletAction { 49 50 public static boolean debug = false; 51 52 public void processAction( 53 ActionMapping mapping, ActionForm form, PortletConfig config, 54 ActionRequest req, ActionResponse res) 55 throws Exception { 56 Logger.debug(this,"START EDIT PRODUCT ACTION"); 57 ProductForm productForm = (ProductForm) form; 58 String cmd = (req.getParameter(Constants.CMD)!=null)? req.getParameter(Constants.CMD) : ""; 59 String referrer = req.getParameter("referrer"); 60 61 if ((referrer!=null) && (referrer.length()!=0)) 62 { 63 referrer = URLDecoder.decode(referrer,"UTF-8"); 64 } 65 66 DotHibernate.startTransaction(); 67 User user = _getUser(req); 68 69 try { 70 _retrieveProduct(req, res, config, form, user); 71 72 } catch (ActionException ae) { 73 _handleException(ae, req); 74 } 75 76 79 if ((cmd != null) && cmd.equals(Constants.SAVE)) { 80 try { 81 ActionErrors ae = null; 82 ae = productForm.validate(mapping,req); 83 if (ae.size() == 0) 84 { 85 _saveProduct(req, res, config, form, user); 86 } 88 else 89 { 90 req.setAttribute(Globals.ERROR_KEY, ae); 91 String input = mapping.getInput(); 92 setForward(req,input); 93 return; 94 } 95 96 } catch (ActionException ae) { 97 _handleException(ae, req); 98 } 99 } 100 105 else if ((cmd != null) && cmd.equals(Constants.DELETE)) { 106 try { 107 _deleteProduct(req, res, config, form, user); 108 _sendToReferral(req,res,referrer); 109 110 } catch (ActionException ae) { 111 _handleException(ae, req); 112 } 113 } 114 else if ((cmd != null) && cmd.equals(com.dotmarketing.util.Constants.COPY)) { 115 try { 116 _copyProduct(req, res, config, form, user); 117 _sendToReferral(req,res,referrer); 118 119 } catch (ActionException ae) { 120 _handleException(ae, req); 121 } 122 } 123 else if ((cmd != null) && cmd.equals(Constants.CANCEL)) { 124 try { 125 _sendToReferral(req,res,referrer); 126 } catch (ActionException ae) { 127 _handleException(ae, req); 128 } 129 } 130 else if ((cmd != null) && cmd.equals(com.dotmarketing.util.Constants.REORDER)) { 131 try { 132 _reorderProduct(req, res, config, form, user); 133 _sendToReferral(req,res,referrer); 134 } catch (ActionException ae) { 135 _handleException(ae, req); 136 } 137 } 138 139 DotHibernate.commitTransaction(); 140 141 _loadForm(req,res,config,form,user); 142 setForward(req, "portlet.ext.product.edit_product"); 143 Logger.debug(this,"END EDIT PRODUCT ACTION"); 144 } 145 146 147 private void _retrieveProduct(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 149 throws Exception { 150 151 String inode = ((UtilMethods.isSet(req.getParameter("inode"))) ? req.getParameter("inode") : "0"); 152 Product product = null; 153 product = ProductFactory.getProduct(inode); 154 req.setAttribute(WebKeys.PRODUCT_PRODUCT,product); 155 } 156 157 private void _saveProduct(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 158 throws Exception { 159 160 ProductForm productForm = (ProductForm) form; 161 Product product = (Product) req.getAttribute(WebKeys.PRODUCT_PRODUCT); 162 BeanUtils.copyProperties(product,productForm); 163 ProductFactory.saveProduct(product); 164 165 java.util.List _cats = InodeFactory.getParentsOfClass(product,Category.class); 168 Iterator it = _cats.iterator(); 169 while (it.hasNext()) { 170 Category cat = ( Category ) it.next(); 171 cat.deleteChild(product); 172 } 174 175 String [] arr = productForm.getProductTypes(); 177 178 List oldCategories = InodeFactory.getParentsOfClassByRelationType(product,Category.class,WebKeys.PRODUCT_PRODUCTS_TYPE); 180 for(int i = 0;i < oldCategories.size();i++) 181 { 182 Category node = (Category) oldCategories.get(i); 183 product.deleteParent(node,WebKeys.PRODUCT_PRODUCTS_TYPE); 184 } 185 186 if (arr != null) { 187 for (int i = 0; i < arr.length; i++) { 188 Category node = ( Category ) InodeFactory.getInode(arr[i], Category.class); 189 node.addChild(product,WebKeys.PRODUCT_PRODUCTS_TYPE); 190 } 192 } 193 arr = productForm.getTopics(); 196 197 if (arr != null) { 198 for (int i = 0; i < arr.length; i++) { 199 Category node = ( Category ) InodeFactory.getInode(arr[i], Category.class); 200 node.addChild(product,WebKeys.PRODUCT_CATEGORIES); 201 } 203 } 204 ProductFactory.saveProduct(product); 206 207 _saveFiles(product, productForm.getFilesInodes()); 209 210 _saveImages(product,productForm); 212 213 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 216 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 217 218 SessionMessages.add(httpReq, "message", "message.product.product.saved"); 219 } 220 private void _saveImages(Product product, ProductForm productForm) { 221 222 Identifier identifier = (Identifier) InodeFactory.getChildOfClassByRelationType(product,Identifier.class,WebKeys.PRODUCT_SMALL_IMAGE); 224 product.deleteChild(identifier,WebKeys.PRODUCT_SMALL_IMAGE); 225 identifier = (Identifier) InodeFactory.getChildOfClassByRelationType(product,Identifier.class,WebKeys.PRODUCT_MEDIUM_IMAGE); 226 product.deleteChild(identifier,WebKeys.PRODUCT_MEDIUM_IMAGE); 227 identifier = (Identifier) InodeFactory.getChildOfClassByRelationType(product,Identifier.class,WebKeys.PRODUCT_LARGE_IMAGE); 228 product.deleteChild(identifier,WebKeys.PRODUCT_LARGE_IMAGE); 229 230 DotHibernate.flush(); 231 232 long smallImage = productForm.getSmallImage(); 234 if (smallImage > 0) { 235 File file = (File) InodeFactory.getInode(smallImage,File.class); 236 identifier = IdentifierFactory.getIdentifierByInode(file); 237 product.addChild(identifier,WebKeys.PRODUCT_SMALL_IMAGE); 238 } 239 240 long mediumImage = productForm.getMediumImage(); 242 if (mediumImage > 0) { 243 File file = (File) InodeFactory.getInode(mediumImage,File.class); 244 identifier = IdentifierFactory.getIdentifierByInode(file); 245 product.addChild(identifier,WebKeys.PRODUCT_MEDIUM_IMAGE); 246 } 247 248 long largeImage = productForm.getLargeImage(); 250 if (largeImage > 0) { 251 File file = (File) InodeFactory.getInode(largeImage,File.class); 252 identifier = IdentifierFactory.getIdentifierByInode(file); 253 product.addChild(identifier,WebKeys.PRODUCT_LARGE_IMAGE); 254 } 255 } 256 257 private void _saveFiles (Product product, String [] fileInodes) { 258 259 java.util.List _files = InodeFactory.getChildrenClassByRelationType(product, Identifier.class,WebKeys.PRODUCT_FILES); 260 Iterator it = _files.iterator(); 261 while (it.hasNext()) { 262 Identifier iden = ( Identifier ) it.next(); 263 product.deleteChild(iden); 264 } 265 if(fileInodes != null) 266 { 267 for (int i = 0; i < fileInodes.length; i++) 268 { 269 String inodes = fileInodes[i]; 270 if (UtilMethods.isSet(inodes)) 271 { 272 String [] inodesArray = inodes.split(","); 273 for(int j = 0;j < inodesArray.length;j++) 274 { 275 String inode = inodesArray[j]; 276 File file = (File)InodeFactory.getInode(inode, File.class); 277 Identifier identifier = IdentifierFactory.getParentIdentifier(file); 278 product.addChild(identifier,WebKeys.PRODUCT_FILES); 279 } 280 } 281 } 282 } 283 } 284 285 private void _deleteProduct(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 286 throws Exception 287 { 288 Product product = (Product) req.getAttribute(WebKeys.PRODUCT_PRODUCT); 289 ProductFactory.deleteProduct(product); 291 SessionMessages.add(req, "message", "message.product.product.deleted"); 292 } 293 294 private void _copyProduct(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 295 throws Exception 296 { 297 Product product = (Product) req.getAttribute(WebKeys.PRODUCT_PRODUCT); 298 ProductFactory.copyProduct(product); 300 SessionMessages.add(req, "message", "message.product.product.copied"); 301 } 302 303 private void _reorderProduct(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 304 throws Exception 305 { 306 Enumeration enumerator = req.getParameterNames(); 307 while(enumerator.hasMoreElements()) 308 { 309 String parameterName = (String ) enumerator.nextElement(); 310 if (parameterName.indexOf("reorder") != -1) 311 { 312 long productInode = Long.parseLong(parameterName.split("_")[1]); 313 String orderString = req.getParameter(parameterName); 314 int order = (UtilMethods.isInt(orderString) ? Integer.parseInt(orderString): 0); 315 Product product = ProductFactory.getProduct(productInode); 316 product.setSortOrder(order); 317 ProductFactory.saveProduct(product); 318 } 319 } 320 SessionMessages.add(req, "message", "message.product.product.reordered"); 321 } 322 323 private void _loadForm(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 324 { 325 Product product = (Product) req.getAttribute(WebKeys.PRODUCT_PRODUCT); 326 ProductForm productForm = (ProductForm) form; 327 try 328 { 329 BeanUtils.copyProperties(productForm,product); 330 } 331 catch(Exception ex) 332 { 333 Logger.debug(this,ex.toString()); 334 } 335 337 Identifier identifier = (Identifier) InodeFactory.getChildOfClassByRelationType(product,Identifier.class,WebKeys.PRODUCT_SMALL_IMAGE); 340 File file = (File) IdentifierFactory.getWorkingChildOfClass(identifier,File.class); 341 productForm.setSmallImage(file.getInode()); 342 343 identifier = (Identifier) InodeFactory.getChildOfClassByRelationType(product,Identifier.class,WebKeys.PRODUCT_MEDIUM_IMAGE); 345 file = (File) IdentifierFactory.getWorkingChildOfClass(identifier,File.class); 346 productForm.setMediumImage(file.getInode()); 347 348 identifier = (Identifier) InodeFactory.getChildOfClassByRelationType(product,Identifier.class,WebKeys.PRODUCT_LARGE_IMAGE); 350 file = (File) IdentifierFactory.getWorkingChildOfClass(identifier,File.class); 351 productForm.setLargeImage(file.getInode()); 352 353 ArrayList <String > fileInodes = new ArrayList <String >(); 355 List identifiers = InodeFactory.getChildrenClassByRelationType(product,Identifier.class,WebKeys.PRODUCT_FILES); 356 Iterator it = identifiers.iterator(); 357 while (it.hasNext()) { 358 identifier = (Identifier)it.next(); 359 file = (File) IdentifierFactory.getWorkingChildOfClass(identifier, File.class); 360 fileInodes.add(Long.toString(file.getInode())); 361 } 362 String [] files = new String [fileInodes.size()]; 363 for(int i = 0;i < files.length;i++) 364 { 365 files[i] = fileInodes.get(i); 366 } 367 productForm.setFilesInodesList(files); 368 productForm.setFilesInodes(files); 369 370 java.util.List categories = InodeFactory.getParentsOfClass(product,Category.class); 372 String [] categoriesInodes = new String [categories.size()]; 373 for(int i = 0;i < categories.size();i++) 374 { 375 categoriesInodes[i] = Long.toString(((Category) categories.get(i)).getInode()); 376 } 377 productForm.setTopics(categoriesInodes); 378 productForm.setProductTypes(categoriesInodes); 379 } 380 } 381 382 383 384 385 | Popular Tags |