KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > admin > homepages > HomePageTool


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  * Contributor(s):
36  * 29-AUG-2003, Jahia Solutions Sarl, Fulco Houkes
37  *
38  * ----- END LICENSE BLOCK -----
39  */

40
41 package org.jahia.admin.homepages;
42
43 import java.io.IOException JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.Enumeration JavaDoc;
46 import java.util.Vector JavaDoc;
47
48 import javax.servlet.ServletException JavaDoc;
49 import javax.servlet.http.HttpServletRequest JavaDoc;
50 import javax.servlet.http.HttpServletResponse JavaDoc;
51 import javax.servlet.http.HttpSession JavaDoc;
52
53 import org.jahia.bin.JahiaAdministration;
54 import org.jahia.data.JahiaData;
55 import org.jahia.engines.selectpage.SelectPage_Engine;
56 import org.jahia.exceptions.JahiaException;
57 import org.jahia.params.ParamBean;
58 import org.jahia.registries.ServicesRegistry;
59 import org.jahia.resourcebundle.JahiaResourceBundle;
60 import org.jahia.services.fields.ContentField;
61 import org.jahia.services.homepages.HomepageTypes;
62 import org.jahia.services.homepages.JahiaHomepage;
63 import org.jahia.services.homepages.JahiaHomepageLink;
64 import org.jahia.services.homepages.JahiaHomepagesService;
65 import org.jahia.services.pages.ContentPage;
66 import org.jahia.services.pages.JahiaPageBaseService;
67 import org.jahia.services.sites.JahiaSite;
68 import org.jahia.services.version.EntryLoadRequest;
69 import org.jahia.utils.LanguageCodeConverters;
70
71 /**
72  * desc: This class is used by the administration to choose an home page.
73  *
74  * Copyright: Copyright (c) 2002
75  * Company: Jahia Ltd
76  *
77  * @version 1.0
78  */

79 public class HomePageTool {
80
81     /** logging */
82     private static org.apache.log4j.Logger logger =
83             org.apache.log4j.Logger.getLogger (HomePageTool.class);
84
85     private static final String JavaDoc JSP_PATH = JahiaAdministration.JSP_PATH;
86
87     private JahiaSite jahiaSite;
88
89     private String JavaDoc message = "";
90     private ParamBean jParams;
91
92     public static final String JavaDoc ENGINE_LAUNCHER_NAME = HomePageTool.class.getName ();
93
94     /**
95      * Default constructor.
96      *
97      * @param request Servlet request.
98      * @param response Servlet response.
99      * @param session Servlet session.
100      */

101     public HomePageTool (HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
102                          HttpSession JavaDoc session)
103             throws Throwable JavaDoc {
104         // get the current website. get the jahiaserver if it's null...
105
jahiaSite = (JahiaSite)session.getAttribute (ParamBean.SESSION_SITE);
106
107         JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData");
108         this.jParams = jData.params ();
109
110         // continue the execution of the request...
111
requestDispatcher (request, response, session);
112     }
113
114     /**
115      * This method is used like a dispatcher for home page tool requests.
116      *
117      * @param request Servlet request.
118      * @param response Servlet response.
119      * @param session Servlet session.
120      */

121     public void requestDispatcher (HttpServletRequest JavaDoc request,
122                                    HttpServletResponse JavaDoc response,
123                                    HttpSession JavaDoc session)
124             throws Throwable JavaDoc {
125         String JavaDoc operation = request.getParameter ("subaction");
126
127         try {
128             if (operation.equals ("display")) {
129                 displayHomePageTool (request, response, session);
130             } else if (operation.equals ("process")) {
131                 processHomePageTool (request, response, session);
132             }
133         } catch (Throwable JavaDoc t) {
134             logger.warn (t);
135         }
136     }
137
138     /**
139      * Forward the servlet request and servlet response objects, using the request
140      * dispatcher (from the ServletContext). Note: please be careful, use only
141      * context relative path.
142      *
143      * @param request servlet request.
144      * @param response servlet response.
145      * @param target target, context-relative path.
146      * @exception IOException an I/O exception occured during the process.
147      * @exception ServletException a servlet exception occured during the process.
148      */

149     private void doRedirect (HttpServletRequest JavaDoc request,
150                              HttpServletResponse JavaDoc response,
151                              HttpSession JavaDoc session,
152                              String JavaDoc target)
153             throws IOException JavaDoc, ServletException JavaDoc {
154         try {
155             // check null warning msg
156
if (request.getAttribute ("warningMsg") == null) {
157                 request.setAttribute ("warningMsg", "");
158             }
159
160             // check null jsp bottom message, and fill in if necessary...
161
if (request.getAttribute ("msg") == null) {
162                 request.setAttribute ("msg",
163                     JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
164                         jParams, jParams.getLocale()));
165             }
166
167             if (request.getAttribute ("focus") == null) {
168                 request.setAttribute ("focus", "-none-");
169             }
170
171             // check null configuration step title, and fill in if necessary...
172
if (request.getAttribute ("title") == null) {
173                 request.setAttribute ("title", "Home Page Tool");
174             }
175
176             // redirect!
177
JahiaAdministration.doRedirect (request, response, session, target);
178
179         } catch (IOException JavaDoc ie) {
180             logger.warn (ie);
181
182         } catch (ServletException JavaDoc se) {
183             logger.warn (se);
184         }
185     }
186
187
188     private void displayHomePageTool (HttpServletRequest JavaDoc request,
189                                       HttpServletResponse JavaDoc response,
190                                       HttpSession JavaDoc session)
191             throws IOException JavaDoc, ServletException JavaDoc, JahiaException {
192         logger.info("Started");
193         // get the home page
194
JahiaHomepagesService homePageService = ServicesRegistry.getInstance ().getJahiaHomepagesService ();
195         Enumeration JavaDoc homePageEnumeration = homePageService.getHomepages (jahiaSite);
196         request.setAttribute ("homePageEnumeration", homePageEnumeration);
197
198         // get all site pages id...
199
JahiaPageBaseService pageService = JahiaPageBaseService.getInstance ();
200         Vector JavaDoc allPagesIDVector = pageService.getPageIDsInSite (jahiaSite.getID (),
201                 org.jahia.services.pages.PageInfoInterface.TYPE_DIRECT);
202         // get all site pages infos...
203
ArrayList JavaDoc sitePages = new ArrayList JavaDoc ();
204         Enumeration JavaDoc allPagesIDEnumeration = allPagesIDVector.elements ();
205         while (allPagesIDEnumeration.hasMoreElements ()) {
206             Integer JavaDoc pageID = (Integer JavaDoc)allPagesIDEnumeration.nextElement ();
207             ContentPage contentPage = pageService.lookupContentPage (pageID.intValue (), jParams.getEntryLoadRequest (), false);
208             sitePages.add (contentPage);
209         }
210         request.setAttribute ("sitePages", sitePages);
211         int pageID = SelectPage_Engine.getInstance ().getSelectedPageID (session);
212         if (pageID != -1) {
213             request.setAttribute ("pageID", new Integer JavaDoc (pageID));
214             request.setAttribute ("homePageID", new Integer JavaDoc (pageID));
215         } else {
216             Integer JavaDoc homePageID = (Integer JavaDoc)session.getAttribute ("homePageID");
217             if (homePageID == null) {
218                 homePageID = new Integer JavaDoc (-1);
219             }
220         }
221         request.setAttribute ("jahiaSite", jahiaSite);
222         request.setAttribute ("jspSource", JSP_PATH + "homepages/popup/homepagetool.jsp");
223         session.setAttribute ("jahiaDisplayMessage",
224             JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
225                 jParams, jParams.getLocale()));
226         request.setAttribute ("message", message);
227         doRedirect (request, response, session, JSP_PATH + "adminpopup.jsp");
228         message = "";
229     }
230
231     private void processHomePageTool (HttpServletRequest JavaDoc request,
232                                       HttpServletResponse JavaDoc response,
233                                       HttpSession JavaDoc session)
234             throws IOException JavaDoc, ServletException JavaDoc, JahiaException {
235         logger.info("Started");
236         String JavaDoc choice = request.getParameter ("choice");
237         if (choice != null) {
238             if (choice.equals ("sitePage")) {
239                 String JavaDoc pageID = request.getParameter ("pageID");
240                 logger.debug("Site Page ID : " + pageID);
241                 if (pageID == null) {
242                     message = JahiaResourceBundle.getAdminResource ("org.jahia.admin.Message.selectSitePage.label",
243                             jParams, jParams.getLocale ());
244                 } else {
245                     ArrayList JavaDoc locales = jahiaSite.getLanguageSettingsAsLocales (false);
246                     EntryLoadRequest loadRequest = jParams.getEntryLoadRequest ();
247                     if (!locales.contains (jParams.getLocale ())) {
248                         loadRequest = EntryLoadRequest.STAGED;
249                         locales.add (0, LanguageCodeConverters.languageCodeToLocale (ContentField.SHARED_LANGUAGE));
250                         loadRequest.setLocales (locales);
251                     }
252                     session.setAttribute ("homePageID", new Integer JavaDoc (pageID));
253                     session.setAttribute ("homePageLabel", JahiaPageBaseService.
254                             getInstance ().lookupContentPage (Integer.parseInt (pageID), loadRequest, false).getTitle (loadRequest));
255                     request.setAttribute ("suicide", "");
256                 }
257
258             } else if (choice.equals ("removeHomePage")) {
259                 session.setAttribute ("homePageID", new Integer JavaDoc (-1));
260                 session.setAttribute ("homePageLabel", "none");
261                 request.setAttribute ("suicide", "");
262
263             } else { // Home page definition
264
JahiaHomepagesService homePageService =
265                         ServicesRegistry.getInstance ().getJahiaHomepagesService ();
266                 String JavaDoc homePageDefID = request.getParameter ("homePageDefID");
267                 JahiaHomepage homePage = homePageService.getHomepage (Integer.parseInt (homePageDefID));
268                 if (homePage.getType () == HomepageTypes.HOMEPAGE_LINK) {
269                     logger.debug ("Home Page ID : " + homePageDefID);
270                     JahiaHomepageLink homePageLink = (JahiaHomepageLink)homePage;
271                     session.setAttribute ("homePageID", new Integer JavaDoc (homePageLink.getPageID ()));
272                     session.setAttribute ("homePageLabel", JahiaPageBaseService.
273                             getInstance ().lookupPageWhitoutTemplates (homePageLink.getPageID ()).getTitle ());
274                 } else if (homePage.getType () == HomepageTypes.HOMEPAGE_NEW) {
275                     //
276
} else if (homePage.getType () == HomepageTypes.HOMEPAGE_COPY) {
277                     //
278
}
279                 request.setAttribute ("suicide", "");
280             }
281
282         } else {
283             message = JahiaResourceBundle.getAdminResource ("org.jahia.admin.Message.selectRadioBoxes.label",
284                     jParams, jParams.getLocale ());
285         }
286         displayHomePageTool (request, response, session);
287     }
288
289 }
290
Popular Tags