1 23 24 package org.apache.slide.content; 25 26 import java.io.Serializable ; 27 import java.text.ParseException ; 28 import java.text.SimpleDateFormat ; 29 import java.util.Date ; 30 import java.util.Enumeration ; 31 import java.util.Hashtable ; 32 import java.util.Locale ; 33 import java.util.TimeZone ; 34 import java.util.Vector ; 35 36 import org.apache.slide.common.ObjectValidationFailedException; 37 import org.apache.slide.util.EmptyEnumeration; 38 import org.apache.slide.util.Messages; 39 40 45 public final class NodeRevisionDescriptor implements Serializable , Cloneable { 46 47 48 50 51 54 public static final String CREATION_DATE = "creationdate"; 55 public static final String CREATION_USER = "creationuser"; 56 public static final String MODIFICATION_DATE = "modificationdate"; 57 public static final String MODIFICATION_USER = "modificationuser"; 58 59 60 63 public static final String LAST_MODIFIED = "getlastmodified"; 64 65 66 69 public static final String NAME = "displayname"; 70 71 72 75 public static final String RESOURCE_TYPE = "resourcetype"; 76 public static final String TYPE = RESOURCE_TYPE; 77 78 79 82 public static final String SOURCE = "source"; 83 84 85 88 public static final String OWNER = "owner"; 89 90 91 94 public static final String CONTENT_TYPE = "getcontenttype"; 95 96 97 100 public static final String CONTENT_LANGUAGE = "getcontentlanguage"; 101 102 103 106 public static final String CONTENT_LENGTH = "getcontentlength"; 107 108 109 112 public static final String ETAG = "getetag"; 113 114 115 118 public static final String COLLECTION_TYPE = "<collection/>"; 119 120 121 124 protected static final SimpleDateFormat format = 125 new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); 126 127 128 131 protected static final SimpleDateFormat formats[] = { 132 new SimpleDateFormat ("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US), 133 new SimpleDateFormat ("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US), 134 new SimpleDateFormat ("EEEEEE, dd-MMM-yy HH:mm:ss zzz", Locale.US), 135 new SimpleDateFormat ("EEE MMMM d HH:mm:ss yyyy", Locale.US), 136 new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'Z'") 137 }; 138 139 140 143 protected static final SimpleDateFormat creationDateFormat = 144 new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss'Z'"); 145 146 147 static { 148 format.setTimeZone(TimeZone.getTimeZone("GMT")); 149 creationDateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); 150 for (int i = 0; i < formats.length; i++) { 151 formats[i].setTimeZone(TimeZone.getTimeZone("GMT")); 152 } 153 } 154 155 156 158 159 162 public NodeRevisionDescriptor() { 163 this.properties = new Hashtable (); 164 this.labels = new Vector (); 165 this.branchName = NodeRevisionDescriptors.MAIN_BRANCH; 166 initDefaultProperties(); 167 } 168 169 170 173 public NodeRevisionDescriptor(long contentLength) { 174 this(); 175 setContentLength(contentLength); 176 } 177 178 179 184 public NodeRevisionDescriptor(NodeRevisionNumber number, String branchName, 185 Vector labels, Hashtable properties) { 186 this(); 187 this.number = number; 188 if (branchName != null) { 189 this.branchName = branchName; 190 } 191 this.labels = labels; 192 setProperties(properties); 193 } 194 195 196 198 199 202 private String branchName; 203 204 205 208 private NodeRevisionNumber number; 209 210 211 214 private Vector labels; 215 216 217 220 private Hashtable properties; 221 222 225 private Hashtable updatedProperties = null; 226 private Hashtable removedProperties = null; 227 228 229 231 232 235 public String getBranchName() { 236 return this.branchName; 237 } 238 239 240 243 void setBranchName(String branchName) { 244 this.branchName = branchName; 245 } 246 247 248 251 public NodeRevisionNumber getRevisionNumber() { 252 return this.number; 253 } 254 255 256 259 void setRevisionNumber(NodeRevisionNumber number) { 260 this.number = number; 261 } 262 263 264 267 public void addLabel(String label) { 268 this.labels.addElement(label); 269 } 270 271 272 275 public void removeLabel(String label) { 276 this.labels.removeElement(label); 277 } 278 279 280 283 public void removeLabels() { 284 this.labels.removeAllElements(); 285 } 286 287 288 291 294 295 296 299 public Enumeration enumerateLabels() { 300 return this.labels.elements(); 301 } 302 303 304 307 Hashtable getProperties() { 308 return this.properties; 309 } 310 311 312 315 void setProperties(Hashtable properties) { 316 this.properties = properties; 318 this.updatedProperties = new Hashtable (properties); 319 } 320 321 322 328 public boolean exists(String name) { 329 return exists(name, NodeProperty.DEFAULT_NAMESPACE); 330 } 331 332 333 339 public boolean exists(String name, String namespace) { 340 if (name != null) 341 return (properties.get(getNamespacedPropertyName(namespace,name)) != null); 342 else 343 return (false); 344 } 345 346 347 353 public NodeProperty getProperty(String name) { 354 return getProperty(name, NodeProperty.DEFAULT_NAMESPACE); 355 } 356 357 358 365 public NodeProperty getProperty(String name, String namespace) { 366 Object result = properties.get(getNamespacedPropertyName(namespace,name)); 367 if (result != null) { 368 return (NodeProperty) result; 369 } else { 370 return null; 371 } 372 } 373 374 375 381 public void setProperty(String name, Object value) { 382 setProperty(new NodeProperty(name, value)); 383 } 384 385 386 393 public void setProperty(String name, String namespace, Object value) { 394 setProperty(new NodeProperty(name, value, namespace)); 395 } 396 397 398 403 public void setProperty(NodeProperty property) { 404 String name = getNamespacedPropertyName(property.getNamespace(), property.getName()); 405 properties.put(name, property); 406 407 if (this.updatedProperties == null) this.updatedProperties = new Hashtable (); 408 if (this.removedProperties == null) this.removedProperties = new Hashtable (); 409 updatedProperties.put(name, property); 410 removedProperties.remove(name); 411 } 412 413 414 420 public void removeProperty(NodeProperty property) { 421 removeProperty(property.getName(), property.getNamespace()); 422 } 423 424 425 430 public void removeProperty(String property) { 431 removeProperty(property, NodeProperty.DEFAULT_NAMESPACE); 432 } 433 434 435 440 public void removeProperty(String property, String nameSpace) { 441 String name = getNamespacedPropertyName(nameSpace, property); 442 NodeProperty nodeProperty = (NodeProperty )properties.remove(name); 443 if (nodeProperty != null) { 445 if (this.updatedProperties == null) this.updatedProperties = new Hashtable (); 446 if (this.removedProperties == null) this.removedProperties = new Hashtable (); 447 448 removedProperties.put(name, nodeProperty); 449 updatedProperties.remove(name); 450 } 451 } 452 453 454 460 public Enumeration getPropertiesNames() { 461 return enumeratePropertiesName(); 462 } 463 464 465 470 public Enumeration enumeratePropertiesName() { 471 Vector result = new Vector (); 472 Enumeration propertyList = enumerateProperties(); 473 while (propertyList.hasMoreElements()) { 474 NodeProperty currentProperty = 475 (NodeProperty) propertyList.nextElement(); 476 result.addElement(currentProperty.getName()); 477 } 478 return result.elements(); 479 } 480 481 482 488 public Enumeration getPropertiesValues() { 489 return enumerateProperties(); 490 } 491 492 493 498 public Enumeration enumerateProperties() { 499 return properties.elements(); 500 } 501 502 public Enumeration enumerateRemovedProperties() { 503 if (this.removedProperties == null) { 504 return EmptyEnumeration.INSTANCE; 505 } else { 506 return removedProperties.elements(); 507 } 508 } 509 510 public Enumeration enumerateUpdatedProperties() { 511 if (this.updatedProperties == null) { 512 return EmptyEnumeration.INSTANCE; 513 } else { 514 return updatedProperties.elements(); 515 } 516 } 517 518 public void resetUpdatedProperties() { 519 updatedProperties = null; 520 } 521 522 public void resetRemovedProperties() { 523 removedProperties = null; 524 } 525 526 535 public boolean propertyValueContains( String name, String substr ) { 536 boolean result = false; 537 NodeProperty p = getProperty( name ); 538 539 if( p != null ) { 540 Object v = p.getValue(); 541 if( v instanceof String && ((String )v).indexOf(substr) >= 0 ) 542 result = true; 543 } 544 545 return result; 546 } 547 548 549 559 public boolean propertyValueContains( String name, String namespace, String substr ) { 560 boolean result = false; 561 NodeProperty p = getProperty( name, namespace ); 562 563 if( p != null ) { 564 Object v = p.getValue(); 565 if( v instanceof String && ((String )v).indexOf(substr) >= 0 ) 566 result = true; 567 } 568 569 return result; 570 } 571 572 573 578 public String getName() { 579 NodeProperty name = getProperty(NAME); 580 if (name == null) { 581 return new String (); 582 } else { 583 return (String ) name.getValue(); 584 } 585 } 586 587 588 593 public void setName(String name) { 594 setProperty(NAME, name); } 596 597 598 603 public String getETag() { 604 NodeProperty contentType = getProperty(ETAG); 605 if (contentType == null) { 606 return new String (); 607 } else { 608 return (String ) contentType.getValue(); 609 } 610 } 611 612 613 618 public void setETag(String eTag) { 619 setProperty(ETAG, eTag); } 621 622 623 624 625 626 631 public String getOwner() { 632 NodeProperty owner = getProperty(OWNER); 633 if (owner == null) { 634 return new String (); 635 } else { 636 return (String ) owner.getValue(); 637 } 638 } 639 640 641 646 public void setOwner(String owner) { 647 setProperty(OWNER, owner); } 649 650 655 public void setOwner(String owner, String userpath) { 656 setProperty(OWNER, userpath + "/" + owner); } 658 659 664 public String getSource() { 665 NodeProperty source = getProperty(SOURCE); 666 if (source == null) { 667 return new String (); 668 } else { 669 return (String ) source.getValue(); 670 } 671 } 672 673 674 679 public void setSource(String source) { 680 setProperty(SOURCE, source); } 682 683 684 685 686 687 688 693 public String getResourceType() { 694 NodeProperty resourceType = getProperty(RESOURCE_TYPE); 695 if (resourceType == null) { 696 return new String (); 697 } else { 698 return String.valueOf(resourceType.getValue()); 700 701 } 702 } 703 704 705 710 public void setResourceType(String resourceType) { 711 setProperty(RESOURCE_TYPE, resourceType); } 713 714 715 716 717 722 public String getContentType() { 723 NodeProperty contentType = getProperty(CONTENT_TYPE); 724 if (contentType == null) { 725 return new String (); 726 } else { 727 return (String ) contentType.getValue(); 728 } 729 } 730 731 732 737 public void setContentType(String contentType) { 738 setProperty(CONTENT_TYPE, contentType); } 740 741 742 747 public String getContentLanguage() { 748 NodeProperty contentLanguage = getProperty(CONTENT_LANGUAGE); 749 if (contentLanguage == null) { 750 return new String (); 751 } else { 752 return (String ) contentLanguage.getValue(); 753 } 754 } 755 756 757 762 public void setContentLanguage(String contentLanguage) { 763 setProperty(CONTENT_LANGUAGE, contentLanguage); } 765 766 767 772 public String getCreationDate() { 773 NodeProperty creationDate = getProperty(CREATION_DATE); 774 if (creationDate == null) { 775 return null; 776 } else { 777 if (creationDate.getValue() instanceof Date ) { 778 return creationDateFormat.format 779 ((Date ) creationDate.getValue()); 780 } 781 return creationDate.getValue().toString(); 782 } 783 } 784 785 786 791 public String getModificationDate() { 792 NodeProperty modificationDate = getProperty(MODIFICATION_DATE); 793 if (modificationDate == null) { 794 return null; 795 } else { 796 if (modificationDate.getValue() instanceof Date ) { 797 return creationDateFormat.format 798 ((Date ) modificationDate.getValue()); 799 } 800 return modificationDate.getValue().toString(); 801 } 802 } 803 804 805 810 public String getCreationUser() { 811 NodeProperty creationUser = getProperty(CREATION_USER); 812 if (creationUser == null) { 813 return new String (); 814 } else { 815 return (String ) creationUser.getValue(); 816 } 817 } 818 819 820 825 public String getModificationUser() { 826 NodeProperty modificationUser = getProperty(MODIFICATION_USER); 827 if (modificationUser == null) { 828 return new String (); 829 } else { 830 return (String ) modificationUser.getValue(); 831 } 832 } 833 834 835 840 public Date getCreationDateAsDate() { 841 NodeProperty creationDate = getProperty(CREATION_DATE); 842 if (creationDate == null) 843 return null; 844 if (creationDate.getValue() instanceof Date ) { 845 return (Date ) creationDate.getValue(); 846 } else { 847 String creationDateValue = creationDate.getValue().toString(); 848 Date result = null; 849 for (int i = 0; (result == null) && (i < formats.length); i++) { 851 try { 852 synchronized (formats[i]) { 853 result = formats[i].parse(creationDateValue); 854 } 855 } catch (ParseException e) { 856 ; 857 } 858 } 859 return result; 860 } 861 } 862 863 864 869 public void setCreationDate(Date creationDate) { 870 setProperty(CREATION_DATE, creationDateFormat.format(creationDate)); 872 } 873 874 875 880 public void setModificationDate(Date modificationDate) { 881 setProperty(MODIFICATION_DATE, creationDateFormat.format(modificationDate)); 883 } 884 885 886 891 public void setModificationDate(String modificationDate) { 892 setProperty(MODIFICATION_DATE, modificationDate); 894 } 895 896 897 902 public void setCreationUser(String creationUser) { 903 setProperty(CREATION_USER, creationUser); 905 } 906 907 908 913 public void setModificationUser(String modificationUser) { 914 setProperty(MODIFICATION_USER, modificationUser); 916 } 917 918 919 924 public void setCreationDate(String creationDate) { 925 setProperty(CREATION_DATE, creationDate); } 927 928 929 934 public String getLastModified() { 935 NodeProperty lastModified = getProperty(LAST_MODIFIED); 936 if (lastModified == null) { 937 return null; 938 } else { 939 if (lastModified.getValue() instanceof Date ) { 940 return format.format((Date ) lastModified.getValue()); 941 } else { 942 return lastModified.getValue().toString(); 943 } 944 } 945 } 946 947 948 953 public Date getLastModifiedAsDate() { 954 NodeProperty lastModified = getProperty(LAST_MODIFIED); 955 if (lastModified == null) 956 return null; 957 if (lastModified.getValue() instanceof Date ) { 958 return (Date ) lastModified.getValue(); 959 } else { 960 String lastModifiedValue = lastModified.getValue().toString(); 961 Date result = null; 962 for (int i = 0; (result == null) && (i < formats.length); i++) { 964 try { 965 synchronized (formats[i]) { 966 result = formats[i].parse(lastModifiedValue); 967 } 968 } catch (ParseException e) { 969 ; 970 } 971 } 972 return result; 973 } 974 } 975 976 977 982 public void setLastModified(Date lastModified) { 983 setProperty(LAST_MODIFIED, format.format(lastModified)); 985 } 986 987 988 993 public void setLastModified(String lastModified) { 994 setProperty(LAST_MODIFIED, lastModified); } 996 997 998 1003 public void setContentLength(long contentLength) { 1004 setProperty(CONTENT_LENGTH, new Long (contentLength)); } 1006 1007 1008 1013 public void setContentLength(String contentLength) { 1014 Long contentLengthValue = null; 1015 try { 1016 contentLengthValue = new Long (contentLength); 1017 } catch (NumberFormatException e) { 1018 } 1020 if (contentLengthValue == null) { 1021 contentLengthValue = new Long (0); 1022 } 1023 setProperty(CONTENT_LENGTH, contentLengthValue); } 1025 1026 1027 1032 public long getContentLength() { 1033 NodeProperty contentLength = getProperty(CONTENT_LENGTH); 1034 if (contentLength == null) { 1035 return -1L; 1036 } else { 1037 if (contentLength.getValue() instanceof Long ) { 1038 return ((Long ) contentLength.getValue()).longValue(); 1039 } 1040 1041 if (contentLength.getValue() instanceof String ) { 1042 return (new Long ((String ) contentLength.getValue())) 1043 .longValue(); 1044 } 1045 return -1L; 1046 } 1047 } 1048 1049 1050 1053 void setDefaultProperties(Enumeration defaultProperties) { 1054 while (defaultProperties.hasMoreElements()) { 1055 NodeProperty currentProperty = 1056 (NodeProperty) defaultProperties.nextElement(); 1057 String name = currentProperty.getName(); 1058 String namespace = currentProperty.getNamespace(); 1059 NodeProperty lookup = getProperty(name, namespace); 1060 if (lookup == null) { 1061 setProperty(name, namespace, currentProperty.getValue()); 1063 } 1064 } 1065 } 1066 1067 1068 1070 1071 private void initDefaultProperties() { 1072 1073 setCreationDate(new Date ()); 1074 setResourceType(COLLECTION_TYPE); 1077 setProperty(SOURCE, ""); setContentLength(-1); 1079 setLastModified(new Date ()); 1080 1081 } 1082 1083 1091 public String getNamespacedPropertyName(String namespace, String propertyName) { 1092 String result; 1093 if (namespace == null) result = propertyName; 1094 else result = namespace + propertyName; 1095 return result; 1096 } 1097 1098 1099 1101 1102 1107 public NodeRevisionDescriptor cloneObject() { 1108 NodeRevisionDescriptor result = null; 1109 try { 1110 result = (NodeRevisionDescriptor) super.clone(); 1111 Hashtable propertiesClone = new Hashtable (); 1113 Enumeration propertiesList = this.properties.keys(); 1114 while (propertiesList.hasMoreElements()) { 1115 Object key = propertiesList.nextElement(); 1116 Object value = this.properties.get(key); 1117 propertiesClone.put(key, value); 1118 } 1119 result.properties = propertiesClone; 1120 result.labels = (Vector ) this.labels.clone(); 1122 } catch(CloneNotSupportedException e) { 1123 e.printStackTrace(); 1124 } 1125 return result; 1126 } 1127 1128 1129 1137 public boolean equals(Object obj) { 1138 boolean result = false; 1139 if ((obj != null) && (obj instanceof NodeRevisionDescriptor)) { 1140 NodeRevisionDescriptor revisionDescriptor = 1141 (NodeRevisionDescriptor) obj; 1142 result = this.getRevisionNumber() 1143 .equals(revisionDescriptor.getRevisionNumber()); 1144 } 1145 return result; 1146 } 1147 1148 1149 1152 public void validate() { 1153 1154 if (branchName == null) 1155 throw new ObjectValidationFailedException 1156 (Messages.message 1157 (NodeRevisionDescriptor.class.getName() + ".nullBranchName")); 1158 1159 if (number == null) 1160 throw new ObjectValidationFailedException 1161 (Messages.message 1162 (NodeRevisionDescriptor.class.getName() + ".nullNumber")); 1163 number.validate(); 1164 1165 if (labels == null) 1166 throw new ObjectValidationFailedException 1167 (Messages.message 1168 (NodeRevisionDescriptor.class.getName() + ".nullLabels")); 1169 1170 if (properties == null) 1171 throw new ObjectValidationFailedException 1172 (Messages.message 1173 (NodeRevisionDescriptor.class.getName() + ".nullProperties")); 1174 Enumeration propertyList = properties.elements(); 1175 while (propertyList.hasMoreElements()) { 1176 Object obj = propertyList.nextElement(); 1177 if (!(obj instanceof NodeProperty)) 1178 throw new ObjectValidationFailedException 1179 (Messages.message 1180 (NodeRevisionDescriptor.class.getName() 1181 + ".invalidPropertyType")); 1182 ((NodeProperty) obj).validate(); 1183 } 1184 1185 } 1186 1187 1188} 1189 | Popular Tags |