1 24 package org.ofbiz.entity; 25 26 import java.io.PrintWriter ; 27 import java.io.Serializable ; 28 import java.math.BigDecimal ; 29 import java.text.NumberFormat ; 30 import java.util.Collection ; 31 import java.util.Collections ; 32 import java.util.Iterator ; 33 import java.util.Locale ; 34 import java.util.Map ; 35 import java.util.MissingResourceException ; 36 import java.util.Observable ; 37 import java.util.ResourceBundle ; 38 import java.util.TreeSet ; 39 40 import javolution.lang.Reusable; 41 import javolution.util.FastList; 42 import javolution.util.FastMap; 43 44 import org.ofbiz.base.util.Base64; 45 import org.ofbiz.base.util.Debug; 46 import org.ofbiz.base.util.ObjectType; 47 import org.ofbiz.base.util.UtilDateTime; 48 import org.ofbiz.base.util.UtilProperties; 49 import org.ofbiz.base.util.UtilValidate; 50 import org.ofbiz.base.util.UtilXml; 51 import org.ofbiz.base.util.collections.LocalizedMap; 52 import org.ofbiz.entity.condition.EntityCondition; 53 import org.ofbiz.entity.jdbc.SqlJdbcUtil; 54 import org.ofbiz.entity.model.ModelEntity; 55 import org.ofbiz.entity.model.ModelField; 56 import org.ofbiz.entity.model.ModelFieldType; 57 import org.ofbiz.entity.util.ByteWrapper; 58 import org.w3c.dom.Document ; 59 import org.w3c.dom.Element ; 60 61 62 74 public class GenericEntity extends Observable implements Map , LocalizedMap, Serializable , Comparable , Cloneable , Reusable { 75 public static final String module = GenericEntity.class.getName(); 76 77 public static final GenericEntity NULL_ENTITY = new NullGenericEntity(); 78 public static final NullField NULL_FIELD = new NullField(); 79 80 81 protected String delegatorName = null; 82 83 84 protected transient GenericDelegator internalDelegator = null; 85 86 93 protected Map fields = FastMap.newInstance(); 94 95 96 protected String entityName = null; 97 98 99 protected transient ModelEntity modelEntity = null; 100 101 102 protected boolean modified = false; 103 protected boolean generateHashCode = true; 104 protected int cachedHashCode = 0; 105 106 107 protected boolean mutable = true; 108 109 110 protected boolean isFromEntitySync = false; 111 112 113 protected GenericEntity() { } 114 115 116 public static GenericEntity createGenericEntity(ModelEntity modelEntity) { 117 if (modelEntity == null) { 118 throw new IllegalArgumentException ("Cannot create a GenericEntity with a null modelEntity parameter"); 119 } 120 121 GenericEntity newEntity = new GenericEntity(); 122 newEntity.init(modelEntity); 123 return newEntity; 124 } 125 126 127 public static GenericEntity createGenericEntity(ModelEntity modelEntity, Map fields) { 128 if (modelEntity == null) { 129 throw new IllegalArgumentException ("Cannot create a GenericEntity with a null modelEntity parameter"); 130 } 131 132 GenericEntity newEntity = new GenericEntity(); 133 newEntity.init(modelEntity, fields); 134 return newEntity; 135 } 136 137 138 public static GenericEntity createGenericEntity(GenericEntity value) { 139 if (value == null) { 140 throw new IllegalArgumentException ("Cannot create a GenericEntity with a null value parameter"); 141 } 142 143 GenericEntity newEntity = new GenericEntity(); 144 newEntity.init(value); 145 return newEntity; 146 } 147 148 149 protected void init(ModelEntity modelEntity) { 150 if (modelEntity == null) { 151 throw new IllegalArgumentException ("Cannont create a GenericEntity with a null modelEntity parameter"); 152 } 153 this.modelEntity = modelEntity; 154 this.entityName = modelEntity.getEntityName(); 155 156 if (this.entityName == null) { 158 throw new IllegalArgumentException ("Cannont create a GenericEntity with a null entityName in the modelEntity parameter"); 159 } 160 } 161 162 163 protected void init(ModelEntity modelEntity, Map fields) { 164 if (modelEntity == null) { 165 throw new IllegalArgumentException ("Cannont create a GenericEntity with a null modelEntity parameter"); 166 } 167 this.modelEntity = modelEntity; 168 this.entityName = modelEntity.getEntityName(); 169 setFields(fields); 170 171 if (this.entityName == null) { 173 throw new IllegalArgumentException ("Cannont create a GenericEntity with a null entityName in the modelEntity parameter"); 174 } 175 } 176 177 178 protected void init(GenericEntity value) { 179 if (value.modelEntity == null) { 180 throw new IllegalArgumentException ("Cannont create a GenericEntity from another GenericEntity with a null modelEntity in the value parameter"); 181 } 182 this.entityName = value.modelEntity.getEntityName(); 183 this.modelEntity = value.modelEntity; 184 if (value.fields != null) this.fields.putAll(value.fields); 185 this.delegatorName = value.delegatorName; 186 this.internalDelegator = value.internalDelegator; 187 188 if (this.entityName == null) { 190 throw new IllegalArgumentException ("Cannont create a GenericEntity with a null entityName in the modelEntity parameter"); 191 } 192 } 193 194 public void reset() { 195 this.delegatorName = null; 197 this.internalDelegator = null; 198 this.fields = FastMap.newInstance(); 199 this.entityName = null; 200 this.modelEntity = null; 201 this.modified = false; 202 this.generateHashCode = true; 203 this.cachedHashCode = 0; 204 this.mutable = true; 205 this.isFromEntitySync = false; 206 } 207 208 public void refreshFromValue(GenericEntity newValue) throws GenericEntityException { 209 if (newValue == null) { 210 throw new GenericEntityException("Could not refresh value, new value not found for: " + this); 211 } 212 GenericPK thisPK = this.getPrimaryKey(); 213 GenericPK newPK = newValue.getPrimaryKey(); 214 if (!thisPK.equals(newPK)) { 215 throw new GenericEntityException("Could not refresh value, new value did not have the same primary key; this PK=" + thisPK + ", new value PK=" + newPK); 216 } 217 this.fields = newValue.fields; 218 this.setDelegator(newValue.getDelegator()); 219 this.generateHashCode = newValue.generateHashCode; 220 this.cachedHashCode = newValue.cachedHashCode; 221 this.modified = false; 222 } 223 224 public boolean isModified() { 225 return this.modified; 226 } 227 228 public void synchronizedWithDatasource() { 229 this.modified = false; 230 } 231 232 public void removedFromDatasource() { 233 this.modified = true; 235 } 236 237 public boolean isMutable() { 238 return this.mutable; 239 } 240 241 public void setImmutable() { 242 this.mutable = false; 243 } 244 245 248 public boolean getIsFromEntitySync() { 249 return this.isFromEntitySync; 250 } 251 252 255 public void setIsFromEntitySync(boolean isFromEntitySync) { 256 this.isFromEntitySync = isFromEntitySync; 257 } 258 259 public String getEntityName() { 260 return entityName; 261 } 262 263 public ModelEntity getModelEntity() { 264 if (modelEntity == null) { 265 if (entityName != null) modelEntity = this.getDelegator().getModelEntity(entityName); 266 if (modelEntity == null) { 267 throw new IllegalStateException ("[GenericEntity.getModelEntity] could not find modelEntity for entityName " + entityName); 268 } 269 } 270 return modelEntity; 271 } 272 273 276 public GenericDelegator getDelegator() { 277 if (internalDelegator == null) { 278 if (delegatorName == null) delegatorName = "default"; 279 if (delegatorName != null) internalDelegator = GenericDelegator.getGenericDelegator(delegatorName); 280 if (internalDelegator == null) { 281 throw new IllegalStateException ("[GenericEntity.getDelegator] could not find delegator with name " + delegatorName); 282 } 283 } 284 return internalDelegator; 285 } 286 287 288 public void setDelegator(GenericDelegator internalDelegator) { 289 if (internalDelegator == null) return; 290 this.delegatorName = internalDelegator.getDelegatorName(); 291 this.internalDelegator = internalDelegator; 292 } 293 294 public Object get(String name) { 295 if (getModelEntity().getField(name) == null) { 296 throw new IllegalArgumentException ("[GenericEntity.get] \"" + name + "\" is not a field of " + entityName); 297 } 298 return fields.get(name); 299 } 300 301 302 public boolean isPrimaryKey() { 303 return isPrimaryKey(false); 304 } 305 public boolean isPrimaryKey(boolean requireValue) { 306 TreeSet fieldKeys = new TreeSet (this.fields.keySet()); 307 Iterator pkIter = getModelEntity().getPksIterator(); 308 while (pkIter.hasNext()) { 309 ModelField curPk = (ModelField) pkIter.next(); 310 String fieldName = curPk.getName(); 311 if (requireValue) { 312 if (this.fields.get(fieldName) == null) return false; 313 } else { 314 if (!this.fields.containsKey(fieldName)) return false; 315 } 316 fieldKeys.remove(fieldName); 317 } 318 if (!fieldKeys.isEmpty()) return false; 319 return true; 320 } 321 322 323 public boolean containsPrimaryKey() { 324 return containsPrimaryKey(false); 325 } 326 public boolean containsPrimaryKey(boolean requireValue) { 327 Iterator pkIter = getModelEntity().getPksIterator(); 329 while (pkIter.hasNext()) { 330 ModelField curPk = (ModelField) pkIter.next(); 331 String fieldName = curPk.getName(); 332 if (requireValue) { 333 if (this.fields.get(fieldName) == null) return false; 334 } else { 335 if (!this.fields.containsKey(fieldName)) return false; 336 } 337 } 338 return true; 339 } 340 341 345 public void set(String name, Object value) { 346 set(name, value, true); 347 } 348 349 358 public synchronized Object set(String name, Object value, boolean setIfNull) { 359 if (!this.mutable) { 360 throw new IllegalStateException ("This object has been flagged as immutable (unchangeable), probably because it came from an Entity Engine cache. Cannot set a value in an immutable entity object."); 362 } 363 364 ModelField modelField = getModelEntity().getField(name); 365 if (modelField == null) { 366 throw new IllegalArgumentException ("[GenericEntity.set] \"" + name + "\" is not a field of " + entityName + ", must be one of: " + getModelEntity().fieldNameString()); 367 } 368 if (value != null || setIfNull) { 369 ModelFieldType type = null; 370 try { 371 type = getDelegator().getEntityFieldType(getModelEntity(), modelField.getType()); 372 } catch (GenericEntityException e) { 373 Debug.logWarning(e, module); 374 } 375 if (type == null) { 376 throw new IllegalArgumentException ("Type " + modelField.getType() + " not found"); 377 } 378 379 if (value instanceof Boolean ) { 380 try { 382 int fieldType = SqlJdbcUtil.getType(type.getJavaType()); 383 if (fieldType != 9) { 384 value = ((Boolean ) value).booleanValue() ? "Y" : "N"; 385 } 386 } catch (GenericNotImplementedException e) { 387 throw new IllegalArgumentException (e.getMessage()); 388 } 389 } else if (value != null) { 390 if (!ObjectType.instanceOf(value, type.getJavaType())) { 392 String errMsg = "In entity field [" + this.getEntityName() + "." + name + "] set the value passed in [" + value.getClass().getName() + "] is not compatible with the Java type of the field [" + type.getJavaType() + "]"; 393 Debug.logWarning(errMsg, module); 395 } 396 } 397 Object old = fields.put(name, value); 398 399 generateHashCode = true; 400 modified = true; 401 this.setChanged(); 402 this.notifyObservers(name); 403 return old; 404 } else { 405 return fields.get(name); 406 } 407 } 408 409 public void dangerousSetNoCheckButFast(ModelField modelField, Object value) { 410 if (modelField == null) throw new IllegalArgumentException ("Cannot set field with a null modelField"); 411 generateHashCode = true; 412 this.fields.put(modelField.getName(), value); 413 } 414 415 public Object dangerousGetNoCheckButFast(ModelField modelField) { 416 if (modelField == null) throw new IllegalArgumentException ("Cannot get field with a null modelField"); 417 return this.fields.get(modelField.getName()); 418 } 419 420 424 public void setString(String name, String value) { 425 if (value == null) { 426 set(name, null); 427 return; 428 } 429 430 boolean isNullString = false; 431 if ("null".equals(value)) { 432 isNullString = true; 434 } 435 436 ModelField field = getModelEntity().getField(name); 437 if (field == null) set(name, value); 439 ModelFieldType type = null; 440 try { 441 type = getDelegator().getEntityFieldType(getModelEntity(), field.getType()); 442 } catch (GenericEntityException e) { 443 Debug.logWarning(e, module); 444 } 445 if (type == null) throw new IllegalArgumentException ("Type " + field.getType() + " not found"); 446 String fieldType = type.getJavaType(); 447 448 try { 449 switch (SqlJdbcUtil.getType(fieldType)) { 450 case 1: 451 set(name, value); 452 break; 453 454 case 2: 455 set(name, isNullString ? null : java.sql.Timestamp.valueOf(value)); 456 break; 457 458 case 3: 459 set(name, isNullString ? null : java.sql.Time.valueOf(value)); 460 break; 461 462 case 4: 463 set(name, isNullString ? null : java.sql.Date.valueOf(value)); 464 break; 465 466 case 5: 467 set(name, isNullString ? null : Integer.valueOf(value)); 468 break; 469 470 case 6: 471 set(name, isNullString ? null : Long.valueOf(value)); 472 break; 473 474 case 7: 475 set(name, isNullString ? null : Float.valueOf(value)); 476 break; 477 478 case 8: 479 set(name, isNullString ? null : Double.valueOf(value)); 480 break; 481 482 case 9: 483 set(name, isNullString ? null : Boolean.valueOf(value)); 484 break; 485 486 case 10: set(name, isNullString ? null : new BigDecimal (value)); 488 break; 489 490 case 11: set(name, value); 492 break; 493 494 case 12: set(name, value); 497 break; 498 499 case 13: set(name, value); 502 break; 503 504 case 14: set(name, UtilDateTime.toDate(fieldType)); 506 break; 507 508 case 15: set(name, value); 511 break; 512 } 513 } catch (GenericNotImplementedException ex) { 514 throw new IllegalArgumentException (ex.getMessage()); 515 } 516 } 517 518 522 public void setBytes(String name, byte[] bytes) { 523 this.set(name, new ByteWrapper(bytes)); 524 } 525 526 public Boolean getBoolean(String name) { 527 Object obj = get(name); 528 529 if (obj == null) { 530 return null; 531 } 532 if (obj instanceof Boolean ) { 533 return (Boolean ) obj; 534 } else if (obj instanceof String ) { 535 String value = (String ) obj; 536 537 if ("Y".equalsIgnoreCase(value) || "T".equalsIgnoreCase(value)) { 538 return Boolean.TRUE; 539 } else if ("N".equalsIgnoreCase(value) || "F".equalsIgnoreCase(value)) { 540 return Boolean.FALSE; 541 } else { 542 throw new IllegalArgumentException ("getBoolean could not map the String '" + value + "' to Boolean type"); 543 } 544 } else { 545 throw new IllegalArgumentException ("getBoolean could not map the object '" + obj.toString() + "' to Boolean type, unknown object type: " + obj.getClass().getName()); 546 } 547 } 548 549 public String getString(String name) { 550 Object object = get(name); 552 if (object == null) return null; 553 if (object instanceof java.lang.String ) { 554 return (String ) object; 555 } else { 556 return object.toString(); 557 } 558 } 559 560 public java.sql.Timestamp getTimestamp(String name) { 561 return (java.sql.Timestamp ) get(name); 562 } 563 564 public java.sql.Time getTime(String name) { 565 return (java.sql.Time ) get(name); 566 } 567 568 public java.sql.Date getDate(String name) { 569 return (java.sql.Date ) get(name); 570 } 571 572 public Integer getInteger(String name) { 573 return (Integer ) get(name); 574 } 575 576 public Long getLong(String name) { 577 return (Long ) get(name); 578 } 579 580 public Float getFloat(String name) { 581 return (Float ) get(name); 582 } 583 584 public Double getDouble(String name) { 585 Object value = get(name); 587 if (value instanceof BigDecimal ) { 588 return new Double (((BigDecimal ) value).doubleValue()); 589 } else { 590 return (Double ) get(name); 591 } 592 } 593 594 public BigDecimal getBigDecimal(String name) { 595 Object value = get(name); 598 if (value instanceof Double ) { 599 return new BigDecimal (((Double ) value).doubleValue()); 600 } else { 601 return (BigDecimal ) get(name); 602 } 603 } 604 605 public byte[] getBytes(String name) { 606 Object value = get(name); 607 if (value == null) { 608 return null; 609 } 610 if (value instanceof ByteWrapper) { 611 ByteWrapper wrapper = (ByteWrapper) value; 612 return wrapper.getBytes(); 613 } 614 if (value instanceof byte[]) { 615 return (byte[]) value; 616 } 617 throw new IllegalArgumentException ("In call to getBytes the value is not a supported type, should be byte[] or ByteWrapper, is: " + value.getClass().getName()); 619 } 620 621 637 public Object get(String name, Locale locale) { 638 return get(name, null, locale); 639 } 640 641 652 public Object get(String name, String resource, Locale locale) { 653 Object fieldValue = get(name); 654 if (UtilValidate.isEmpty(resource)) { 655 resource = this.getModelEntity().getDefaultResourceName(); 656 if (UtilValidate.isEmpty(resource)) { 658 return fieldValue; 660 } 661 } 662 ResourceBundle bundle = null; 663 try { 664 bundle = UtilProperties.getResourceBundle(resource, locale); 665 } catch (IllegalArgumentException e) { 666 bundle = null; 667 } 668 if (bundle == null) { 669 return fieldValue; 671 } 672 673 StringBuffer keyBuffer = new StringBuffer (); 674 keyBuffer.append(this.getEntityName()); 676 keyBuffer.append('.'); 678 keyBuffer.append(name); 679 Iterator iter = this.getModelEntity().getPksIterator(); 681 while (iter != null && iter.hasNext()) { 682 ModelField curField = (ModelField) iter.next(); 683 keyBuffer.append('.'); 684 keyBuffer.append(this.get(curField.getName())); 685 } 686 687 String bundleKey = keyBuffer.toString(); 688 689 Object resourceValue = null; 690 try { 691 resourceValue = bundle.getObject(bundleKey); 692 } catch (MissingResourceException e) { 693 return fieldValue; 694 } 695 if (resourceValue == null) { 696 return fieldValue; 697 } else { 698 return resourceValue; 699 } 700 } 701 702 public GenericPK getPrimaryKey() { 703 Collection pkNames = FastList.newInstance(); 704 Iterator iter = this.getModelEntity().getPksIterator(); 705 while (iter != null && iter.hasNext()) { 706 ModelField curField = (ModelField) iter.next(); 707 pkNames.add(curField.getName()); 708 } 709 GenericPK newPK = GenericPK.create(getModelEntity(), this.getFields(pkNames)); 710 newPK.setDelegator(this.getDelegator()); 711 return newPK; 712 } 713 714 715 public void setPKFields(Map fields) { 716 setAllFields(fields, true, null, Boolean.TRUE); 717 } 718 719 720 public void setPKFields(Map fields, boolean setIfEmpty) { 721 setAllFields(fields, setIfEmpty, null, Boolean.TRUE); 722 } 723 724 725 public void setNonPKFields(Map fields) { 726 setAllFields(fields, true, null, Boolean.FALSE); 727 } 728 729 730 public void setNonPKFields(Map fields, boolean setIfEmpty) { 731 setAllFields(fields, setIfEmpty, null, Boolean.FALSE); 732 } 733 734 735 741 public void setAllFields(Map fields, boolean setIfEmpty, String namePrefix, Boolean pks) { 742 if (fields == null) { 743 return; 744 } 745 Iterator iter = null; 746 if (pks != null) { 747 if (pks.booleanValue()) { 748 iter = this.getModelEntity().getPksIterator(); 749 } else { 750 iter = this.getModelEntity().getNopksIterator(); 751 } 752 } else { 753 iter = this.getModelEntity().getFieldsIterator(); 754 } 755 756 while (iter != null && iter.hasNext()) { 757 ModelField curField = (ModelField) iter.next(); 758 String fieldName = curField.getName(); 759 String sourceFieldName = null; 760 if (UtilValidate.isNotEmpty(namePrefix)) { 761 sourceFieldName = namePrefix + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); 762 } else { 763 sourceFieldName = curField.getName(); 764 } 765 766 if (fields.containsKey(sourceFieldName)) { 767 Object field = fields.get(sourceFieldName); 768 769 if (setIfEmpty) { 771 if (field != null && field instanceof String && ((String ) field).length() == 0) { 773 this.set(curField.getName(), null); 774 } else { 775 this.set(curField.getName(), field); 776 } 777 } else { 778 if (field != null) { 780 if (field instanceof String ) { 782 String fieldStr = (String ) field; 783 784 if (fieldStr.length() > 0) { 785 this.set(curField.getName(), field); 786 } 787 } else { 788 this.set(curField.getName(), field); 789 } 790 } 791 } 792 } 793 } 794 } 795 796 799 public Collection getAllKeys() { 800 return fields.keySet(); 801 } 802 803 806 public Map getAllFields() { 807 Map newMap = FastMap.newInstance(); 808 newMap.putAll(this.fields); 809 return newMap; 810 } 811 812 816 public Map getFields(Collection keysofFields) { 817 if (keysofFields == null) return null; 818 Iterator keys = keysofFields.iterator(); 819 Object aKey = null; 820 Map aMap = FastMap.newInstance(); 821 822 while (keys.hasNext()) { 823 aKey = keys.next(); 824 aMap.put(aKey, this.fields.get(aKey)); 825 } 826 return aMap; 827 } 828 829 832 public synchronized void setFields(Map keyValuePairs) { 833 if (keyValuePairs == null) return; 834 Iterator entries = keyValuePairs.entrySet().iterator(); 835 Map.Entry anEntry = null; 836 837 while (entries.hasNext()) { 839 anEntry = (Map.Entry ) entries.next(); 840 this.set((String ) anEntry.getKey(), anEntry.getValue(), true); 841 } 842 } 843 844 public boolean matchesFields(Map keyValuePairs) { 845 if (fields == null) return true; 846 if (keyValuePairs == null || keyValuePairs.size() == 0) return true; 847 Iterator entries = keyValuePairs.entrySet().iterator(); 848 849 while (entries.hasNext()) { 850 Map.Entry anEntry = (Map.Entry ) entries.next(); 851 852 if (!UtilValidate.areEqual(anEntry.getValue(), this.fields.get(anEntry.getKey()))) { 853 return false; 854 } 855 } 856 return true; 857 } 858 859 862 public boolean lockEnabled() { 863 return getModelEntity().lock(); 864 } 865 866 public static Document makeXmlDocument(Collection values) { 868 Document document = UtilXml.makeEmptyXmlDocument("entity-engine-xml"); 869 870 if (document == null) return null; 871 872 addToXmlDocument(values, document); 873 return document; 874 } 875 876 public static int addToXmlDocument(Collection values, Document document) { 877 return addToXmlElement(values, document, document.getDocumentElement()); 878 } 879 880 public static int addToXmlElement(Collection values, Document document, Element element) { 881 if (values == null) return 0; 882 if (document == null) return 0; 883 884 Iterator iter = values.iterator(); 885 int numberAdded = 0; 886 887 while (iter.hasNext()) { 888 GenericValue value = (GenericValue) iter.next(); 889 Element valueElement = value.makeXmlElement(document); 890 891 element.appendChild(valueElement); 892 numberAdded++; 893 } 894 return numberAdded; 895 } 896 897 901 public Element makeXmlElement(Document document) { 902 return makeXmlElement(document, null); 903 } 904 905 910 public Element makeXmlElement(Document document, String prefix) { 911 Element element = null; 912 913 if (prefix == null) prefix = ""; 914 if (document != null) element = document.createElement(prefix + this.getEntityName()); 915 if (element == null) return null; 917 918 Iterator modelFields = this.getModelEntity().getFieldsIterator(); 919 while (modelFields.hasNext()) { 920 ModelField modelField = (ModelField) modelFields.next(); 921 String name = modelField.getName(); 922 String value = this.getString(name); 923 924 if (value != null) { 925 if (value.indexOf('\n') >= 0 || value.indexOf('\r') >= 0) { 926 UtilXml.addChildElementCDATAValue(element, name, value, document); 927 } else { 928 element.setAttribute(name, value); 929 } 930 } 931 } 932 933 return element; 934 } 935 936 940 public void writeXmlText(PrintWriter writer, String prefix) { 941 final int indent = 4; 942 943 if (prefix == null) prefix = ""; 944 945 for (int i = 0; i < indent; i++) writer.print(' '); 946 writer.print('<'); 947 writer.print(prefix); 948 writer.print(this.getEntityName()); 949 950 Map cdataMap = FastMap.newInstance(); 952 953 Iterator modelFields = this.getModelEntity().getFieldsIterator(); 954 while (modelFields.hasNext()) { 955 ModelField modelField = (ModelField) modelFields.next(); 956 String name = modelField.getName(); 957 958 String type = modelField.getType(); 959 if (type != null && type.equals("blob")) { 960 Object obj = get(name); 961 boolean b1 = obj instanceof byte []; 962 if (b1) { 963 byte [] binData = (byte [])obj; 964 String strData = new String (Base64.base64Encode(binData)); 965 cdataMap.put(name, strData); 966 } else { 967 Debug.logWarning("Field:" + name + " is not of type 'byte[]'. obj: " + obj, module); 968 } 969 } else { 970 String valueStr = this.getString(name); 971 972 if (valueStr != null) { 973 StringBuffer value = new StringBuffer (valueStr); 974 boolean needsCdata = false; 975 976 for (int i = 0; i < value.length(); i++) { 978 char curChar = value.charAt(i); 979 989 990 switch (curChar) { 991 case '\'': 992 value.replace(i, i+1, "'"); 993 break; 994 case '"': 995 value.replace(i, i+1, """); 996 break; 997 case '&': 998 value.replace(i, i+1, "&"); 999 break; 1000 case '<': 1001 value.replace(i, i+1, "<"); 1002 break; 1003 case '>': 1004 value.replace(i, i+1, ">"); 1005 break; 1006 case 0xA: needsCdata = true; 1008 break; 1009 case 0xD: needsCdata = true; 1011 break; 1012 case 0x9: break; 1015 case 0x5: value.replace(i, i+1, "..."); 1017 break; 1018 case 0x12: value.replace(i, i+1, "'"); 1020 break; 1021 case 0x13: value.replace(i, i+1, """); 1023 break; 1024 case 0x14: value.replace(i, i+1, """); 1026 break; 1027 case 0x16: value.replace(i, i+1, "-"); 1029 break; 1030 case 0x17: value.replace(i, i+1, "-"); 1032 break; 1033 case 0x19: value.replace(i, i+1, "tm"); 1035 break; 1036 default: 1037 if (curChar < 0x20) { 1038 Debug.logInfo("Removing invalid character [" + curChar + "] numeric value [" + (int) curChar + "] for field " + name + " of entity with PK: " + this.getPrimaryKey().toString(), module); 1040 value.deleteCharAt(i); 1041 } 1042 } 1043 } 1044 1045 if (needsCdata) { 1046 cdataMap.put(name, valueStr); 1048 } else { 1049 writer.print(' '); 1050 writer.print(name); 1051 writer.print("=\""); 1052 writer.print(value.toString()); 1054 writer.print("\""); 1055 } 1056 } 1057 } 1058 } 1059 1060 if (cdataMap.size() == 0) { 1061 writer.println("/>"); 1062 } else { 1063 writer.println('>'); 1064 1065 Iterator cdataIter = cdataMap.entrySet().iterator(); 1066 1067 while (cdataIter.hasNext()) { 1068 Map.Entry entry = (Map.Entry ) cdataIter.next(); 1069 1070 for (int i = 0; i < (indent << 1); i++) writer.print(' '); 1071 writer.print('<'); 1072 writer.print((String ) entry.getKey()); 1073 writer.print("><![CDATA["); 1074 writer.print((String ) entry.getValue()); 1075 writer.print("]]></"); 1076 writer.print((String ) entry.getKey()); 1077 writer.println('>'); 1078 } 1079 1080 for (int i = 0; i < indent; i++) writer.print(' '); 1082 writer.print("</"); 1083 writer.print(this.getEntityName()); 1084 writer.println(">"); 1085 } 1086 } 1087 1088 1092 public boolean equals(Object obj) { 1093 if (obj == null) return false; 1094 1095 try { 1097 return this.compareTo(obj) == 0; 1098 } catch (ClassCastException e) { 1099 return false; 1100 } 1101 } 1102 1103 1106 public int hashCode() { 1107 if (generateHashCode) { 1109 cachedHashCode = 0; 1110 if (getEntityName() != null) { 1111 cachedHashCode += getEntityName().hashCode() >> 1; 1112 } 1113 cachedHashCode += fields.hashCode() >> 1; 1114 generateHashCode = false; 1115 } 1116 return cachedHashCode; 1117 } 1118 1119 1122 public String toString() { 1123 StringBuffer theString = new StringBuffer (); 1124 1125 theString.append("[GenericEntity:"); 1126 theString.append(getEntityName()); 1127 theString.append(']'); 1128 1129 Iterator keyNames = new TreeSet (fields.keySet()).iterator(); 1130 while (keyNames.hasNext()) { 1131 String curKey = (String ) keyNames.next(); 1132 Object curValue = fields.get(curKey); 1133 theString.append('['); 1134 theString.append(curKey); 1135 theString.append(','); 1136 theString.append(curValue); 1137 theString.append('('); 1138 theString.append(curValue != null ? curValue.getClass().getName() : ""); 1139 theString.append(')'); 1140 theString.append(']'); 1141 } 1142 return theString.toString(); 1143 } 1144 1145 1149 public int compareTo(Object obj) { 1150 if (obj == null) return -1; 1152 1153 GenericEntity that = (GenericEntity) obj; 1157 1158 int tempResult = this.entityName.compareTo(that.entityName); 1159 1160 if (tempResult != 0) return tempResult; 1162 1163 Iterator pkIter = getModelEntity().getPksIterator(); 1165 while (pkIter.hasNext()) { 1166 ModelField curField = (ModelField) pkIter.next(); 1167 Comparable thisVal = (Comparable ) this.fields.get(curField.getName()); 1168 Comparable thatVal = (Comparable ) that.fields.get(curField.getName()); 1169 1170 if (thisVal == null) { 1171 if (thatVal == null) 1172 tempResult = 0; 1173 else 1175 tempResult = 1; 1176 } else { 1177 if (thatVal == null) 1179 tempResult = -1; 1180 else 1181 tempResult = thisVal.compareTo(thatVal); 1182 } 1183 if (tempResult != 0) return tempResult; 1184 } 1185 1186 Iterator nopkIter = getModelEntity().getNopksIterator(); 1188 while (nopkIter.hasNext()) { 1189 ModelField curField = (ModelField) nopkIter.next(); 1190 if (!curField.getIsAutoCreatedInternal()) { 1191 Comparable thisVal = (Comparable ) this.fields.get(curField.getName()); 1192 Comparable thatVal = (Comparable ) that.fields.get(curField.getName()); 1193 1194 if (thisVal == null) { 1195 if (thatVal == null) { 1196 tempResult = 0; 1197 } else { 1199 tempResult = 1; 1200 } 1201 } else { 1202 if (thatVal == null) { 1204 tempResult = -1; 1205 } else { 1206 tempResult = thisVal.compareTo(thatVal); 1207 } 1208 } 1209 if (tempResult != 0) return tempResult; 1210 } 1211 } 1212 1213 return tempResult; 1215 } 1216 1217 1220 public Object clone() { 1221 GenericEntity newEntity = new GenericEntity(); 1222 newEntity.init(this); 1223 1224 newEntity.setDelegator(internalDelegator); 1225 return newEntity; 1226 } 1227 1228 1230 public Object remove(Object key) { 1231 return this.fields.remove(key); 1232 } 1233 1234 public boolean containsKey(Object key) { 1235 return this.fields.containsKey(key); 1236 } 1237 1238 public java.util.Set entrySet() { 1239 return Collections.unmodifiableSet(this.fields.entrySet()); 1240 } 1241 1242 public Object put(Object key, Object value) { 1243 return this.set((String ) key, value, true); 1244 } 1245 1246 public void putAll(java.util.Map map) { 1247 this.setFields(map); 1248 } 1249 1250 public void clear() { 1251 this.fields.clear(); 1252 } 1253 1254 public Object get(Object key) { 1255 try { 1256 return this.get((String ) key); 1257 } catch (IllegalArgumentException e) { 1258 Debug.logWarning(e, "The field name (or key) [" + key + "] is not valid, printing IllegalArgumentException instead of throwing it because Map interface specification does not allow throwing that exception.", module); 1259 return null; 1260 } 1261 } 1262 1263 public java.util.Set keySet() { 1264 return Collections.unmodifiableSet(this.fields.keySet()); 1265 } 1266 1267 public boolean isEmpty() { 1268 return this.fields.isEmpty(); 1269 } 1270 1271 public java.util.Collection values() { 1272 return Collections.unmodifiableCollection(this.fields.values()); 1273 } 1274 1275 public boolean containsValue(Object value) { 1276 return this.fields.containsValue(value); 1277 } 1278 1279 public int size() { 1280 return this.fields.size(); 1281 } 1282 1283 public boolean matches(EntityCondition condition) { 1284 return condition.entityMatches(this); 1285 } 1286 1287 public static interface NULL { 1288 } 1289 1290 public static class NullGenericEntity extends GenericEntity implements NULL { 1291 protected NullGenericEntity() { } 1292 1293 public String toString() { 1294 return "[null-entity]"; 1295 } 1296 } 1297 1298 public static class NullField implements NULL { 1299 protected NullField() { } 1300 1301 public String toString() { 1302 return "[null-field]"; 1303 } 1304 } 1305} 1306 | Popular Tags |