1 package com.dotmarketing.factories; 2 3 import java.lang.reflect.InvocationTargetException ; 4 import java.util.ArrayList ; 5 import java.util.Date ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Map ; 9 10 import org.apache.commons.beanutils.PropertyUtils; 11 12 import com.dotmarketing.beans.Host; 13 import com.dotmarketing.beans.Identifier; 14 import com.dotmarketing.beans.Inode; 15 import com.dotmarketing.beans.PermissionAsset; 16 import com.dotmarketing.beans.WebAsset; 17 import com.dotmarketing.cache.LiveCache; 18 import com.dotmarketing.cache.PermissionCache; 19 import com.dotmarketing.cms.factories.PublicRoleFactory; 20 import com.dotmarketing.db.DotConnect; 21 import com.dotmarketing.db.DotHibernate; 22 import com.dotmarketing.menubuilders.RefreshMenus; 23 import com.dotmarketing.portlets.containers.model.Container; 24 import com.dotmarketing.portlets.contentlet.factories.ContentletFactory; 25 import com.dotmarketing.portlets.contentlet.model.Contentlet; 26 import com.dotmarketing.portlets.folders.model.Folder; 27 import com.dotmarketing.portlets.htmlpages.model.HTMLPage; 28 import com.dotmarketing.portlets.templates.model.Template; 29 import com.dotmarketing.services.ContainerServices; 30 import com.dotmarketing.services.ContentletMapServices; 31 import com.dotmarketing.services.ContentletServices; 32 import com.dotmarketing.services.PageServices; 33 import com.dotmarketing.services.TemplateServices; 34 import com.dotmarketing.util.Config; 35 import com.dotmarketing.util.Logger; 36 import com.dotmarketing.util.WebKeys; 37 import com.liferay.portal.model.Role; 38 import com.liferay.portal.model.User; 39 import com.liferay.portal.struts.ActionException; 40 41 45 public class WebAssetFactory { 46 47 public static void createAsset(WebAsset webasset, String userId, Inode parent) { 48 49 webasset.setWorking(true); 50 webasset.setLive(false); 51 webasset.setDeleted(false); 52 webasset.setLocked(false); 53 webasset.setModDate(new java.util.Date ()); 54 webasset.setModUser(userId); 55 InodeFactory.saveInode(webasset); 57 58 parent.addChild(webasset); 60 61 Identifier id = IdentifierFactory.createNewIdentifier(webasset, (Folder) parent); 63 64 InodeFactory.saveInode(id); 67 68 } 69 70 public static void createAsset(WebAsset webasset, String userId) { 71 72 webasset.setWorking(true); 73 webasset.setLive(false); 74 webasset.setDeleted(false); 75 webasset.setLocked(false); 76 webasset.setModDate(new java.util.Date ()); 77 webasset.setModUser(userId); 78 InodeFactory.saveInode(webasset); 80 81 Identifier id = IdentifierFactory.createNewIdentifier(webasset); 83 84 webasset.setIdentifier(id.getInode()); 87 88 InodeFactory.saveInode(id); 89 InodeFactory.saveInode(webasset); 90 } 91 92 public static void createAsset(WebAsset webasset, String userId, Inode parent, Identifier identifier) { 93 94 webasset.setWorking(true); 95 webasset.setLive(false); 96 webasset.setDeleted(false); 97 webasset.setLocked(false); 98 webasset.setModDate(new java.util.Date ()); 99 webasset.setModUser(userId); 100 101 webasset.setIdentifier(identifier.getInode()); 104 105 106 InodeFactory.saveInode(webasset); 108 109 parent.addChild(webasset); 111 112 identifier.addChild(webasset); 114 115 116 } 117 118 public static void createAsset(WebAsset webasset, String userId, Identifier identifier) { 119 120 webasset.setWorking(true); 121 webasset.setLive(false); 122 webasset.setDeleted(false); 123 webasset.setLocked(false); 124 webasset.setModDate(new java.util.Date ()); 125 webasset.setModUser(userId); 126 127 webasset.setIdentifier(identifier.getInode()); 130 131 132 InodeFactory.saveInode(webasset); 134 135 identifier.addChild(webasset); 137 138 139 } 140 141 public static void createAsset(WebAsset webasset, String userId, Inode parent, Identifier identifier, 142 boolean working) { 143 webasset.setWorking(working); 144 webasset.setLive(false); 145 webasset.setDeleted(false); 146 webasset.setLocked(false); 147 webasset.setModDate(new java.util.Date ()); 148 webasset.setModUser(userId); 149 InodeFactory.saveInode(webasset); 151 152 parent.addChild(webasset); 154 155 identifier.addChild(webasset); 157 webasset.addParent(identifier); 158 webasset.setIdentifier(identifier.getInode()); 159 160 InodeFactory.saveInode(webasset); 161 } 162 163 public static void createAsset(WebAsset webasset, String userId, Identifier identifier, boolean working) { 164 webasset.setWorking(working); 165 webasset.setLive(false); 166 webasset.setDeleted(false); 167 webasset.setLocked(false); 168 webasset.setModDate(new java.util.Date ()); 169 webasset.setModUser(userId); 170 InodeFactory.saveInode(webasset); 172 173 identifier.addChild(webasset); 175 webasset.addParent(identifier); 176 webasset.setIdentifier(identifier.getInode()); 177 178 InodeFactory.saveInode(webasset); 179 } 180 181 public static WebAsset getParentWebAsset(Inode i) { 182 DotHibernate dh = new DotHibernate(WebAsset.class); 183 dh.setQuery("from inode in class " + WebAsset.class.getName() + " where ? in inode.children.elements"); 184 dh.setParam(i.getInode()); 185 return (WebAsset) dh.load(); 186 } 187 188 189 190 public static void renameAsset(WebAsset webasset) { 191 java.util.List versions = getAssetVersionsandLive(webasset); 192 java.util.Iterator versIter = versions.iterator(); 193 while (versIter.hasNext()) { 194 WebAsset currWebAsset = (WebAsset) versIter.next(); 195 currWebAsset.setFriendlyName(webasset.getFriendlyName()); 196 } 197 } 198 199 public static boolean editAsset(WebAsset currWebAsset, String userId) { 200 201 Identifier identifier = IdentifierFactory.getParentIdentifier(currWebAsset); 203 WebAsset workingwebasset = null; 204 205 if (currWebAsset instanceof Contentlet) { 206 String condition = "language_id = " + ((Contentlet) currWebAsset).getLanguageId(); 208 workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClassByCondition(identifier, currWebAsset 209 .getClass(), condition); 210 } else { 211 workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClass(identifier, currWebAsset.getClass()); 213 } 214 215 if (!workingwebasset.isLocked()) { 216 workingwebasset.setLocked(true); 218 workingwebasset.setModUser(userId); 219 InodeFactory.saveInode(workingwebasset); 221 return true; 222 } 223 if ((workingwebasset.isLocked()) && (workingwebasset.getModUser().equals(userId))) { 224 return true; 225 } 226 return false; 227 } 228 229 public static WebAsset getBackAssetVersion(WebAsset versionWebAsset) throws Exception { 230 Identifier id = (Identifier) IdentifierFactory.getParentIdentifier(versionWebAsset); 231 if (id.getInode() == 0) { 232 throw new Exception ("Web asset Identifier not found!"); 233 } 234 WebAsset working = (WebAsset) IdentifierFactory.getWorkingChildOfClass(id, versionWebAsset.getClass()); 235 if (working.getInode() == 0) { 236 throw new Exception ("Working copy not found!"); 237 } 238 return swapAssets(working, versionWebAsset); 239 } 240 241 252 @SuppressWarnings ("unchecked") 253 public static WebAsset publishAsset(WebAsset currWebAsset) { 254 255 Logger.debug(WebAssetFactory.class, "Publishing asset!!!!"); 256 Identifier identifier = IdentifierFactory.getParentIdentifier(currWebAsset); 258 260 WebAsset workingwebasset = null; 261 262 if (currWebAsset instanceof Contentlet) { 263 String condition = "language_id = " + ((Contentlet) currWebAsset).getLanguageId(); 265 workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClassByCondition(identifier, currWebAsset 266 .getClass(), condition); 267 } else { 268 workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClass(identifier, currWebAsset.getClass()); 270 } 271 272 if (workingwebasset.getInode() == 0) { 273 workingwebasset = currWebAsset; 274 } 275 276 Logger.debug(WebAssetFactory.class, "workingwebasset=" + workingwebasset.getInode()); 277 278 List <WebAsset> livewebassets = new ArrayList <WebAsset>(); 279 280 try { 281 if (currWebAsset instanceof Contentlet) { 282 String condition = "language_id = " + ((Contentlet) currWebAsset).getLanguageId(); 284 livewebassets = (List <WebAsset>) IdentifierFactory.getLiveChildrenOfClass(identifier, currWebAsset.getClass(), condition); 285 } else { 286 livewebassets = (List <WebAsset>) IdentifierFactory.getLiveChildrenOfClass(identifier, currWebAsset.getClass()); 288 } 289 } catch (Exception e) { 290 } 291 292 workingwebasset.setLive(true); 294 workingwebasset.setModDate(new java.util.Date ()); 295 InodeFactory.saveInode(workingwebasset); 297 Logger.debug(WebAssetFactory.class, "InodeFactory.saveInode(workingwebasset)"); 298 299 for (WebAsset livewebasset : livewebassets) 300 if ((livewebasset != null) && (livewebasset.getInode() > 0) 301 && (livewebasset.getInode() != workingwebasset.getInode())) { 302 303 Logger.debug(WebAssetFactory.class, "livewebasset.getInode()=" + livewebasset.getInode()); 304 livewebasset.setLive(false); 306 livewebasset.setModDate(new java.util.Date ()); 307 java.util.List parents = InodeFactory.getParentsOfClass(livewebasset, Folder.class); 309 java.util.Iterator parentsIter = parents.iterator(); 310 while (parentsIter.hasNext()) { 311 Inode inode = (Inode) parentsIter.next(); 312 inode.deleteChild(livewebasset); 313 } 314 identifier.addChild(livewebasset); 315 316 InodeFactory.saveInode(livewebasset); 318 } 319 return livewebassets.size()>0?livewebassets.get(0):null; 320 } 321 322 public static WebAsset getLiveAsset(WebAsset currWebAsset) throws Exception { 323 324 Logger.debug(WebAssetFactory.class, "Publishing asset!!!!"); 325 Identifier identifier = IdentifierFactory.getParentIdentifier(currWebAsset); 327 328 WebAsset livewebasset = null; 329 330 if (currWebAsset instanceof Contentlet) { 331 String condition = "language_id = " + ((Contentlet) currWebAsset).getLanguageId(); 333 livewebasset = (WebAsset) IdentifierFactory.getLiveChildOfClassByCondition(identifier, currWebAsset 334 .getClass(), condition); 335 } else { 336 livewebasset = (WebAsset) IdentifierFactory.getLiveChildOfClass(identifier, currWebAsset.getClass()); 338 } 339 340 return livewebasset; 341 } 342 343 public static boolean deleteAsset(WebAsset currWebAsset, String userId) { 344 345 Identifier identifier = IdentifierFactory.getParentIdentifier(currWebAsset); 347 348 WebAsset workingwebasset = null; 349 350 if (currWebAsset instanceof Contentlet) { 351 String condition = "language_id = " + ((Contentlet) currWebAsset).getLanguageId(); 353 workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClassByCondition(identifier, currWebAsset 354 .getClass(), condition); 355 } else { 356 workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClass(identifier, currWebAsset.getClass()); 358 } 359 360 WebAsset live = (WebAsset) IdentifierFactory.getLiveChildOfClass(identifier, currWebAsset.getClass()); 361 362 if ((!workingwebasset.isLocked() || workingwebasset.getModUser().equals(userId))) { 363 364 if (live.getInode() > 0) { 365 live.setLive(false); 366 live.setModDate(new Date ()); 367 InodeFactory.saveInode(live); 368 } 369 370 workingwebasset.setModDate(new Date ()); 372 workingwebasset.setDeleted(true); 374 InodeFactory.saveInode(workingwebasset); 376 377 if (workingwebasset instanceof Contentlet) { 378 ContentletFactory.reIndexContentlet((Contentlet) workingwebasset); 380 } 381 382 return true; 383 } 384 return false; 385 } 386 387 public static boolean deleteAssetVersion(WebAsset currWebAsset) { 388 389 if (!currWebAsset.isLive() && !currWebAsset.isWorking()) { 390 InodeFactory.deleteInode(currWebAsset); 392 return true; 393 } 394 return false; 395 396 } 397 398 public static void unLockAsset(WebAsset currWebAsset) { 399 400 Identifier identifier = IdentifierFactory.getParentIdentifier(currWebAsset); 402 WebAsset workingwebasset = null; 403 404 if (currWebAsset instanceof Contentlet) { 405 String condition = "language_id = " + ((Contentlet) currWebAsset).getLanguageId(); 407 workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClassByCondition(identifier, currWebAsset 408 .getClass(), condition); 409 } else { 410 workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClass(identifier, currWebAsset.getClass()); 412 } 413 workingwebasset.setLocked(false); 415 416 InodeFactory.saveInode(workingwebasset); 418 419 if (currWebAsset instanceof Contentlet) { 420 ContentletFactory.reIndexContentlet((Contentlet) currWebAsset); 422 } 423 424 } 425 426 public static void unDeleteAsset(WebAsset currWebAsset) { 427 428 Identifier identifier = IdentifierFactory.getParentIdentifier(currWebAsset); 430 WebAsset workingwebasset = null; 432 433 if (currWebAsset instanceof Contentlet) { 434 String condition = "language_id = " + ((Contentlet) currWebAsset).getLanguageId(); 436 workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClassByCondition(identifier, currWebAsset 437 .getClass(), condition); 438 } else { 439 workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClass(identifier, currWebAsset.getClass()); 441 } 442 443 WebAsset livewebasset = null; 444 try { 445 if (currWebAsset instanceof Contentlet) { 446 String condition = "language_id = " + ((Contentlet) currWebAsset).getLanguageId(); 448 livewebasset = (WebAsset) IdentifierFactory.getLiveChildOfClassByCondition(identifier, currWebAsset 449 .getClass(), condition); 450 } else { 451 livewebasset = (WebAsset) IdentifierFactory.getLiveChildOfClass(identifier, currWebAsset.getClass()); 453 } 454 } catch (Exception e) { 455 } 456 457 workingwebasset.setDeleted(false); 459 InodeFactory.saveInode(workingwebasset); 461 462 if ((livewebasset != null) && (livewebasset.getInode() > 0) 463 && (livewebasset.getInode() != workingwebasset.getInode())) { 464 465 livewebasset.setDeleted(false); 467 InodeFactory.saveInode(livewebasset); 469 } 470 471 if (workingwebasset instanceof Contentlet) { 472 ContentletFactory.reIndexContentlet((Contentlet) workingwebasset); 474 } 475 476 477 } 478 479 public static boolean unPublishAsset(WebAsset currWebAsset, String userId, Inode parent) { 480 481 Identifier identifier = IdentifierFactory.getParentIdentifier(currWebAsset); 483 484 WebAsset workingwebasset = null; 485 486 if (currWebAsset instanceof Contentlet) { 487 String condition = "language_id = " + ((Contentlet) currWebAsset).getLanguageId(); 489 workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClassByCondition(identifier, currWebAsset 490 .getClass(), condition); 491 } else { 492 workingwebasset = (WebAsset) IdentifierFactory.getWorkingChildOfClass(identifier, currWebAsset.getClass()); 494 } 495 496 WebAsset livewebasset = null; 497 498 if (!workingwebasset.isLocked() || workingwebasset.getModUser().equals(userId)) { 499 try { 500 if (currWebAsset instanceof Contentlet) { 502 String condition = "language_id = " + ((Contentlet) currWebAsset).getLanguageId(); 504 livewebasset = (WebAsset) IdentifierFactory.getLiveChildOfClassByCondition(identifier, currWebAsset 505 .getClass(), condition); 506 } else { 507 livewebasset = (WebAsset) IdentifierFactory 509 .getLiveChildOfClass(identifier, currWebAsset.getClass()); 510 } 511 512 livewebasset.setLive(false); 513 livewebasset.setWorking(true); 514 livewebasset.setModDate(new java.util.Date ()); 515 livewebasset.setModUser(userId); 516 InodeFactory.saveInode(livewebasset); 517 518 if ((livewebasset.getInode() != workingwebasset.getInode())) { 519 520 workingwebasset.setWorking(false); 522 workingwebasset.setLocked(false); 523 InodeFactory.saveInode(workingwebasset); 525 parent.deleteChild(workingwebasset); 527 } 528 529 if (currWebAsset instanceof HTMLPage) { 530 PageServices.unpublishPageFile((HTMLPage)currWebAsset); 532 533 RefreshMenus.deleteMenus(); 535 536 } else if (currWebAsset instanceof Container) { 537 ContainerServices.unpublishContainerFile((Container)currWebAsset); 539 } else if (currWebAsset instanceof Contentlet) { 540 ContentletServices.unpublishContentletFile((Contentlet)currWebAsset); 542 ContentletMapServices.unpublishContentletMapFile((Contentlet)currWebAsset); 544 } else if (currWebAsset instanceof Template) { 545 TemplateServices.unpublishTemplateFile((Template)currWebAsset); 547 } 548 549 LiveCache.removeAssetFromCache(currWebAsset); 550 551 if (currWebAsset instanceof Contentlet) { 552 ContentletFactory.reIndexContentlet((Contentlet) currWebAsset); 554 } 555 556 return true; 557 } catch (Exception e) { 558 return false; 559 } 560 } 561 return false; 562 } 563 564 public static java.util.List getAssetVersions(WebAsset currWebAsset) { 567 if (currWebAsset.isWorking()) { 569 Identifier identifier = IdentifierFactory.getParentIdentifier(currWebAsset); 570 return IdentifierFactory.getVersionsChildrenOfClass(identifier, currWebAsset.getClass()); 571 } 572 return new java.util.ArrayList (); 573 } 574 575 579 580 public static java.util.List getAssetVersionsandLive(WebAsset currWebAsset) { 583 if (currWebAsset.isWorking()) { 585 Identifier identifier = IdentifierFactory.getParentIdentifier(currWebAsset); 586 return IdentifierFactory.getVersionsandLiveChildrenOfClass(identifier, currWebAsset.getClass()); 587 } 588 return new java.util.ArrayList (); 589 } 590 591 public static java.util.List getAssetVersionsandLiveandWorking(WebAsset currWebAsset) { 593 if (currWebAsset.isWorking()) { 595 Identifier identifier = IdentifierFactory.getParentIdentifier(currWebAsset); 596 return IdentifierFactory.getVersionsandLiveandWorkingChildrenOfClass(identifier, currWebAsset.getClass()); 597 } 598 return new java.util.ArrayList (); 599 } 600 601 private static WebAsset swapAssets(WebAsset workingAsset, WebAsset newAsset) throws Exception { 607 Folder parentFolder = null; 608 if (!isAbstractAsset(workingAsset)){ 609 parentFolder = (Folder) InodeFactory.getParentOfClass(workingAsset, Folder.class); 610 } 611 Identifier identifier = (Identifier) IdentifierFactory.getIdentifierByInode(workingAsset); 612 613 Map workingAssetProps = PropertyUtils.describe(workingAsset); 616 workingAssetProps.remove("class"); 617 workingAssetProps.remove("inode"); 618 workingAssetProps.remove("children"); 619 workingAssetProps.remove("parents"); 620 workingAssetProps.remove("parent"); 621 622 Map newAssetProps = PropertyUtils.describe(newAsset); 623 newAssetProps.remove("class"); 624 newAssetProps.remove("inode"); 625 newAssetProps.remove("children"); 626 newAssetProps.remove("parents"); 627 newAssetProps.remove("parent"); 628 629 630 631 632 633 Iterator keys = workingAssetProps.keySet().iterator(); 635 while (keys.hasNext()) { 636 try { 637 String key = (String ) keys.next(); 638 Object x = workingAssetProps.get(key); 639 if(x != null){ 640 PropertyUtils.setProperty(newAsset, key, x); 641 } 642 } catch (NoSuchMethodException e) { 643 } catch (InvocationTargetException e) { 644 } 645 646 } 647 keys = newAssetProps.keySet().iterator(); 648 while (keys.hasNext()) { 649 try { 650 String key = (String ) keys.next(); 651 Object x = newAssetProps.get(key); 652 if(x!=null){ 653 PropertyUtils.setProperty(workingAsset, key, x); 654 } 655 } catch (NoSuchMethodException e) { 656 } catch (InvocationTargetException e) { 657 } 658 } 659 660 workingAsset.setWorking(true); 662 workingAsset.setLive(false); 663 workingAsset.setDeleted(false); 664 workingAsset.setLocked(false); 665 workingAsset.setModDate(new java.util.Date ()); 666 667 newAsset.setWorking(false); 668 newAsset.setLocked(false); 669 670 if (!isAbstractAsset(workingAsset)) { 671 parentFolder.deleteChild(newAsset); 673 } 674 675 InodeFactory.saveInode(workingAsset); 677 InodeFactory.saveInode(newAsset); 678 679 TreeFactory.swapTrees(workingAsset, newAsset); 681 682 if (!isAbstractAsset(workingAsset)) { 683 parentFolder.addChild(workingAsset); 685 parentFolder.deleteChild(newAsset); 686 } 687 identifier.addChild(workingAsset); 688 identifier.addChild(newAsset); 689 690 if (!isAbstractAsset(workingAsset)) { 691 if (newAsset.isLive()) { 692 parentFolder.addChild(newAsset); 693 } 694 } 695 696 DotHibernate.flush(); 697 DotHibernate.getSession().refresh(workingAsset); 698 DotHibernate.getSession().refresh(newAsset); 699 700 return workingAsset; 701 702 } 703 704 717 public static WebAsset saveAsset(WebAsset newWebAsset, Identifier id) throws Exception { 718 if (id.getInode() == 0) { 719 throw new Exception ("Web asset Identifier not found!"); 720 } 721 WebAsset currWebAsset = null; 722 if (newWebAsset instanceof Contentlet) { 723 String condition = "language_id = " + ((Contentlet) newWebAsset).getLanguageId(); 725 currWebAsset = (WebAsset) IdentifierFactory.getWorkingChildOfClassByCondition(id, newWebAsset.getClass(), 726 condition); 727 } else { 728 currWebAsset = (WebAsset) IdentifierFactory.getWorkingChildOfClass(id, newWebAsset.getClass()); 730 } 731 if (currWebAsset.getInode() == 0) { 732 throw new Exception ("Working copy not found!"); 733 } 734 735 WebAsset workingAsset = swapAssets(currWebAsset, newWebAsset); 736 737 List workingAssets = null; 739 if (workingAsset instanceof Contentlet) { 740 String condition = "language_id = " + ((Contentlet) workingAsset).getLanguageId(); 742 workingAssets = IdentifierFactory.getWorkingChildrenOfClassByCondition(id, workingAsset.getClass(), 743 condition); 744 } else { 745 workingAssets = IdentifierFactory.getWorkingChildrenOfClass(id, workingAsset.getClass()); 747 } 748 749 if (workingAssets.size() > 1) { 751 Iterator iter = workingAssets.iterator(); 752 while (iter.hasNext()) { 753 WebAsset webAsset = (WebAsset) iter.next(); 754 if (webAsset.getInode() != workingAsset.getInode()) { 755 webAsset.setWorking(false); 756 InodeFactory.saveInode(webAsset); 757 } 758 } 759 } 760 761 return workingAsset; 762 } 763 764 public static List getAssetsPerConditionWithPermission(Host host, String condition, Class c, Role[] roles, 765 int limit, int offset, String orderby, long parent) { 766 return getAssetsPerConditionWithPermission(host.getInode(), condition, c, roles, limit, offset, orderby, parent); 767 } 768 769 public static List getAssetsPerConditionWithPermission(long hostId, String condition, Class c, Role[] roles, 770 int limit, int offset, String orderby, long parent) { 771 DotHibernate dh = new DotHibernate(c); 772 773 StringBuffer sb = new StringBuffer (); 774 try { 775 776 boolean isCMSAdmin = PublicRoleFactory.containsRole(roles, PublicRoleFactory.getCMSAdminRole()); 777 778 String tableName = ((Inode) c.newInstance()).getType(); 779 780 String rolesStr = ""; 781 782 for (int i = 0; i < roles.length; i++) { 783 rolesStr += roles[i].getRoleId(); 784 if (i != (roles.length - 1)) { 785 rolesStr += ","; 786 } 787 } 788 Logger.debug(WebAssetFactory.class, "rolesStr=" + rolesStr); 789 sb.append("select {" + tableName + ".*} from " + tableName + ", inode " + tableName + "_1_ where " 790 + tableName + ".inode = " + tableName + "_1_.inode and " + tableName + ".inode in ("); 791 sb.append("select distinct " + tableName + "_condition.inode "); 792 sb.append(" from " + tableName + " " + tableName + "_condition"); 793 if (!isCMSAdmin) { 794 sb.append(", tree tree, identifier identifier, permission permission "); 795 } 796 if (parent != 0) { 797 sb.append(", tree tree2"); 798 } 799 sb.append(" where " + condition); 800 801 if (parent != 0) { 802 sb.append(" and " + tableName + "_condition.inode = tree2.child"); 803 sb.append(" and tree2.parent = " + parent); 804 } 805 if (!isCMSAdmin) { 806 sb.append(" and " + tableName + "_condition.inode = tree.child "); 807 sb.append(" and tree.parent = identifier.inode "); 808 sb.append(" and identifier.inode = permission.inode_id "); 809 sb.append(" and permission.roleid in (" + rolesStr + ") "); 810 sb.append(" and permission.permission = " + Config.getStringProperty("PERMISSION_READ")); 811 } 812 sb.append(") and " + tableName + ".inode in (select tree.child from identifier, tree where host_inode = '" 813 + hostId + "' and tree.parent = identifier.inode)"); 814 sb.append(" order by " + orderby); 815 if (limit != 0) { 816 dh.setFirstResult(offset); 817 dh.setMaxResults(limit); 818 } 819 820 Logger.debug(WebAssetFactory.class, sb.toString()); 821 822 dh.setSQLQuery(sb.toString()); 823 return dh.list(); 824 825 } catch (Exception e) { 826 Logger.warn(WebAssetFactory.class, "getAssetsPerConditionWithPermission failed:" + e, e); 827 } 828 829 return new ArrayList (); 830 } 831 832 public static List getAssetsPerConditionWithPermission(String condition, Class c, Role[] roles, int limit, 833 int offset, String orderby, long parent) { 834 DotHibernate dh = new DotHibernate(c); 835 836 StringBuffer sb = new StringBuffer (); 837 try { 838 839 String tableName = ((Inode) c.newInstance()).getType(); 840 841 String rolesStr = ""; 842 843 for (int i = 0; i < roles.length; i++) { 844 rolesStr += roles[i].getRoleId(); 845 if (i != (roles.length - 1)) { 846 rolesStr += ","; 847 } 848 } 849 Logger.debug(WebAssetFactory.class, "rolesStr=" + rolesStr); 850 sb.append("select {" + tableName + ".*} from " + tableName + ", inode " + tableName + "_1_ where " 851 + tableName + ".inode = " + tableName + "_1_.inode and " + tableName + ".inode in ("); 852 sb.append("select distinct " + tableName + "_condition.inode "); 853 sb.append(" from " + tableName + " " + tableName + "_condition, tree tree, identifier identifier "); 854 if (parent != 0) { 855 sb.append(", tree tree2"); 856 } 857 858 sb.append(", permission permission "); 859 860 sb.append(" where " + condition); 861 862 if (parent != 0) { 863 sb.append(" and " + tableName + "_condition.inode = tree2.child"); 864 sb.append(" and tree2.parent = " + parent); 865 } 866 sb.append(" and " + tableName + "_condition.inode = tree.child "); 867 sb.append(" and tree.parent = identifier.inode "); 868 869 sb.append(" and identifier.inode = permission.inode_id "); 870 sb.append(" and permission.roleid in (" + rolesStr + ") "); 871 sb.append(" and permission.permission = " + Config.getStringProperty("PERMISSION_READ") + ")"); 872 873 sb.append(" order by " + orderby); 874 875 if (limit != 0) { 876 dh.setFirstResult(offset); 877 dh.setMaxResults(limit); 878 } 879 880 Logger.debug(WebAssetFactory.class, sb.toString()); 881 882 dh.setSQLQuery(sb.toString()); 883 return dh.list(); 884 885 } catch (Exception e) { 886 Logger.warn(WebAssetFactory.class, "getAssetsPerConditionWithPermission failed:" + e, e); 887 } 888 889 return new ArrayList (); 890 891 } 892 893 public static int getCountAssetsPerConditionWithPermission(Host host, String condition, Class c, Role[] roles) { 894 return getCountAssetsPerConditionWithPermission(host.getInode(), condition, c, roles); 895 } 896 897 public static int getCountAssetsPerConditionWithPermission(long hostId, String condition, Class c, Role[] roles) { 898 return getCountAssetsPerConditionWithPermission(hostId, condition, c, roles, 0); 899 } 900 901 public static int getCountAssetsPerConditionWithPermission(String condition, Class c, Role[] roles) { 902 return getCountAssetsPerConditionWithPermission(condition, c, roles, 0); 903 904 } 905 906 public static int getCountAssetsPerConditionWithPermission(Host host, String condition, Class c, Role[] roles, 907 long parent) { 908 return getCountAssetsPerConditionWithPermission(host.getInode(), condition, c, roles, parent); 909 } 910 911 public static int getCountAssetsPerConditionWithPermission(long hostId, String condition, Class c, Role[] roles, 912 long parent) { 913 DotConnect db = new DotConnect(); 914 915 StringBuffer sb = new StringBuffer (); 916 try { 917 918 String tableName = ((Inode) c.newInstance()).getType(); 919 920 String rolesStr = ""; 921 for (int i = 0; i < roles.length; i++) { 922 rolesStr += roles[i].getRoleId(); 923 if (i != (roles.length - 1)) { 924 rolesStr += ","; 925 } 926 } 927 Logger.debug(WebAssetFactory.class, "rolesStr=" + rolesStr); 928 929 sb.append("select count(distinct " + tableName + "_condition.inode) as mycount "); 930 sb.append(" from " + tableName + " " + tableName + "_condition, inode " + tableName 931 + "_1_, tree tree, identifier identifier, permission permission "); 932 if (parent != 0) { 933 sb.append(", tree tree2"); 934 } 935 sb.append(" where " + condition); 936 sb.append(" and " + tableName + "_condition.inode = " + tableName + "_1_.inode "); 937 if (parent != 0) { 938 sb.append(" and " + tableName + "_1_.inode = tree2.child"); 939 sb.append(" and tree2.parent = " + parent); 940 } 941 sb.append(" and " + tableName + "_1_.inode = tree.child "); 942 sb.append(" and tree.parent = identifier.inode "); 943 sb.append(" and identifier.inode = permission.inode_id "); 944 sb.append(" and permission.roleid in (" + rolesStr + ") "); 945 sb.append(" and permission.permission = " + Config.getStringProperty("PERMISSION_READ")); 946 sb.append(" and " + tableName 947 + "_condition.inode in (select tree.child from identifier, tree where host_inode = '" + hostId 948 + "' and tree.parent = identifier.inode)"); 949 950 Logger.debug(WebAssetFactory.class, sb.toString()); 951 952 db.setSQL(sb.toString()); 953 return db.getInt("mycount"); 954 955 } catch (Exception e) { 956 Logger.error(WebAssetFactory.class, "getCountAssetsPerConditionWithPermission failed:" + e, e); 957 } 958 959 return 0; 960 961 } 962 963 public static int getCountAssetsPerConditionWithPermission(String condition, Class c, Role[] roles, long parent) { 964 DotConnect db = new DotConnect(); 965 966 StringBuffer sb = new StringBuffer (); 967 try { 968 969 String tableName = ((Inode) c.newInstance()).getType(); 970 971 String rolesStr = ""; 972 for (int i = 0; i < roles.length; i++) { 973 rolesStr += roles[i].getRoleId(); 974 if (i != (roles.length - 1)) { 975 rolesStr += ","; 976 } 977 } 978 Logger.debug(WebAssetFactory.class, "rolesStr=" + rolesStr); 979 980 sb.append("select count(distinct " + tableName + "_condition.inode) as count "); 981 sb.append(" from " + tableName + " " + tableName + "_condition, inode " + tableName 982 + "_1_, tree tree, identifier identifier, permission permission "); 983 if (parent != 0) { 984 sb.append(", tree tree2"); 985 } 986 sb.append(" where " + condition); 987 sb.append(" and " + tableName + "_condition.inode = " + tableName + "_1_.inode "); 988 if (parent != 0) { 989 sb.append(" and " + tableName + "_1_.inode = tree2.child"); 990 sb.append(" and tree2.parent = " + parent); 991 } 992 sb.append(" and " + tableName + "_1_.inode = tree.child "); 993 sb.append(" and tree.parent = identifier.inode "); 994 sb.append(" and identifier.inode = permission.inode_id "); 995 sb.append(" and permission.roleid in (" + rolesStr + ") "); 996 sb.append(" and permission.permission = " + Config.getStringProperty("PERMISSION_READ")); 997 998 Logger.debug(WebAssetFactory.class, sb.toString()); 999 1000 db.setSQL(sb.toString()); 1001 return db.getInt("count"); 1002 1003 } catch (Exception e) { 1004 Logger.error(WebAssetFactory.class, "getCountAssetsPerConditionWithPermission failed:" + e, e); 1005 } 1006 1007 return 0; 1008 } 1009 1010 public static java.util.List getAssetsAndPermissionsPerRoleAndCondition(Host host, Role[] roles, String condition, 1012 int limit, int offset, String orderby, Class assetsClass, String tableName) { 1013 return getAssetsAndPermissionsPerRoleAndCondition(host.getInode(), roles, condition, limit, offset, orderby, 1014 assetsClass, tableName); 1015 } 1016 1017 public static java.util.List <PermissionAsset> getAssetsAndPermissionsPerRoleAndCondition(long hostId, Role[] roles, 1018 String condition, int limit, int offset, String orderby, Class assetsClass, String tableName) { 1019 java.util.List <PermissionAsset> entries = new java.util.ArrayList <PermissionAsset>(); 1020 orderby = tableName + "." + orderby; 1021 java.util.List elements = WebAssetFactory.getAssetsPerConditionWithPermission(hostId, condition, assetsClass, 1022 roles, limit, offset, orderby, 0); 1023 java.util.Iterator elementsIter = elements.iterator(); 1024 1025 while (elementsIter.hasNext()) { 1026 1027 WebAsset asset = (WebAsset) elementsIter.next(); 1028 Folder folderParent = null; 1029 if (!WebAssetFactory.isAbstractAsset(asset)) 1030 folderParent = (Folder) InodeFactory.getParentOfClass(asset, Folder.class); 1031 1032 java.util.List permissions = PermissionCache.getPermissionIdsByRolesFromCache(asset, roles); 1033 1034 PermissionAsset permAsset = new PermissionAsset(); 1035 if (!WebAssetFactory.isAbstractAsset(asset)) 1036 permAsset.setPathToMe(folderParent.getPath()); 1037 else 1038 permAsset.setPathToMe(""); 1039 permAsset.setPermissions(permissions); 1040 permAsset.setAsset(asset); 1041 entries.add(permAsset); 1042 } 1043 return entries; 1044 } 1045 1046 public static java.util.List <PermissionAsset> getAssetsAndPermissionsPerRoleAndCondition(Role[] roles, 1048 String condition, int limit, int offset, String orderby, Class assetsClass, String tableName) { 1049 1050 java.util.List <PermissionAsset> entries = new java.util.ArrayList <PermissionAsset>(); 1051 orderby = tableName + "." + orderby; 1052 java.util.List elements = WebAssetFactory.getAssetsPerConditionWithPermission(condition, assetsClass, roles, 1053 limit, offset, orderby, 0); 1054 java.util.Iterator elementsIter = elements.iterator(); 1055 1056 while (elementsIter.hasNext()) { 1057 1058 WebAsset asset = (WebAsset) elementsIter.next(); 1059 Folder folderParent = null; 1060 if (!WebAssetFactory.isAbstractAsset(asset)) 1061 folderParent = (Folder) InodeFactory.getParentOfClass(asset, Folder.class); 1062 1063 java.util.List permissions = PermissionCache.getPermissionIdsByRolesFromCache(asset, roles); 1064 1065 PermissionAsset permAsset = new PermissionAsset(); 1066 if (!WebAssetFactory.isAbstractAsset(asset)) 1067 permAsset.setPathToMe(folderParent.getPath()); 1068 else 1069 permAsset.setPathToMe(""); 1070 permAsset.setPermissions(permissions); 1071 permAsset.setAsset(asset); 1072 entries.add(permAsset); 1073 } 1074 return entries; 1075 } 1076 1077 public static boolean isAbstractAsset(WebAsset asset) { 1078 if (asset instanceof Contentlet || asset instanceof Container || asset instanceof Template) 1079 return true; 1080 return false; 1081 } 1082 1083 public static void changeAssetMenuOrder(WebAsset asset, int newValue, User user) throws ActionException { 1084 1085 if (!PermissionFactory.doesUserHavePermission(asset, Config.getIntProperty("PERMISSION_WRITE"), user)) 1087 throw new ActionException(WebKeys.USER_PERMISSIONS_EXCEPTION); 1088 1089 if (newValue == -1) { 1090 asset.setShowOnMenu(false); 1091 } else { 1092 asset.setShowOnMenu(true); 1093 } 1094 asset.setSortOrder(newValue); 1095 1096 InodeFactory.saveInode(asset); 1097 RefreshMenus.deleteMenus(); 1098 } 1099 1100} 1101 | Popular Tags |