1 28 package com.opencms.defaults.master; 30 31 import org.opencms.db.CmsDbUtil; 32 import org.opencms.file.CmsGroup; 33 import org.opencms.file.CmsObject; 34 import org.opencms.file.CmsProperty; 35 import org.opencms.file.CmsPropertyDefinition; 36 import org.opencms.file.CmsResource; 37 import org.opencms.file.CmsResourceFilter; 38 import org.opencms.file.types.CmsResourceTypeFolder; 39 import org.opencms.lock.CmsLock; 40 import org.opencms.main.CmsException; 41 import org.opencms.main.CmsLog; 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.CmsFilterMethod; 48 import com.opencms.defaults.I_CmsExtendedContentDefinition; 49 import com.opencms.legacy.CmsLegacyException; 50 import com.opencms.legacy.CmsXmlTemplateLoader; 51 52 import java.util.Hashtable ; 53 import java.util.List ; 54 import java.util.Map ; 55 import java.util.Vector ; 56 57 70 public class CmsChannelContent extends A_CmsContentDefinition implements I_CmsExtendedContentDefinition{ 71 72 private static String C_CHANNELNAME_ERRFIELD="channelname"; 74 private static String C_PARENT_ERRFIELD="channelparent"; 75 private static String C_ERRCODE_EMPTY="empty"; 77 79 80 protected CmsObject m_cms = null; 81 82 85 private String m_channelId; 86 87 90 private CmsResource m_channel; 91 92 95 private String m_channelname; 96 97 100 private String m_parentchannel; 101 102 105 private Map m_properties; 106 107 110 112 115 117 120 private int m_accessflags; 121 122 127 public CmsChannelContent(CmsObject cms) { 128 m_cms = cms; 129 initValues(); 130 } 131 132 142 public CmsChannelContent(CmsObject cms, String channelId) throws CmsException { 143 new CmsChannelContent(cms, new CmsUUID(channelId)); 144 } 145 155 public CmsChannelContent(CmsObject cms, CmsUUID channelId) throws CmsException { 156 throw new RuntimeException ("Method CmsChannelContent(CmsObject cms, CmsUUID channelId) not longer supported"); 157 } 158 159 164 public CmsChannelContent(CmsObject cms, CmsResource resource) { 165 String channelId = CmsDbUtil.UNKNOWN_ID+""; 166 String fullName = cms.getSitePath(resource); 167 m_cms = cms; 168 m_channel = resource; 169 m_channelname = resource.getName(); 170 m_parentchannel = CmsResource.getParentFolder(cms.getSitePath(resource)); 171 try{ 175 m_properties = cms.readProperties(fullName); 176 channelId = (String )m_properties.get(CmsPropertyDefinition.PROPERTY_CHANNELID); 177 } catch (CmsException exc){ 178 m_properties = new Hashtable (); 179 m_properties.put(CmsPropertyDefinition.PROPERTY_CHANNELID, CmsDbUtil.UNKNOWN_ID+""); 180 } finally { 181 if(channelId == null || "".equals(channelId)){ 182 channelId = CmsDbUtil.UNKNOWN_ID+""; 183 } 184 m_channelId = channelId; 185 } 186 } 187 188 191 protected void initValues() { 192 m_channelId = CmsDbUtil.UNKNOWN_ID+""; 193 m_channelname = ""; 194 m_parentchannel = ""; 195 m_accessflags = com.opencms.core.I_CmsConstants.C_ACCESS_DEFAULT_FLAGS; 196 m_channel = new CmsResource(CmsUUID.getNullUUID(), CmsUUID.getNullUUID(), 198 "", CmsResourceTypeFolder.RESOURCE_TYPE_ID, true, 0, 199 m_cms.getRequestContext().currentProject().getId(), 200 1, System.currentTimeMillis(), 201 m_cms.getRequestContext().currentUser().getId(), System.currentTimeMillis(), 202 m_cms.getRequestContext().currentUser().getId(), CmsResource.DATE_RELEASED_DEFAULT, 203 CmsResource.DATE_EXPIRED_DEFAULT, 1, 0); 204 m_properties = new Hashtable (); 205 } 206 207 212 public void delete(CmsObject cms) throws Exception { 213 cms.getRequestContext().saveSiteRoot(); 214 cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS); 215 try{ 216 cms.deleteResource(cms.getSitePath(m_channel), CmsResource.DELETE_PRESERVE_SIBLINGS); 217 } catch (CmsException exc){ 218 throw exc; 219 224 } finally { 225 cms.getRequestContext().restoreSiteRoot(); 226 } 227 } 228 229 234 public void undelete(CmsObject cms) throws Exception { 235 cms.getRequestContext().saveSiteRoot(); 236 cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS); 237 try{ 238 cms.undeleteResource(cms.getSitePath(m_channel)); 239 } catch (CmsException exc){ 240 if (CmsLog.getLog(this).isErrorEnabled() ) { 241 CmsLog.getLog(this).error("Could not undelete channel " + cms.getSitePath(m_channel), exc); 242 } 243 } finally { 244 cms.getRequestContext().restoreSiteRoot(); 245 } 246 } 247 248 253 public void publishResource(CmsObject cms) { 254 if (CmsLog.getLog(this).isWarnEnabled() ) { 255 CmsLog.getLog(this).warn("Channels can't be published directly"); 256 } 257 } 258 259 265 public void restore(CmsObject cms, int versionId) { 266 if (CmsLog.getLog(this).isWarnEnabled() ) { 267 CmsLog.getLog(this).warn("Channels can't be restored from history"); 268 } 269 } 270 271 277 public void chown(CmsObject cms, CmsUUID owner) { 278 if (CmsLog.getLog(this).isWarnEnabled() ) { 279 CmsLog.getLog(this).warn("Permissions of Channels can be changed only in EditBackoffice"); 280 } 281 } 282 283 289 public void chgrp(CmsObject cms, CmsUUID group) { 290 if (CmsLog.getLog(this).isWarnEnabled() ) { 291 CmsLog.getLog(this).warn("Permissions of Channels can be changed only in EditBackoffice"); 292 } 293 } 294 295 301 public void chmod(CmsObject cms, int accessflags) { 302 if (CmsLog.getLog(this).isWarnEnabled() ) { 303 CmsLog.getLog(this).warn("Permissions of Channels can be changed only in EditBackoffice"); 304 } 305 } 306 312 public CmsUUID copy(CmsObject cms) { 313 if (CmsLog.getLog(this).isWarnEnabled() ) { 314 CmsLog.getLog(this).warn("Channels can't be copied!"); 315 } 316 return CmsUUID.getNullUUID(); 317 } 318 319 324 public void write(CmsObject cms) throws Exception { 325 CmsResource newChannel = null; 326 CmsLock lock = null; 327 cms.getRequestContext().saveSiteRoot(); 329 cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS); 330 try{ 331 if((CmsDbUtil.UNKNOWN_ID+"").equals(m_channelId)) { 332 setNewChannelId(); 335 newChannel = cms.createResource(m_parentchannel + m_channelname, CmsResourceTypeFolder.RESOURCE_TYPE_ID, null, CmsProperty.toList(m_properties)); 336 cms.lockResource(cms.getSitePath(newChannel)); 337 } else { 338 if (!"".equals(m_channel.getName())) { 339 newChannel = cms.readFolder(cms.getSitePath(m_channel)); 340 } 341 342 if (newChannel!=null && !cms.getSitePath(newChannel).equals(m_parentchannel+m_channelname+"/")){ 343 String parent = CmsResource.getParentFolder(cms.getSitePath(newChannel)); 346 if(! parent.equals(m_parentchannel)){ 347 cms.moveResource(cms.getSitePath(newChannel), m_parentchannel+m_channelname); 349 } else if (!newChannel.getName().equals(m_channelname)){ 350 cms.renameResource(cms.getSitePath(newChannel), m_channelname); 352 } 353 } 354 newChannel = cms.readFolder(m_parentchannel+m_channelname+"/"); 356 lock = cms.getLock(newChannel); 357 String propTitle = cms.readProperty(cms.getSitePath(newChannel), CmsPropertyDefinition.PROPERTY_TITLE); 359 if (propTitle == null){ 360 propTitle = ""; 361 } 362 if (!propTitle.equals(this.getTitle())){ 363 cms.writeProperty(cms.getSitePath(newChannel), CmsPropertyDefinition.PROPERTY_TITLE, this.getTitle()); 364 } 365 if(!lock.getUserId().equals(this.getLockstate()) || 367 lock.getProjectId() != cms.getRequestContext().currentProject().getId()){ 368 if(this.getLockstate().isNullUUID()){ 369 cms.unlockResource(cms.getSitePath(newChannel)); 371 } else { 372 cms.lockResource(cms.getSitePath(newChannel)); 374 } 375 } 376 } 377 m_channel = cms.readFolder(cms.getSitePath(newChannel)); 391 } catch (CmsException exc){ 392 throw exc; 393 398 } finally { 399 cms.getRequestContext().restoreSiteRoot(); 400 } 401 } 402 403 408 public String getUniqueId(CmsObject cms) { 409 return m_channel.getStructureId().toString(); 410 } 411 412 417 public CmsUUID getId() { 418 return m_channel.getStructureId(); 419 } 420 421 426 public int getLockedInProject() { 427 try { 428 return m_cms.getLock(m_channel).getProjectId(); 429 } catch (CmsException e) { 430 return CmsDbUtil.UNKNOWN_ID; 431 } 432 } 433 434 439 public int getState() { 440 return m_channel.getState(); 441 } 442 443 448 public int getProjectId() { 449 return m_channel.getProjectLastModified(); 450 } 451 452 456 public String getChannelId() { 457 return m_channelId; 458 } 459 460 463 public void setChannelId(String id) { 464 m_properties.put(CmsPropertyDefinition.PROPERTY_CHANNELID, id); 465 m_channelId = id; 466 } 467 468 471 public String getTitle(){ 472 String title = (String )m_properties.get(CmsPropertyDefinition.PROPERTY_TITLE); 473 if (title == null){ 474 title = ""; 475 } 476 return title; 477 } 478 479 482 public void setTitle(String title) { 483 m_properties.put(CmsPropertyDefinition.PROPERTY_TITLE, title); 484 } 485 486 490 public CmsUUID getLockstate() { 491 try { 492 return m_cms.getLock(m_channel).getUserId(); 493 } catch (CmsException e) { 494 return CmsUUID.getNullUUID(); 495 } 496 } 497 498 502 public void setLockstate(CmsUUID lockstate) { 503 } 505 506 509 public String getOwnerName() { 510 String ownername = ""; 511 try{ 512 ownername = m_cms.readUser(getOwner()).getName(); 513 } catch (CmsException exc){ 514 } 516 return ownername; 517 } 518 519 522 public CmsUUID getOwner() { 523 return CmsUUID.getNullUUID(); 524 } 526 527 530 public void setOwner(CmsUUID id) { 531 } 533 534 537 public String getGroup() { 538 String groupname = "*NOT USED*"; 539 return groupname; 545 } 546 547 550 public CmsUUID getGroupId() { 551 return CmsUUID.getNullUUID(); 552 } 554 555 558 public void setGroup(CmsUUID groupId) { 559 } 561 562 565 public String getChannelPath() { 566 return m_cms.getSitePath(m_channel); 567 } 568 569 572 public String getChannelName() { 573 return m_channelname; 574 } 575 576 579 public void setChannelName(String name) { 580 m_channelname = name; 581 } 582 583 586 public void setParentName(String name) { 587 m_parentchannel = name; 588 } 589 590 593 public String getParentName() { 594 return m_parentchannel; 595 } 596 597 600 public void setAccessFlags(int flags) { 601 m_accessflags = flags; 602 } 603 604 607 public int getAccessFlags() { 608 return m_accessflags; 609 } 610 611 614 public long getDateLastModified(){ 615 return m_channel.getDateLastModified(); 616 } 617 618 621 public long getDateCreated(){ 622 return m_channel.getDateCreated(); 623 } 624 625 628 public CmsUUID getLastModifiedBy(){ 629 return m_channel.getUserLastModified(); 630 } 631 632 635 public String getLastModifiedByName(){ 636 return ""; 637 } 638 639 642 public int getVersionId(){ 643 return CmsDbUtil.UNKNOWN_ID; 644 } 645 646 659 660 public Integer getGroups(CmsObject cms, Vector names, Vector values) throws CmsException { 661 662 List groups = cms.getGroups(); 664 int retValue = -1; 665 String defaultGroup = OpenCms.getDefaultUsers().getGroupUsers(); 666 CmsXmlTemplateLoader.getSession(cms.getRequestContext(), true); 668 String enteredGroup = this.getGroup(); 669 if(enteredGroup != null && !enteredGroup.equals("")) { 670 671 defaultGroup = enteredGroup; 673 } 674 675 int n = 0; 677 for(int z = 0;z < groups.size();z++) { 678 if(((CmsGroup)groups.get(z)).getProjectCoWorker()) { 679 String name = ((CmsGroup)groups.get(z)).getName(); 680 if(defaultGroup.equals(name)) { 681 retValue = n; 682 } 683 names.addElement(name); 684 values.addElement(name); 685 n++; } 687 } 688 return new Integer (retValue); 689 } 690 691 697 public Vector getHistory(CmsObject cms) { 698 if (CmsLog.getLog(this).isWarnEnabled() ) { 699 CmsLog.getLog(this).warn("Channels have no history"); 700 } 701 return null; 702 } 703 704 712 public Object getVersionFromHistory(CmsObject cms, int versionId){ 713 if (CmsLog.getLog(this).isWarnEnabled() ) { 714 CmsLog.getLog(this).warn("Channels have no history"); 715 } 716 return null; 717 } 718 722 public boolean isReadable() { 723 m_cms.getRequestContext().saveSiteRoot(); 724 m_cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS); 725 try { 726 return m_cms.hasPermissions(m_channel, CmsPermissionSet.ACCESS_READ); 727 } catch(CmsException exc) { 730 return false; 732 } finally { 733 m_cms.getRequestContext().restoreSiteRoot(); 734 } 735 } 736 737 741 public boolean isWriteable() { 742 m_cms.getRequestContext().saveSiteRoot(); 743 m_cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS); 744 try { 745 return m_cms.hasPermissions(m_channel, CmsPermissionSet.ACCESS_WRITE); 747 } catch(CmsException exc) { 748 return false; 750 } finally { 751 m_cms.getRequestContext().restoreSiteRoot(); 752 } 753 } 754 755 760 762 766 public String toString() { 767 StringBuffer returnValue = new StringBuffer (); 768 returnValue.append(this.getClass().getName() + "{"); 769 returnValue.append("ChannelId=" + getChannelId() + ";"); 770 returnValue.append("ChannelName=" + getChannelPath() + ";"); 771 returnValue.append("Lockstate=" + getLockstate() + ";"); 772 returnValue.append("AccessFlags=" + getAccessFlagsAsString() + ";"); 773 returnValue.append(m_channel.toString() + "}"); 774 return returnValue.toString(); 775 } 776 777 781 public String getAccessFlagsAsString() { 782 783 int accessFlags = getAccessFlags(); 784 StringBuffer buf = new StringBuffer (); 785 786 buf.append(((accessFlags & I_CmsExtendedContentDefinition.C_PERMISSION_READ) > 0 ? "r" : "-")); 787 buf.append(((accessFlags & I_CmsExtendedContentDefinition.C_PERMISSION_WRITE) > 0 ? "w" : "-")); 788 buf.append(((accessFlags & I_CmsExtendedContentDefinition.C_PERMISSION_VIEW) > 0 ? "v" : "-")); 789 buf.append(((accessFlags & com.opencms.core.I_CmsConstants.C_ACCESS_GROUP_READ) > 0 ? "r" : "-")); 790 buf.append(((accessFlags & com.opencms.core.I_CmsConstants.C_ACCESS_GROUP_WRITE) > 0 ? "w" : "-")); 791 buf.append(((accessFlags & com.opencms.core.I_CmsConstants.C_ACCESS_GROUP_VISIBLE) > 0 ? "v" : "-")); 792 buf.append(((accessFlags & com.opencms.core.I_CmsConstants.C_ACCESS_PUBLIC_READ) > 0 ? "r" : "-")); 793 buf.append(((accessFlags & com.opencms.core.I_CmsConstants.C_ACCESS_PUBLIC_WRITE) > 0 ? "w" : "-")); 794 buf.append(((accessFlags & com.opencms.core.I_CmsConstants.C_ACCESS_PUBLIC_VISIBLE) > 0 ? "v" : "-")); 795 buf.append(((accessFlags & CmsResource.FLAG_INTERNAL) > 0 ? "i" : "-")); 796 797 return buf.toString(); 798 } 799 800 803 public void setAccessFlagsAsString(String permissions){ 804 String perm=permissions; 806 permissions=""; 807 for(int x=9;x >= 0;x--) { 808 char temp=perm.charAt(x); 809 permissions+=temp; 810 } 811 setAccessFlags(Integer.parseInt(permissions,2)); 812 } 813 814 817 private void setNewChannelId() throws CmsException{ 818 int newChannelId = org.opencms.db.CmsDbUtil.nextId(com.opencms.core.I_CmsConstants.C_TABLE_CHANNELID); 819 m_properties.put(CmsPropertyDefinition.PROPERTY_CHANNELID, newChannelId+""); 820 m_channelId = newChannelId+""; 821 } 822 823 827 public static boolean isLockable() { 828 return true; 829 } 830 831 836 public static Vector getFieldNames(CmsObject cms) { 837 Vector names = new Vector (); 838 names.addElement("channelId"); 839 names.addElement("channelPath"); 840 names.addElement("title"); 841 names.addElement("ownerName"); 842 names.addElement("group"); 843 names.addElement("accessFlagsAsString"); 844 return names; 845 } 846 847 852 public static Vector getFieldMethods(CmsObject cms) { 853 Vector methods = new Vector (); 854 try { 855 methods.addElement(CmsChannelContent.class.getMethod("getChannelId", new Class [0])); 856 methods.addElement(CmsChannelContent.class.getMethod("getChannelPath", new Class [0])); 857 methods.addElement(CmsChannelContent.class.getMethod("getTitle", new Class [0])); 858 methods.addElement(CmsChannelContent.class.getMethod("getOwnerName", new Class [0])); 859 methods.addElement(CmsChannelContent.class.getMethod("getGroup", new Class [0])); 860 methods.addElement(CmsChannelContent.class.getMethod("getAccessFlagsAsString", new Class [0])); 861 } catch(NoSuchMethodException exc) { 862 } 864 return methods; 865 } 866 867 872 public static Vector getFilterMethods(CmsObject cms) { 873 Vector filterMethods = new Vector (); 874 try { 875 CmsFilterMethod filterUp = new CmsFilterMethod("All Channels", 876 CmsChannelContent.class.getMethod("getChannelList", 877 new Class [] {CmsObject.class} ) , new Object [0]); 878 filterMethods.addElement(filterUp); 879 } catch (NoSuchMethodException nsm) { 880 } 882 return filterMethods; 883 } 884 885 890 public static Vector getChannelList(CmsObject cms) throws CmsException { 891 Vector content = new Vector (); 892 cms.getRequestContext().saveSiteRoot(); 893 cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS); 894 try { 895 getAllResources(cms, "/", content); 896 } catch(CmsException e) { 897 if (CmsLog.getLog(CmsChannelContent.class).isWarnEnabled()) { 899 CmsLog.getLog(CmsChannelContent.class).warn("Error while reading subfolders of cos root", e); 900 } 901 } finally { 902 cms.getRequestContext().restoreSiteRoot(); 903 } 904 return content; 905 } 906 909 private static void getAllResources(CmsObject cms, String rootFolder, Vector allFolders) throws CmsException { 910 Vector subFolders = new Vector (); 912 try{ 913 subFolders.addAll(cms.getResourcesInFolder(rootFolder, CmsResourceFilter.ONLY_VISIBLE)); 914 } catch (CmsException e){ 915 if ((e instanceof CmsLegacyException) && (((CmsLegacyException)e).getType() != CmsLegacyException.C_NOT_FOUND)) { 917 throw e; 918 } 919 } 920 for(int i = 0;i < subFolders.size();i++) { 922 CmsResource curFolder = (CmsResource)subFolders.elementAt(i); 923 CmsChannelContent curChannel = new CmsChannelContent(cms, curFolder); 924 allFolders.addElement(curChannel); 925 getAllResources(cms, cms.getSitePath(curFolder), allFolders); 926 } 927 } 928 934 public void check() throws CmsPlausibilizationException { 935 Vector errorCodes = new Vector (); 937 if (m_channelname == null || "".equals(m_channelname)) { 939 errorCodes.addElement(C_CHANNELNAME_ERRFIELD+com.opencms.core.I_CmsConstants.C_ERRSPERATOR+C_ERRCODE_EMPTY); 940 } 941 if (m_parentchannel == null || "".equals(m_parentchannel)) { 943 errorCodes.addElement(C_PARENT_ERRFIELD+com.opencms.core.I_CmsConstants.C_ERRSPERATOR+C_ERRCODE_EMPTY); 944 } 945 if (errorCodes.size()>0) { 947 throw new CmsPlausibilizationException(errorCodes); 948 } 949 } 950 }
| Popular Tags
|