1 4 package org.roller.presentation.weblog.actions; 5 6 import java.util.Comparator ; 7 import java.util.Iterator ; 8 import java.util.LinkedList ; 9 import java.util.TreeSet ; 10 11 import javax.servlet.http.HttpServletRequest ; 12 import javax.servlet.http.HttpServletResponse ; 13 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 import org.apache.struts.action.ActionError; 17 import org.apache.struts.action.ActionErrors; 18 import org.apache.struts.action.ActionForm; 19 import org.apache.struts.action.ActionForward; 20 import org.apache.struts.action.ActionMapping; 21 import org.apache.struts.action.ActionMessage; 22 import org.apache.struts.action.ActionMessages; 23 import org.apache.struts.actions.DispatchAction; 24 import org.roller.RollerException; 25 import org.roller.model.WeblogManager; 26 import org.roller.pojos.WeblogCategoryData; 27 import org.roller.pojos.WebsiteData; 28 import org.roller.presentation.RollerRequest; 29 import org.roller.presentation.weblog.formbeans.CategoriesForm; 30 31 39 public class CategoriesAction extends DispatchAction 40 { 41 private static Log mLogger = 42 LogFactory.getFactory().getInstance(CategoriesAction.class); 43 44 53 public ActionForward selectCategory( 54 ActionMapping mapping, 55 ActionForm actionForm, 56 HttpServletRequest request, 57 HttpServletResponse response) 58 throws RollerException 59 { 60 RollerRequest rreq = RollerRequest.getRollerRequest(request); 61 if (rreq.isUserAuthorizedToEdit()) 62 { 63 addModelObjects(request, (CategoriesForm)actionForm); 64 return mapping.findForward("CategoriesForm"); 65 } 66 else 67 { 68 return mapping.findForward("access-denied"); 69 } 70 } 71 72 81 public ActionForward moveSelected( 82 ActionMapping mapping, 83 ActionForm actionForm, 84 HttpServletRequest request, 85 HttpServletResponse response) 86 throws RollerException 87 { 88 ActionMessages messages = new ActionMessages(); 89 ActionForward forward = mapping.findForward("CategoriesForm"); 90 RollerRequest rreq = RollerRequest.getRollerRequest(request); 91 if (rreq.isUserAuthorizedToEdit()) 92 { 93 try 94 { 95 WeblogManager wmgr = rreq.getRoller().getWeblogManager(); 96 CategoriesForm form = (CategoriesForm)actionForm; 97 98 mLogger.debug("Moving categories to category, id=" 99 + form.getMoveToCategoryId()); 100 101 String Categories[] = form.getSelectedCategories(); 103 WeblogCategoryData parent = 104 wmgr.retrieveWeblogCategory(form.getMoveToCategoryId()); 105 if (null != Categories) 106 { 107 for (int i = 0; i < Categories.length; i++) 108 { 109 WeblogCategoryData cd = 110 wmgr.retrieveWeblogCategory(Categories[i]); 111 112 if ( !cd.getId().equals(parent.getId()) 114 && !parent.descendentOf(cd)) 115 { 116 cd.setParent(parent); 117 cd.save(); 118 } 119 else 120 { 121 messages.add(null, new ActionMessage( 122 "categoriesForm.warn.notMoving",cd.getName())); 123 } 124 } 125 } 126 127 rreq.getRoller().commit(); 128 129 addModelObjects(request, (CategoriesForm)actionForm); 130 saveMessages(request, messages); 131 } 132 catch (RollerException e) 133 { 134 ActionErrors errors = new ActionErrors(); 135 errors.add(null, new ActionError("categoriesForm.error.move")); 136 saveErrors(request, errors); 137 } 138 } 139 else 140 { 141 forward = mapping.findForward("access-denied"); 142 } 143 return forward; 144 } 145 146 151 private void addModelObjects(HttpServletRequest request, CategoriesForm form) 152 throws RollerException 153 { 154 RollerRequest rreq = RollerRequest.getRollerRequest(request); 155 WebsiteData wd = rreq.getWebsite(); 156 WeblogManager wmgr = rreq.getRoller().getWeblogManager(); 157 158 TreeSet allCategories = new TreeSet (new CategoryPathComparator()); 159 160 String catId = (String )request.getAttribute(RollerRequest.WEBLOGCATEGORYID_KEY); 162 if (null == catId) 163 { 164 catId = request.getParameter(RollerRequest.WEBLOGCATEGORYID_KEY); 165 } 166 if (null == catId) 167 { 168 catId = form.getId(); 169 } 170 171 WeblogCategoryData cat = null; 172 if (null == catId || catId.equals("null")) 173 { 174 cat = wmgr.getRootWeblogCategory(wd); 175 } 176 else 177 { 178 cat = wmgr.retrieveWeblogCategory(catId); 179 } 180 form.setId(cat.getId()); 181 182 request.setAttribute("category", cat); 183 request.setAttribute("categories", cat.getWeblogCategories()); 184 185 if (null != cat.getParent()) 186 { 187 LinkedList catPath = new LinkedList (); 188 catPath.add(0, cat); 189 WeblogCategoryData parent = cat.getParent(); 190 while (parent != null) 191 { 192 catPath.add(0, parent); 193 parent = parent.getParent(); 194 } 195 request.setAttribute("categoryPath", catPath); 196 197 request.setAttribute( 198 RollerRequest.PARENTID_KEY, cat.getParent().getId()); 199 } 200 201 Iterator iter = wmgr.getWeblogCategories(wd).iterator(); 204 while (iter.hasNext()) 205 { 206 WeblogCategoryData cd = (WeblogCategoryData) iter.next(); 207 if (!cd.getId().equals(catId)) 208 { 209 allCategories.add(cd); 210 } 211 } 212 request.setAttribute("allCategories", allCategories); 213 } 214 215 private static final class CategoryPathComparator implements Comparator 216 { 217 public int compare(Object o1, Object o2) { 218 WeblogCategoryData f1 = (WeblogCategoryData)o1; 219 WeblogCategoryData f2 = (WeblogCategoryData)o2; 220 return f1.getPath().compareTo(f2.getPath()); 221 } 222 } 223 } 224 | Popular Tags |