KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.jahia.blogs.actions;
38
39 import org.jahia.blogs.model.BlogInfo;
40
41 import org.jahia.data.containers.JahiaContainerList;
42
43 import org.jahia.data.fields.LoadFlags;
44
45 import org.jahia.services.usermanager.JahiaUser;
46
47 import org.jahia.services.pages.JahiaPage;
48 import org.jahia.services.pages.ContentPage;
49 import org.jahia.services.pages.JahiaPageDefinition;
50 import org.jahia.services.pages.JahiaPageBaseService;
51
52 import org.jahia.exceptions.JahiaException;
53
54 import java.util.Enumeration JavaDoc;
55 import java.util.Vector JavaDoc;
56 import java.util.Hashtable JavaDoc;
57
58 import org.apache.log4j.Logger;
59
60 /**
61  * Action used to get users' blogs information from the Jahia content repository.
62  * Compliant with Blogger API's getUserBlogs method.
63  *
64  * @author Xavier Lawrence
65  */

66 public class GetUserBlogsAction extends AbstractAction {
67     
68     // log4j logger
69
static Logger log = Logger.getLogger(GetUserBlogsAction.class);
70     
71     /** Creates a new instance of GetUserBlogsAction */
72     public GetUserBlogsAction(String JavaDoc appKey, String JavaDoc userName, String JavaDoc password) {
73         super.appKey = appKey;
74         super.userName = userName;
75         super.password = password;
76     }
77     
78     /**
79      * Returns information (blogID, blogName and url) of each blog a given user
80      * is a member of.
81      *
82      * @return A Vector of Hashtables containing the blogs' information
83      */

84     public Object JavaDoc execute() throws JahiaException {
85         
86         // Create commmon resources
87
super.init();
88         
89         // First check that the user is registered to this site.
90
final JahiaUser user = super.checkLogin();
91         
92         // Get all pages with template definition name "Blog"
93
final JahiaPageDefinition pageDef =
94                 servicesRegistry.getJahiaPageTemplateService()
95                 .lookupPageTemplateByName("Blog", jParams.getSiteID());
96         final Vector JavaDoc blogPagesId =
97                 JahiaPageBaseService.getInstance().getPageIDsWithTemplate(
98                 pageDef.getID());
99         
100         Enumeration JavaDoc blogPagesIdEnum = blogPagesId.elements();
101         Vector JavaDoc result = new Vector JavaDoc();
102
103         // For each page matching the Blog definition
104
while (blogPagesIdEnum.hasMoreElements()) {
105             int blogPageId = ((Integer JavaDoc)blogPagesIdEnum.nextElement()).intValue();
106
107             ContentPage blogContentPage = super.changePage(blogPageId);
108             
109             int deleteVersionID = blogContentPage.getDeleteVersionID();
110             
111             // ignore all deleted pages
112
if ((deleteVersionID != -1)) continue;
113             
114             JahiaPage blogPage =
115                     blogContentPage.getPage(jParams.getEntryLoadRequest(),
116                     jParams.getOperationMode(), user);
117             if (blogPage != null) {
118                 log.debug("Found blog ["+ blogPageId + "]: " +
119                         blogPage.getTitle());
120                 
121                 // get the entries container List
122
final String JavaDoc containerListName = super.containerNames.getValue(
123                         BlogDefinitionNames.BLOG_POSTS_LIST_NAME);
124                 final int containerListID = containerService.getContainerListID(
125                         containerListName, blogPageId);
126                 
127                 // In that case, there is no post on the blog page, so the
128
// container list does not exist yet. We thus have to check the
129
// write access of the page
130
boolean ok = false;
131                 JahiaContainerList entryList = null;
132                 if (containerListID == -1) {
133                     log.debug("entryList does not exist");
134                     ok = blogPage.checkWriteAccess(user);
135                     
136                 } else {
137                     entryList = containerService.
138                             loadContainerList(containerListID, LoadFlags.ALL,
139                             jParams);
140                 }
141                 
142                 // Check ACL to see if the user has write access to the blog.
143
// If the container list does not exist, test the boolean
144
if ((entryList != null && entryList.checkWriteAccess(user)) ||
145                         ok) {
146                     log.debug("User: "+user.getUsername()+" has write "+
147                             "access to blog: "+blogPage.getTitle()+ "(" +
148                             blogPage.getID() + ")");
149                     
150                     Hashtable JavaDoc blogInfo = new Hashtable JavaDoc(3);
151                     
152                     blogInfo.put(BlogInfo.BLOG_URL, super.getPageURL(blogPage));
153                     blogInfo.put(BlogInfo.BLOG_ID, Integer.toString(
154                             blogPage.getID()));
155                     blogInfo.put(BlogInfo.BLOG_NAME, blogPage.getTitle());
156                     
157                     result.add(blogInfo);
158                 }
159             }
160         }
161         
162         log.debug("Returning Blog info of "+result.size()+" blogs");
163         return result;
164     }
165 }
166
Popular Tags