| 1 28 29 package com.opencms.defaults.master; 30 31 import org.opencms.db.CmsDbContext; 32 import org.opencms.db.CmsDbUtil; 33 import org.opencms.db.CmsPublishedResource; 34 import org.opencms.file.CmsObject; 35 import org.opencms.file.CmsProject; 36 import org.opencms.file.CmsResource; 37 import org.opencms.file.CmsResourceFilter; 38 import org.opencms.file.CmsUser; 39 import org.opencms.main.CmsEvent; 40 import org.opencms.main.CmsException; 41 import org.opencms.main.I_CmsEventListener; 42 import org.opencms.main.OpenCms; 43 import org.opencms.security.CmsPermissionSet; 44 import org.opencms.util.CmsUUID; 45 46 import com.opencms.defaults.A_CmsContentDefinition; 47 import com.opencms.defaults.I_CmsExtendedContentDefinition; 48 import com.opencms.defaults.master.genericsql.CmsDbAccess; 49 import com.opencms.legacy.CmsXmlTemplateLoader; 50 51 import java.util.HashMap ; 52 import java.util.Map ; 53 import java.util.StringTokenizer ; 54 import java.util.Vector ; 55 56 69 public abstract class CmsMasterContent 70 extends A_CmsContentDefinition 71 implements I_CmsExtendedContentDefinition { 72 73 74 protected CmsObject m_cms = null; 75 76 77 protected CmsMasterDataSet m_dataSet = null; 78 79 80 protected boolean m_lockstateWasChanged = false; 81 82 83 private static HashMap c_accessObjects = new HashMap (); 84 85 86 protected Vector m_selectedChannels = null; 87 88 89 protected Vector m_availableChannels = null; 90 91 97 protected static void registerDbAccessObject(int subId, CmsDbAccess dBAccessObject) { 98 c_accessObjects.put(new Integer (subId), dBAccessObject); 99 } 100 101 107 protected static CmsDbAccess getDbAccessObject(int subId) { 108 return (CmsDbAccess) c_accessObjects.get(new Integer (subId)); 109 } 110 111 116 public CmsMasterContent(CmsObject cms) { 117 m_cms = cms; 118 initValues(); 119 } 120 121 126 public CmsMasterContent(CmsObject cms, CmsMasterDataSet dataset) { 127 this(cms); 128 m_dataSet = dataset; 129 } 130 131 141 public CmsMasterContent(CmsObject cms, CmsUUID contentId) throws CmsException { 142 m_cms = cms; 143 initValues(); 144 getDbAccessObject(getSubId()).read(m_cms, this, m_dataSet, contentId); 145 } 146 147 151 public CmsMasterContent() {} 152 153 156 protected void initValues() { 157 m_dataSet = new CmsMasterDataSet(); 158 m_dataSet.m_masterId = CmsUUID.getNullUUID(); 159 m_dataSet.m_subId = CmsDbUtil.UNKNOWN_ID; 160 m_dataSet.m_lockedBy = CmsUUID.getNullUUID(); 161 m_dataSet.m_versionId = CmsDbUtil.UNKNOWN_ID; 162 m_dataSet.m_userName = null; 163 m_dataSet.m_groupName = null; 164 m_dataSet.m_lastModifiedByName = null; 165 m_dataSet.m_userId = CmsUUID.getNullUUID(); 166 setAccessFlags(com.opencms.core.I_CmsConstants.C_ACCESS_DEFAULT_FLAGS); 167 } 168 169 172 public String getTitle() { 173 return m_dataSet.m_title; 174 } 175 176 179 public void setTitle(String title) { 180 m_dataSet.m_title = title; 181 } 182 183 188 public Vector getMedia() throws CmsException { 189 if(m_dataSet.m_media == null) { 190 m_dataSet.m_media = getDbAccessObject(getSubId()).readMedia(m_cms, this); 193 } 194 return m_dataSet.m_media; 195 } 196 197 201 public void addMedia(CmsMasterMedia media) { 202 m_dataSet.m_mediaToAdd.add(media); 203 } 204 205 209 public void deleteMedia(CmsMasterMedia media) { 210 m_dataSet.m_mediaToDelete.add(media); 211 } 212 213 217 public void updateMedia(CmsMasterMedia media) { 218 m_dataSet.m_mediaToUpdate.add(media); 219 } 220 221 226 public Vector getChannels() throws CmsException { 227 if(m_dataSet.m_channel == null) { 228 m_dataSet.m_channel = getDbAccessObject(getSubId()).readChannels(m_cms, this); 231 } 232 return m_dataSet.m_channel; 233 } 234 235 239 public void addChannel(String channel) { 240 m_dataSet.m_channelToAdd.add(channel); 241 } 242 243 247 public void deleteChannel(String channel) { 248 m_dataSet.m_channelToDelete.add(channel); 249 } 250 251 256 public void delete(CmsObject cms) throws Exception { 257 getDbAccessObject(getSubId()).delete(m_cms, this, m_dataSet); 258 } 259 260 266 public void chgrp(CmsObject cms, CmsUUID group) throws Exception { 267 m_dataSet.m_groupId = group; 268 getDbAccessObject(getSubId()).changePermissions(m_cms, this, m_dataSet); 269 } 270 271 277 public void chown(CmsObject cms, CmsUUID owner) throws Exception { 278 m_dataSet.m_userId = owner; 279 getDbAccessObject(getSubId()).changePermissions(m_cms, this, m_dataSet); 280 } 281 282 288 public void chmod(CmsObject cms, int accessflags) throws Exception { 289 m_dataSet.m_accessFlags = accessflags; 290 getDbAccessObject(getSubId()).changePermissions(m_cms, this, m_dataSet); 291 } 292 293 299 public CmsUUID copy(CmsObject cms) throws Exception { 300 return getDbAccessObject(getSubId()).copy(cms, this, (CmsMasterDataSet)m_dataSet.clone(), this.getMedia(), this.getChannels()); 302 } 303 304 309 public void write(CmsObject cms) throws CmsException { 310 updateChannels(); 312 if(m_dataSet.m_masterId.isNullUUID()) { 314 getDbAccessObject(getSubId()).insert(m_cms, this, m_dataSet); 316 } else { 317 if(m_lockstateWasChanged) { 319 getDbAccessObject(getSubId()).writeLockstate(m_cms, this, m_dataSet); 321 } else { 322 getDbAccessObject(getSubId()).write(m_cms, this, m_dataSet); 324 } 325 } 326 m_lockstateWasChanged = false; 328 m_dataSet.m_media = null; 331 m_dataSet.m_mediaToAdd = new Vector (); 332 m_dataSet.m_mediaToDelete = new Vector (); 333 m_dataSet.m_mediaToUpdate = new Vector (); 334 335 m_dataSet.m_channel = null; 338 m_dataSet.m_channelToAdd = new Vector (); 339 m_dataSet.m_channelToDelete = new Vector (); 340 } 341 342 346 public void importMaster() throws Exception { 347 getDbAccessObject(getSubId()).insert(m_cms, this, m_dataSet); 348 m_lockstateWasChanged = false; 350 m_dataSet.m_media = null; 353 m_dataSet.m_mediaToAdd = new Vector (); 354 m_dataSet.m_mediaToDelete = new Vector (); 355 m_dataSet.m_mediaToUpdate = new Vector (); 356 357 m_dataSet.m_channel = null; 360 m_dataSet.m_channelToAdd = new Vector (); 361 m_dataSet.m_channelToDelete = new Vector (); 362 } 363 364 369 public String getUniqueId(CmsObject cms) { 370 return getId() + ""; 371 } 372 373 378 public CmsUUID getId() { 379 return m_dataSet.m_masterId; 380 } 381 382 386 public CmsUUID getLockstate() { 387 400 401 return m_dataSet.m_lockedBy; 402 } 403 404 408 public void setLockstate(CmsUUID lockstate) { 409 m_lockstateWasChanged = true; 410 m_dataSet.m_lockedBy = lockstate; 411 } 412 413 416 public CmsUUID getOwner() { 417 return m_dataSet.m_userId; 418 } 419 420 423 public String getOwnerName() { 424 425 String retValue = m_dataSet.m_userId + ""; 426 if ((m_dataSet.m_userName == null || "".equals(m_dataSet.m_userName.trim())) 427 && !CmsUUID.getNullUUID().equals(m_dataSet.m_userId)) { 428 try { retValue = m_cms.readUser(m_dataSet.m_userId).getName(); 430 } catch (CmsException exc) { 431 } 434 } else { 435 retValue = m_dataSet.m_userName; 437 } 438 return retValue; 439 } 440 441 444 public void setOwner(CmsUUID id) { 445 m_dataSet.m_userId = id; 446 } 447 448 451 public String getGroup() { 452 453 String retValue = ""; 454 455 if (m_dataSet.m_groupId != null) { 456 retValue = m_dataSet.m_groupId + ""; 457 } 458 459 if ((m_dataSet.m_groupName == null || "".equals(m_dataSet.m_groupName.trim())) 460 && (m_dataSet.m_groupId != null && !CmsUUID.getNullUUID().equals(m_dataSet.m_groupId))) { 461 try { 462 retValue = m_cms.readGroup(m_dataSet.m_groupId).getName(); 463 } catch (CmsException exc) { 464 } 467 } else { 468 retValue = m_dataSet.m_groupName; 470 } 471 472 return retValue; 473 } 474 475 478 public CmsUUID getGroupId() { 479 return m_dataSet.m_groupId; 480 } 481 482 485 public void setGroup(CmsUUID groupId) { 486 m_dataSet.m_groupId = groupId; 487 } 488 489 497 public int getProjectId() { 498 return m_dataSet.m_projectId; 499 } 500 501 507 public int getState() { 508 return m_dataSet.m_state; 509 } 510 511 518 public int getLockedInProject() { 519 return m_dataSet.m_lockedInProject; 520 } 521 522 527 abstract public int getSubId(); 528 529 533 public String toString() { 534 StringBuffer returnValue = new StringBuffer (); 535 returnValue.append(this.getClass().getName() + "{"); 536 returnValue.append("UniqueId=" + getUniqueId(m_cms) + ";"); 537 returnValue.append("Lockstate=" + getLockstate() + ";"); 538 returnValue.append("AccessFlags=" + getAccessFlagsAsString() + ";"); 539 returnValue.append(m_dataSet.toString() + "}"); 540 return returnValue.toString(); 541 } 542 543 547 public void setAccessFlags(int accessFlags) { 548 m_dataSet.m_accessFlags = accessFlags; 549 } 550 551 555 public int getAccessFlags() { 556 return m_dataSet.m_accessFlags; 557 } 558 559 563 public String getAccessFlagsAsString() { 564 565 int accessFlags = getAccessFlags(); 566 StringBuffer buf = new StringBuffer (); 567 568 buf.append(((accessFlags & I_CmsExtendedContentDefinition.C_PERMISSION_READ) > 0 ? "r" : "-")); 569 buf.append(((accessFlags & I_CmsExtendedContentDefinition.C_PERMISSION_WRITE) > 0 ? "w" : "-")); 570 buf.append(((accessFlags & I_CmsExtendedContentDefinition.C_PERMISSION_VIEW) > 0 ? "v" : "-")); 571 buf.append(((accessFlags & com.opencms.core.I_CmsConstants.C_ACCESS_GROUP_READ) > 0 ? "r" : "-")); 572 buf.append(((accessFlags & com.opencms.core.I_CmsConstants.C_ACCESS_GROUP_WRITE) > 0 ? "w" : "-")); 573 buf.append(((accessFlags & com.opencms.core.I_CmsConstants.C_ACCESS_GROUP_VISIBLE) > 0 ? "v" : "-")); 574 buf.append(((accessFlags & com.opencms.core.I_CmsConstants.C_ACCESS_PUBLIC_READ) > 0 ? "r" : "-")); 575 buf.append(((accessFlags & com.opencms.core.I_CmsConstants.C_ACCESS_PUBLIC_WRITE) > 0 ? "w" : "-")); 576 buf.append(((accessFlags & com.opencms.core.I_CmsConstants.C_ACCESS_PUBLIC_VISIBLE) > 0 ? "v" : "-")); 577 buf.append(((accessFlags & CmsResource.FLAG_INTERNAL) > 0 ? "i" : "-")); 578 579 return buf.toString(); 580 } 581 582 586 public boolean isVisible() { 587 CmsUser currentUser = m_cms.getRequestContext().currentUser(); 588 try { 589 if(m_cms.isAdmin()) { 590 return true; 591 } else { 592 if ( !accessOther(com.opencms.core.I_CmsConstants.C_ACCESS_PUBLIC_VISIBLE) 593 && !accessOwner(m_cms, currentUser, CmsPermissionSet.PERMISSION_VIEW) 594 && !accessGroup(m_cms, currentUser, com.opencms.core.I_CmsConstants.C_ACCESS_GROUP_VISIBLE)) { 595 return false; 596 } else { 597 return true; 598 } 599 } 600 } catch(CmsException exc) { 601 return false; 603 } 604 } 605 606 610 public boolean isReadable() { 611 try { 612 if(m_cms.isAdmin()) { 613 return true; 614 } else { 615 return hasReadAccess(m_cms); 616 } 617 } catch(CmsException exc) { 618 return false; 620 } 621 } 622 623 627 public boolean isWriteable() { 628 try { 629 if(m_cms.isAdmin()) { 630 return true; 631 } else { 632 return this.hasWriteAccess(m_cms); 633 } 634 } catch(CmsException exc) { 635 return false; 637 } 638 } 639 640 645 public void publishResource(CmsObject cms) throws Exception { 646 Vector changedResources = new Vector (); 647 Vector changedModuleData = new Vector (); 648 int versionId = 0; 649 long publishDate = System.currentTimeMillis(); 650 boolean historyEnabled = OpenCms.getSystemInfo().isVersionHistoryEnabled(); 651 CmsUUID publishHistoryId = new CmsUUID(); 652 653 try { 654 if (historyEnabled) { 655 versionId = cms.getBackupTagId(); 657 cms.backupProject(versionId, publishDate); 659 } 660 661 getDbAccessObject(getSubId()).publishResource(cms, publishHistoryId, m_dataSet, getSubId(), this.getClass().getName(), historyEnabled, versionId, publishDate, changedResources, changedModuleData); 663 664 if (CmsXmlTemplateLoader.isElementCacheEnabled()) { 666 CmsXmlTemplateLoader.getOnlineElementCache().cleanupCache(changedResources, changedModuleData); 667 } 668 } finally { 669 670 Map eventData = new HashMap (); 672 eventData.put(I_CmsEventListener.KEY_PUBLISHID, publishHistoryId.toString()); 673 eventData.put(I_CmsEventListener.KEY_PROJECTID, new Integer (cms.getRequestContext().currentProject().getId())); 674 eventData.put(I_CmsEventListener.KEY_DBCONTEXT, new CmsDbContext(cms.getRequestContext())); 675 OpenCms.fireCmsEvent(new CmsEvent(I_CmsEventListener.EVENT_PUBLISH_PROJECT, eventData)); 676 } 677 } 678 679 689 public void writePublishHistory(CmsProject project, CmsPublishedResource publishedBoResource, CmsUUID publishId, int tagId) throws CmsException { 690 691 getDbAccessObject(getSubId()).writePublishHistory( 692 project, 693 publishId, 694 tagId, 695 publishedBoResource.getRootPath(), 697 publishedBoResource.getResourceId(), 699 publishedBoResource.getType(), publishedBoResource.getState()); 702 703 } 704 705 711 public void undelete(CmsObject cms) throws Exception { 712 getDbAccessObject(getSubId()).undelete(m_cms, this, m_dataSet); 713 } 714 715 718 public static boolean beforePublish( CmsObject cms, Boolean enableHistory, 719 Integer projectId, Integer versionId, Long publishingDate, 720 Vector changedRessources, Vector changedModuleData, CmsMasterDataSet dataset ) { 721 722 if (false && ((cms == null) && (enableHistory == null) && (projectId == null) && 723 (versionId == null) && (publishingDate == null) && (changedRessources == null) && 724 (changedModuleData == null) && (dataset == null))) { 725 } 727 728 return true; 729 } 730 731 748 protected static void publishProject(CmsObject cms, CmsUUID publishHistoryId, 749 boolean enableHistory, int projectId, int versionId, long publishingDate, 750 int subId, String contentDefinitionClassName, 751 Vector changedRessources, Vector changedModuleData) throws CmsException { 752 753 getDbAccessObject(subId).publishProject(cms, publishHistoryId, enableHistory, 755 projectId, versionId, publishingDate, subId, 756 contentDefinitionClassName, changedRessources, changedModuleData ); 757 } 758 759 765 protected static Vector readAllByChannel(CmsObject cms, String channelId, int subId) throws CmsException{ 766 return getDbAccessObject(subId).readAllByChannel(cms, channelId, subId); 767 } 768 769 774 public long getDateLastModified() { 775 return m_dataSet.m_dateLastModified; 776 } 777 778 783 public long getDateCreated() { 784 return m_dataSet.m_dateCreated; 785 } 786 787 792 public CmsUUID getLastModifiedBy() { 793 return m_dataSet.m_lastModifiedBy; 794 } 795 796 801 public String getLastModifiedByName() { 802 803 String retValue = ""; 804 if (m_dataSet.m_lastModifiedBy != null) { 805 retValue = m_dataSet.m_lastModifiedBy + ""; 806 } 807 if (m_dataSet.m_lastModifiedByName == null && m_dataSet.m_lastModifiedBy != null) { 808 try { 809 retValue = m_cms.readUser(m_dataSet.m_lastModifiedBy).getName(); 810 } catch (CmsException exc) { 811 } 813 } else { 814 retValue = m_dataSet.m_lastModifiedByName; 815 } 816 return retValue; 817 } 818 819 824 public int getVersionId() { 825 return m_dataSet.m_versionId; 826 } 827 828 835 public void restore(CmsObject cms, int versionId) throws Exception { 836 getDbAccessObject(this.getSubId()).restore(cms, this, m_dataSet, versionId); 837 } 838 839 846 public Vector getHistory(CmsObject cms) throws Exception { 847 return getDbAccessObject(this.getSubId()).getHistory(cms, this.getClass(), m_dataSet.m_masterId, this.getSubId()); 848 } 849 850 858 public Object getVersionFromHistory(CmsObject cms, int versionId) throws Exception { 859 return getDbAccessObject(this.getSubId()).getVersionFromHistory(cms, this.getClass(), m_dataSet.m_masterId, this.getSubId(), versionId); 860 } 861 862 863 867 public Vector getSelectedChannels() throws CmsException{ 868 if (m_selectedChannels == null) { 869 Vector dbChannels = getChannels(); 870 m_selectedChannels = new Vector (); 871 String rootChannel = getDbAccessObject(this.getSubId()).getRootChannel(); 872 int offset = rootChannel.length()-1; 873 for (int i=0; i< dbChannels.size(); i++) { 874 m_selectedChannels.add(((String )dbChannels.elementAt(i)).substring(offset)); 877 } 878 } 879 return m_selectedChannels; 880 } 881 882 886 public void setSelectedChannels(String channels) { 887 StringTokenizer tk = new StringTokenizer (channels, ","); 888 Vector v = new Vector (); 889 int tokens = tk.countTokens(); 890 if (channels != null && channels.equals("empty")) { 891 m_selectedChannels = v; 892 }else if (tokens > 0) { 893 for (int i=0; i<tokens; i++) { 894 v.addElement(tk.nextToken()); 895 } 896 m_selectedChannels = v; 897 } 898 } 899 900 909 public Vector getAvailableChannels(CmsObject cms) throws CmsException { 910 if (m_availableChannels == null) { 911 Vector selectedChannels = getSelectedChannels(); 912 Vector subChannels = getAllSubChannelsOfRootChannel(cms); 913 for (int i=0; i<subChannels.size(); i++) { 914 for (int j=0; j<selectedChannels.size(); j++) { 915 if (subChannels.elementAt(i).equals(selectedChannels.elementAt(j))) { 916 subChannels.removeElementAt(i); 917 i--; 918 break; 919 } 920 } 921 } 922 m_availableChannels = subChannels; 923 } 924 return m_availableChannels; 925 } 926 927 931 public void setAvailableChannels(String channels) { 932 StringTokenizer tk = new StringTokenizer (channels, ","); 933 Vector v = new Vector (); 934 int tokens = tk.countTokens(); 935 if (channels != null && channels.equals("empty")) { 936 m_availableChannels = v; 937 } else if (tokens > 0) { 938 for (int i=0; i<tokens; i++) { 939 v.addElement(tk.nextToken()); 940 } 941 m_availableChannels = v; 942 } 943 } 944 945 957 public static Vector getAllSubChannelsOf (CmsObject cms, String channel) 958 throws CmsException { 959 Vector allChannels = new Vector (); 960 Vector subChannels = new Vector (); 961 String siteRoot = cms.getRequestContext().getSiteRoot(); 962 try { 963 cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS); 964 subChannels.addAll(cms.getResourcesInFolder(channel, CmsResourceFilter.ONLY_VISIBLE)); 965 966 for (int i=0; i < subChannels.size(); i++) { 967 CmsResource resource = (CmsResource)subChannels.get(i); 968 if (resource.getState() != CmsResource.STATE_DELETED) { 969 String folder = cms.getSitePath(resource); 970 Vector v = getAllSubChannelsOf(cms, folder); 971 if (v.size() == 0) { 972 allChannels.add(folder); 973 }else { 974 for (int j=0; j < v.size(); j++) { 975 allChannels.add(v.get(j)); 976 } 977 } 978 } 979 } 980 981 } catch (CmsException e) { 982 } finally { 984 cms.getRequestContext().setSiteRoot(siteRoot); 985 } 986 987 return allChannels; 988 } 989 990 991 1003 public Vector getAllSubChannelsOfRootChannel (CmsObject cms) 1004 throws CmsException { 1005 Vector allChannels = new Vector (); 1006 cms.getRequestContext().saveSiteRoot(); 1007 try { 1008 String rootChannel = getDbAccessObject(this.getSubId()).getRootChannel(); 1009 cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS); 1010 Vector subChannels = new Vector (cms.getResourcesInFolder(rootChannel, CmsResourceFilter.ONLY_VISIBLE)); 1012 int offset = rootChannel.length()-1; 1013 for (int i=0; i < subChannels.size(); i++) { 1014 CmsResource resource = (CmsResource)subChannels.get(i); 1015 if (resource.getState() != CmsResource.STATE_DELETED) { 1016 String folder = cms.getSitePath(resource); 1017 Vector v = getAllSubChannelsOf(cms, folder); 1018 if (v.size() == 0 && cms.hasPermissions(resource, CmsPermissionSet.ACCESS_VIEW)) { 1019 allChannels.add(folder.substring(offset)); 1020 } else { 1021 for (int j=0; j < v.size(); j++) { 1022 allChannels.add(((String )v.get(j)).substring(offset)); 1023 } 1024 } 1025 } 1026 } 1027 } finally { 1028 cms.getRequestContext().restoreSiteRoot(); 1029 } 1030 return allChannels; 1031 } 1032 1033 1038 protected void updateChannels() throws CmsException{ 1039 Vector dbChannels = getChannels(); 1040 Vector selectedChannels = getSelectedChannels(); 1041 String rootChannel = getDbAccessObject(this.getSubId()).getRootChannel(); 1042 String prefix = rootChannel.substring(0, rootChannel.length()-1); 1043 for (int i=0; i < dbChannels.size(); i++) { 1045 boolean found = false; 1046 for (int j=0; j < selectedChannels.size(); j++) { 1047 if (dbChannels.elementAt(i).equals(prefix + ((String )selectedChannels.elementAt(j)))) { 1048 found = true; 1049 break; 1050 } 1051 } 1052 if (!found) { 1053 deleteChannel((String )dbChannels.elementAt(i)); 1054 } 1055 } 1056 for (int i=0; i < selectedChannels.size(); i++) { 1058 boolean found = false; 1059 for (int j=0; j < dbChannels.size(); j++) { 1060 if ((prefix + ((String )selectedChannels.elementAt(i))).equals(dbChannels.elementAt(j))) { 1061 found = true; 1062 break; 1063 } 1064 } 1065 if (!found) { 1066 addChannel(prefix + (String )selectedChannels.elementAt(i)); 1067 } 1068 } 1069 } 1070 1071 1075 public String getRootChannel() { 1076 return getDbAccessObject(this.getSubId()).getRootChannel(); 1077 } 1078 1079 1104 1105 1127 1128 1144} | Popular Tags |