1 37 38 package org.jahia.blogs.actions; 39 40 import org.jahia.blogs.model.BlogCategory; 41 42 import org.jahia.services.pages.ContentPage; 43 import org.jahia.services.pages.JahiaPage; 44 45 import org.jahia.services.categories.Category; 46 47 import org.jahia.exceptions.JahiaException; 48 49 import org.apache.log4j.Logger; 50 51 import java.util.Hashtable ; 52 import java.util.ArrayList ; 53 import java.util.Iterator ; 54 import java.util.Vector ; 55 56 62 public class GetCategoriesAction extends AbstractAction { 63 64 static Logger log = Logger.getLogger(GetCategoriesAction.class); 66 67 private String blogID; 68 private boolean meta; 69 70 71 public GetCategoriesAction(String blogID, String userName, 72 String password, boolean meta) { 73 74 super.userName = userName; 75 super.password = password; 76 this.blogID = blogID; 77 this.meta = meta; 78 } 79 80 85 public Object execute() throws JahiaException { 86 87 super.init(); 89 90 super.checkLogin(); 92 93 ContentPage page = super.changePage(Integer.parseInt(blogID)); 94 JahiaPage blogPage = page.getPage(jParams); 95 96 Category rootCategory = Category.getRootCategory(); 97 ArrayList categories = buildCategoryTree(new ArrayList (), 98 rootCategory); 99 100 Object categoryResult; 101 if (meta) { 102 categoryResult = new Hashtable (); 103 } else { 104 categoryResult = new Vector (); 105 } 106 107 Iterator ite = categories.iterator(); 109 while (ite.hasNext()) { 110 Category cat = Category.getCategory((String )ite.next()); 111 112 Hashtable categoryDef = new Hashtable (3); 113 114 String catDescription = cat.getTitle(jParams.getLocale()); 115 if (catDescription == null || catDescription.length() < 1) { 116 catDescription = cat.getKey(); 117 } 118 119 if (meta) { 120 categoryDef.put(BlogCategory.DESCRIPTION, catDescription); 121 categoryDef.put(BlogCategory.HTML_URL, getCategoryURL(blogPage, cat)); 122 categoryDef.put(BlogCategory.RSS_URL, getCategoryURL(blogPage, cat)); 123 ((Hashtable )categoryResult).put(cat.getKey(), categoryDef); 124 125 } else { 126 categoryDef.put(BlogCategory.MT_CATEGORY_ID, cat.getKey()); 127 categoryDef.put(BlogCategory.MT_CATEGORY_NAME, catDescription); 128 ((Vector )categoryResult).addElement(categoryDef); 129 } 130 } 131 132 return categoryResult; 133 } 134 135 142 protected ArrayList buildCategoryTree (ArrayList categories, 143 Category parentCategory) throws JahiaException { 144 145 if ( parentCategory != null ){ 146 ArrayList childCategories = parentCategory.getChildCategories(); 147 Iterator childIter = childCategories.iterator(); 148 while (childIter.hasNext()) { 149 Category curChildCategory = (Category) childIter.next(); 150 categories.add(curChildCategory.getKey()); 151 buildCategoryTree(categories, curChildCategory); 152 } 153 } 154 return categories; 155 } 156 157 165 protected String getCategoryURL(JahiaPage page, Category category) 166 throws JahiaException { 167 StringBuffer buffer = new StringBuffer (); 168 buffer.append(super.getPageURL(page)); 169 buffer.append("?category="); 170 buffer.append(category.getKey()); 171 return buffer.toString(); 172 } 173 } 174 | Popular Tags |