1 package com.dotmarketing.portlets.categories.action; 2 3 import javax.portlet.ActionRequest; 4 import javax.portlet.ActionResponse; 5 import javax.portlet.PortletConfig; 6 import javax.portlet.RenderRequest; 7 import javax.portlet.RenderResponse; 8 import javax.portlet.WindowState; 9 import javax.servlet.http.HttpServletRequest ; 10 11 import org.apache.commons.beanutils.BeanUtils; 12 import org.apache.struts.action.ActionForm; 13 import org.apache.struts.action.ActionForward; 14 import org.apache.struts.action.ActionMapping; 15 16 import com.dotmarketing.factories.InodeFactory; 17 import com.dotmarketing.portal.struts.DotPortletAction; 18 import com.dotmarketing.portlets.categories.factories.CategoryFactory; 19 import com.dotmarketing.portlets.categories.model.Category; 20 import com.dotmarketing.util.Logger; 21 import com.dotmarketing.util.WebKeys; 22 import com.liferay.portlet.ActionRequestImpl; 23 import com.liferay.util.servlet.SessionMessages; 24 25 public class ViewCategoryAction extends DotPortletAction { 26 public ActionForward render(ActionMapping mapping, ActionForm form, PortletConfig config, RenderRequest req, 27 RenderResponse res) throws Exception { 28 29 _viewCategory(form,req,res); 30 31 return mapping.findForward("portlet.ext.categories.view_category"); 32 33 } 34 35 public void processAction(ActionMapping mapping, ActionForm form, PortletConfig config, ActionRequest req, 36 ActionResponse res) throws Exception { 37 String cmd = req.getParameter(com.liferay.portal.util.Constants.CMD); 38 39 ActionRequestImpl reqImpl = (ActionRequestImpl)req; 41 HttpServletRequest httpReq = reqImpl.getHttpServletRequest(); 42 43 String referer = req.getParameter("referer"); 44 _viewCategory(form,req,res); 46 47 if (com.liferay.portal.util.Constants.DELETE.equals(cmd)) { 49 Logger.debug(this, "Category: Deleting Category"); 50 51 try { 52 _deleteCategory(form,req,res); 53 54 if (req.getParameter("parent") !=null) { 55 56 java.util.Map params = new java.util.HashMap (); 57 params.put("struts_action",new String [] {"/ext/categories/view_category"}); 58 params.put("inode", new String [] { req.getParameter("parent") }); 59 60 referer = com.dotmarketing.util.PortletURLUtil.getActionURL(httpReq,WindowState.MAXIMIZED.toString(),params); 61 } 62 } 63 catch(Exception e) { 64 _handleException(e, req); 65 } 66 _sendToReferral(req,res,referer); 67 } 68 69 else if ((cmd != null) && cmd.equals("REORDER")) { 71 Logger.debug(this, "Category: Reordering Categories"); 72 73 try { 74 _reorderCategories(form,req,res); 75 } 76 catch(Exception e) { 77 _handleException(e, req); 78 } 79 setForward(req,"portlet.ext.categories.view_category"); 80 } 81 82 else if ((cmd != null) && cmd.equals("ADDCHILD")) { 84 Logger.debug(this, "Category: Add Child Categories"); 85 86 try { 87 _addChildCategories(form,req,res); 88 } 89 catch(Exception e) { 90 _handleException(e, req); 91 } 92 setForward(req,"portlet.ext.categories.view_category"); 93 } 94 95 else if ((cmd != null) && cmd.equals("DELCHILD")) { 97 Logger.debug(this, "Category: Delete Child Categories"); 98 99 try { 100 _deleteChildCategories(form,req,res); 101 } 102 catch(Exception e) { 103 _handleException(e, req); 104 } 105 setForward(req,"portlet.ext.categories.view_category"); 106 } 107 } 108 109 110 111 private void _saveCategory(ActionForm form, ActionRequest req, ActionResponse res) throws Exception { 113 114 BeanUtils.copyProperties(req.getAttribute(WebKeys.CATEGORY_EDIT),form); 115 Category cat = ( Category ) req.getAttribute(WebKeys.CATEGORY_EDIT); 116 Logger.debug(this, "_saveCategory: Inode =" + cat.getInode()); 117 InodeFactory.saveInode(cat); 118 119 if ((req.getParameter("parent") !=null) && 120 (!req.getParameter("parent").equals("0"))) { 121 Category parent = (Category) InodeFactory.getInode(req.getParameter("parent"), Category.class); 122 parent.addChild(cat); 123 InodeFactory.saveInode(parent); 124 } 125 SessionMessages.add(req, "message", "message.category.update"); 126 127 } 128 private void _deleteCategory(ActionForm form, ActionRequest req, ActionResponse res) throws Exception { 130 131 Category cat = ( Category ) req.getAttribute(WebKeys.CATEGORY_EDIT); 132 req.removeAttribute(WebKeys.CATEGORY_EDIT); 133 InodeFactory.deleteInode(cat); 134 SessionMessages.add(req, "message", "message.category.delete"); 135 136 } 137 private void _reorderCategories(ActionForm form, ActionRequest req, ActionResponse res) throws Exception { 139 140 int count = 0; 141 try { 142 count = Integer.parseInt(req.getParameter("count")); 143 } 144 catch (Exception e) {} 145 String [] order = new String [(count)]; 146 for (int i = 0; i < order.length; i++) { 147 Category cat = (Category) InodeFactory.getInode(req.getParameter("inode" + i), Category.class); 148 cat.setSortOrder(req.getParameter("newOrder" + i)); 149 InodeFactory.saveInode(cat); 150 } 151 SessionMessages.add(req, "message", "message.category.reorder"); 152 153 } 154 private void _addChildCategories(ActionForm form, ActionRequest req, ActionResponse res) throws Exception { 156 157 Category cat = ( Category ) req.getAttribute(WebKeys.CATEGORY_EDIT); 158 Category child = (Category) InodeFactory.getInode(req.getParameter("child"), Category.class); 159 cat.addChild(child); 160 InodeFactory.saveInode(cat); 161 Logger.debug(this, "Child Category Added"); 162 SessionMessages.add(req, "message", "message.category.addchild"); 163 } 164 private void _deleteChildCategories(ActionForm form, ActionRequest req, ActionResponse res) throws Exception { 166 167 Category cat = ( Category ) req.getAttribute(WebKeys.CATEGORY_EDIT); 168 Category child = (Category) InodeFactory.getInode(req.getParameter("child"), Category.class); 169 boolean delChild = cat.deleteChild(child); 170 Logger.debug(this, "After deleting child category = " + delChild); 171 InodeFactory.saveInode(cat); 172 req.setAttribute(WebKeys.CATEGORY_EDIT,cat); 173 SessionMessages.add(req, "message", "message.category.delchild"); 174 } 175 176 private void _viewCategory(ActionForm form, ActionRequest req, ActionResponse res) throws Exception { 178 179 Category cat = (Category) InodeFactory.getInode(req.getParameter("inode"),Category.class); 180 req.setAttribute(WebKeys.CATEGORY_EDIT, cat); 181 req.setAttribute(WebKeys.CATEGORY_LIST_TOP, CategoryFactory.getTopLevelCategories()); 182 183 } 184 private void _viewCategory(ActionForm form, RenderRequest req, RenderResponse res) throws Exception { 186 187 Category cat = (Category) InodeFactory.getInode(req.getParameter("inode"),Category.class); 188 req.setAttribute(WebKeys.CATEGORY_EDIT, cat); 189 req.setAttribute(WebKeys.CATEGORY_LIST_TOP, CategoryFactory.getTopLevelCategories()); 190 191 } 192 } 193 | Popular Tags |