KickJava   Java API By Example, From Geeks To Geeks.

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


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.data.containers.JahiaContainerList;
41 import org.jahia.data.containers.JahiaContainer;
42 import org.jahia.data.containers.ContainerSorterBean;
43
44 import org.jahia.services.version.EntryLoadRequest;
45
46 import org.jahia.data.fields.LoadFlags;
47
48 import org.jahia.exceptions.JahiaException;
49
50 import org.apache.log4j.Logger;
51
52 import java.util.Vector JavaDoc;
53 import java.util.Enumeration JavaDoc;
54 import java.util.Set JavaDoc;
55
56 /**
57  * Action used to get a user's most recent posts' titles from the Jahia
58  * content repository.
59  * Compliant with MovableType API's getRecentPostTitles method.
60  *
61  * @author Xavier Lawrence
62  */

63 public class GetRecentPostTitlesAction extends AbstractAction {
64     
65     // log4j logger
66
static Logger log = Logger.getLogger(GetRecentPostTitlesAction.class);
67     
68     private String JavaDoc blogID;
69     private int numberOfPosts;
70     
71     /** Creates a new instance of GetRecentPostTitlesAction */
72     public GetRecentPostTitlesAction(String JavaDoc blogID,
73             String JavaDoc userName, String JavaDoc password, int numberOfPosts) {
74         super.userName = userName;
75         super.password = password;
76         this.blogID = blogID;
77         this.numberOfPosts = numberOfPosts;
78     }
79     
80     /**
81      * Retrieves a given number of posts based on the creation date.
82      *
83      * @return A Vector of Hashtables containing the posts
84      */

85     public Object JavaDoc execute() throws JahiaException {
86         // Create commmon resources
87
super.init();
88         
89         // First check that the user is registered to this site.
90
super.checkLogin();
91         
92         // Set the correct page
93
super.changePage(Integer.parseInt(blogID));
94         
95         // Name of the containerList containing all the posts of the blog
96
final String JavaDoc containerListName = super.containerNames.getValue(
97                 BlogDefinitionNames.BLOG_POSTS_LIST_NAME);
98         final int containerListID = containerService.getContainerListID(
99                 containerListName, Integer.parseInt(blogID));
100         
101         if (containerListID == -1) {
102             return new Vector JavaDoc(0);
103         }
104         
105         // This is the ContainerList containing all the entries of a Blog
106
final JahiaContainerList entryList = containerService.loadContainerList(
107                 containerListID, LoadFlags.ALL, jParams);
108         
109         log.debug("ContainerList for Blog: "+blogID+" is: "+containerListID);
110         
111         int posts = entryList.getFullSize();
112         
113         Vector JavaDoc result = new Vector JavaDoc(numberOfPosts);
114         entryList.setCtnListPagination(numberOfPosts, 0);
115         
116         if (numberOfPosts >= posts) {
117             log.debug("Getting all the posts of blog: "+blogID+ "("+
118                     numberOfPosts +" >= "+ posts + ")");
119             // simply return all the posts stored in the container list
120
Enumeration JavaDoc enu = entryList.getContainers();
121             
122             while (enu.hasMoreElements()) {
123                 JahiaContainer postContainer = (JahiaContainer)enu.nextElement();
124                 
125                 result.addElement(super.createMovableTypePostInfo(postContainer));
126             }
127             
128         } else {
129             // Only return the "numberOfPosts" most recent posts
130
log.debug("Getting "+numberOfPosts+" recent posts of blog: "+
131                     blogID);
132             
133             // sort by date desc
134
EntryLoadRequest elr = new EntryLoadRequest(
135                     EntryLoadRequest.STAGING_WORKFLOW_STATE,
136                     jParams.getEntryLoadRequest().getVersionID(),
137                     jParams.getEntryLoadRequest().getLocales());
138             ContainerSorterBean entries_sort_handler =
139                     new ContainerSorterBean(containerListID, "date", true,
140                     elr);
141             entries_sort_handler.setDescOrdering();
142             
143             Vector JavaDoc sortedList = entries_sort_handler.doSort(null);
144             
145             for (int i=0; i<numberOfPosts; i++) {
146                 int cntID = ((Integer JavaDoc)sortedList.get(i)).intValue();
147                 JahiaContainer postContainer = super.getContainer(cntID);
148                 
149                 result.addElement(super.createMovableTypePostInfo(postContainer));
150             }
151         }
152         
153         log.debug("Post(s): "+result);
154         return result;
155     }
156 }
157
Popular Tags