1 27 package com.genimen.djeneric.repository; 28 29 import java.util.ArrayList ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 33 import com.genimen.djeneric.repository.exceptions.DjenericException; 34 35 public class DjUid 36 { 37 DjExtent _extent; 38 HashMap _properties; 39 long _associatedObjectid; 40 String _descriptor; 41 42 public DjUid(DjExtent extent) 43 { 44 _extent = extent; 45 _properties = new HashMap (); 46 } 47 48 public void setProperty(String propertyName, String value) throws DjenericException 49 { 50 if (!_extent.getProperty(propertyName).isPartOfUID()) throw new DjenericException("Property " + propertyName 51 + " is not part of the UID"); 52 _properties.put(propertyName, value); 53 } 54 55 public void setProperty(String propertyName, DjUid value) throws DjenericException 56 { 57 DjProperty prop = _extent.getProperty(propertyName); 58 if (!prop.isPartOfUID()) throw new DjenericException("Property " + propertyName + " is not part of the UID"); 59 if (!(prop.getType() instanceof DjExtent)) throw new DjenericException("Property " + propertyName 60 + " is a link to a master"); 61 _properties.put(propertyName, value); 62 } 63 64 public int getPropertyCount() 65 { 66 return _properties.size(); 67 } 68 69 public String [] getPropertyNames() 70 { 71 ArrayList lst = new ArrayList (); 72 Iterator it = _properties.keySet().iterator(); 73 while (it.hasNext()) 74 lst.add(it.next()); 75 return (String []) lst.toArray(new String [0]); 76 } 77 78 public boolean isUidProperty(String propertyName) 79 { 80 return _properties.get(propertyName) instanceof DjUid; 81 } 82 83 public String getString(String propertyName) 84 { 85 return _properties.get(propertyName).toString(); 86 } 87 88 public DjUid getUid(String propertyName) 89 { 90 return (DjUid) _properties.get(propertyName); 91 } 92 93 public long getAssociatedObjectid() 94 { 95 return _associatedObjectid; 96 } 97 98 public void setAssociatedObjectid(long objectid) 99 { 100 _associatedObjectid = objectid; 101 } 102 103 public String getDescriptor() 104 { 105 return _descriptor; 106 } 107 108 public void setDescriptor(String descriptor) 109 { 110 _descriptor = descriptor; 111 } 112 113 public String toString() 114 { 115 return toString(0); 116 } 117 118 public String toString(int indent) 119 { 120 StringBuffer result = new StringBuffer (500); 121 StringBuffer indentStr = new StringBuffer (20); 122 123 for (int i = 0; i < indent; i++) 124 indentStr.append(" "); 125 126 result.append(indentStr.toString()); 127 result.append("<uid extent=\"" + _extent.getName() + "\" id=\"" + getAssociatedObjectid() + "\">\n"); 128 if (getDescriptor() != null) 129 { 130 result.append(indentStr.toString()); 131 result.append(" <descr><![CDATA[" + getDescriptor() + "]]></descr>\n"); 132 } 133 Iterator it = _properties.keySet().iterator(); 134 while (it.hasNext()) 135 { 136 String key = it.next().toString(); 137 Object value = _properties.get(key); 138 result.append(indentStr.toString()); 139 result.append(" <property name=\"" + key + "\" type=\""); 140 141 if (value instanceof DjUid) 142 { 143 result.append("uid\">\n" + ((DjUid) value).toString(indent + 4) + "\n" + indentStr.toString() 144 + " </property>\n"); 145 } 146 else 147 { 148 if (value != null) result.append("text\"><![CDATA[" + value.toString() + "]]></property>\n"); 149 } 150 } 151 result.append(indentStr.toString()); 152 result.append("</uid>"); 153 return result.toString(); 154 } 155 156 public DjExtent getExtent() 157 { 158 return _extent; 159 } 160 161 public void setExtent(DjExtent extent) 162 { 163 _extent = extent; 164 } 165 166 public boolean equals(Object obj) 167 { 168 if (!(obj instanceof DjUid)) return false; 169 DjUid other = (DjUid) obj; 170 171 if (getPropertyCount() != other.getPropertyCount()) return false; 172 173 String [] names = getPropertyNames(); 174 for (int i = 0; i < names.length; i++) 175 { 176 Object v1 = _properties.get(names[i]); 177 Object v2 = other._properties.get(names[i]); 178 if (v1 == null && v2 != null) return false; 179 if (v1 != null && v2 == null) return false; 180 if (!v1.equals(v2)) return false; 181 } 182 return true; 183 } 184 185 public int hashCode() 186 { 187 int result = 0; 188 189 String [] names = getPropertyNames(); 190 for (int i = 0; i < names.length; i++) 191 { 192 Object o = _properties.get(names[i]); 193 if (o != null) result += o.hashCode(); 194 } 195 return result; 196 } 197 198 } 199 | Popular Tags |