KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > sites > JahiaSite


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
// JahiaSite
15
//
16
// NK 12.03.2001
17
// AK 28.04.2001 move this class from data/sites to services/sites.
18
// NK 02.05.2001 added purge apps, purge templates, purge users
19
//
20
//
21

22 package org.jahia.services.sites;
23
24 import org.jahia.exceptions.JahiaException;
25 import org.jahia.params.ParamBean;
26 import org.jahia.registries.ServicesRegistry;
27 import org.jahia.services.acl.ACLResourceInterface;
28 import org.jahia.services.acl.JahiaACLException;
29 import org.jahia.services.acl.JahiaBaseACL;
30 import org.jahia.services.pages.ContentPage;
31 import org.jahia.services.pages.JahiaPage;
32 import org.jahia.services.pages.JahiaPageService;
33 import org.jahia.services.usermanager.JahiaUser;
34 import org.jahia.services.version.EntryLoadRequest;
35 import org.jahia.utils.LanguageCodeConverters;
36 import org.jahia.utils.xml.XMLSerializationOptions;
37 import org.jahia.utils.xml.XmlWriter;
38 import org.jahia.bin.Jahia;
39
40 import java.io.IOException JavaDoc;
41 import java.io.Serializable JavaDoc;
42 import java.text.Collator JavaDoc;
43 import java.util.*;
44
45
46 /**
47  * Class JahiaSite.<br>
48  * A site item in Jahia
49  *
50  * @author Khue ng
51  * @version 1.0
52  */

53 public class JahiaSite implements ACLResourceInterface, Serializable JavaDoc {
54
55     private static org.apache.log4j.Logger logger =
56             org.apache.log4j.Logger.getLogger (JahiaSite.class);
57
58
59     /** the site id * */
60     private int mSiteID = -1;
61
62     /** the site display title * */
63     private String JavaDoc mTitle = "";
64
65     /** a unique String identifier key choosed by the creator * */
66     private String JavaDoc mSiteKey = "";
67
68     /** Server Name www.jahia.org * */
69     private String JavaDoc mServerName = "";
70
71     /** is active or not * */
72     private boolean mIsActive = false;
73
74     /** the site's home page * */
75     private int mHomePageID = -1;
76
77     /** desc * */
78     private String JavaDoc mDescr;
79
80     private JahiaBaseACL mACL;
81
82     private Properties mSettings = new Properties ();
83
84     public static final String JavaDoc LANGUAGES_PROP_SEPARATOR = ",";
85
86     private Boolean JavaDoc mixLanguagesActive;
87
88     /**
89      * Constructor, the purpose of this empty constructor is to enable
90      * <jsp:useBean...> tag in JSP
91      */

92     public JahiaSite () {
93     }
94
95     /**
96      * Constructor
97      */

98     public JahiaSite (int id, String JavaDoc title, String JavaDoc serverName, String JavaDoc siteKey,
99                       boolean isActive, int homePageID, String JavaDoc descr,
100                       JahiaBaseACL acl, Properties settings) {
101         mSiteID = id;
102         mTitle = title;
103         mServerName = serverName;
104         mSiteKey = siteKey;
105         mIsActive = isActive;
106         mHomePageID = homePageID;
107
108         if (descr == null) {
109             descr = "no desc";
110         }
111         mDescr = descr;
112
113         mACL = acl;
114
115         if (settings != null) {
116             mSettings = settings;
117         }
118
119     }
120
121
122     public int getID () {
123         return mSiteID;
124     }
125
126     public void setID (int id) {
127         mSiteID = id;
128     }
129
130     public String JavaDoc getTitle () {
131         return mTitle;
132     }
133
134     public void setTitle (String JavaDoc value) {
135         mTitle = value;
136     }
137
138     /**
139      * Return the Full Qualified Domain Name ( www.jahia.org )
140      */

141     public String JavaDoc getServerName () {
142         return mServerName;
143     }
144
145
146     /**
147      * Set the Full Qualified Domain Name ( www.jahia.org )
148      */

149     public void setServerName (String JavaDoc name) {
150         mServerName = name;
151     }
152
153     /**
154      * Return the unique String identifier key ( ex: jahia )
155      */

156     public String JavaDoc getSiteKey () {
157         return mSiteKey;
158     }
159
160
161     /**
162      * Set the unique String identifier key ( ex: jahia )
163      */

164     private void setSiteKey (String JavaDoc siteKey) {
165         mSiteKey = siteKey;
166     }
167
168
169     public boolean isActive () {
170         return mIsActive;
171     }
172
173     public void setActive (boolean value) {
174         mIsActive = value;
175     }
176
177     public int getHomePageID () {
178         return mHomePageID;
179     }
180
181     /**
182      * @return
183      */

184     public JahiaPage getHomePage () {
185         JahiaPage page = null;
186         JahiaPageService ps = ServicesRegistry.getInstance ().getJahiaPageService ();
187         if (ps == null) {
188             logger.error("Cannot find home page, null pageService");
189             return null;
190         }
191         try {
192             page = ps.lookupPage (getHomePageID (), Jahia.getThreadParamBean().getEntryLoadRequest(), Jahia.getThreadParamBean().getUser(), true);
193         } catch (JahiaException je) {
194             logger.error("Cannot find home page, "+mHomePageID, je);
195             return null;
196         }
197         return page;
198     }
199
200     /**
201      * @return
202      */

203     public JahiaPage getHomePage (EntryLoadRequest entryLoadRequest) {
204         JahiaPage page = null;
205         JahiaPageService ps = ServicesRegistry.getInstance ().getJahiaPageService ();
206         if (ps == null) {
207             return null;
208         }
209         try {
210             page = ps.lookupPage (getHomePageID (), entryLoadRequest);
211         } catch (JahiaException je) {
212             return null;
213         }
214         return page;
215     }
216
217     public ContentPage getHomeContentPage () {
218         ContentPage contentPage = null;
219         JahiaPageService ps = ServicesRegistry.getInstance ().getJahiaPageService ();
220         if (ps == null) {
221             return null;
222         }
223         try {
224             contentPage = ps.lookupContentPage (getHomePageID (), true);
225         } catch (JahiaException je) {
226             return null;
227         }
228         return contentPage;
229     }
230
231
232     public void setHomePageID (int id) {
233         mHomePageID = id;
234     }
235
236     public int getDefaultTemplateID () {
237
238         if (mSettings == null) {
239             return -1;
240         }
241         String JavaDoc id = mSettings.getProperty ("defaultTemplateID");
242         if (id == null) {
243             return -1;
244         }
245         return Integer.parseInt (id);
246     }
247
248     public int getHtmlCleanup () {
249
250         // we activate HTML cleanup by default if no setting was found.
251

252         if (mSettings == null) {
253             return 1;
254         }
255         String JavaDoc val = mSettings.getProperty (SitesSettings.HTML_CLEANUP_ENABLED);
256         if (val == null) {
257             return 1;
258         }
259         return Integer.parseInt (val);
260     }
261
262     public void setHtmlCleanup (int val) {
263
264         if (mSettings == null) {
265             mSettings = new Properties ();
266         }
267
268         mSettings.setProperty (SitesSettings.HTML_CLEANUP_ENABLED, String.valueOf (val));
269     }
270
271     public int getHtmlMarkupFiltering () {
272
273         // we activate HTML markup filtering by default if no setting was found.
274

275         if (mSettings == null) {
276             return 1;
277         }
278         String JavaDoc val = mSettings.getProperty (SitesSettings.HTML_MARKUP_FILTERING_ENABLED);
279         if (val == null) {
280             return 1;
281         }
282         return Integer.parseInt (val);
283     }
284
285     public void setHtmlMarkupFiltering (int val) {
286
287         if (mSettings == null) {
288             mSettings = new Properties ();
289         }
290
291         mSettings.setProperty (SitesSettings.HTML_MARKUP_FILTERING_ENABLED,
292                 String.valueOf (val));
293     }
294
295     public void setDefaultTemplateID (int id) {
296
297         if (mSettings == null) {
298             mSettings = new Properties ();
299         }
300
301         mSettings.setProperty ("defaultTemplateID", Integer.toString (id));
302     }
303
304     public boolean getTemplatesAutoDeployMode () {
305
306         if (mSettings == null) {
307             return false;
308         }
309         String JavaDoc val = mSettings.getProperty ("templatesAutoDeployMode");
310         if (val == null) {
311             return false;
312         }
313         return (val.equals ("true"));
314     }
315
316     public void setTemplatesAutoDeployMode (boolean mode) {
317
318         if (mSettings == null) {
319             mSettings = new Properties ();
320         }
321
322         if (mode) {
323             mSettings.setProperty ("templatesAutoDeployMode", "true");
324         } else {
325             mSettings.setProperty ("templatesAutoDeployMode", "false");
326         }
327     }
328
329     public boolean getWebAppsAutoDeployMode () {
330
331         if (mSettings == null) {
332             return false;
333         }
334         String JavaDoc val = mSettings.getProperty ("webAppsAutoDeployMode");
335         if (val == null) {
336             return false;
337         }
338         return (val.equals ("true"));
339     }
340
341     public void setWebAppsAutoDeployMode (boolean mode) {
342
343         if (mSettings == null) {
344             mSettings = new Properties ();
345         }
346
347         if (mode) {
348             mSettings.setProperty ("webAppsAutoDeployMode", "true");
349         } else {
350             mSettings.setProperty ("webAppsAutoDeployMode", "false");
351         }
352     }
353
354
355     public String JavaDoc getTemplateFolder () {
356         return mSiteKey;
357     }
358
359
360     public String JavaDoc getDescr () {
361         return mDescr;
362     }
363
364     public void setDescr (String JavaDoc descr) {
365         mDescr = descr;
366     }
367
368
369     //-------------------------------------------------------------------------
370
/*
371     public String getSiteUrl(ParamBean jParam){
372
373         try {
374             return jParam.composeSiteUrl(this);
375         } catch ( JahiaException je ){
376             logger.debug("JahiaSite.getSiteUrl() exception " + je.getMessage(), je);
377         }
378         return "";
379     }
380     */

381
382
383
384     /**
385      * ACL handling based on JahiaPage model
386      */

387
388     private boolean checkAccess (JahiaUser user, int permission) {
389         if (user == null) {
390             return false;
391         }
392
393         // Test the access rights
394
boolean result = false;
395         try {
396             result = mACL.getPermission (user, permission);
397         } catch (JahiaACLException ex) {
398             // if an error occured, just return false;
399
}
400
401         if (!result) {
402             logger.debug ("Permission denied for user [" +
403                     user.getName () + "] to page [" + getID () +
404                     "] for access permission [" + permission + "]");
405         } else {
406             logger.debug ("Permission granted for user [" +
407                     user.getName () + "] to page [" + getID () +
408                     "] for access permission [" + permission + "]");
409         }
410
411         return result;
412     }
413
414
415     /**
416      * Check if the user has administration access on the specified site. Admin
417      * access means having the ability to admin the site ( manage users, groups,..)
418      *
419      * @param user Reference to the user.
420      *
421      * @return Return true if the user has admin right
422      */

423     public final boolean checkAdminAccess (JahiaUser user) {
424         return checkAccess (user, JahiaBaseACL.ADMIN_RIGHTS);
425     }
426
427     /**
428      * Check if the user has read access on the site.
429      *
430      * @param user Reference to the user.
431      *
432      * @return Return true if the user has read access
433      * or false in any other case.
434      */

435     public final boolean checkReadAccess (JahiaUser user) {
436         return checkAccess (user, JahiaBaseACL.READ_RIGHTS);
437     }
438
439     /**
440      * Check if the user has Write access on the site.
441      *
442      * @param user Reference to the user.
443      *
444      * @return Return true if the user has read access
445      * or false in any other case.
446      */

447     public final boolean checkWriteAccess (JahiaUser user) {
448         return checkAccess (user, JahiaBaseACL.WRITE_RIGHTS);
449     }
450
451
452     /**
453      * Return the site's ACL object.
454      *
455      * @return Return the page's ACL.
456      */

457     public final JahiaBaseACL getACL () {
458         return mACL;
459     }
460
461     /**
462      * Return the ACL unique identification number.
463      *
464      * @return Return the ACL ID.
465      */

466     public final int getAclID () {
467         int id = 0;
468         try {
469             id = mACL.getID ();
470         } catch (JahiaACLException ex) {
471             // This exception should not happen ... :)
472
}
473         return id;
474     }
475
476
477
478
479
480     /**
481      * Site's Settings stored in jahia_site_prop table
482      *
483      * Khue : better to store in a XML config file. TODO
484      *
485      */

486
487     /**
488      * set the default homepage definition for users
489      */

490     public boolean setUserDefaultHomepageDef (int id) {
491
492         try {
493             // delete old value first
494
JahiaSitesPersistance.getInstance ().deleteProperty (getID (),
495                     SitesSettings.USER_DEFAULT_HOMEPAGE_DEF);
496
497             JahiaSitesPersistance.getInstance ().addProperty (getID (),
498                     SitesSettings.USER_DEFAULT_HOMEPAGE_DEF,
499                     Integer.toString (id));
500         } catch (Throwable JavaDoc t) {
501             t.printStackTrace ();
502             return false;
503         }
504         return true;
505     }
506
507     /**
508      * returns the default homepage definition for users
509      * -1 : undefined
510      */

511     public int getUserDefaultHomepageDef () {
512
513         try {
514             String JavaDoc value = JahiaSitesPersistance
515                     .getInstance ().getProperty (getID (),
516                             SitesSettings.USER_DEFAULT_HOMEPAGE_DEF);
517             if (value == null || value.trim ().equals ("")) {
518                 return -1;
519             }
520             return Integer.parseInt (value);
521
522         } catch (Throwable JavaDoc t) {
523             t.printStackTrace ();
524         }
525         return -1;
526
527     }
528
529     /**
530      * set the default homepage definition for groups
531      */

532     public boolean setGroupDefaultHomepageDef (int id) {
533
534         try {
535             // delete old value first
536
JahiaSitesPersistance.getInstance ().deleteProperty (getID (),
537                     SitesSettings.GROUP_DEFAULT_HOMEPAGE_DEF);
538
539             JahiaSitesPersistance.getInstance ().addProperty (getID (),
540                     SitesSettings.GROUP_DEFAULT_HOMEPAGE_DEF,
541                     Integer.toString (id));
542
543         } catch (Throwable JavaDoc t) {
544             t.printStackTrace ();
545             return false;
546         }
547         return true;
548
549     }
550
551     /**
552      * returns the default homepage definition for groups
553      */

554     public int getGroupDefaultHomepageDef () {
555
556         try {
557             String JavaDoc value = JahiaSitesPersistance
558                     .getInstance ().getProperty (getID (),
559                             SitesSettings.GROUP_DEFAULT_HOMEPAGE_DEF);
560             if (value == null || value.trim ().equals ("")) {
561                 return -1;
562             }
563             return Integer.parseInt (value);
564         } catch (Throwable JavaDoc t) {
565             t.printStackTrace ();
566         }
567         return -1;
568
569     }
570
571
572     /**
573      * set the default homepage definition for users activation
574      */

575     public boolean setUserDefaultHomepageDefActiveState (boolean active) {
576
577         try {
578             // delete old value first
579
JahiaSitesPersistance.getInstance ().deleteProperty (getID (),
580                     SitesSettings.USER_DEFAULT_HOMEPAGE_DEF_ACTIVE);
581
582             int value = 0; // not active;
583
if (active)
584                 value = 1;
585
586             JahiaSitesPersistance.getInstance ().addProperty (getID (),
587                     SitesSettings.USER_DEFAULT_HOMEPAGE_DEF,
588                     Integer.toString (value));
589         } catch (Throwable JavaDoc t) {
590             t.printStackTrace ();
591         }
592         return true;
593
594     }
595
596     /**
597      * returns the default homepage definition for users activation,
598      * -1: if not defined
599      * 0: not active
600      * 1. active
601      */

602     public int getUserDefaultHomepageDefActiveState () {
603
604         try {
605             String JavaDoc value = JahiaSitesPersistance
606                     .getInstance ().getProperty (getID (),
607                             SitesSettings.USER_DEFAULT_HOMEPAGE_DEF_ACTIVE);
608             if (value == null || value.trim ().equals ("")) {
609                 return -1;
610             }
611
612             return Integer.parseInt (value);
613
614         } catch (Throwable JavaDoc t) {
615             t.printStackTrace ();
616         }
617         return -1;
618
619     }
620
621
622     /**
623      * set the default homepage definition for groups activation
624      */

625     public boolean setGroupDefaultHomepageDefActiveState (boolean active) {
626
627         try {
628             // delete old value first
629
JahiaSitesPersistance.getInstance ().deleteProperty (getID (),
630                     SitesSettings.GROUP_DEFAULT_HOMEPAGE_DEF_ACTIVE);
631
632             int value = 0; // not active;
633
if (active)
634                 value = 1;
635
636             JahiaSitesPersistance.getInstance ().addProperty (getID (),
637                     SitesSettings.GROUP_DEFAULT_HOMEPAGE_DEF,
638                     Integer.toString (value));
639
640         } catch (Throwable JavaDoc t) {
641             t.printStackTrace ();
642             return false;
643         }
644         return true;
645
646     }
647
648     /**
649      * returns the default homepage definition for users activation,
650      * -1: if not defined
651      * 0: not active
652      * 1. active
653      */

654     public int getGroupDefaultHomepageDefActiveState () {
655
656         try {
657             String JavaDoc value = JahiaSitesPersistance
658                     .getInstance ().getProperty (getID (),
659                             SitesSettings.GROUP_DEFAULT_HOMEPAGE_DEF_ACTIVE);
660             if (value == null || value.trim ().equals ("")) {
661                 return -1;
662             }
663
664             return Integer.parseInt (value);
665
666         } catch (Throwable JavaDoc t) {
667             t.printStackTrace ();
668         }
669         return -1;
670
671     }
672
673     /**
674      * set the default homepage definition for users at creation only state
675      */

676     public boolean setUserDefaultHomepageDefAtCreationOnly (boolean active) {
677
678         try {
679             // delete old value first
680
JahiaSitesPersistance.getInstance ().deleteProperty (getID (),
681                     SitesSettings.USER_DEFAULT_HOMEPAGE_DEF_ATCREATION);
682
683             int value = 0; // not active;
684
if (active)
685                 value = 1;
686
687             JahiaSitesPersistance.getInstance ().addProperty (getID (),
688                     SitesSettings.USER_DEFAULT_HOMEPAGE_DEF_ATCREATION,
689                     Integer.toString (value));
690
691         } catch (Throwable JavaDoc t) {
692             t.printStackTrace ();
693             return false;
694         }
695         return true;
696
697     }
698
699     /**
700      * returns the default homepage definition for users at creation only,
701      * -1: if not defined
702      * 0: false
703      * 1. true
704      */

705     public int getUserDefaultHomepageDefAtCreationOnly () {
706
707         try {
708             String JavaDoc value = JahiaSitesPersistance
709                     .getInstance ().getProperty (getID (),
710                             SitesSettings.USER_DEFAULT_HOMEPAGE_DEF_ATCREATION);
711             if (value == null || value.trim ().equals ("")) {
712                 return -1;
713             }
714
715             return Integer.parseInt (value);
716
717         } catch (Throwable JavaDoc t) {
718             t.printStackTrace ();
719         }
720         return -1;
721
722     }
723
724     /**
725      * set the default homepage definition for groups at creation only state
726      */

727     public boolean setGroupDefaultHomepageDefAtCreationOnly (boolean active) {
728
729         try {
730             // delete old value first
731
JahiaSitesPersistance.getInstance ().deleteProperty (getID (),
732                     SitesSettings.GROUP_DEFAULT_HOMEPAGE_DEF_ATCREATION);
733
734             int value = 0; // not active;
735
if (active)
736                 value = 1;
737
738             JahiaSitesPersistance.getInstance ().addProperty (getID (),
739                     SitesSettings.GROUP_DEFAULT_HOMEPAGE_DEF_ATCREATION,
740                     Integer.toString (value));
741
742         } catch (Throwable JavaDoc t) {
743             t.printStackTrace ();
744             return false;
745         }
746         return true;
747
748     }
749
750     /**
751      * returns the default homepage definition for groups activation,
752      * -1: if not defined
753      * 0: false
754      * 1. true
755      */

756     public int getGroupDefaultHomepageDefAtCreationOnly () {
757
758         try {
759             String JavaDoc value = JahiaSitesPersistance
760                     .getInstance ().getProperty (getID (),
761                             SitesSettings.GROUP_DEFAULT_HOMEPAGE_DEF_ATCREATION);
762             if (value == null || value.trim ().equals ("")) {
763                 return -1;
764             }
765
766             return Integer.parseInt (value);
767
768         } catch (Throwable JavaDoc t) {
769             t.printStackTrace ();
770         }
771         return -1;
772
773     }
774
775     /**
776      * Change the versioning status.
777      *
778      * @param status active or deactivate versioning
779      */

780     public boolean setVersioning (boolean status) {
781         synchronized (mSettings) {
782             try {
783                 // delete old value first
784
JahiaSitesPersistance.getInstance ().deleteProperty (getID (),
785                         SitesSettings.VERSIONING_ENABLED);
786                 int value = 0; // not active;
787
if (status)
788                     value = 1;
789                 JahiaSitesPersistance.getInstance ().addProperty (getID (),
790                         SitesSettings.VERSIONING_ENABLED, Integer.toString (value));
791
792                 mSettings.setProperty (SitesSettings.VERSIONING_ENABLED,
793                         Integer.toString (value));
794             } catch (Throwable JavaDoc t) {
795                 t.printStackTrace ();
796                 return false;
797             }
798         }
799         return true;
800     }
801
802     /**
803      * returns the versioning status.
804      */

805     public boolean isVersioningEnabled () {
806
807         synchronized (mSettings) {
808             String JavaDoc prop = mSettings.getProperty (SitesSettings.VERSIONING_ENABLED);
809             try {
810                 if (prop != null) {
811                     return (Integer.parseInt (prop) == 1);
812                 } else {
813                     prop =
814                             JahiaSitesPersistance
815                             .getInstance ().getProperty (getID (),
816                                     SitesSettings.VERSIONING_ENABLED);
817                     if (prop == null || prop.trim ().equals ("")) {
818                         return false;
819                     } else {
820                         mSettings.setProperty (SitesSettings.VERSIONING_ENABLED, prop.trim ());
821                     }
822                     return (Integer.parseInt (prop) == 1);
823                 }
824             } catch (Throwable JavaDoc t) {
825                 t.printStackTrace ();
826             }
827         }
828         return false;
829     }
830
831     /**
832      * Change the staging status.
833      *
834      * @param status active or deactivate staging
835      */

836     public boolean setStaging (boolean status) {
837         synchronized (mSettings) {
838             try {
839                 // delete old value first
840
JahiaSitesPersistance.getInstance ().deleteProperty (getID (),
841                         SitesSettings.STAGING_ENABLED);
842                 int value = 0; // not active;
843
if (status)
844                     value = 1;
845                 JahiaSitesPersistance.getInstance ().addProperty (getID (),
846                         SitesSettings.STAGING_ENABLED, Integer.toString (value));
847
848                 mSettings.setProperty (SitesSettings.STAGING_ENABLED, Integer.toString (value));
849             } catch (Throwable JavaDoc t) {
850                 t.printStackTrace ();
851                 return false;
852             }
853         }
854         return true;
855     }
856
857     /**
858      * returns the staging status.
859      */

860     public boolean isStagingEnabled () {
861
862         synchronized (mSettings) {
863             String JavaDoc prop = mSettings.getProperty (SitesSettings.STAGING_ENABLED);
864             try {
865                 if (prop != null) {
866                     return (Integer.parseInt (prop) == 1);
867                 } else {
868                     prop =
869                             JahiaSitesPersistance
870                             .getInstance ().getProperty (getID (),
871                                     SitesSettings.STAGING_ENABLED);
872                     if (prop == null || prop.trim ().equals ("")) {
873                         return false;
874                     } else {
875                         mSettings.setProperty (SitesSettings.STAGING_ENABLED, prop.trim ());
876                     }
877                     return (Integer.parseInt (prop) == 1);
878                 }
879             } catch (Throwable JavaDoc t) {
880                 t.printStackTrace ();
881             }
882         }
883         return false;
884     }
885
886
887     /**
888      * Returns a vector of site language settings. The order of this vector
889      * corresponds to the ranking of the languages.
890      *
891      * @return a Vector containing SiteLanguageSettings elements.
892      *
893      * @throws JahiaException if an error occured while retrieving the
894      * list of languages
895      */

896     public Vector getLanguageSettings ()
897             throws JahiaException {
898         return getLanguageSettings (false);
899     }
900
901     /**
902      * Returns a vector of site language settings. The order of this vector
903      * corresponds to the ranking of the languages.
904      * If activeOnly is true, return the active language only.
905      *
906      * @return a Vector containing SiteLanguageSettings elements.
907      *
908      * @throws JahiaException if an error occured while retrieving the
909      * list of languages
910      */

911     public Vector getLanguageSettings (boolean activeOnly)
912             throws JahiaException {
913         Vector siteLanguageSettings = new Vector();
914         Vector v = SiteLanguagesPersistance.getInstance ().getSiteLanguages (
915                 getID ());
916         if ( !activeOnly ){
917             if ( v != null ){
918                 return (Vector) v.clone();
919             }
920         } else {
921             if (v != null) {
922                 for (int i = 0; i < v.size (); i++) {
923                     SiteLanguageSettings curSetting = (SiteLanguageSettings)
924                             v.get (i);
925                     if (curSetting.isActivated ()) {
926                         siteLanguageSettings.add(curSetting);
927                     }
928                 }
929             }
930         }
931         return siteLanguageSettings;
932     }
933
934     /**
935      * Returns an ArrayList of site language settings ( as Locale ).
936      * If activeOnly is true, return the active language only.
937      *
938      * @param activeOnly
939      *
940      * @return an ArrayList of SiteLanguageSettings elements.
941      *
942      * @throws JahiaException if an error occured while retrieving the
943      * list of languages
944      */

945     public ArrayList getLanguageSettingsAsLocales (boolean activeOnly)
946             throws JahiaException {
947         Vector siteLanguageSettings = this.getLanguageSettings ();
948         ArrayList localeList = new ArrayList ();
949         if (siteLanguageSettings != null) {
950             for (int i = 0; i < siteLanguageSettings.size (); i++) {
951                 SiteLanguageSettings curSetting = (SiteLanguageSettings)
952                         siteLanguageSettings.get (i);
953                 if (!activeOnly || curSetting.isActivated ()) {
954                     Locale tempLocale =
955                             LanguageCodeConverters.languageCodeToLocale (curSetting.getCode ());
956                     localeList.add (tempLocale);
957                 }
958             }
959         }
960         return localeList;
961     }
962
963
964     /**
965      * Returns a vector of site language mappings.
966      *
967      * @return a Vector containing SiteLanguageMappings elements.
968      *
969      * @throws JahiaException if an error occured while retrieving the
970      * list of languages
971      */

972     public Vector getLanguageMappings ()
973             throws JahiaException {
974         Vector siteLanguageMappings = SiteLanguageMappingPersistance.getInstance ()
975                 .getSiteLanguageMappings (getID ());
976         return siteLanguageMappings;
977     }
978
979     /**
980      * Sets the language settings for this site. This directly interfaces with
981      * the persistant storage to store the modifications if there were any.
982      *
983      * @param siteLanguagesSettings a Vector of SiteLanguageSettings objects.
984      *
985      * @throws JahiaException when an error occured while storing the modified
986      * site language settings values.
987      */

988     public void setLanguageSettings (Vector siteLanguagesSettings)
989             throws JahiaException {
990         for (int i = 0; i < siteLanguagesSettings.size (); i++) {
991             Object JavaDoc curObject = siteLanguagesSettings.get (i);
992             if (curObject instanceof SiteLanguageSettings) {
993                 SiteLanguageSettings curLanguageSettings =
994                         (SiteLanguageSettings) siteLanguagesSettings.get (i);
995                 if (curLanguageSettings.isInPersistantStorage ()) {
996                     if (curLanguageSettings.isDataModified ()) {
997                         SiteLanguagesPersistance.getInstance ().updateSiteLanguageSettings (
998                                 curLanguageSettings);
999                     }
1000                } else {
1001                    SiteLanguagesPersistance.getInstance ().addSiteLanguageSettings (
1002                            curLanguageSettings);
1003                }
1004            } else {
1005                logger.debug ("Invalid object passed in vector : " +
1006                        curObject.toString ());
1007            }
1008        }
1009    }
1010
1011    /**
1012     * Sets the value of the site property that controls
1013     *
1014     * @param mixLanguagesActive
1015     */

1016    public void setMixLanguagesActive (boolean mixLanguagesActive) {
1017        try {
1018            // delete old value first
1019
JahiaSitesPersistance.getInstance ().deleteProperty (getID (),
1020                    SitesSettings.MIX_LANGUAGES_ACTIVE);
1021
1022
1023            JahiaSitesPersistance.getInstance ().addProperty (getID (),
1024                    SitesSettings.MIX_LANGUAGES_ACTIVE,
1025                    new Boolean JavaDoc (mixLanguagesActive).toString ());
1026
1027        } catch (Throwable JavaDoc t) {
1028            t.printStackTrace ();
1029        }
1030        this.mixLanguagesActive = new Boolean JavaDoc (mixLanguagesActive);
1031    }
1032
1033    public boolean isMixLanguagesActive () {
1034
1035        if (this.mixLanguagesActive != null) {
1036            return mixLanguagesActive.booleanValue ();
1037        }
1038
1039        try {
1040            String JavaDoc value = JahiaSitesPersistance
1041                    .getInstance ().getProperty (getID (),
1042                            SitesSettings.MIX_LANGUAGES_ACTIVE);
1043            if (value == null || value.trim ().equals ("")) {
1044                setMixLanguagesActive (false);
1045                return false;
1046            }
1047            Boolean JavaDoc mixLanguagesActive = Boolean.valueOf (value);
1048
1049            this.mixLanguagesActive = mixLanguagesActive;
1050
1051            return mixLanguagesActive.booleanValue ();
1052
1053        } catch (Throwable JavaDoc t) {
1054            t.printStackTrace ();
1055        }
1056        return false;
1057    }
1058
1059    /**
1060     * Writes an XML serialization version of this content page, according to
1061     * the seriliazation options specified. This is very useful for exporting
1062     * Jahia content to external systems.
1063     *
1064     * @param xmlWriter the XML writer object in which to output the XML
1065     * exported data
1066     * @param xmlSerializationOptions the options that activate/deactivate
1067     * parts of the XML exported data.
1068     * @param paramBean specifies context of serialization, such as current
1069     * user, current request parameters, entry load request, URL generation
1070     * information such as ServerName, ServerPort, ContextPath, etc... URL
1071     * generation is an important part of XML serialization and this is why
1072     * we pass this parameter down, as well as user rights checking.
1073     *
1074     * @throws IOException upon error writing to the XMLWriter
1075     * @todo FIXME : only container lists output for the moment. Still to be
1076     * done are page fields.
1077     */

1078    public synchronized void serializeToXML (XmlWriter xmlWriter,
1079                                             XMLSerializationOptions xmlSerializationOptions,
1080                                             ParamBean paramBean) throws IOException JavaDoc {
1081        try {
1082            ContentPage contentPage = ServicesRegistry.getInstance ().getJahiaPageService ()
1083                    .lookupContentPage (getHomePageID (), true);
1084            xmlWriter.writeEntity ("contentSite").
1085                    writeAttribute ("name", getSiteKey ()).
1086                    writeAttribute ("title", getTitle ());
1087            if (contentPage != null) {
1088                contentPage.serializeToXML (xmlWriter, new XMLSerializationOptions (),
1089                        paramBean);
1090            } else {
1091                xmlWriter.writeEntityWithText ("error",
1092                        "Page " + getHomePageID () + " not found");
1093            }
1094            xmlWriter.endEntity ();
1095        } catch (JahiaException je) {
1096            logger.debug ("Error while serializing site " + getID () + " to XML", je);
1097        }
1098    }
1099
1100
1101    public void setSettings (Properties props) {
1102        this.mSettings = props;
1103    }
1104
1105    public Properties getSettings () {
1106        return this.mSettings;
1107    }
1108
1109    public static class TitleComparator implements Comparator {
1110
1111        private Collator JavaDoc collator = Collator.getInstance();
1112
1113        public TitleComparator(Locale locale) {
1114            if (locale != null) {
1115                collator = Collator.getInstance(locale);
1116            }
1117        }
1118
1119        public TitleComparator() {
1120        }
1121
1122        public int compare(Object JavaDoc o1,
1123                   Object JavaDoc o2) {
1124            JahiaSite site1 = (JahiaSite) o1;
1125            JahiaSite site2 = (JahiaSite) o2;
1126            return collator.compare(site1.getTitle(), site2.getTitle());
1127        }
1128
1129        public boolean equals(Object JavaDoc obj) {
1130            if (obj instanceof TitleComparator) {
1131                return true;
1132            } else {
1133                return false;
1134            }
1135        }
1136    }
1137
1138    public static TitleComparator getTitleComparator() {
1139        return new TitleComparator();
1140    }
1141
1142    public static TitleComparator getTitleComparator(Locale locale) {
1143        return new TitleComparator(locale);
1144    }
1145
1146}
1147
Popular Tags