1 29 package com.genimen.djeneric.repository.util; 30 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 34 import com.genimen.djeneric.language.Messages; 35 import com.genimen.djeneric.repository.DjAssociation; 36 import com.genimen.djeneric.repository.DjDomain; 37 import com.genimen.djeneric.repository.DjExtent; 38 import com.genimen.djeneric.repository.DjList; 39 import com.genimen.djeneric.repository.DjObject; 40 import com.genimen.djeneric.repository.DjProperty; 41 import com.genimen.djeneric.repository.DjRelation; 42 import com.genimen.djeneric.repository.DjUid; 43 import com.genimen.djeneric.repository.exceptions.CatalogException; 44 import com.genimen.djeneric.repository.exceptions.DjenericException; 45 import com.genimen.djeneric.structure.ExtentUsage; 46 import com.genimen.djeneric.structure.RelationUsage; 47 import com.genimen.djeneric.util.DjBase64; 48 import com.genimen.djeneric.util.DjStatusDisplayer; 49 50 public class DjObjectExporter 51 { 52 DjList _exportList = new DjList(); 53 DjList _masters = new DjList(); 54 HashMap _object2structure = new HashMap (); 55 HashMap _structureKeys = new HashMap (); 56 DjStatusDisplayer _statusDisplayer = null; 57 58 int _currentKey = 1; 59 60 public DjObjectExporter() 61 { 62 } 63 64 public DjObjectExporter(DjObject source) 65 { 66 markForExport(source); 67 } 68 69 public String export() throws DjenericException 70 { 71 StringBuffer result = new StringBuffer (2000); 72 73 result.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<djenericexport>\n"); 74 HashMap uids = new HashMap (); 75 HashMap hitlist = new HashMap (); 76 77 Iterator it = _exportList.iterator(); 78 while (it.hasNext()) 79 result.append(doExportSingle((DjObject) it.next(), uids, hitlist)); 80 81 result.append(writeUids(uids)); 82 result.append(writeStructures(_structureKeys)); 83 result.append("</djenericexport>"); 84 setStatus(Messages.getString("ObjectExporter.ExportDone", String.valueOf(_exportList.size()))); 85 return result.toString(); 86 } 87 88 public void markForExport(DjObject source) 89 { 90 if (!_exportList.contains(source)) 91 { 92 _exportList.add(source); 93 _masters.add(source); 94 } 95 } 96 97 public void markForExport(DjObject source, ExtentUsage usage) throws DjenericException 98 { 99 _masters.add(source); 100 _object2structure.put(source, usage); 101 if (!_structureKeys.containsKey(usage)) _structureKeys.put(usage, new Integer (_currentKey++)); 102 doMarkForExport(source, usage); 103 } 104 105 protected void doMarkForExport(DjObject source, ExtentUsage usage) throws DjenericException 106 { 107 if (!_exportList.contains(source)) 108 { 109 _exportList.add(source); 110 111 RelationUsage[] rels = usage.getDetailRelations(); 112 for (int i = 0; i < rels.length; i++) 113 { 114 DjRelation rel = rels[i].getRelation(); 115 DjExtent detailExtent = rels[i].getDetail().getExtent(); 116 DjAssociation srcAssoc = source.getDetailAssociationByName(rel.getName()); 117 118 DjList details = srcAssoc.getObjects(); 119 for (int d = 0; d < details.size(); d++) 120 { 121 DjObject detailObject = details.getDjenericObjectAt(d); 122 if (!detailObject.isInstanceOf(detailExtent)) continue; 124 125 doMarkForExport(detailObject, rels[i].getDetail()); 126 } 127 } 128 } 129 } 130 131 public void clear() 132 { 133 _exportList = new DjList(); 134 _masters = new DjList(); 135 _object2structure = new HashMap (); 136 _structureKeys = new HashMap (); 137 _currentKey = 1; 138 } 139 140 protected String doExportSingle(DjObject source, HashMap uids, HashMap hitlist) throws DjenericException 141 { 142 if (hitlist.containsKey(source)) return ""; 143 hitlist.put(source, source); 144 145 StringBuffer result = new StringBuffer (2000); 146 147 result.append(writeObject(source, uids)); 148 return result.toString(); 149 } 150 151 protected String writeUids(HashMap uids) throws CatalogException 152 { 153 StringBuffer result = new StringBuffer (1000); 154 Iterator it = uids.keySet().iterator(); 155 while (it.hasNext()) 156 { 157 Object object = it.next(); 158 DjUid uid = (DjUid) uids.get(object); 159 if (uid.getPropertyCount() == 0) throw new CatalogException(Messages.getString("DjPersistenceManager.NoUid", uid 160 .getExtent().getName())); 161 result.append(uid.toString(2)); 162 result.append("\n"); 163 } 164 return result.toString(); 165 } 166 167 protected String writeStructures(HashMap structures) 168 { 169 StringBuffer result = new StringBuffer (1000); 170 Iterator it = structures.keySet().iterator(); 171 while (it.hasNext()) 172 { 173 ExtentUsage usg = (ExtentUsage) it.next(); 174 result.append(" <structure extent=\"" + usg.getExtent().getName() + "\" id=\"" + _structureKeys.get(usg) 175 + "\">\n"); 176 result.append(usg.getRelationsAsXml(4)); 177 result.append(" </structure>\n"); 178 } 179 return result.toString(); 180 } 181 182 protected String writeObject(DjObject source, HashMap uids) throws DjenericException 183 { 184 setStatus(Messages 185 .getString("ObjectExporter.ExportExtent", source.getExtent().getNameSingular(), source.toString())); 186 if (source.getExtent().hasUidDefined()) 187 { 188 uids.put(source, source.getUID()); 189 } 190 191 StringBuffer result = new StringBuffer (1000); 192 result.append(" <object extent=\"" + source.getExtent().getName() + "\" id=\"" + source.getObjectId() + "\""); 193 if (_masters.contains(source)) 194 { 195 if (!source.getExtent().hasUidDefined()) throw new CatalogException(Messages 196 .getString("DjPersistenceManager.NoUid", source.getExtent().getName())); 197 result.append(" rootobject=\"true\""); 198 } 199 ExtentUsage usg = (ExtentUsage) _object2structure.get(source); 200 if (usg != null) result.append(" structure=\"" + _structureKeys.get(usg) + "\""); 201 202 result.append(">\n"); 203 result.append(writeSimpleProperties(source)); 204 result.append(writeFKProperties(source, uids)); 205 result.append(writeTextProperties(source)); 206 result.append(" </object>\n"); 207 return result.toString(); 208 } 209 210 protected String writeSimpleProperties(DjObject source) throws DjenericException 211 { 212 StringBuffer result = new StringBuffer (1000); 213 DjExtent extent = source.getExtent(); 214 215 for (int i = 0; i < extent.getPropertyCount(); i++) 216 { 217 DjProperty prop = extent.getProperty(i); 218 if (isSimpleProperty(prop)) 219 { 220 if (prop == extent.getIdProperty()) continue; 221 222 String value = source.getString(prop.getName()); 223 if (value != null) result.append(" <property name=\"" + prop.getName() + "\" type=\"simple\">" + value 224 + "</property>\n"); 225 } 226 } 227 return result.toString(); 228 } 229 230 protected String writeFKProperties(DjObject source, HashMap uids) throws DjenericException 231 { 232 StringBuffer result = new StringBuffer (1000); 233 DjExtent extent = source.getExtent(); 234 235 for (int i = 0; i < extent.getPropertyCount(); i++) 236 { 237 DjProperty prop = extent.getProperty(i); 238 if (isFKProperty(prop)) 239 { 240 DjObject master = (DjObject) source.get(prop.getName()); 241 if (master != null) 242 { 243 DjUid uid = master.getUID(); 244 result.append(" <property name=\"" + prop.getName() + "\" type=\"uidptr\">" + uid.getAssociatedObjectid() 245 + "</property>\n"); 246 uids.put(master, uid); 247 } 248 } 249 } 250 return result.toString(); 251 } 252 253 protected String writeTextProperties(DjObject source) throws DjenericException 254 { 255 StringBuffer result = new StringBuffer (1000); 256 DjExtent extent = source.getExtent(); 257 258 for (int i = 0; i < extent.getPropertyCount(); i++) 259 { 260 DjProperty prop = extent.getProperty(i); 261 if (isTextProperty(prop)) 262 { 263 String value = source.getString(prop.getName()); 264 if (value != null) result.append(" <property name=\"" + prop.getName() + "\" type=\"text\"><![CDATA[" 265 + value + "]]></property>\n"); 266 } 267 } 268 return result.toString(); 269 } 270 271 protected String writeByteProperties(DjObject source, DjExtent extent) throws DjenericException 272 { 273 StringBuffer result = new StringBuffer (1000); 274 275 for (int i = 0; i < extent.getPropertyCount(); i++) 276 { 277 DjProperty prop = extent.getProperty(i); 278 if (isByteProperty(prop)) 279 { 280 byte[] bytes = source.getBytes(prop.getName()); 281 if (bytes != null) 282 { 283 String value = DjBase64.encode(source.getBytes(prop.getName())); 284 if (value != null) result.append(" <property name=\"" + prop.getName() + "\" type=\"byte\"><![CDATA[" 285 + value + "]]></property>\n"); 286 } 287 } 288 } 289 return result.toString(); 290 } 291 292 protected boolean isSimpleProperty(DjProperty prop) 293 { 294 if (prop.getType() instanceof DjDomain) 295 { 296 return prop.getTypeCode() == DjDomain.BIGDECIMAL_TYPE || prop.getTypeCode() == DjDomain.DATE_TYPE 297 || prop.getTypeCode() == DjDomain.INT_TYPE || prop.getTypeCode() == DjDomain.LONG_TYPE; 298 } 299 return false; 300 } 301 302 protected boolean isByteProperty(DjProperty prop) 303 { 304 if (prop.getType() instanceof DjDomain) 305 { 306 return prop.getTypeCode() == DjDomain.BYTE_TYPE; 307 } 308 return false; 309 } 310 311 protected boolean isFKProperty(DjProperty prop) 312 { 313 return prop.getType() instanceof DjExtent; 314 } 315 316 protected boolean isTextProperty(DjProperty prop) 317 { 318 return !isSimpleProperty(prop) && !isFKProperty(prop) && !isByteProperty(prop); 319 } 320 321 protected void setStatus(String msg) 322 { 323 if (_statusDisplayer != null) _statusDisplayer.setStatusMessageNow(msg, true); 324 } 325 326 public DjStatusDisplayer getStatusDisplayer() 327 { 328 return _statusDisplayer; 329 } 330 331 public void setStatusDisplayer(DjStatusDisplayer statusDisplayer) 332 { 333 _statusDisplayer = statusDisplayer; 334 } 335 336 } | Popular Tags |