1 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 ; 47 import java.util.Enumeration ; 48 import java.util.Hashtable ; 49 import java.util.Vector ; 50 import org.jahia.settings.SettingsBean; 51 52 53 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 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 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 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 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 String jahiaTemplatesRootPath = jtds.getTemplateRootPath (); 154 if (jahiaTemplatesRootPath == null) { 155 return false; 156 } 157 158 StringBuffer buff = new StringBuffer (jahiaTemplatesRootPath); 160 buff.append (File.separator); 161 buff.append (site.getSiteKey ()); 162 163 logger.debug (" start creating the template path " + buff.toString ()); 164 165 File f = new File (buff.toString ()); 167 if (!f.isDirectory ()) { 168 return f.mkdirs (); 169 } else { 170 return true; 171 } 172 173 } 174 175 176 183 public static String getSiteTemplateFolder (JahiaSite site) throws JahiaException { 184 185 if (site == null) { 186 return null; 187 } 188 189 String templateRootPath = ServicesRegistry.getInstance () 190 .getJahiaTemplatesDeployerService () 191 .getTemplateRootPath (); 192 193 String templateContext = ServicesRegistry.getInstance () 194 .getJahiaTemplatesDeployerService () 195 .getTemplatesContext (); 196 197 if (templateRootPath == null || templateContext == null) { 198 return null; 199 } 200 201 202 String path = templateRootPath + File.separator + site.getSiteKey (); 203 204 return path; 205 206 } 207 208 209 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 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 if (tds == null) { 242 return false; 243 } 244 245 StringBuffer buff = null; 246 File f = null; 247 String path = tds.getNewTemplatesPath (); 248 if (path != null) { 249 buff = new StringBuffer (path); 250 buff.append (File.separator); 251 buff.append (site.getSiteKey ()); 252 253 f = new File (buff.toString ()); 254 logger.debug ("delete site new templates folder " + f.getAbsolutePath ()); 255 JahiaTools.deleteFile (f); 256 } 257 258 path = tds.getTemplateRootPath (); 260 261 if (path != null) { 262 buff = new StringBuffer (path); 263 buff.append (File.separator); 264 buff.append (site.getSiteKey ()); 265 266 f = new File (buff.toString ()); 267 logger.debug ("delete site templates folder " + f.getAbsolutePath ()); 268 JahiaTools.deleteFile (f); 269 } 270 271 JahiaPageTemplateService pts = ServicesRegistry.getInstance () 273 .getJahiaPageTemplateService (); 274 275 276 if (pts != null) { 277 Enumeration 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 JahiaFieldDefinitionsDB fDefDB = new JahiaFieldDefinitionsDB (); 284 fDefDB.db_delete_field_sub_definition (template.getID ()); 285 286 pts.deletePageTemplate (template.getID ()); 287 288 } 289 } 290 291 JahiaContainersService cServ = ServicesRegistry.getInstance () 293 .getJahiaContainersService (); 294 295 Vector ids = cServ.getAllContainerDefinitionIDs (); 296 297 for (int i = 0; i < ids.size (); i++) { 298 int theID = ((Integer ) 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 JahiaContainerDefinitionsDB cDefDB = new JahiaContainerDefinitionsDB (); 306 307 JahiaContainerStructuresDB c_struct = new JahiaContainerStructuresDB (); 308 309 Hashtable hash = cDef.getSubDefs (); 310 Enumeration 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 JahiaFieldService fServ = ServicesRegistry.getInstance ().getJahiaFieldService (); 325 326 ids = fServ.getAllFieldDefinitionIDs (); 327 for (int i = 0; i < ids.size (); i++) { 328 int theID = ((Integer ) 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 349 public static boolean deleteApplications (JahiaUser user, JahiaSite site) 350 throws JahiaException { 351 352 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 buff = null; 376 File f = null; 377 String path = JahiaWebAppsDeployerService.getNewWebAppsPath (); 378 if (path != null) { 379 buff = new StringBuffer (path); 380 buff.append (File.separator); 381 buff.append (site.getSiteKey ()); 382 383 f = new File (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 395 if (aps != null) { 396 397 398 String jahiaHomeDiskPath = JahiaWebAppsDeployerService.getJahiaHomeDiskPath (); 399 logger.debug ( 400 "delete site applications folder jahiaHomeDiskPath=" + jahiaHomeDiskPath); 401 402 Vector vec = aps.getApplications (site.getID ()); 404 ApplicationBean app = null; 405 String webAppsRootPath = JahiaWebAppsDeployerService.getWebAppsRootPath (); 406 407 StringBuffer 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 AppsShareService ass = ServicesRegistry.getInstance ().getAppsShareService (); 415 if (ass != null) { 416 ass.removeShares (app); 417 } 418 419 appPath = new StringBuffer (webAppsRootPath); 420 appPath.append (File.separator); 421 422 appPath.append (app.getContext ().substring (1, app.getContext ().length ())); 423 f = new File (appPath.toString ()); 424 if (!f.getAbsolutePath ().equals (jahiaHomeDiskPath)) { 425 logger.debug ("delete site applications folder " + f.getAbsolutePath ()); 427 JahiaTools.deleteFile (f); 428 429 aps.removeApplication (app.getID ()); 431 } 432 } 433 } 434 435 return true; 436 } 437 438 439 448 public static boolean deleteUsers (JahiaUser user, JahiaSite site, Vector 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 UserManagerProviderBean usrProviderBean = null; 472 JahiaUser theUser = null; 473 String userKey = null; 474 Vector sites = null; 475 int size = providers.size (); 476 for (int i = 0; i < size; i++) { 477 usrProviderBean = (UserManagerProviderBean) providers.get (i); 478 Vector 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 ) v.get (j); 484 if (!user.getName ().equals (userKey)) { 485 theUser = usrProviderBean.getInstance ().lookupUser (userKey); 486 if (theUser != null) { 487 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 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 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 544 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 fileRepositoryRootPath = fms.getFileRepositoryRootPath (); 564 Filemanager fmng = fms.getFilemanagerByOwner (site.getID ()); 565 if (fmng != null) { 566 String filemanagerFolder = fmng.getStoragePath (); 567 fmng.deleteFolder (fmng.getRootFolder ()); 568 569 fms.deleteFilemanager (user, site.getID ()); 570 572 StringBuffer buff = new StringBuffer (fileRepositoryRootPath); 574 buff.append (File.separator); 575 buff.append (filemanagerFolder); 576 File f = new File (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 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 path = JahiaWebAppsDeployerService.getNewWebAppsPath (); 606 if (path == null) { 607 return false; 608 } 609 610 StringBuffer buff = new StringBuffer (path); 611 buff.append (File.separator); 612 buff.append (site.getSiteKey ()); 613 614 File f = new File (buff.toString ()); 615 616 if (!f.isDirectory ()) { 617 return f.mkdirs (); 618 } 619 return true; 620 } 621 622 623 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 buff = new StringBuffer (JahiaWebAppsDeployerService.getNewWebAppsPath ()); 641 buff.append (File.separator); 642 buff.append (site.getSiteKey ()); 643 644 File f = new File (buff.toString ()); 645 646 boolean success = true; 647 648 if (!f.isDirectory ()) { 649 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 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 path = tds.getNewTemplatesPath (); 697 if (path == null) { 698 return false; 699 } 700 701 StringBuffer buff = new StringBuffer (path); 702 buff.append (File.separator); 703 buff.append (site.getSiteKey ()); 704 705 File f = new File (buff.toString ()); 706 707 if (!f.isDirectory ()) { 708 return f.mkdirs (); 709 } 710 return true; 711 } 712 713 714 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 buff = new StringBuffer (tds.getNewTemplatesPath ()); 732 buff.append (File.separator); 733 buff.append (site.getSiteKey ()); 734 735 File f = new File (buff.toString ()); 736 737 boolean success = true; 738 739 if (!f.isDirectory ()) { 740 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 |