KickJava   Java API By Example, From Geeks To Geeks.

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


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.services.categories.Category;
47
48 import org.jahia.data.fields.LoadFlags;
49
50 import org.jahia.exceptions.JahiaException;
51
52 import org.apache.log4j.Logger;
53
54 import java.util.Vector JavaDoc;
55 import java.util.Enumeration JavaDoc;
56 import java.util.Set JavaDoc;
57
58 /**
59  * Action used to get a user's most recent posts' information from the Jahia
60  * content repository.
61  * Compliant with Blogger API's and MetaWeblog API's getRecentPosts method.
62  *
63  * @author Xavier Lawrence
64  */

65 public class GetRecentPostsAction extends AbstractAction {
66     
67     // log4j logger
68
static Logger log = Logger.getLogger(GetRecentPostsAction.class);
69     
70      private String JavaDoc blogID;
71      private int numberOfPosts;
72      private boolean meta;
73     
74     /**
75      * Creates a new instance of GetRecentPostsAction (Blogger API)
76      */

77      public GetRecentPostsAction(String JavaDoc appKey, String JavaDoc blogID,
78              String JavaDoc userName, String JavaDoc password, int numberOfPosts) {
79          super.appKey = appKey;
80          super.userName = userName;
81          super.password = password;
82          this.blogID = blogID;
83          this.numberOfPosts = numberOfPosts;
84          meta = false;
85      }
86      
87     /**
88      * Creates a new instance of GetRecentPostsAction (MetaWeblog API)
89      */

90      public GetRecentPostsAction(String JavaDoc blogID,
91              String JavaDoc userName, String JavaDoc password, int numberOfPosts) {
92          super.userName = userName;
93          super.password = password;
94          this.blogID = blogID;
95          this.numberOfPosts = numberOfPosts;
96          meta = true;
97      }
98        
99      /**
100       * Retrieves a given number of posts based on the creation date.
101       *
102       * @return A Vector of Hashtables containing the posts
103       */

104      public Object JavaDoc execute() throws JahiaException {
105          // Create commmon resources
106
super.init();
107          
108          // First check that the user is registered to this site.
109
super.checkLogin();
110          
111          // Set the correct page
112
super.changePage(Integer.parseInt(blogID));
113          
114          // Name of the containerList containing all the posts of the blog
115
final String JavaDoc containerListName = super.containerNames.getValue(
116                  BlogDefinitionNames.BLOG_POSTS_LIST_NAME);
117          final int containerListID = containerService.getContainerListID(
118                  containerListName, Integer.parseInt(blogID));
119          
120          if (containerListID == -1) {
121              return new Vector JavaDoc(0);
122          }
123          
124          EntryLoadRequest elr = new EntryLoadRequest(
125                  EntryLoadRequest.STAGING_WORKFLOW_STATE,
126                  0,
127                  jParams.getEntryLoadRequest().getLocales());
128          
129          // This is the ContainerList containing all the entries of a Blog
130
final JahiaContainerList entryList = containerService.loadContainerList(
131                  containerListID, LoadFlags.ALL, jParams, elr, null, null, null);
132          
133          log.debug("ContainerList for Blog: "+blogID+" is: "+containerListID);
134          
135          int posts = entryList.size();
136          
137          Vector JavaDoc result = new Vector JavaDoc(numberOfPosts);
138          entryList.setCtnListPagination(numberOfPosts, 0);
139          
140          if (numberOfPosts >= posts) {
141              log.debug("Getting all the posts of blog: "+blogID+ " ("+
142                      numberOfPosts +" >= "+ posts + ")");
143             // simply return all the posts stored in the container list
144
Enumeration JavaDoc enu = entryList.getContainers();
145              
146              while (enu.hasMoreElements()) {
147                  JahiaContainer postContainer = (JahiaContainer)enu.nextElement();
148                  
149                  if (meta) {
150                      Set JavaDoc set = Category.getObjectCategories(postContainer.
151                              getContentContainer().getObjectKey());
152                      result.addElement(super.createMetaPostInfo(postContainer,
153                              set));
154                  } else {
155                      result.addElement(super.createPostInfo(postContainer));
156                  }
157              }
158              
159          } else {
160             // Only return the "numberOfPosts" most recent posts
161
log.debug("Getting "+numberOfPosts+" recent posts of blog: "+
162                      blogID);
163              
164              // sort by date desc
165
ContainerSorterBean entries_sort_handler =
166                      new ContainerSorterBean(containerListID, "date", true,
167                      elr);
168              entries_sort_handler.setDescOrdering();
169              
170              Vector JavaDoc sortedList = entries_sort_handler.doSort(null);
171              
172              for (int i=0; i<numberOfPosts; i++) {
173                  int cntID = ((Integer JavaDoc)sortedList.get(i)).intValue();
174                  JahiaContainer postContainer = super.getContainer(cntID);
175                  
176                  if (meta) {
177                      Set JavaDoc set = Category.getObjectCategories(postContainer.
178                              getContentContainer().getObjectKey());
179                      result.addElement(super.createMetaPostInfo(postContainer,
180                              set));
181                  } else {
182                      result.addElement(super.createPostInfo(postContainer));
183                  }
184              }
185          }
186          
187          log.debug("Post(s): "+result);
188          return result;
189      }
190 }
191
Popular Tags