KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > blogs > actions > GetCategoriesAction


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Shared Modifications are Jahia View Helper.
30  *
31  * The Developer of the Shared Modifications is Jahia Solution Sàrl.
32  * Portions created by the Initial Developer are Copyright (C) 2002 by the
33  * Initial Developer. All Rights Reserved.
34  *
35  * ----- END LICENSE BLOCK -----
36  */

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 JavaDoc;
52 import java.util.ArrayList JavaDoc;
53 import java.util.Iterator JavaDoc;
54 import java.util.Vector JavaDoc;
55
56 /**
57  * Action used to obtain all the categories of a given blog.
58  * Compliant with MetaWeblog API's getCategories method.
59  *
60  * @author Xavier Lawrence
61  */

62 public class GetCategoriesAction extends AbstractAction {
63     
64     // log4j logger
65
static Logger log = Logger.getLogger(GetCategoriesAction.class);
66     
67     private String JavaDoc blogID;
68     private boolean meta;
69     
70     /** Creates a new instance of getCategories */
71     public GetCategoriesAction(String JavaDoc blogID, String JavaDoc userName,
72             String JavaDoc password, boolean meta) {
73         
74         super.userName = userName;
75         super.password = password;
76         this.blogID = blogID;
77         this.meta = meta;
78     }
79     
80     /**
81      * Retrieves the categories of the blog.
82      *
83      * @return A Hashtable containing the categories
84      */

85     public Object JavaDoc execute() throws JahiaException {
86         
87         // Create commmon resources
88
super.init();
89         
90         // First check that the user is registered to this site.
91
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 JavaDoc categories = buildCategoryTree(new ArrayList JavaDoc(),
98                 rootCategory);
99         
100         Object JavaDoc categoryResult;
101         if (meta) {
102             categoryResult = new Hashtable JavaDoc();
103         } else {
104             categoryResult = new Vector JavaDoc();
105         }
106         
107         // Contains all the category keys
108
Iterator JavaDoc ite = categories.iterator();
109         while (ite.hasNext()) {
110             Category cat = Category.getCategory((String JavaDoc)ite.next());
111             
112             Hashtable JavaDoc categoryDef = new Hashtable JavaDoc(3);
113             
114             String JavaDoc 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 JavaDoc)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 JavaDoc)categoryResult).addElement(categoryDef);
129             }
130         }
131  
132         return categoryResult;
133     }
134     
135     /**
136      * Build the category tree for a given root category
137      * @param categories List of current category
138      * @param parentCategory The current category being processed
139      *
140      * @throws JahiaException If something goes wrong
141      */

142     protected ArrayList JavaDoc buildCategoryTree (ArrayList JavaDoc categories,
143             Category parentCategory) throws JahiaException {
144         
145         if ( parentCategory != null ){
146             ArrayList JavaDoc childCategories = parentCategory.getChildCategories();
147             Iterator JavaDoc 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     /**
158      * Constructs the URL of a Category
159      * @param page The page where the Category should be applied
160      * @param category The Category to construct the URL
161      *
162      * @return A String representing the URL
163      * @throws JahiaException If something goes wrong
164      */

165     protected String JavaDoc getCategoryURL(JahiaPage page, Category category)
166     throws JahiaException {
167         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
168         buffer.append(super.getPageURL(page));
169         buffer.append("?category=");
170         buffer.append(category.getKey());
171         return buffer.toString();
172     }
173 }
174
Popular Tags