KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > admin > pages > ManagePages


1 // $Id: ManagePages.java 10570 2005-09-13 09:35:40Z pvollenweider $
2
//
3
// ____.
4
// __/\ ______| |__/\. _______
5
// __ .____| | \ | +----+ \
6
// _______| /--| | | - \ _ | : - \_________
7
// \\______: :---| : : | : | \________>
8
// |__\---\_____________:______: :____|____:_____\
9
// /_____|
10
//
11
// . . . i n j a h i a w e t r u s t . . .
12
//
13
//
14
// ManagePages
15
//
16
// 01.04.2001 AK added in jahia.
17
// 23.05.2001 NK Lot of change mainly to display the page's template when the user choose another page in the page selectbox.
18
// 13.16.2001 MJ Avoid crash when pointing to a page referencing a deleted template.
19
//
20

21 package org.jahia.admin.pages;
22
23 import java.io.IOException JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 import javax.servlet.ServletException JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
30 import javax.servlet.http.HttpSession JavaDoc;
31
32 import org.jahia.bin.JahiaAdministration;
33 import org.jahia.data.JahiaData;
34 import org.jahia.exceptions.JahiaException;
35 import org.jahia.exceptions.JahiaTemplateNotFoundException;
36 import org.jahia.params.ParamBean;
37 import org.jahia.registries.ServicesRegistry;
38 import org.jahia.resourcebundle.JahiaResourceBundle;
39 import org.jahia.services.pages.ContentPage;
40 import org.jahia.services.pages.JahiaPage;
41 import org.jahia.services.pages.JahiaPageBaseService;
42 import org.jahia.services.pages.JahiaPageDefinition;
43 import org.jahia.services.pages.JahiaPageService;
44 import org.jahia.services.sites.JahiaSite;
45 import org.jahia.services.sites.JahiaSitesService;
46
47
48 /**
49  * desc: This class is used by the administration to manage
50  * pages settings. For each page on the site, you can choose wich template
51  * you want. It's a very good tool when you use the template swapping and
52  * the template doesn't work correctly.
53  *
54  * Copyright: Copyright (c) 2002
55  * Company: Jahia Ltd
56  *
57  * @author Alexandre Kraft
58  * @version 1.0
59  */

60 public class ManagePages
61 {
62     private static final String JavaDoc CLASS_NAME = JahiaAdministration.CLASS_NAME;
63     private static final String JavaDoc JSP_PATH = JahiaAdministration.JSP_PATH;
64     private JahiaSite site;
65
66
67
68     /**
69      * Default constructor.
70      * @author Alexandre Kraft
71      *
72      * @param request Servlet request.
73      * @param response Servlet response.
74      * @param session Servlet session for the current user.
75      */

76     public ManagePages( HttpServletRequest JavaDoc request,
77                         HttpServletResponse JavaDoc response,
78                         HttpSession JavaDoc session )
79     throws Throwable JavaDoc
80     {
81         // get the current website. get the jahiaserver if it's null...
82
site = (JahiaSite) session.getAttribute( ParamBean.SESSION_SITE );
83         if(site==null) {
84             JahiaSitesService sitesService = ServicesRegistry.getInstance().getJahiaSitesService();
85             site = sitesService.getSite( 0 );
86             session.setAttribute( ParamBean.SESSION_SITE, site );
87         }
88
89         userRequestDispatcher( request, response, session );
90     } // end constructor
91

92
93
94     /**
95      * This method is used like a dispatcher for user requests.
96      * @author Alexandre Kraft
97      *
98      * @param request Servlet request.
99      * @param response Servlet response.
100      * @param session Servlet session for the current user.
101      */

102     private void userRequestDispatcher( HttpServletRequest JavaDoc request,
103                                         HttpServletResponse JavaDoc response,
104                                         HttpSession JavaDoc session )
105     throws Throwable JavaDoc
106     {
107         String JavaDoc operation = request.getParameter("sub");
108
109         if(operation.equals("display")) {
110             displayPagesSettings( request, response, session );
111         } else if(operation.equals("process")) {
112             processPagesSettings( request, response, session );
113         }
114     } // userRequestDispatcher
115

116
117
118     /**
119      * Display the pages settings view, using doRedirect(). It's on this
120      * page where you can change the template for each page on your Jahia
121      * portal.
122      * @author Alexandre Kraft
123      * @author Khue Nguyen
124      *
125      * @param request Servlet request.
126      * @param response Servlet response.
127      * @param session Servlet session for the current user.
128      */

129     private void displayPagesSettings( HttpServletRequest JavaDoc request,
130                                        HttpServletResponse JavaDoc response,
131                                        HttpSession JavaDoc session )
132     throws IOException JavaDoc, ServletException JavaDoc
133     {
134         try {
135
136             // get all pages id...
137
JahiaPageBaseService pageService = JahiaPageBaseService.getInstance();
138             Vector JavaDoc allPagesIDVector = pageService.getPageIDsInSite(site.getID(),
139                 org.jahia.services.pages.PageInfoInterface.TYPE_DIRECT);
140
141             // get all pages infos...
142
Vector JavaDoc allPagesInfosVector = new Vector JavaDoc();
143             Enumeration JavaDoc allPagesIDEnumeration = allPagesIDVector.elements();
144             while(allPagesIDEnumeration.hasMoreElements()) {
145                 Integer JavaDoc pageID = (Integer JavaDoc) allPagesIDEnumeration.nextElement();
146                 try {
147                     ContentPage contentPage = ContentPage.getPage(pageID.intValue());
148                     if ( contentPage != null ){
149                         allPagesInfosVector.add (contentPage);
150                     }
151                 } catch (JahiaTemplateNotFoundException jtnfe) {
152                 }
153             }
154
155             // get homepage id for the current site...
156
Integer JavaDoc homePageID = new Integer JavaDoc( site.getHomePageID() );
157
158             // retrieve previous form values...
159
Integer JavaDoc basePageID = (Integer JavaDoc) request.getAttribute(CLASS_NAME + "basePageID");
160             Integer JavaDoc baseTemplateID = (Integer JavaDoc) request.getAttribute(CLASS_NAME + "baseTemplateID");
161
162             // get only visible templates
163
Enumeration JavaDoc allTemplatesEnumeration = ServicesRegistry.getInstance().getJahiaPageTemplateService().getPageTemplates (site.getID(), true);
164
165             // set default values...
166
if(basePageID == null) { basePageID = new Integer JavaDoc(0); }
167             if(baseTemplateID == null) { baseTemplateID = new Integer JavaDoc(0); }
168             if(homePageID == null) { homePageID = new Integer JavaDoc(0); }
169
170             // set all pages infos into an enumeration and redirect...
171
request.setAttribute("homePageID", homePageID);
172             request.setAttribute("basePageID", basePageID);
173             request.setAttribute("baseTemplateID", baseTemplateID);
174             request.setAttribute("allTemplatesEnumeration", allTemplatesEnumeration);
175             request.setAttribute("allPagesInfosEnumeration", allPagesInfosVector.elements());
176
177             JahiaAdministration.doRedirect( request, response, session, JSP_PATH + "pages_settings.jsp" );
178
179             // reset message...
180
JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData");
181             ParamBean jParams = null;
182             if (jData != null) {
183                 jParams = jData.params ();
184             }
185             session.setAttribute(CLASS_NAME + "jahiaDisplayMessage",
186                 JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
187                     jParams, jParams.getLocale()));
188         } catch (JahiaException je) {
189         }
190     } // end displayPagesSettings
191

192
193
194     /**
195      * Process and determine which template the user want for wich
196      * page. It's a simple process, but check if the user has choosed
197      * a page and a template in select boxs.
198      * @author Alexandre Kraft
199      * @author Khue Nguyen
200      *
201      * @param request Servlet request.
202      * @param response Servlet response.
203      * @param session Servlet session for the current user.
204      */

205     protected void processPagesSettings( HttpServletRequest JavaDoc request,
206                                          HttpServletResponse JavaDoc response,
207                                          HttpSession JavaDoc session )
208     throws IOException JavaDoc, ServletException JavaDoc
209     {
210         int idPage = 0;
211         int idTemplate = 0;
212         JahiaPage page = null;
213         JahiaData jData = (JahiaData) request.getAttribute("org.jahia.data.JahiaData");
214         ParamBean jParams = null;
215         if (jData != null) {
216             jParams = jData.params();
217         }
218
219         try {
220
221             JahiaPageService pageServ = ServicesRegistry.getInstance()
222                                                         .getJahiaPageService();
223
224             if ( pageServ == null ){
225
226                 throw new JahiaException( "Unavailable Services",
227                                             "Unavailable Services",
228                                             JahiaException.SERVICE_ERROR,
229                                             JahiaException.ERROR_SEVERITY );
230             }
231
232
233             String JavaDoc subAction = (String JavaDoc)request.getParameter("subaction");
234
235             // get form values...
236
String JavaDoc idPageString = (String JavaDoc)request.getParameter("pageid").trim();
237
238             try {
239                 idPage = Integer.parseInt(idPageString);
240                 if ( idPage != 0 ){
241                     page = pageServ.lookupPage(idPage);
242                     if ( page != null ){
243
244                         JahiaPageDefinition pageDef = page.getPageTemplate();
245                         if ( pageDef != null ){
246                             idTemplate = pageDef.getID();
247                         }
248                     }
249                 }
250             } catch (NumberFormatException JavaDoc nfe) {
251
252                 throw new JahiaException( "ManagePages",
253                                             "page not found",
254                                             JahiaException.DATA_ERROR,
255                                             JahiaException.ERROR_SEVERITY );
256             // Mik - 13.06.2001
257
} catch (JahiaTemplateNotFoundException jtnfe) {
258                 // idTemplate remains 0 ... the template referenced by this page has been deleted
259
// so we want to set a new template.
260
}
261
262
263
264             if ( subAction.equals("save") ) {
265
266                 // check form validity...
267
if(idPageString.equals("0")) {
268                   String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.choicePage.label",
269                                                      jParams, jParams.getLocale());
270                   session.setAttribute(CLASS_NAME + "jahiaDisplayMessage", dspMsg);
271                     idTemplate = 0;
272                 } else if ( page!=null ) {
273                     String JavaDoc idTemplateString = (String JavaDoc) request.getParameter("templateid").trim();
274
275                     try {
276                         idTemplate = Integer.parseInt(idTemplateString);
277
278                         if ( idTemplate != 0 ){
279                             // set new settings for this JahiaPage...
280
page.setPageTemplateID (idTemplate);
281                             page.commitChanges (true);
282                             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.changeUpdated.label",
283                                                                jParams, jParams.getLocale());
284                             session.setAttribute(CLASS_NAME + "jahiaDisplayMessage", dspMsg);
285                         } else {
286                           String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.choiceTemplate.label",
287                                                              jParams, jParams.getLocale());
288                           session.setAttribute(CLASS_NAME + "jahiaDisplayMessage", dspMsg);
289                         }
290                     } catch (NumberFormatException JavaDoc nfe) {
291
292                         throw new JahiaException( "ManagePages",
293                                                     nfe.getMessage(),
294                                                     JahiaException.DATA_ERROR,
295                                                     JahiaException.ERROR_SEVERITY );
296
297                     }
298                 } else {
299
300                         throw new JahiaException( "ManagePages",
301                                                     "Page not found [" + idPage +"]",
302                                                     JahiaException.DATA_ERROR,
303                                                     JahiaException.ERROR_SEVERITY );
304                 }
305             }
306
307             request.setAttribute(CLASS_NAME + "basePageID", new Integer JavaDoc(idPage));
308             request.setAttribute(CLASS_NAME + "baseTemplateID", new Integer JavaDoc(idTemplate));
309             displayPagesSettings( request, response, session );
310
311         } catch ( JahiaException je ){
312           String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource("org.jahia.admin.JahiaDisplayMessage.requestProcessingError.label",
313                                              jParams, jParams.getLocale());
314           request.setAttribute("jahiaDisplayMessage", dspMsg);
315             JahiaAdministration.doRedirect( request,
316                                             response,
317                                             session,
318                                             JSP_PATH + "menu.jsp" );
319         }
320
321
322     } // end processPagesSettings
323

324
325 } // end ManagePages
Popular Tags