1 13 package org.jahia.layout; 14 15 import java.io.PrintWriter ; 16 import java.io.StringWriter ; 17 import java.util.Enumeration ; 18 import java.util.Hashtable ; 19 import java.util.Vector ; 20 21 import org.jahia.data.JahiaData; 22 import org.jahia.data.containers.JahiaContainer; 23 import org.jahia.data.containers.JahiaContainerDefinition; 24 import org.jahia.exceptions.JahiaException; 25 import org.jahia.params.ParamBean; 26 import org.jahia.registries.ServicesRegistry; 27 import org.jahia.services.acl.JahiaACLEntry; 28 import org.jahia.services.acl.JahiaBaseACL; 29 import org.jahia.services.usermanager.JahiaGroup; 30 import org.jahia.services.usermanager.JahiaGroupManagerService; 31 import org.jahia.services.usermanager.JahiaUser; 32 import org.jahia.utils.JahiaConsole; 33 34 46 public class PortletsPersistanceManager { 47 48 private static PortletsPersistanceManager sharingManager = null; 49 private static PortletsPersistanceManager personalizationManager = null; 50 private static String theDefaultUser = "guest"; 51 private DesktopBean theDesktop; 52 private PortletBean thePortlet; 53 private SkinBean theSkin; 54 private Enumeration thePortletList; 55 private int theColumnCount; 56 57 private boolean personalizationActivated = false; 63 69 private PortletsPersistanceManager(boolean personalizationActivated) 70 { 71 this.personalizationActivated = personalizationActivated; 72 JahiaConsole.println( "PortletsPersistanceManager", "***** Starting the Portlets Persistance Manager *****" ); 73 } 74 75 85 public static synchronized PortletsPersistanceManager getInstance(boolean personalizationActivatedMode) 86 { 87 if (personalizationActivatedMode) { 88 if (personalizationManager == null) { 89 personalizationManager = 90 new PortletsPersistanceManager(personalizationActivatedMode); 91 } 92 return personalizationManager; 93 } else { 94 if (sharingManager == null) { 95 sharingManager = new PortletsPersistanceManager(personalizationActivatedMode); 96 } 97 return sharingManager; 98 } 99 } 101 117 public Enumeration getPortlets(ParamBean jParams, 118 JahiaData jData, 119 int pageID, 120 String portletGroupName, 121 String theUserName, 122 String uRLtoTemplatesDir) { 123 124 Enumeration personalPortletList = null; 125 Enumeration sharedPortletList = null; 126 127 try { 128 String theFile = theUserName + ".xml"; 129 String theDefaultFile = theDefaultUser + ".xml"; 130 131 if (jData == null) { 132 jData = new JahiaData(jParams); 133 } 134 PortletsJahiaContentSerializer contentPortletSerializer = 135 new PortletsJahiaContentSerializer(jData, portletGroupName); 136 sharedPortletList = contentPortletSerializer.getPortlets(theUserName); 137 138 if (personalizationActivated) { 139 140 PortletsXMLSerializer XMLFile = new PortletsXMLSerializer(uRLtoTemplatesDir, 141 theFile, 142 theDefaultFile, 143 pageID, 144 portletGroupName); 145 personalPortletList = XMLFile.getPortlets(); 146 } 147 } catch (JahiaException e) { 148 StringWriter strWriter = new StringWriter (); 149 PrintWriter ptrWriter = new PrintWriter (strWriter); 150 e.printStackTrace(ptrWriter); 151 JahiaConsole.println( "PortletsPersistanceManager.getPortlets", strWriter.toString() ); 152 } 153 154 try { 155 thePortletList = mergePortletLists(sharedPortletList, personalPortletList); 156 } catch (Throwable t) { 157 StringWriter strWriter = new StringWriter (); 158 PrintWriter ptrWriter = new PrintWriter (strWriter); 159 t.printStackTrace(ptrWriter); 160 JahiaConsole.println("PortletsPersistanceManager", 161 "Exception generated -> " + strWriter.toString()); 162 } 163 return thePortletList; 164 } 166 167 182 public Enumeration getPortletsFromColumn(ParamBean jParams, 183 JahiaData jData, 184 int theColumn, 185 String portletGroupName, 186 String theUserName, 187 String uRLtoTemplatesDir) { 188 try { 189 if (jData == null) { 190 jData = new JahiaData(jParams); 191 } 192 PortletsJahiaContentSerializer jPortletSerializer= 193 new PortletsJahiaContentSerializer(jData, portletGroupName); 194 195 196 197 if (personalizationActivated) { 198 String theFile = theUserName + ".xml"; 199 String theDefaultFile = theDefaultUser + ".xml"; 200 PortletsXMLSerializer XMLFile = new PortletsXMLSerializer(uRLtoTemplatesDir, 201 theFile, 202 theDefaultFile, 203 jParams.getPageID(), 204 portletGroupName); 205 thePortletList = XMLFile.getPortletsFromColumn(theColumn); 206 } 207 } catch (JahiaException e) { 208 StringWriter strWriter = new StringWriter (); 209 PrintWriter ptrWriter = new PrintWriter (strWriter); 210 e.printStackTrace(ptrWriter); 211 JahiaConsole.println( "PortletsPersistanceManager.getPortletsFromColumn", strWriter.toString() ); 212 } 213 return thePortletList; 214 } 216 217 220 public PortletBean getPortlet(ParamBean jParams, 221 int thePortletID, 222 int thePageID, 223 String theUserName, 224 String uRLtoTemplatesDir) { 225 try { 226 227 if (personalizationActivated) { 228 String theFile = theUserName + ".xml"; 229 String theDefaultFile = theDefaultUser + ".xml"; 230 PortletsXMLSerializer XMLFile = new PortletsXMLSerializer(uRLtoTemplatesDir, 231 theFile, 232 theDefaultFile, 233 thePageID); 234 thePortlet = XMLFile.getPortlet(thePortletID); 235 } 236 } catch (JahiaException e) { 237 StringWriter strWriter = new StringWriter (); 238 PrintWriter ptrWriter = new PrintWriter (strWriter); 239 e.printStackTrace(ptrWriter); 240 JahiaConsole.println( "PortletsPersistanceManager.getPortlet", strWriter.toString() ); 241 } 242 return thePortlet; 243 } 245 246 247 253 public void addPortlet(ParamBean jParams, 254 JahiaData jData, 255 JahiaContainer container, 256 int theJahiaEventPageID, String theJahiaEventUserName, 257 String uRLtoTemplatesDir) 258 throws JahiaException { 259 263 if (personalizationActivated) { 264 266 String theFile = theJahiaEventUserName + ".xml"; 267 String theDefaultFile = theDefaultUser + ".xml"; 268 PortletsXMLSerializer XMLFile = new PortletsXMLSerializer(uRLtoTemplatesDir, 269 theFile, 270 theDefaultFile, 271 theJahiaEventPageID); 272 273 XMLFile.storePortlet(new PortletBean(container.getID()), theJahiaEventUserName); 274 } else { 275 if (jData == null) { 276 jData = new JahiaData(jParams); 277 } 278 JahiaContainerDefinition containerDef = container.getDefinition(); 279 String portletGroupName = containerDef.getName(); 280 PortletsJahiaContentSerializer jPortletSerializer = 281 new PortletsJahiaContentSerializer(jData, portletGroupName); 282 283 jPortletSerializer.storePortlet(new PortletBean(container.getID()), 284 theJahiaEventUserName); 285 } 286 } 288 289 295 public void setPortlet(ParamBean jParams, 296 JahiaData jData, 297 PortletBean thePortlet, String portletGroupName, int thePageID, 298 String theUserName, String uRLtoTemplatesDir) throws JahiaException { 299 if (personalizationActivated) { 300 String theFile = theUserName + ".xml"; 301 String theDefaultFile = theDefaultUser + ".xml"; 302 PortletsXMLSerializer XMLFile = new PortletsXMLSerializer(uRLtoTemplatesDir, 303 theFile, 304 theDefaultFile, 305 thePageID); 306 XMLFile.storePortlet(thePortlet, theUserName); 307 } else { 308 if (jData == null) { 309 jData = new JahiaData(jParams); 310 } 311 PortletsJahiaContentSerializer jPortletSerializer = 312 new PortletsJahiaContentSerializer(jData, portletGroupName); 313 jPortletSerializer.storePortlet(thePortlet, theUserName); 314 } 315 316 } 318 319 323 public void delPortlet(ParamBean jParams, 324 JahiaData jData, 325 JahiaContainer container, 326 int theJahiaEventPageID, String theJahiaEventUserName, 327 String uRLtoTemplatesDir) throws JahiaException { 328 if (personalizationActivated) { 329 String theFile = theJahiaEventUserName + ".xml"; 330 String theDefaultFile = theDefaultUser + ".xml"; 331 PortletsXMLSerializer XMLFile = new PortletsXMLSerializer(uRLtoTemplatesDir, 332 theFile, 333 theDefaultFile, 334 theJahiaEventPageID); 335 XMLFile.deletePortlet(container.getID()); 336 } else { 337 if (jData == null) { 338 jData = new JahiaData(jParams); 339 } 340 JahiaContainerDefinition containerDef = container.getDefinition(); 341 String portletGroupName = containerDef.getName(); 342 PortletsJahiaContentSerializer jPortletSerializer = 343 new PortletsJahiaContentSerializer(jData, portletGroupName); 344 jPortletSerializer.removePortlet(new PortletBean(container.getID()), 345 theJahiaEventUserName); 346 } 347 } 349 350 356 public void addPortletGroup(ParamBean jParams, 357 int theJahiaEventObjectID, 358 String theJahiaEventUserName, 359 String uRLtoTemplatesDir) throws JahiaException { 360 if (personalizationActivated) { 361 String theFile = theJahiaEventUserName + ".xml"; 362 String theDefaultFile = theDefaultUser + ".xml"; 363 PortletsXMLSerializer XMLFile = new PortletsXMLSerializer(uRLtoTemplatesDir, 364 theFile, 365 theDefaultFile, 366 theJahiaEventObjectID); 367 XMLFile.addPortletGroup(theJahiaEventObjectID); 368 } 369 } 371 372 378 391 397 421 422 428 public SkinBean loadSkin(String theType, int theID) { 429 try { 430 PortletWidgetsDBSerializer DB = PortletWidgetsDBSerializer.getInstance(); 431 if (theType.equals("portlet")) { 432 theSkin = DB.load_portlet_skin(theID); 433 } else if (theType.equals("desktop")) { 434 theSkin = DB.load_desktop_skin(theID); 435 } 436 if (theSkin == null) { 437 theSkin = new SkinBean(1,"",""); 438 } 439 } catch (JahiaException e) { 440 theSkin = new SkinBean(1,"",""); 441 JahiaConsole.println( "PortletsPersistanceManager.loadSkin", e.toString() ); 442 } 443 return theSkin; 444 } 446 447 448 private void unassignRightsToPortlets (ParamBean jParams, Vector containerIDs) { 450 451 String adminGroupName = 453 JahiaGroupManagerService.ADMINISTRATORS_GROUPNAME; 454 455 JahiaGroup adminGroup = ServicesRegistry.getInstance(). 457 getJahiaGroupManagerService (). 458 lookupGroup (JahiaGroupManagerService.ADMINISTRATORS_GROUPNAME); 459 460 JahiaGroup usersGroup = ServicesRegistry.getInstance(). 462 getJahiaGroupManagerService (). 463 lookupGroup (JahiaGroupManagerService.USERS_GROUPNAME); 464 465 JahiaGroup guestGroup = ServicesRegistry.getInstance(). 467 getJahiaGroupManagerService (). 468 lookupGroup (JahiaGroupManagerService.GUEST_GROUPNAME); 469 470 471 if (adminGroup == null) { 474 return; 475 } 476 477 478 for (int index = 0; index < containerIDs.size(); index++) { 480 try { 481 int containerID = ((Integer )containerIDs.get (index)).intValue(); 483 484 JahiaContainer container = ServicesRegistry.getInstance(). 486 getJahiaContainersService().loadContainerInfo (containerID); 487 488 if (container != null) { 489 491 try { 492 JahiaBaseACL acl = new JahiaBaseACL (container.getAclID()); 493 if (acl != null) { 494 495 499 Vector usernames = acl.getUsernameList (null); 501 if (usernames.size() > 0) { 502 for (int i=0; i<usernames.size(); i++) { 503 String username = (String )usernames.get(i); 504 JahiaUser user = ServicesRegistry. 505 getInstance(). 506 getJahiaUserManagerService(). 507 lookupUser (username); 508 if ((user != null) && 509 (!user.isMemberOfGroup (jParams.getSiteID(), adminGroupName))) 510 { 511 acl.removeUserEntry (user); 512 } 513 } 514 } 515 516 JahiaACLEntry entry = new JahiaACLEntry (0, 0); 518 entry.setPermission (JahiaBaseACL.READ_RIGHTS, JahiaACLEntry.ACL_YES); 519 520 acl.setGroupEntry (usersGroup, entry); 521 acl.setGroupEntry (guestGroup, entry); 522 523 JahiaACLEntry adminEntry = new JahiaACLEntry (); 525 for (int i=0; i<acl.size(); i++) { 526 adminEntry.setPermission (i, JahiaACLEntry.ACL_YES); 527 } 528 529 acl.setGroupEntry (adminGroup, adminEntry); 530 } 531 } 532 catch (Throwable t) { 533 } 535 } 536 537 } 538 catch (JahiaException ex) { 539 } 541 } 542 } 543 544 567 private Enumeration mergePortletLists( Enumeration masterList, 568 Enumeration slaveList ) 569 throws JahiaException { 570 571 JahiaConsole.println("PortletsPersistanceManager.mergePortletLists", "Merging portlet lists...."); 572 573 Hashtable resultList = new Hashtable (); 574 if (masterList == null) { 575 return slaveList; 576 } 577 if (slaveList == null) { 578 return masterList; 579 } 580 581 while (masterList.hasMoreElements()) { 583 Object curElement = masterList.nextElement(); 584 if (!(curElement instanceof PortletBean)) { 585 throw new JahiaException("PortletManager.mergePortletLists", 586 "Master portlet list contains invalid objects !", 587 JahiaException.ERROR_SEVERITY, JahiaException.ENGINE_ERROR); 588 } 589 PortletBean pb = (PortletBean) curElement; 590 resultList.put(new Integer (pb.getPortletSourceID()), pb); 591 } 592 593 while (slaveList.hasMoreElements()) { 595 Object curElement = slaveList.nextElement(); 596 if (!(curElement instanceof PortletBean)) { 597 throw new JahiaException("PortletManager.mergePortletLists", 598 "Slave portlet list contains invalid objects !", 599 JahiaException.ERROR_SEVERITY, JahiaException.ENGINE_ERROR); 600 } 601 PortletBean pb = (PortletBean) curElement; 602 if (resultList.containsKey(new Integer (pb.getPortletSourceID()))) { 603 PortletBean masterPB = (PortletBean) resultList.get(new Integer (pb.getPortletSourceID())); 606 610 } else { 611 resultList.put(new Integer (pb.getPortletSourceID()), pb); 613 } 614 } 615 return resultList.elements(); 616 } 617 618 } 619 | Popular Tags |