KickJava   Java API By Example, From Geeks To Geeks.

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


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  * 18-DEC-2001, Xo3 SA, Khue Nguyen : initial version
37  * 29-AUG-2003, Jahia Solutions Sarl, Fulco Houkes
38  *
39  * ----- END LICENSE BLOCK -----
40  */

41
42
43 package org.jahia.admin.homepages;
44
45 import java.io.IOException JavaDoc;
46 import java.util.Enumeration JavaDoc;
47 import java.util.Hashtable JavaDoc;
48 import java.util.Locale JavaDoc;
49 import java.util.Vector JavaDoc;
50
51 import javax.servlet.ServletContext JavaDoc;
52 import javax.servlet.ServletException JavaDoc;
53 import javax.servlet.http.HttpServletRequest JavaDoc;
54 import javax.servlet.http.HttpServletResponse JavaDoc;
55 import javax.servlet.http.HttpSession JavaDoc;
56
57 import org.jahia.bin.JahiaAdministration;
58 import org.jahia.data.JahiaData;
59 import org.jahia.engines.selectpage.SelectPage_Engine;
60 import org.jahia.exceptions.JahiaException;
61 import org.jahia.params.ParamBean;
62 import org.jahia.registries.ServicesRegistry;
63 import org.jahia.resourcebundle.JahiaResourceBundle;
64 import org.jahia.services.homepages.HomepageTypes;
65 import org.jahia.services.homepages.JahiaHomepage;
66 import org.jahia.services.homepages.JahiaHomepageCopy;
67 import org.jahia.services.homepages.JahiaHomepageLink;
68 import org.jahia.services.homepages.JahiaHomepageNew;
69 import org.jahia.services.homepages.JahiaHomepagesService;
70 import org.jahia.services.pages.ContentPage;
71 import org.jahia.services.pages.JahiaPage;
72 import org.jahia.services.pages.JahiaPageBaseService;
73 import org.jahia.services.pages.JahiaPageDefinition;
74 import org.jahia.services.sites.JahiaSite;
75 import org.jahia.services.sites.JahiaSiteTools;
76 import org.jahia.services.sites.JahiaSitesService;
77 import org.jahia.services.usermanager.JahiaUser;
78
79
80 /**
81  * This class is used by the administration to manage homepages definitions.
82  *
83  * Copyright: Copyright (c) 2002
84  * Company: Jahia Ltd
85  *
86  * @author Khue Nguyen
87  * @version 1.0
88  */

89 public class ManageHomepages {
90     /** logging */
91     private static org.apache.log4j.Logger logger =
92             org.apache.log4j.Logger.getLogger (ManageHomepages.class);
93
94     private static final String JavaDoc JSP_PATH = JahiaAdministration.JSP_PATH;
95     private static final String JavaDoc HTTP_FILES = "/jsp/jahia/engines";
96     private static final String JavaDoc JSP = "/jsp/jahia/administration/homepages/main.jsp?includejsp=";
97
98     private static ServletContext JavaDoc context;
99
100     private ServicesRegistry sReg;
101     private JahiaSitesService siteServ;
102     private JahiaHomepagesService hpServ;
103
104     private JahiaSite site;
105     private JahiaUser user;
106     private Hashtable JavaDoc values = new Hashtable JavaDoc ();
107     private ParamBean jParams = null;
108
109     private String JavaDoc operation = ""; // current operation
110

111     public static final String JavaDoc CHOOSEPAGE_LAUNCHER_LINK_SOURCEPAGE
112             = "org.jahia.admin.ManageHomepages.link_sourcepage";
113     public static final String JavaDoc CHOOSEPAGE_LAUNCHER_NEW_DESTPAGE
114             = "org.jahia.admin.ManageHomepages.new_destpage";
115     public static final String JavaDoc CHOOSEPAGE_LAUNCHER_COPY_DESTPAGE
116             = "org.jahia.admin.ManageHomepages.copy_destpage";
117     public static final String JavaDoc CHOOSEPAGE_LAUNCHER_COPY_SOURCEPAGE
118             = "org.jahia.admin.ManageHomepages.copy_sourcepage";
119
120     //--------------------------------------------------------------------------
121
/**
122      * Default constructor.
123      *
124      * @param request Servlet request.
125      * @param response Servlet response.
126      * @param session Servlet session for the current user.
127      * @param context Servlet context.
128      */

129     public ManageHomepages (HttpServletRequest JavaDoc request,
130                             HttpServletResponse JavaDoc response,
131                             HttpSession JavaDoc session,
132                             ServletContext JavaDoc context)
133             throws Throwable JavaDoc {
134         ManageHomepages.context = context;
135
136         sReg = ServicesRegistry.getInstance ();
137         JahiaData jData = (JahiaData)request.getAttribute ("org.jahia.data.JahiaData");
138         this.jParams = jData.params ();
139
140         if (sReg == null) {
141             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.serviceRegistyNotAvailable.label",
142                     jParams, jParams.getLocale ());
143             displayError (request, response, dspMsg);
144             return;
145         }
146
147         siteServ = sReg.getJahiaSitesService ();
148         hpServ = sReg.getJahiaHomepagesService ();
149
150         if (siteServ == null || hpServ == null) {
151             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.requestServiceNotAvailable.label",
152                     jParams, jParams.getLocale ());
153             displayError (request, response, dspMsg);
154             return;
155         }
156
157         userRequestDispatcher (request, response, session);
158     }
159
160
161     //--------------------------------------------------------------------------
162
/**
163      * This method is used like a dispatcher for user requests.
164      *
165      * @param request Servlet request.
166      * @param response Servlet response.
167      * @param session Servlet session for the current user.
168      */

169     private void userRequestDispatcher (HttpServletRequest JavaDoc request,
170                                         HttpServletResponse JavaDoc response,
171                                         HttpSession JavaDoc session)
172             throws IOException JavaDoc, ServletException JavaDoc {
173
174         // check if the user has really admin access to this site...
175
user = (JahiaUser)session.getAttribute (ParamBean.SESSION_USER);
176         site = (JahiaSite)session.getAttribute (ParamBean.SESSION_SITE);
177
178         if (site == null || user == null || sReg == null) {
179             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.requestInfoNotAvailable.label",
180                     jParams, jParams.getLocale ());
181             displayError (request, response, dspMsg);
182             return;
183         }
184
185         try {
186
187             if (JahiaSiteTools.getAdminGroup (site).isMember (user)) {
188
189                 operation = request.getParameter ("sub");
190
191                 if (operation.equals ("display")) {
192                     displayMain (request, response, session);
193                 } else if (operation.equals ("add")) {
194                     displayAdd (request, response, session);
195                 } else if (operation.equals ("detail")) {
196                     displayDetail (request, response, session);
197                 } else if (operation.equals ("options")) {
198                     displayOptions (request, response, session);
199                 }
200
201             } else {
202                 //System.out.println(" --> no admin access on this site <--");
203
String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.noAdminAcessSite.label",
204                         jParams, jParams.getLocale ());
205                 displayError (request, response, dspMsg);
206             }
207
208         } catch (Throwable JavaDoc t) {
209             t.printStackTrace ();
210             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.processingRequestError.label",
211                     jParams, jParams.getLocale ());
212             displayError (request, response, dspMsg);
213         }
214     }
215
216     /**
217      * Forward the servlet request and servlet response objects, using the request
218      * dispatcher (from the ServletContext). Note: please be careful, use only
219      * context relative path.
220      *
221      * @param request servlet request.
222      * @param response servlet response.
223      * @param target target, context-relative path.
224      * @exception IOException an I/O exception occured during the process.
225      * @exception ServletException a servlet exception occured during the process.
226      */

227     private void doRedirect (HttpServletRequest JavaDoc request,
228                              HttpServletResponse JavaDoc response,
229                              String JavaDoc target)
230             throws IOException JavaDoc, ServletException JavaDoc {
231         try {
232             request.setAttribute ("currentSite", site);
233             request.setAttribute ("user", user);
234
235             // check current operation
236
if (request.getAttribute ("operation") == null) {
237                 request.setAttribute ("operation", operation);
238             }
239
240             // check null warning msg
241
if (request.getAttribute ("warningMsg") == null) {
242                 request.setAttribute ("warningMsg", "");
243             }
244
245             // check null jsp bottom message, and fill in if necessary...
246
if (request.getAttribute ("msg") == null) {
247                 request.setAttribute ("msg",
248                     JahiaResourceBundle.getAdminResource("org.jahia.admin.copyright",
249                         jParams, jParams.getLocale()));
250             }
251
252             if (request.getAttribute ("focus") == null) {
253                 request.setAttribute ("focus", "-none-");
254             }
255
256             // check null configuration step title, and fill in if necessary...
257
if (request.getAttribute ("title") == null) {
258                 request.setAttribute ("title", "Manage Home pages");
259             }
260
261             // set input values on request...
262
request.setAttribute ("values", values);
263
264             // add http files path in request...
265
request.setAttribute ("url", request.getContextPath () + HTTP_FILES);
266
267             // get browser entity...
268
String JavaDoc userAgent = request.getHeader ("user-agent");
269
270             Integer JavaDoc navigator = (userAgent.indexOf ("IE"))
271                     != -1 ? new Integer JavaDoc (0) : new Integer JavaDoc (1);
272             request.setAttribute ("navigator", navigator);
273
274             boolean isLynx = false;
275
276             if (userAgent.indexOf ("Lynx") != -1) {
277                 isLynx = true;
278             }
279             request.setAttribute ("isLynx", new Boolean JavaDoc (isLynx));
280
281             // set browser response content type
282
HttpSession JavaDoc session = request.getSession ();
283             Locale JavaDoc currentLocale = (Locale JavaDoc)session.getAttribute (ParamBean.SESSION_LOCALE);
284             if (currentLocale == null) {
285                 currentLocale = jParams.getLocale ();
286             }
287
288             String JavaDoc contentTypeStr = "text/html;charset=";
289             contentTypeStr = contentTypeStr + "ISO8859-1";
290             request.setAttribute ("content-type", contentTypeStr);
291
292             // redirect!
293
context.getRequestDispatcher (target).forward (request, response);
294
295         } catch (IOException JavaDoc ie) {
296             logger.error ("IOException while redirecting to " + target, ie);
297
298         } catch (ServletException JavaDoc se) {
299             logger.error("Servlet exception while redirecting to " + target, se);
300             Throwable JavaDoc t = se.getRootCause();
301             while (t != null) {
302                 logger.error("Root cause", t);
303                 if (t instanceof ServletException JavaDoc) {
304                     t = ((ServletException JavaDoc)t).getRootCause();
305                 } else {
306                     t = null;
307                 }
308             }
309         }
310     }
311
312     //--------------------------------------------------------------------------
313
/**
314      * Display the main admin page for homepage definitions.
315      *
316      * @param request Servlet request.
317      * @param response Servlet response.
318      * @param session Servlet session for the current user.
319      */

320     private void displayMain (HttpServletRequest JavaDoc request,
321                               HttpServletResponse JavaDoc response,
322                               HttpSession JavaDoc session)
323             throws IOException JavaDoc, ServletException JavaDoc, JahiaException {
324         // reset session
325
session.setAttribute ("homepageBean", null);
326
327         Enumeration JavaDoc enumeration = hpServ.getHomepages (site);
328
329         request.setAttribute ("homepages", enumeration);
330
331         // set the default action
332
request.setAttribute ("subAction", "list");
333         // set the default step
334
request.setAttribute ("go", "change");
335
336         doRedirect (request, response, JSP + "list");
337     }
338
339     //--------------------------------------------------------------------------
340
/**
341      * Forward to error page.
342      *
343      * @param request Servlet request.
344      * @param response Servlet response.
345      * @param msg the warning msg.
346      */

347     private void displayError (HttpServletRequest JavaDoc request,
348                                HttpServletResponse JavaDoc response,
349                                String JavaDoc msg)
350             throws IOException JavaDoc, ServletException JavaDoc {
351         request.setAttribute ("warningMsg", msg);
352         doRedirect (request, response, JSP + "error");
353     }
354
355
356     /***************************************************************************
357      * ADD SECTION
358      *
359      */

360
361     //--------------------------------------------------------------------------
362
/**
363      * Display the add new homepage form.
364      *
365      * @param request Servlet request.
366      * @param response Servlet response.
367      * @param session Servlet session for the current user.
368      */

369     private void displayAdd (HttpServletRequest JavaDoc request,
370                              HttpServletResponse JavaDoc response,
371                              HttpSession JavaDoc session)
372             throws IOException JavaDoc, ServletException JavaDoc, JahiaException {
373         logger.info("Started");
374
375         JahiaHomepage hp = null;
376
377         String JavaDoc go = request.getParameter ("go");
378         if (go == null) {
379             go = "";
380             //reset session
381
session.setAttribute ("homepageBean", null);
382         }
383
384         // set the default action
385
request.setAttribute ("subAction", "add");
386         // set the default step
387
if (go.equals (""))
388             go = "change";
389         request.setAttribute ("go", go);
390
391         hp = (JahiaHomepage)session.getAttribute ("homepageBean");
392         if (hp == null) {
393             hp = hpServ.createHomepage ("",
394                     "",
395                     site.getSiteKey (),
396                     HomepageTypes.HOMEPAGE_LINK,
397                     new Hashtable JavaDoc (),
398                     0);
399             session.setAttribute ("homepageBean", hp);
400
401         } else {
402
403             // default values
404
String JavaDoc name = "";
405             int type = HomepageTypes.HOMEPAGE_LINK;
406             String JavaDoc descr = "";
407
408
409             // retrieve submitted Data
410
String JavaDoc value = request.getParameter ("name");
411             if (value != null)
412                 name = value;
413
414             value = request.getParameter ("type");
415             if (value != null)
416                 type = Integer.parseInt (value);
417
418             value = request.getParameter ("descr");
419             if (value != null)
420                 descr = value;
421
422             if (hp.getType () != type) {
423                 // type has changed ! Recreate a new Bean with correct type
424
hp = hpServ.createHomepage (name,
425                         descr,
426                         site.getSiteKey (),
427                         type,
428                         new Hashtable JavaDoc (),
429                         0);
430                 session.setAttribute ("homepageBean", hp);
431
432             }
433
434             // set new values
435
hp.setName (name);
436             hp.setDescr (descr);
437
438             if (go.equals ("save")) {
439
440                 boolean error = false;
441
442                 if (name == null || name.trim ().equals ("")) {
443                     String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.nameRequired.label",
444                             jParams, jParams.getLocale ());
445                     request.setAttribute ("warningMsg", dspMsg);
446                     error = true;
447                 }
448
449                 if (!error) {
450                     // specific homepage save
451
if (hp.getType () == HomepageTypes.HOMEPAGE_LINK) {
452                         error = !saveLink (request, response, session);
453                     } else if (hp.getType () == HomepageTypes.HOMEPAGE_COPY) {
454                         error = !saveCopy (request, response, session);
455                     } else if (hp.getType () == HomepageTypes.HOMEPAGE_NEW) {
456                         error = !saveNew (request, response, session);
457                     }
458                 }
459
460                 if (!error) {
461                     // reset session
462
session.setAttribute ("homepageBean", null);
463                     displayMain (request, response, session);
464                     return;
465                 }
466             }
467         }
468
469         if (hp.getType () == HomepageTypes.HOMEPAGE_LINK) {
470             prepareAddLink (request, response, session);
471         } else if (hp.getType () == HomepageTypes.HOMEPAGE_COPY) {
472             prepareAddCopy (request, response, session);
473         } else if (hp.getType () == HomepageTypes.HOMEPAGE_NEW) {
474             prepareAddNew (request, response, session);
475         }
476         // display the form
477
doRedirect (request, response, JSP + "add");
478
479     }
480
481     //--------------------------------------------------------------------------
482
/**
483      * Prepares information to display specific data for homepage of type link.
484      *
485      * @param request Servlet request.
486      * @param response Servlet response.
487      * @param session Servlet session for the current user.
488      */

489     private void prepareAddLink (HttpServletRequest JavaDoc request,
490                                  HttpServletResponse JavaDoc response,
491                                  HttpSession JavaDoc session)
492             throws IOException JavaDoc, ServletException JavaDoc, JahiaException {
493
494         prepareAdd (request, response, session);
495
496         JahiaHomepageLink hp = (JahiaHomepageLink)session.getAttribute ("homepageBean");
497         if (hp == null) {
498             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.requestedHomepageNotFound.label",
499                     jParams, jParams.getLocale ());
500             displayError (request, response, dspMsg);
501             return;
502         }
503
504         // retrieve form values...
505

506         String JavaDoc value = request.getParameter ("pageid");
507         int pageID = -1;
508         if (value != null) {
509             pageID = Integer.parseInt (value);
510             hp.setPageID (pageID);
511         } else {
512             pageID = SelectPage_Engine.getInstance ().getSelectedPageID (session);
513             if (pageID != -1) {
514                 hp.setPageID (pageID);
515             }
516         }
517         // reset engine map
518
session.setAttribute (ParamBean.SESSION_JAHIA_ENGINEMAP, null);
519         // set the initial source page for the Page Tree Engine
520
request.setAttribute ("linkInitialSourcePage", new Integer JavaDoc (hp.getPageID ()));
521
522         if (operation.equals ("add")) {
523             request.setAttribute ("includeSpecificSection", "addlink");
524         } else if (operation.equals ("detail"))
525             request.setAttribute ("includeSpecificSection", "detaillink");
526     }
527
528     //--------------------------------------------------------------------------
529
/**
530      * Prepares information to display specific data for homepage of type copy.
531      *
532      * @param request Servlet request.
533      * @param response Servlet response.
534      * @param session Servlet session for the current user.
535      */

536     private void prepareAddCopy (HttpServletRequest JavaDoc request,
537                                  HttpServletResponse JavaDoc response,
538                                  HttpSession JavaDoc session)
539             throws IOException JavaDoc, ServletException JavaDoc, JahiaException {
540
541         prepareAdd (request, response, session);
542
543         JahiaHomepageCopy hp = (JahiaHomepageCopy)session.getAttribute ("homepageBean");
544         if (hp == null) {
545             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.requestedHomepageNotFound.label",
546                     jParams, jParams.getLocale ());
547             displayError (request, response, dspMsg);
548             return;
549         }
550
551         // retrieve form values...
552

553         String JavaDoc value = request.getParameter ("pageid");
554         int pageID = -1;
555         int destPageID = -1;
556         int depth = 1;
557         int depthType = 2;
558
559         if (value != null) {
560             pageID = Integer.parseInt (value);
561             hp.setPageID (pageID);
562         }
563
564         value = request.getParameter ("destpageid");
565         if (value != null) {
566             destPageID = Integer.parseInt (value);
567             hp.setParentPageID (destPageID);
568         }
569
570         value = request.getParameter ("depth");
571         if (value != null) {
572             try {
573                 depth = Integer.parseInt (value);
574             } catch (Throwable JavaDoc t) {
575             }
576             if (depth >= 0)
577                 hp.setCopyDepth (depth);
578
579         }
580
581         value = request.getParameter ("depthtype");
582         if (value != null) {
583             depthType = Integer.parseInt (value);
584         }
585
586
587         request.setAttribute ("depthType", new Integer JavaDoc (depthType));
588         if (operation.equals ("add")) {
589             request.setAttribute ("includeSpecificSection", "addcopy");
590         } else if (operation.equals ("detail"))
591             request.setAttribute ("includeSpecificSection", "detailcopy");
592
593     }
594
595     //--------------------------------------------------------------------------
596
/**
597      * Prepares information to display specific data for homepage of type new.
598      *
599      * @param request Servlet request.
600      * @param response Servlet response.
601      * @param session Servlet session for the current user.
602      */

603     private void prepareAddNew (HttpServletRequest JavaDoc request,
604                                 HttpServletResponse JavaDoc response,
605                                 HttpSession JavaDoc session)
606             throws IOException JavaDoc, ServletException JavaDoc, JahiaException {
607
608         prepareAdd (request, response, session);
609
610         JahiaHomepageNew hp = (JahiaHomepageNew)session.getAttribute ("homepageBean");
611         if (hp == null) {
612             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.requestedHomepageNotFound.label",
613                     jParams, jParams.getLocale ());
614             displayError (request, response, dspMsg);
615             return;
616         }
617
618         // get only visible templates
619
Enumeration JavaDoc templates = ServicesRegistry.getInstance ()
620                 .getJahiaPageTemplateService ().getPageTemplates (site.getID (), true);
621
622         JahiaPageBaseService pageService = JahiaPageBaseService.getInstance ();
623
624         // retrieve previous form values...
625

626         String JavaDoc value = request.getParameter ("destpageid");
627         int pageID = -1;
628         if (value != null) {
629             pageID = Integer.parseInt (value);
630             hp.setParentPageID (pageID);
631         }
632
633         value = request.getParameter ("template");
634         int templateID = -1;
635         if (value != null) {
636             templateID = Integer.parseInt (value);
637             hp.setTemplateID (templateID);
638         }
639
640         request.setAttribute ("templates", templates);
641         if (operation.equals ("add")) {
642             request.setAttribute ("includeSpecificSection", "addnew");
643         } else if (operation.equals ("detail"))
644             request.setAttribute ("includeSpecificSection", "detailnew");
645
646
647     }
648
649     //--------------------------------------------------------------------------
650
/**
651      * Prepares information common to all type of home pages.
652      *
653      * @param request Servlet request.
654      * @param response Servlet response.
655      * @param session Servlet session for the current user.
656      */

657     private void prepareAdd (HttpServletRequest JavaDoc request,
658                              HttpServletResponse JavaDoc response,
659                              HttpSession JavaDoc session)
660             throws IOException JavaDoc, ServletException JavaDoc, JahiaException {
661
662         // get all pages id...
663
JahiaPageBaseService pageService = JahiaPageBaseService.getInstance ();
664         Vector JavaDoc allPagesIDVector = pageService.getPageIDsInSite (site.getID (),
665                 org.jahia.services.pages.PageInfoInterface.TYPE_DIRECT);
666
667         // get all pages infos...
668
Vector JavaDoc pages = new Vector JavaDoc ();
669         Enumeration JavaDoc allPagesIDEnumeration = allPagesIDVector.elements ();
670         while (allPagesIDEnumeration.hasMoreElements ()) {
671             Integer JavaDoc pageID = (Integer JavaDoc)allPagesIDEnumeration.nextElement ();
672             ContentPage contentPage = pageService
673                     .lookupContentPage (pageID.intValue (), jParams.getEntryLoadRequest (), false);
674             pages.add (contentPage);
675         }
676
677         // set all pages infos
678
request.setAttribute ("pages", pages);
679
680     }
681
682     //--------------------------------------------------------------------------
683
/**
684      * Save homepage of type link.
685      *
686      * @param request Servlet request.
687      * @param response Servlet response.
688      * @param session Servlet session for the current user.
689      */

690     private boolean saveLink (HttpServletRequest JavaDoc request,
691                               HttpServletResponse JavaDoc response,
692                               HttpSession JavaDoc session)
693             throws IOException JavaDoc, ServletException JavaDoc, JahiaException {
694
695         JahiaPageBaseService pageService = JahiaPageBaseService.getInstance ();
696
697         // retrieve previous form values...
698
ContentPage selectedPage = null;
699
700         String JavaDoc value = request.getParameter ("pageid");
701         int pageID = -1;
702         if (value != null) {
703             pageID = Integer.parseInt (value);
704
705             try {
706                 if (pageID != -1)
707                     selectedPage = pageService
708                             .lookupContentPage (pageID, jParams.getEntryLoadRequest (), false);
709             } catch (Throwable JavaDoc t) {
710             }
711         }
712
713         if (selectedPage == null) {
714             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.pageNotAvailable.label",
715                     jParams, jParams.getLocale ());
716             request.setAttribute ("warningMsg", dspMsg);
717             return false;
718         }
719
720         JahiaHomepageLink hp = (JahiaHomepageLink)session.getAttribute ("homepageBean");
721         if (hp == null) {
722             throw new JahiaException (this.getClass().getName() + ".saveLink",
723                     "Homepage bean not found in session",
724                     JahiaException.ERROR_SEVERITY,
725                     JahiaException.ERROR_SEVERITY);
726         }
727
728         hp.setPageID (pageID);
729         hpServ.saveHomepage (hp, site.getAclID ());
730
731         return true;
732     }
733
734     //--------------------------------------------------------------------------
735
/**
736      * Save homepage of type copy.
737      *
738      * @param request Servlet request.
739      * @param response Servlet response.
740      * @param session Servlet session for the current user.
741      */

742     private boolean saveCopy (HttpServletRequest JavaDoc request,
743                               HttpServletResponse JavaDoc response,
744                               HttpSession JavaDoc session)
745             throws IOException JavaDoc, ServletException JavaDoc, JahiaException {
746
747         prepareAdd (request, response, session);
748
749         JahiaPageBaseService pageService = JahiaPageBaseService.getInstance ();
750
751         // retrieve previous form values...
752
ContentPage selectedContentPage = null;
753         ContentPage destContentPage = null;
754
755         String JavaDoc value = request.getParameter ("pageid");
756         int pageID = -1;
757         int depth = 1;
758         int depthType = 2;
759
760         if (value != null) {
761             pageID = Integer.parseInt (value);
762
763             try {
764                 if (pageID != -1)
765                     selectedContentPage = pageService
766                             .lookupContentPage (pageID, jParams.getEntryLoadRequest (), false);
767             } catch (Throwable JavaDoc t) {
768             }
769         }
770
771         if (selectedContentPage == null) {
772             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.sourcePageNotAvailable.label",
773                     jParams, jParams.getLocale ());
774             request.setAttribute ("warningMsg", dspMsg);
775             return false;
776         }
777
778         value = request.getParameter ("destpageid");
779         if (value != null) {
780             pageID = Integer.parseInt (value);
781
782             try {
783                 if (pageID != -1)
784                     destContentPage = pageService
785                             .lookupContentPage (pageID, jParams.getEntryLoadRequest (), false);
786             } catch (Throwable JavaDoc t) {
787                 t.printStackTrace ();
788             }
789         }
790
791         if (destContentPage == null) {
792             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.destinationPageNotAvailable.label",
793                     jParams, jParams.getLocale ());
794             request.setAttribute ("warningMsg", dspMsg);
795             return false;
796         }
797
798         if (destContentPage.getID () == selectedContentPage.getID ()) {
799             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.destinationSameAsSourcePage.label",
800                     jParams, jParams.getLocale ());
801             request.setAttribute ("warningMsg", dspMsg);
802             return false;
803         }
804
805         value = request.getParameter ("depthtype");
806         if (value != null) {
807             depthType = Integer.parseInt (value);
808         }
809
810         if (depthType == 2) {
811             depth = 0;
812         } else {
813             value = request.getParameter ("depth");
814             if (value != null) {
815                 depth = Integer.parseInt (value);
816             }
817         }
818         if (depth < 0) {
819             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.invalidDepth.label",
820                     jParams, jParams.getLocale ());
821             request.setAttribute ("warningMsg", dspMsg);
822             return false;
823         }
824
825         JahiaHomepageCopy hp = (JahiaHomepageCopy)session.getAttribute ("homepageBean");
826         if (hp == null) {
827             throw new JahiaException (this.getClass().getName() + ".saveCopy",
828                     "Homepage bean not found in session",
829                     JahiaException.ERROR_SEVERITY,
830                     JahiaException.ERROR_SEVERITY);
831         }
832
833         hp.setPageID (selectedContentPage.getID ());
834         hp.setParentPageID (destContentPage.getID ());
835         hp.setCopyDepth (depth);
836         //hpServ.saveHomepage(hp,site.getAclID());
837

838         if (true) { // disallow save
839
// well as it is not implemented yet completely , let's display a warning msg about that
840
String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.pageDefinitionNotImplemented.label",
841                     jParams, jParams.getLocale ());
842             request.setAttribute ("warningMsg", dspMsg);
843             return false;
844         }
845
846         return true;
847     }
848
849     //--------------------------------------------------------------------------
850
/**
851      * Save a homepage of type new.
852      *
853      * @param request Servlet request.
854      * @param response Servlet response.
855      * @param session Servlet session for the current user.
856      */

857     private boolean saveNew (HttpServletRequest JavaDoc request,
858                              HttpServletResponse JavaDoc response,
859                              HttpSession JavaDoc session)
860             throws IOException JavaDoc, ServletException JavaDoc, JahiaException {
861
862         JahiaPageBaseService pageService = JahiaPageBaseService.getInstance ();
863
864         // retrieve previous form values...
865
JahiaPage selectedPage = null;
866
867         String JavaDoc value = request.getParameter ("destpageid");
868         int pageID = -1;
869         if (value != null) {
870             pageID = Integer.parseInt (value);
871
872             try {
873                 if (pageID != -1)
874                     selectedPage = pageService
875                             .lookupPageWhitoutTemplates (pageID);
876             } catch (Throwable JavaDoc t) {
877                 t.printStackTrace ();
878             }
879         }
880
881         if (selectedPage == null) {
882             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.pageNotAvailable.label",
883                     jParams, jParams.getLocale ());
884             request.setAttribute ("warningMsg", dspMsg);
885             return false;
886         }
887
888         value = request.getParameter ("template");
889         int templateID = -1;
890         if (value != null) {
891             templateID = Integer.parseInt (value);
892         }
893
894         JahiaPageDefinition pageDef = null;
895         try {
896             pageDef = ServicesRegistry.getInstance ()
897                     .getJahiaPageTemplateService ().lookupPageTemplate (templateID);
898         } catch (Throwable JavaDoc t) {
899             t.printStackTrace ();
900         }
901
902         if (pageDef == null) {
903             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.choiseAnotherTemplate.label",
904                     jParams, jParams.getLocale ());
905             request.setAttribute ("warningMsg", dspMsg);
906             return false;
907         }
908
909         JahiaHomepageNew hp = (JahiaHomepageNew)session.getAttribute ("homepageBean");
910         if (hp == null) {
911             throw new JahiaException (this.getClass().getName() + ".saveCopy",
912                     "Homepage bean not found in session",
913                     JahiaException.ERROR_SEVERITY,
914                     JahiaException.ERROR_SEVERITY);
915         }
916
917         hp.setParentPageID (pageID);
918         hp.setTemplateID (templateID);
919         //hpServ.saveHomepage(hp,site.getAclID());
920

921         if (true) { // disallow save
922
// well as it is not implemented yet completely , let's display a warning msg about that
923
String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.pageDefinitionNotImplemented.label",
924                     jParams, jParams.getLocale ());
925             request.setAttribute ("warningMsg", dspMsg);
926             return false;
927         }
928
929         return true;
930
931     }
932
933
934     //--------------------------------------------------------------------------
935
/**
936      * Display the form detail of a homepage.
937      *
938      * @param request Servlet request.
939      * @param response Servlet response.
940      * @param session Servlet session for the current user.
941      */

942     private void displayDetail (HttpServletRequest JavaDoc request,
943                                 HttpServletResponse JavaDoc response,
944                                 HttpSession JavaDoc session)
945             throws IOException JavaDoc, ServletException JavaDoc, JahiaException
946     {
947         logger.info ("Started");
948
949         JahiaHomepage hp = null;
950
951         String JavaDoc value = request.getParameter ("hpid");
952         if (value != null) {
953
954             JahiaHomepage oriHp = hpServ.getHomepage (Integer.parseInt (value));
955             if (oriHp == null) {
956                 String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.requestHomepageCannotFound.label",
957                         jParams, jParams.getLocale ());
958                 displayError (request, response, dspMsg + " [" + value + "]");
959                 return;
960             }
961             hp = (JahiaHomepage)oriHp.clone ();
962             session.setAttribute ("homepageBean", hp);
963         }
964
965         hp = (JahiaHomepage)session.getAttribute ("homepageBean");
966         if (hp == null) {
967             String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.requestHomepageCannotFound.label",
968                     jParams, jParams.getLocale ());
969             displayError (request, response, dspMsg + " [" + value + "]");
970             return;
971         }
972
973         String JavaDoc go = request.getParameter ("go");
974         if (go == null)
975             go = "";
976
977         // set the default action
978
request.setAttribute ("subAction", "detail");
979         // set the default step
980
if (go.equals (""))
981             go = "change";
982         request.setAttribute ("go", go);
983
984
985         if (go.equals ("delete")) {
986             String JavaDoc confirm = request.getParameter ("confirmdelete");
987             if (confirm == null) {
988                 request.setAttribute ("includeSpecificSection", "delete");
989                 doRedirect (request, response, JSP + "delete");
990                 return;
991             }
992
993             if (processDelete (hp)) {
994                 displayMain (request, response, session);
995                 return;
996             } else {
997                 String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.deletingHomepageError.label",
998                         jParams, jParams.getLocale ());
999                 displayError (request, response, dspMsg);
1000            }
1001        }
1002
1003        if (go.equals ("save") || go.equals ("change")) {
1004
1005            // retrieve submitted Data
1006
value = request.getParameter ("name");
1007            if (value != null)
1008                hp.setName (value);
1009
1010            value = request.getParameter ("descr");
1011            if (value != null)
1012                hp.setDescr (value);
1013        }
1014
1015        if (go.equals ("save")) {
1016
1017            boolean error = false;
1018
1019            if (hp.getName () == null || hp.getName ().trim ().equals ("")) {
1020                String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.nameRequired.label",
1021                        jParams, jParams.getLocale ());
1022                request.setAttribute ("warningMsg", dspMsg);
1023                error = true;
1024            }
1025
1026            if (!error) {
1027                // specific homepage save
1028
if (hp.getType () == HomepageTypes.HOMEPAGE_LINK) {
1029                    error = !saveLink (request, response, session);
1030                } else if (hp.getType () == HomepageTypes.HOMEPAGE_COPY) {
1031                    error = !saveCopy (request, response, session);
1032                } else if (hp.getType () == HomepageTypes.HOMEPAGE_NEW) {
1033                    error = !saveNew (request, response, session);
1034                }
1035            }
1036
1037            if (!error) {
1038                // reset session
1039
session.setAttribute ("homepageBean", null);
1040                displayMain (request, response, session);
1041                return;
1042            }
1043        }
1044        if (hp.getType () == HomepageTypes.HOMEPAGE_LINK) {
1045            prepareAddLink (request, response, session);
1046        } else if (hp.getType () == HomepageTypes.HOMEPAGE_COPY) {
1047            prepareAddCopy (request, response, session);
1048        } else if (hp.getType () == HomepageTypes.HOMEPAGE_NEW) {
1049            prepareAddNew (request, response, session);
1050        }
1051        // display the form
1052
doRedirect (request, response, JSP + "detail");
1053
1054    }
1055
1056    //--------------------------------------------------------------------------
1057
/**
1058     * Display Homepage options.
1059     *
1060     * @param request Servlet request.
1061     * @param response Servlet response.
1062     * @param session Servlet session for the current user.
1063     */

1064    private void displayOptions (HttpServletRequest JavaDoc request,
1065                                 HttpServletResponse JavaDoc response,
1066                                 HttpSession JavaDoc session)
1067            throws IOException JavaDoc, ServletException JavaDoc, JahiaException {
1068
1069        logger.info ("Started");
1070
1071        JahiaHomepage hp = null;
1072
1073        String JavaDoc value = null;
1074        int userDefaultHomepageID = site.getUserDefaultHomepageDef ();
1075        int groupDefaultHomepageID = site.getGroupDefaultHomepageDef ();
1076
1077        String JavaDoc go = request.getParameter ("go");
1078        if (go == null)
1079            go = "";
1080
1081        // set the default action
1082
request.setAttribute ("subAction", "options");
1083        // set the default step
1084
if (go.equals (""))
1085            go = "change";
1086        request.setAttribute ("go", go);
1087
1088        if (go.equals ("save") || go.equals ("change")) {
1089
1090            // retrieve submitted Data
1091
value = request.getParameter ("userdefaulthomepage");
1092            if (value != null)
1093                userDefaultHomepageID = Integer.parseInt (value);
1094
1095            value = request.getParameter ("groupdefaulthomepage");
1096            if (value != null)
1097                groupDefaultHomepageID = Integer.parseInt (value);
1098
1099        }
1100
1101        if (go.equals ("save")) {
1102
1103            site.setUserDefaultHomepageDef (userDefaultHomepageID);
1104            site.setGroupDefaultHomepageDef (groupDefaultHomepageID);
1105
1106            String JavaDoc dspMsg = JahiaResourceBundle.getAdminResource ("org.jahia.admin.warningMsg.changSaved.label",
1107                    jParams, jParams.getLocale ());
1108            request.setAttribute ("warningMsg", dspMsg);
1109            //doRedirect( request, response, JSP + "options" );
1110
//displayMain( request, response, session );
1111
//return;
1112
}
1113
1114        Enumeration JavaDoc enumeration = hpServ.getHomepages (site);
1115        request.setAttribute ("homepages1", enumeration);
1116        enumeration = hpServ.getHomepages (site);
1117        request.setAttribute ("homepages2", enumeration);
1118
1119        hp = hpServ.getHomepage (userDefaultHomepageID);
1120        if (hp == null)
1121            hp = hpServ.createHomepage ("", "", "", HomepageTypes.HOMEPAGE_LINK, null, -1);
1122
1123        request.setAttribute ("userDefaultHomepage", hp);
1124
1125        hp = hpServ.getHomepage (groupDefaultHomepageID);
1126        if (hp == null)
1127            hp = hpServ.createHomepage ("", "", "", HomepageTypes.HOMEPAGE_LINK, null, -1);
1128
1129        request.setAttribute ("groupDefaultHomepage", hp);
1130
1131        // display the form
1132
doRedirect (request, response, JSP + "options");
1133
1134
1135    }
1136
1137    //--------------------------------------------------------------------------
1138
/**
1139     * Delete the homepage bean stored in session.
1140     * @param hp the homepage bean.
1141     */

1142    private boolean processDelete (JahiaHomepage hp)
1143            throws JahiaException
1144    {
1145        if (hp == null)
1146            return false;
1147
1148        hpServ.deleteHomepage (hp.getID ());
1149        return true;
1150    }
1151
1152}
1153
Popular Tags