1 5 package com.dotmarketing.portlets.product.action; 6 7 import java.net.URLDecoder ; 8 import java.util.List ; 9 10 import javax.portlet.ActionRequest; 11 import javax.portlet.ActionResponse; 12 import javax.portlet.PortletConfig; 13 import javax.servlet.http.HttpServletRequest ; 14 15 import org.apache.commons.beanutils.BeanUtils; 16 import org.apache.struts.action.ActionForm; 17 import org.apache.struts.action.ActionMapping; 18 19 import com.dotmarketing.db.DotHibernate; 20 import com.dotmarketing.factories.InodeFactory; 21 import com.dotmarketing.portal.struts.DotPortletAction; 22 import com.dotmarketing.util.Logger; 23 import com.dotmarketing.util.UtilMethods; 24 import com.dotmarketing.util.WebKeys; 25 import com.liferay.portal.model.User; 26 import com.liferay.portal.struts.ActionException; 27 import com.liferay.portal.util.Constants; 28 import com.liferay.portlet.ActionRequestImpl; 29 import com.liferay.util.servlet.SessionMessages; 30 import com.dotmarketing.portlets.product.factories.ProductFactory; 31 import com.dotmarketing.portlets.product.model.Product; 32 import com.dotmarketing.portlets.product.struts.ProductForm; 33 34 35 39 public class RelateProductAction extends DotPortletAction { 40 41 public static boolean debug = false; 42 43 public void processAction( 44 ActionMapping mapping, ActionForm form, PortletConfig config, 45 ActionRequest req, ActionResponse res) 46 throws Exception { 47 Logger.debug(this,"START RELATED PRODUCT ACTION"); 48 String cmd = ((req.getParameter(Constants.CMD)!=null)? req.getParameter(Constants.CMD) : ""); 49 String referrer = req.getParameter("referrer"); 50 51 if ((referrer!=null) && (referrer.length()!=0)) 52 { 53 referrer = URLDecoder.decode(referrer,"UTF-8"); 54 } 55 56 DotHibernate.startTransaction(); 57 User user = _getUser(req); 58 59 try { 60 _retrieveProduct(req, res, config, form, user); 61 62 } catch (ActionException ae) { 63 _handleException(ae, req); 64 } 65 68 if ((cmd != null) && cmd.equals(Constants.SAVE)) { 69 try { 70 _saveRelatedProducts(req, res, config, form, user); 71 _sendToReferral(req,res,referrer); 72 } catch (ActionException ae) { 73 _handleException(ae, req); 74 } 75 } 76 81 else if ((cmd != null) && cmd.equals(Constants.DELETE)) 82 { 83 try { 84 _deleteRelatedProduct(req, res, config, form, user); 85 _sendToReferral(req,res,referrer); 86 } catch (ActionException ae) { 87 _handleException(ae, req); 88 } 89 } 90 else if ((cmd != null) && cmd.equals(Constants.CANCEL)) 91 { 92 _sendToReferral(req,res,referrer); 93 } 94 DotHibernate.commitTransaction(); 95 _loadForm(req,res,config,form,user); 96 setForward(req, "portlet.ext.product.related_product"); 97 Logger.debug(this,"END RELATED PRODUCT ACTION"); 98 } 99 100 101 private void _retrieveProduct(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 103 throws Exception { 104 105 String inode = (req.getParameter("inode") != null) ? req.getParameter("inode") : "0"; 106 Product product = null; 107 product = ProductFactory.getProduct(inode); 108 req.setAttribute(WebKeys.PRODUCT_PRODUCT,product); 109 } 110 111 private void _saveRelatedProducts(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 112 throws Exception { 113 ProductForm productForm = (ProductForm) form; 114 Product product = (Product) req.getAttribute(WebKeys.PRODUCT_PRODUCT); 115 116 List <Product> relatedProducts = InodeFactory.getChildrenOfClassByRelationType(product,Product.class,WebKeys.PRODUCT_RELATED); 118 for(Product relatedProduct:relatedProducts) 119 { 120 product.deleteChild(relatedProduct,WebKeys.PRODUCT_RELATED); 121 } 122 123 String [] relatedProductsInodes = productForm.getRelatedProducts(); 124 if (relatedProductsInodes != null) 125 { 126 for(String relatedProductInode : relatedProductsInodes) 127 { 128 if(UtilMethods.isLong(relatedProductInode)) 129 { 130 Product relatedProduct = ProductFactory.getProduct(relatedProductInode); 131 product.addChild(relatedProduct,WebKeys.PRODUCT_RELATED); 132 } 133 } 134 } 135 136 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 139 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 140 141 SessionMessages.add(httpReq, "message", "message.product.product.related"); 142 } 143 144 private void _deleteRelatedProduct(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 145 throws Exception 146 { 147 Product product = (Product) req.getAttribute(WebKeys.PRODUCT_PRODUCT); 148 String relatedProductInode = req.getParameter("relatedProductInode"); 149 if(UtilMethods.isLong(relatedProductInode)) 150 { 151 Product relatedProduct = ProductFactory.getProduct(relatedProductInode); 152 product.deleteChild(relatedProduct,WebKeys.PRODUCT_RELATED); 153 } 154 155 ActionRequestImpl reqImpl = (ActionRequestImpl) req; 158 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 159 160 SessionMessages.add(httpReq, "message", "message.product.product.relationdeleted"); 161 } 162 163 private void _loadForm(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 164 { 165 ProductForm productForm = (ProductForm) form; 166 Product product = (Product) req.getAttribute(WebKeys.PRODUCT_PRODUCT); 167 try 168 { 169 BeanUtils.copyProperties(productForm,product); 170 } 171 catch(Exception ex) 172 { 173 Logger.debug(this,ex.toString()); 174 } 175 176 List <Product> listProducts = ProductFactory.getAllProducts(); 178 productForm.setProducts(listProducts); 179 List <Product> relatedProducts = InodeFactory.getChildrenClassByRelationType(product,Product.class,WebKeys.PRODUCT_RELATED); 181 String [] relatedProductsInodes = new String [relatedProducts.size()]; 182 for(int i = 0; i < relatedProducts.size();i++) 183 { 184 relatedProductsInodes[i] = Long.toString(((Product) relatedProducts.get(i)).getInode()); 185 } 186 productForm.setRelatedProducts(relatedProductsInodes); 187 } 188 } 189 190 | Popular Tags |