KickJava   Java API By Example, From Geeks To Geeks.

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


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 11.07.2001
17
//
18
//
19

20 package org.jahia.services.sites;
21
22 import org.jahia.data.applications.ApplicationBean;
23 import org.jahia.data.containers.JahiaContainerDefinition;
24 import org.jahia.data.containers.JahiaContainerSubDefinition;
25 import org.jahia.data.fields.JahiaFieldDefinition;
26 import org.jahia.exceptions.JahiaException;
27 import org.jahia.registries.ServicesRegistry;
28 import org.jahia.services.applications.JahiaApplicationsManagerService;
29 import org.jahia.services.containers.JahiaContainerDefinitionsDB;
30 import org.jahia.services.containers.JahiaContainerStructuresDB;
31 import org.jahia.services.containers.JahiaContainersService;
32 import org.jahia.services.deamons.filewatcher.templateobserver.TemplatesObserver;
33 import org.jahia.services.deamons.filewatcher.webappobserver.WebAppsObserver;
34 import org.jahia.services.fields.JahiaFieldDefinitionsDB;
35 import org.jahia.services.fields.JahiaFieldService;
36 import org.jahia.services.filemanager.Filemanager;
37 import org.jahia.services.filemanager.JahiaFilemanagerService;
38 import org.jahia.services.pages.JahiaPageDefinition;
39 import org.jahia.services.pages.JahiaPageTemplateService;
40 import org.jahia.services.shares.AppsShareService;
41 import org.jahia.services.templates_deployer.JahiaTemplatesDeployerService;
42 import org.jahia.services.usermanager.*;
43 import org.jahia.services.webapps_deployer.JahiaWebAppsDeployerService;
44 import org.jahia.utils.JahiaTools;
45
46 import java.io.File JavaDoc;
47 import java.util.Enumeration JavaDoc;
48 import java.util.Hashtable JavaDoc;
49 import java.util.Vector JavaDoc;
50 import org.jahia.settings.SettingsBean;
51
52
53 /**
54  * Class JahiaSiteTools.<br>
55  *
56  * @author Khue ng
57  * @version 1.0
58  */

59 public final class JahiaSiteTools {
60
61     private static org.apache.log4j.Logger logger =
62             org.apache.log4j.Logger.getLogger (JahiaSiteTools.class);
63
64
65
66     /**
67      * Create a new membership for a user
68      *
69      * @param user the user to add as member
70      * @param site the site
71      */

72     public static boolean addMember (JahiaUser user, JahiaSite site)
73             throws JahiaException {
74
75         if (site == null || user == null) {
76             return false;
77         }
78         JahiaSiteUserManagerService jsums = ServicesRegistry.getInstance ()
79                 .getJahiaSiteUserManagerService ();
80
81         if (jsums == null) {
82             return false;
83         }
84
85         return jsums.addMember (site.getID (), user);
86     }
87
88
89     /**
90      * Add a group membership on this site ( in a group sharing context )
91      *
92      * @param grp the group reference
93      * @param site the site reference
94      */

95     public static boolean addGroup (JahiaGroup grp, JahiaSite site)
96             throws JahiaException {
97
98         if (site == null || grp == null) {
99             return false;
100         }
101
102         JahiaSiteGroupManagerService jsgms = ServicesRegistry.getInstance ()
103                 .getJahiaSiteGroupManagerService ();
104         if (jsgms == null) {
105             return false;
106         }
107
108         return jsgms.addGroup (site.getID (), grp);
109     }
110
111
112     /**
113      * return the admin group of this site
114      *
115      * @param site the site reference
116      */

117     public static JahiaGroup getAdminGroup (JahiaSite site)
118             throws JahiaException {
119
120         if (site == null) {
121             return null;
122         }
123
124         JahiaGroupManagerService jgms = ServicesRegistry.getInstance ()
125                 .getJahiaGroupManagerService ();
126         if (jgms == null) {
127             return null;
128         }
129
130         return jgms.getAdministratorGroup (site.getID ());
131     }
132
133
134     /**
135      * Create the template directory for a gived site
136      *
137      * @param site the site reference
138      */

139     public static boolean createTemplateDir (JahiaSite site) {
140
141         if (site == null) {
142             return false;
143         }
144
145         JahiaTemplatesDeployerService jtds =
146                 ServicesRegistry.getInstance ()
147                 .getJahiaTemplatesDeployerService ();
148         if (jtds == null) {
149             return false;
150         }
151
152         // get the root folder for all templates
153
String JavaDoc jahiaTemplatesRootPath = jtds.getTemplateRootPath ();
154         if (jahiaTemplatesRootPath == null) {
155             return false;
156         }
157
158         // compose the full template path for this site
159
StringBuffer JavaDoc buff = new StringBuffer JavaDoc (jahiaTemplatesRootPath);
160         buff.append (File.separator);
161         buff.append (site.getSiteKey ());
162
163         logger.debug (" start creating the template path " + buff.toString ());
164
165         // create the folder
166
File JavaDoc f = new File JavaDoc (buff.toString ());
167         if (!f.isDirectory ()) {
168             return f.mkdirs ();
169         } else {
170             return true;
171         }
172
173     }
174
175
176     /**
177      * Return the full path to a site's template root folder
178      *
179      * @param site the site reference
180      *
181      * @return String
182      */

183     public static String JavaDoc getSiteTemplateFolder (JahiaSite site) throws JahiaException {
184
185         if (site == null) {
186             return null;
187         }
188
189         String JavaDoc templateRootPath = ServicesRegistry.getInstance ()
190                 .getJahiaTemplatesDeployerService ()
191                 .getTemplateRootPath ();
192
193         String JavaDoc templateContext = ServicesRegistry.getInstance ()
194                 .getJahiaTemplatesDeployerService ()
195                 .getTemplatesContext ();
196
197         if (templateRootPath == null || templateContext == null) {
198             return null;
199         }
200
201
202         String JavaDoc path = templateRootPath + File.separator + site.getSiteKey ();
203
204         return path;
205
206     }
207
208
209     /**
210      * Purge site's templates and all containers and fields definitions
211      *
212      * @param user the user to check right
213      * @param site the site reference
214      *
215      * @return boolean false on error
216      */

217     public static boolean deleteTemplates (JahiaUser user, JahiaSite site)
218             throws JahiaException {
219
220
221         if (site == null || user == null) {
222             return false;
223         }
224
225         /*
226         if ( !checkAdminAccess(user) ){
227             return false;
228         }
229         */

230
231         JahiaGroup adminGrp = getAdminGroup (site);
232         if (adminGrp == null || !adminGrp.isMember (user)) {
233             return false;
234         }
235
236
237         JahiaTemplatesDeployerService tds = ServicesRegistry
238                 .getInstance ()
239                 .getJahiaTemplatesDeployerService ();
240         // purge new templates folder
241
if (tds == null) {
242             return false;
243         }
244
245         StringBuffer JavaDoc buff = null;
246         File JavaDoc f = null;
247         String JavaDoc path = tds.getNewTemplatesPath ();
248         if (path != null) {
249             buff = new StringBuffer JavaDoc (path);
250             buff.append (File.separator);
251             buff.append (site.getSiteKey ());
252
253             f = new File JavaDoc (buff.toString ());
254             logger.debug ("delete site new templates folder " + f.getAbsolutePath ());
255             JahiaTools.deleteFile (f);
256         }
257
258         // purge site's templates folder
259
path = tds.getTemplateRootPath ();
260
261         if (path != null) {
262             buff = new StringBuffer JavaDoc (path);
263             buff.append (File.separator);
264             buff.append (site.getSiteKey ());
265
266             f = new File JavaDoc (buff.toString ());
267             logger.debug ("delete site templates folder " + f.getAbsolutePath ());
268             JahiaTools.deleteFile (f);
269         }
270
271         // purge templates definitions
272
JahiaPageTemplateService pts = ServicesRegistry.getInstance ()
273                 .getJahiaPageTemplateService ();
274
275
276         if (pts != null) {
277             Enumeration JavaDoc enumeration = pts.getPageTemplates (site.getID (), false);
278             while (enumeration.hasMoreElements ()) {
279                 JahiaPageDefinition template = (JahiaPageDefinition) enumeration.nextElement ();
280                 logger.debug ("delete site templates definition " + template.getName ());
281
282                 // purge field sub def for this template
283
JahiaFieldDefinitionsDB fDefDB = new JahiaFieldDefinitionsDB ();
284                 fDefDB.db_delete_field_sub_definition (template.getID ());
285
286                 pts.deletePageTemplate (template.getID ());
287
288             }
289         }
290
291         // purge site's containers definition
292
JahiaContainersService cServ = ServicesRegistry.getInstance ()
293                 .getJahiaContainersService ();
294
295         Vector JavaDoc ids = cServ.getAllContainerDefinitionIDs ();
296
297         for (int i = 0; i < ids.size (); i++) {
298             int theID = ((Integer JavaDoc) ids.elementAt (i)).intValue ();
299             JahiaContainerDefinition cDef = cServ.loadContainerDefinition (theID);
300             if (cDef.getJahiaID () == site.getID ()) {
301
302                 logger.debug ("delete site cotnainer def " + cDef.getID ());
303
304                 // purge container sub def for this template
305
JahiaContainerDefinitionsDB cDefDB = new JahiaContainerDefinitionsDB ();
306
307                 JahiaContainerStructuresDB c_struct = new JahiaContainerStructuresDB ();
308
309                 Hashtable JavaDoc hash = cDef.getSubDefs ();
310                 Enumeration JavaDoc enumeration = hash.elements ();
311                 while (enumeration.hasMoreElements ()) {
312                     JahiaContainerSubDefinition subDef = (JahiaContainerSubDefinition) enumeration.nextElement ();
313                     c_struct.db_delete_container_structure (subDef.getID ());
314                 }
315
316                 cDefDB.db_delete_container_sub_definition (cDef.getID ());
317
318                 cServ.deleteContainerDefinition (cDef.getID ());
319             }
320
321         }
322
323         // purge site's fields definition
324
JahiaFieldService fServ = ServicesRegistry.getInstance ().getJahiaFieldService ();
325
326         ids = fServ.getAllFieldDefinitionIDs ();
327         for (int i = 0; i < ids.size (); i++) {
328             int theID = ((Integer JavaDoc) ids.elementAt (i)).intValue ();
329             JahiaFieldDefinition fDef = fServ.loadFieldDefinition (theID);
330             if (fDef.getJahiaID () == site.getID ()) {
331                 fServ.deleteFieldDefinition (fDef.getID ());
332             }
333
334         }
335
336
337         return true;
338     }
339
340
341     /**
342      * Purge site's applications
343      *
344      * @param user the user to check right
345      * @param site the site reference
346      *
347      * @return <code>false</code> on error
348      */

349     public static boolean deleteApplications (JahiaUser user, JahiaSite site)
350             throws JahiaException {
351
352         /*
353         if ( !checkAdminAccess(user) ){
354             return false;
355         }
356         */

357
358         if (site == null || user == null) {
359             return false;
360         }
361
362         JahiaGroup adminGrp = getAdminGroup (site);
363         if (adminGrp == null || !adminGrp.isMember (user)) {
364             return false;
365         }
366
367
368         JahiaWebAppsDeployerService ads = ServicesRegistry
369                 .getInstance ()
370                 .getJahiaWebAppsDeployerService ();
371         if (ads == null) {
372             return false;
373         }
374
375         StringBuffer JavaDoc buff = null;
376         File JavaDoc f = null;
377         String JavaDoc path = JahiaWebAppsDeployerService.getNewWebAppsPath ();
378         if (path != null) {
379             buff = new StringBuffer JavaDoc (path);
380             buff.append (File.separator);
381             buff.append (site.getSiteKey ());
382
383             f = new File JavaDoc (buff.toString ());
384             logger.debug ("delete site applications folder " + f.getAbsolutePath ());
385             JahiaTools.deleteFile (f);
386         }
387
388
389         JahiaApplicationsManagerService aps = ServicesRegistry
390                 .getInstance ()
391                 .getJahiaApplicationsManagerService ();
392
393         // purge site's webapps
394

395         if (aps != null) {
396
397
398             String JavaDoc jahiaHomeDiskPath = JahiaWebAppsDeployerService.getJahiaHomeDiskPath ();
399             logger.debug (
400                     "delete site applications folder jahiaHomeDiskPath=" + jahiaHomeDiskPath);
401
402             // get webapps definitions
403
Vector JavaDoc vec = aps.getApplications (site.getID ());
404             ApplicationBean app = null;
405             String JavaDoc webAppsRootPath = JahiaWebAppsDeployerService.getWebAppsRootPath ();
406
407             StringBuffer JavaDoc appPath = null;
408             f = null;
409             int size = vec.size ();
410             for (int i = 0; i < size; i++) {
411                 app = (ApplicationBean) vec.get (i);
412
413                 // delete application shares
414
AppsShareService ass = ServicesRegistry.getInstance ().getAppsShareService ();
415                 if (ass != null) {
416                     ass.removeShares (app);
417                 }
418
419                 appPath = new StringBuffer JavaDoc (webAppsRootPath);
420                 appPath.append (File.separator);
421
422                 appPath.append (app.getContext ().substring (1, app.getContext ().length ()));
423                 f = new File JavaDoc (appPath.toString ());
424                 if (!f.getAbsolutePath ().equals (jahiaHomeDiskPath)) {
425                     // delete from disk
426
logger.debug ("delete site applications folder " + f.getAbsolutePath ());
427                     JahiaTools.deleteFile (f);
428
429                     // delete from database
430
aps.removeApplication (app.getID ());
431                 }
432             }
433         }
434
435         return true;
436     }
437
438
439     /**
440      * Purge site's users
441      *
442      * @param user the user to check right ( the admin )
443      * @param site the site for whcih to delete users
444      * @param providers the vector of users providers.
445      *
446      * @return boolean false on error
447      */

448     public static boolean deleteUsers (JahiaUser user, JahiaSite site, Vector JavaDoc providers)
449             throws JahiaException {
450
451         if (site == null || user == null || providers == null || providers.size () == 0) {
452             return false;
453         }
454
455         JahiaGroup adminGrp = getAdminGroup (site);
456         if (adminGrp == null || !adminGrp.isMember (user)) {
457             return false;
458         }
459
460         JahiaUserManagerService ums = ServicesRegistry.getInstance ()
461                 .getJahiaUserManagerService ();
462
463         JahiaGroupManagerService gms = ServicesRegistry.getInstance ()
464                 .getJahiaGroupManagerService ();
465
466         if (ums == null || gms == null) {
467             return false;
468         }
469
470         // list of user providers
471
UserManagerProviderBean usrProviderBean = null;
472         JahiaUser theUser = null;
473         String JavaDoc userKey = null;
474         Vector JavaDoc sites = null;
475         int size = providers.size ();
476         for (int i = 0; i < size; i++) {
477             usrProviderBean = (UserManagerProviderBean) providers.get (i);
478             // delete users
479
Vector JavaDoc v = usrProviderBean.getInstance ().getUserList (site.getID ());
480             int nbUsers = v.size ();
481             JahiaSite siteItem = null;
482             for (int j = 0; j < nbUsers; j++) {
483                 userKey = (String JavaDoc) v.get (j);
484                 if (!user.getName ().equals (userKey)) {
485                     theUser = usrProviderBean.getInstance ().lookupUser (userKey);
486                     if (theUser != null) {
487                         // check if the user is admin on more than one site
488
sites = gms.getAdminGrantedSites (theUser);
489                         if (sites.size () < 1) {
490                             logger.debug ("site.deleteUsers delete user = " + userKey);
491                             usrProviderBean.getInstance ().deleteUser (theUser);
492                             logger.debug ("site.deleteUsers user " + userKey + " deleted ");
493                         } else if (sites.size () == 1) {
494                             siteItem = (JahiaSite) sites.get (i);
495                             if (siteItem.getID () == site.getID ()) {
496                                 logger.debug ("site.deleteUsers delete user = " + userKey);
497                                 usrProviderBean.getInstance ().deleteUser (theUser);
498                                 logger.debug ("site.deleteUsers user delete ");
499                             }
500                         }
501                     }
502                 }
503             }
504
505             // delete grps
506
v = GroupsTools.getGroups (site.getID (), false);
507             int nbGroups = v.size ();
508             JahiaGroup grp = null;
509             for (int j = 0; j < nbGroups; j++) {
510                 grp = (JahiaGroup) v.get (i);
511                 logger.debug ("site.deleteUsers delete group = " + grp);
512                 gms.deleteGroup (grp);
513             }
514         }
515
516         ServicesRegistry.getInstance ().getJahiaSiteUserManagerService ()
517                 .removeMembers (site.getID ());
518
519         ServicesRegistry.getInstance ().getJahiaSiteGroupManagerService ()
520                 .removeGroups (site.getID ());
521         return true;
522     }
523
524
525     /**
526      * Purge site's filemanager
527      *
528      * @param site the site reference
529      * @param user the user to check right
530      *
531      * @return boolean false on error
532      */

533
534     public static boolean deleteFileRepository (JahiaUser user, JahiaSite site)
535             throws JahiaException {
536
537
538         if (site == null || user == null) {
539             return false;
540         }
541
542         //logger.debug("delete file repository of site " + site.getServerName() );
543

544         /*
545         if ( !checkAdminAccess(user) ){
546             return false;
547         }
548         */

549
550         if (!user.isAdminMember (0)) {
551             logger.debug ("Hey user is not admin of this site !!!");
552             return false;
553         }
554
555         JahiaFilemanagerService fms = ServicesRegistry.getInstance ()
556                 .getJahiaFilemanagerService ();
557
558         if (fms == null) {
559             logger.debug ("Hey file manager service is null !?! ");
560             return false;
561         }
562
563         String JavaDoc fileRepositoryRootPath = fms.getFileRepositoryRootPath ();
564         Filemanager fmng = fms.getFilemanagerByOwner (site.getID ());
565         if (fmng != null) {
566             String JavaDoc filemanagerFolder = fmng.getStoragePath ();
567             fmng.deleteFolder (fmng.getRootFolder ());
568
569             fms.deleteFilemanager (user, site.getID ());
570             //FilemanagerDB.getInstance().delete(fmng.getFilemanagerID());
571

572             // delete the file repository
573
StringBuffer JavaDoc buff = new StringBuffer JavaDoc (fileRepositoryRootPath);
574             buff.append (File.separator);
575             buff.append (filemanagerFolder);
576             File JavaDoc f = new File JavaDoc (buff.toString ());
577             logger.debug ("delete file repository " + f.getAbsolutePath ());
578             JahiaTools.deleteFile (f);
579
580         }
581
582         logger.debug ("Hey file manager is null !?! ");
583
584         return true;
585     }
586
587
588
589     /**
590      * Create a web apps repository used to store new webapps for a site.
591      *
592      * @return boolean false on error
593      *
594      * @param site for which to create the webapps folder
595      */

596     private static boolean createSiteNewWebAppsFolder (JahiaSite site) {
597
598         JahiaWebAppsDeployerService wads = ServicesRegistry
599                 .getInstance ()
600                 .getJahiaWebAppsDeployerService ();
601         if (wads == null || site == null) {
602             return false;
603         }
604
605         String JavaDoc path = JahiaWebAppsDeployerService.getNewWebAppsPath ();
606         if (path == null) {
607             return false;
608         }
609
610         StringBuffer JavaDoc buff = new StringBuffer JavaDoc (path);
611         buff.append (File.separator);
612         buff.append (site.getSiteKey ());
613
614         File JavaDoc f = new File JavaDoc (buff.toString ());
615
616         if (!f.isDirectory ()) {
617             return f.mkdirs ();
618         }
619         return true;
620     }
621
622
623     /**
624      * Start a file watcher to watch a site's new web apps folder
625      *
626      * @return boolean false on error
627      *
628      * @param site for which to watch new webapps
629      */

630     public static boolean startWebAppsObserver (JahiaSite site, SettingsBean settingsBean) {
631
632         JahiaWebAppsDeployerService wads = ServicesRegistry
633                 .getInstance ()
634                 .getJahiaWebAppsDeployerService ();
635
636         if (wads == null || site == null) {
637             return false;
638         }
639
640         StringBuffer JavaDoc buff = new StringBuffer JavaDoc (JahiaWebAppsDeployerService.getNewWebAppsPath ());
641         buff.append (File.separator);
642         buff.append (site.getSiteKey ());
643
644         File JavaDoc f = new File JavaDoc (buff.toString ());
645
646         boolean success = true;
647
648         if (!f.isDirectory ()) {
649             // try to create it
650
success = createSiteNewWebAppsFolder (site);
651         }
652
653         if (success) {
654             long interval = settingsBean.getWebAppsObserverInterval();
655             if (interval == -1) {
656                 return false;
657             }
658
659             boolean fileOnly = true;
660             boolean checkDate = false;
661
662             try {
663                 WebAppsObserver fobs = new WebAppsObserver (site.getID (),
664                         JahiaWebAppsDeployerService.getNewWebAppsPath ()
665                         + File.separator + site.getSiteKey (),
666                         checkDate,
667                         interval,
668                         fileOnly);
669                 return true;
670
671             } catch (JahiaException je) {
672                 logger.error ("exception with FilesObserver" + site.getSiteKey (), je);
673             }
674         }
675         return false;
676     }
677
678
679
680     /**
681      * Create a templates repository used to store new templates for a site.
682      *
683      * @return boolean false on error
684      *
685      * @param site for which to create the templates folder
686      */

687     private static boolean createSiteNewTemplatesFolder (JahiaSite site) {
688
689         JahiaTemplatesDeployerService tds = ServicesRegistry
690                 .getInstance ()
691                 .getJahiaTemplatesDeployerService ();
692         if (tds == null || site == null) {
693             return false;
694         }
695
696         String JavaDoc path = tds.getNewTemplatesPath ();
697         if (path == null) {
698             return false;
699         }
700
701         StringBuffer JavaDoc buff = new StringBuffer JavaDoc (path);
702         buff.append (File.separator);
703         buff.append (site.getSiteKey ());
704
705         File JavaDoc f = new File JavaDoc (buff.toString ());
706
707         if (!f.isDirectory ()) {
708             return f.mkdirs ();
709         }
710         return true;
711     }
712
713
714     /**
715      * Start a file watcher to watch a site's new templates folder
716      *
717      * @return boolean false on error
718      *
719      * @param site for which to create the templates folder
720      */

721     public static boolean startTemplateObserver (JahiaSite site, SettingsBean settingsBean) {
722
723         JahiaTemplatesDeployerService tds = ServicesRegistry
724                 .getInstance ()
725                 .getJahiaTemplatesDeployerService ();
726
727         if (tds == null || site == null) {
728             return false;
729         }
730
731         StringBuffer JavaDoc buff = new StringBuffer JavaDoc (tds.getNewTemplatesPath ());
732         buff.append (File.separator);
733         buff.append (site.getSiteKey ());
734
735         File JavaDoc f = new File JavaDoc (buff.toString ());
736
737         boolean success = true;
738
739         if (!f.isDirectory ()) {
740             // try to create it
741
success = createSiteNewTemplatesFolder (site);
742         }
743
744         if (success) {
745             long interval = settingsBean.getTemplatesObserverInterval();
746             if (interval == -1) {
747                 return false;
748             }
749
750             boolean fileOnly = true;
751             boolean checkDate = false;
752
753             try {
754                 TemplatesObserver fobs = new TemplatesObserver (site.getID (),
755                         tds.getNewTemplatesPath ()
756                         + File.separator + site.getSiteKey (),
757                         checkDate,
758                         interval,
759                         fileOnly);
760                 return true;
761
762             } catch (JahiaException je) {
763                 logger.error ("exception with FilesObserver" + site.getSiteKey (), je);
764             }
765         }
766         return false;
767     }
768
769
770 }
771
Popular Tags