KickJava   Java API By Example, From Geeks To Geeks.

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


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.data.containers.JahiaContainerList;
43 import org.jahia.data.containers.JahiaContainer;
44 import org.jahia.data.containers.JahiaContainerDefinition;
45
46 import org.jahia.data.fields.LoadFlags;
47 import org.jahia.data.fields.JahiaField;
48
49 import org.jahia.services.usermanager.JahiaUser;
50
51 import org.jahia.services.version.EntryLoadRequest;
52
53 import org.jahia.services.categories.Category;
54
55 import org.jahia.exceptions.JahiaException;
56
57 import org.apache.log4j.Logger;
58
59 import java.util.Vector JavaDoc;
60 import java.util.Set JavaDoc;
61 import java.util.Hashtable JavaDoc;
62 import java.util.Iterator JavaDoc;
63
64
65 /**
66  * Action used to get a post's categories from the Jahia content repository.
67  * Compliant with MovableType API's getPostCategories method.
68  *
69  * @author Xavier Lawrence
70  */

71 public class GetPostCategoriesAction extends AbstractAction {
72     
73     // log4j logger
74
static Logger log = Logger.getLogger(GetPostCategoriesAction.class);
75     
76     private String JavaDoc postID;
77     
78     /** Creates a new instance of GetPostCategories */
79     public GetPostCategoriesAction(String JavaDoc postID, String JavaDoc userName, String JavaDoc passWord) {
80         this.postID = postID;
81         super.userName = userName;
82         super.password = passWord;
83     }
84     
85     /**
86      * Retrieves a given post.
87      *
88      * @return A Hashtable containing the post information
89      */

90     public Object JavaDoc execute() throws JahiaException {
91         
92         // Create commmon resources
93
super.init();
94         
95         // First check that the user is registered to this site.
96
final JahiaUser user = super.checkLogin();
97         
98         // Load the Container and check the structure
99
final JahiaContainer postContainer = super.getContainer(
100                 Integer.parseInt(postID));
101         
102         if (postContainer == null) {
103             throw new JahiaException("Post: "+postID+
104                     " does not exist", "Container: "+postID+ " does not exist",
105                     JahiaException.ENTRY_NOT_FOUND,
106                     JahiaException.WARNING_SEVERITY);
107         }
108         
109         log.debug("Working on container: "+postContainer.getID());
110         
111         // Prepare the return object
112
Vector JavaDoc cats = this.fromSet(Category.getObjectCategories(postContainer.
113                 getContentContainer().getObjectKey()));
114
115         log.debug("Post categories are: "+ cats);
116         return cats;
117     }
118     
119     /**
120      * Transforms a Set of category IDs into a Vector of IDs
121      */

122     protected Vector JavaDoc fromSet(Set JavaDoc categories) {
123         
124         if (categories == null) return new Vector JavaDoc(0);
125         
126         Vector JavaDoc cats = new Vector JavaDoc(categories.size());
127         Iterator JavaDoc ite = categories.iterator();
128         while (ite.hasNext()) {
129             Hashtable JavaDoc catInfo = new Hashtable JavaDoc(3);
130             Category cat = (Category)ite.next();
131             String JavaDoc catName = cat.getTitle(jParams.getLocale());
132             
133             if (catName == null || catName.length() < 1) {
134                 catName = cat.getKey();
135             }
136             
137             catInfo.put(BlogCategory.MT_CATEGORY_ID, cat.getKey());
138             catInfo.put(BlogCategory.MT_CATEGORY_NAME, catName);
139             catInfo.put(BlogCategory.MT_IS_PRIMARY, new Boolean JavaDoc(false));
140             
141             cats.addElement(catInfo);
142         }
143         return cats;
144     }
145 }
146
Popular Tags