1 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 ; 55 import java.util.Enumeration ; 56 import java.util.Set ; 57 58 65 public class GetRecentPostsAction extends AbstractAction { 66 67 static Logger log = Logger.getLogger(GetRecentPostsAction.class); 69 70 private String blogID; 71 private int numberOfPosts; 72 private boolean meta; 73 74 77 public GetRecentPostsAction(String appKey, String blogID, 78 String userName, String 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 90 public GetRecentPostsAction(String blogID, 91 String userName, String 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 104 public Object execute() throws JahiaException { 105 super.init(); 107 108 super.checkLogin(); 110 111 super.changePage(Integer.parseInt(blogID)); 113 114 final String 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 (0); 122 } 123 124 EntryLoadRequest elr = new EntryLoadRequest( 125 EntryLoadRequest.STAGING_WORKFLOW_STATE, 126 0, 127 jParams.getEntryLoadRequest().getLocales()); 128 129 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 result = new Vector (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 Enumeration enu = entryList.getContainers(); 145 146 while (enu.hasMoreElements()) { 147 JahiaContainer postContainer = (JahiaContainer)enu.nextElement(); 148 149 if (meta) { 150 Set 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 log.debug("Getting "+numberOfPosts+" recent posts of blog: "+ 162 blogID); 163 164 ContainerSorterBean entries_sort_handler = 166 new ContainerSorterBean(containerListID, "date", true, 167 elr); 168 entries_sort_handler.setDescOrdering(); 169 170 Vector sortedList = entries_sort_handler.doSort(null); 171 172 for (int i=0; i<numberOfPosts; i++) { 173 int cntID = ((Integer )sortedList.get(i)).intValue(); 174 JahiaContainer postContainer = super.getContainer(cntID); 175 176 if (meta) { 177 Set 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 |