1 18 21 package org.apache.roller.ui.authoring.struts.actions; 22 23 import java.text.MessageFormat ; 24 import java.util.Comparator ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.LinkedList ; 28 import java.util.Set ; 29 import java.util.TreeSet ; 30 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 import org.apache.struts.action.ActionError; 37 import org.apache.struts.action.ActionErrors; 38 import org.apache.struts.action.ActionForm; 39 import org.apache.struts.action.ActionForward; 40 import org.apache.struts.action.ActionMapping; 41 import org.apache.struts.action.ActionMessage; 42 import org.apache.struts.action.ActionMessages; 43 import org.apache.struts.actions.DispatchAction; 44 import org.apache.roller.RollerException; 45 import org.apache.roller.model.RollerFactory; 46 import org.apache.roller.model.WeblogManager; 47 import org.apache.roller.pojos.WeblogCategoryData; 48 import org.apache.roller.ui.core.BasePageModel; 49 import org.apache.roller.ui.core.RollerRequest; 50 import org.apache.roller.ui.core.RollerSession; 51 import org.apache.roller.ui.authoring.struts.formbeans.CategoriesForm; 52 import org.apache.roller.ui.core.RequestConstants; 53 54 62 public class CategoriesAction extends DispatchAction 63 { 64 private static Log mLogger = 65 LogFactory.getFactory().getInstance(CategoriesAction.class); 66 67 76 public ActionForward selectCategory( 77 ActionMapping mapping, 78 ActionForm actionForm, 79 HttpServletRequest request, 80 HttpServletResponse response) 81 throws RollerException 82 { 83 CategoriesPageModel pageModel = new CategoriesPageModel( 84 request, response, mapping, (CategoriesForm)actionForm); 85 RollerSession rses = RollerSession.getRollerSession(request); 86 if (rses.isUserAuthorizedToAuthor(pageModel.getCategory().getWebsite())) 87 { 88 request.setAttribute("model", pageModel); 89 return mapping.findForward("CategoriesForm"); 90 } 91 else 92 { 93 return mapping.findForward("access-denied"); 94 } 95 } 96 97 106 public ActionForward moveSelected( 107 ActionMapping mapping, 108 ActionForm actionForm, 109 HttpServletRequest request, 110 HttpServletResponse response) 111 throws RollerException 112 { 113 ActionMessages messages = new ActionMessages(); 114 ActionForward forward = mapping.findForward("CategoriesForm"); 115 116 CategoriesPageModel pageModel = new CategoriesPageModel( 117 request, response, mapping, (CategoriesForm)actionForm); 118 119 RollerSession rses = RollerSession.getRollerSession(request); 120 if (rses.isUserAuthorizedToAuthor(pageModel.getCategory().getWebsite())) 121 { 122 request.setAttribute("model", pageModel); 123 try 124 { 125 WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager(); 126 CategoriesForm form = (CategoriesForm)actionForm; 127 128 mLogger.debug("Moving categories to category, id=" 129 + form.getMoveToCategoryId()); 130 131 String Categories[] = form.getSelectedCategories(); 133 WeblogCategoryData parent = 134 wmgr.getWeblogCategory(form.getMoveToCategoryId()); 135 if (null != Categories) 136 { 137 for (int i = 0; i < Categories.length; i++) 138 { 139 WeblogCategoryData cd = 140 wmgr.getWeblogCategory(Categories[i]); 141 142 if ( !cd.getId().equals(parent.getId()) 144 && !parent.descendentOf(cd)) 145 { 146 cd.setParent(parent); 147 wmgr.saveWeblogCategory(cd); 148 } 149 else 150 { 151 messages.add(null, new ActionMessage( 152 "categoriesForm.warn.notMoving",cd.getName())); 153 } 154 } 155 } 156 RollerFactory.getRoller().flush(); 158 saveMessages(request, messages); 159 } 160 catch (RollerException e) 161 { 162 ActionErrors errors = new ActionErrors(); 163 errors.add(null, new ActionError("categoriesForm.error.move")); 164 saveErrors(request, errors); 165 } 166 } 167 else 168 { 169 forward = mapping.findForward("access-denied"); 170 } 171 return forward; 172 } 173 174 private static final class CategoryPathComparator implements Comparator 175 { 176 public int compare(Object o1, Object o2) { 177 WeblogCategoryData f1 = (WeblogCategoryData)o1; 178 WeblogCategoryData f2 = (WeblogCategoryData)o2; 179 return f1.getPath().compareTo(f2.getPath()); 180 } 181 } 182 183 184 public class CategoriesPageModel extends BasePageModel 185 { 186 private CategoriesForm form = null; 187 private WeblogCategoryData cat = null; 188 private TreeSet allCategories = null; 189 private List catPath = null; 190 191 public WeblogCategoryData getCategory() { return cat; } 192 public Set getAllCategories() { return allCategories; } 193 public List getCategoryPath() { return catPath; } 194 195 public CategoriesPageModel( 196 HttpServletRequest request, 197 HttpServletResponse response, 198 ActionMapping mapping, 199 CategoriesForm form) throws RollerException 200 { 201 super("dummy", request, response, mapping); 202 this.form = form; 203 204 RollerRequest rreq = RollerRequest.getRollerRequest(request); 205 WeblogManager wmgr = RollerFactory.getRoller().getWeblogManager(); 206 207 allCategories = new TreeSet (new CategoryPathComparator()); 208 209 String catId = (String ) 211 request.getAttribute(RequestConstants.WEBLOGCATEGORY_ID); 212 if (null == catId) 213 { 214 catId = request.getParameter(RequestConstants.WEBLOGCATEGORY_ID); 215 } 216 if (null == catId) 217 { 218 catId = form.getId(); 219 } 220 221 cat = null; 222 if (null == catId || catId.equals("null")) 223 { 224 cat = wmgr.getRootWeblogCategory(website); 225 } 226 else 227 { 228 cat = wmgr.getWeblogCategory(catId); 229 website = cat.getWebsite(); 230 } 231 form.setId(cat.getId()); 232 233 235 if (null != cat.getParent()) 236 { 237 catPath = new LinkedList (); 238 catPath.add(0, cat); 239 WeblogCategoryData parent = cat.getParent(); 240 while (parent != null) 241 { 242 catPath.add(0, parent); 243 parent = parent.getParent(); 244 } 245 247 request.setAttribute( 248 RequestConstants.PARENT_ID, cat.getParent().getId()); 249 } 250 251 Iterator iter = wmgr.getWeblogCategories(website).iterator(); 254 while (iter.hasNext()) 255 { 256 WeblogCategoryData cd = (WeblogCategoryData) iter.next(); 257 if (!cd.getId().equals(catId)) 258 { 259 allCategories.add(cd); 260 } 261 } 262 request.setAttribute("allCategories", allCategories); 264 request.setAttribute("category", cat); 265 } 266 public String getTitle() 267 { 268 if (catPath == null || catPath.isEmpty()) 269 { 270 return bundle.getString("categoriesForm.rootTitle"); 271 } 272 else 273 { 274 return MessageFormat.format( 275 bundle.getString("categoriesForm.parent"), 276 new Object [] {cat.getName()}); 277 } 278 } 279 } 280 } 281 | Popular Tags |