1 29 package com.genimen.djeneric.repository.util; 30 31 import java.util.ArrayList ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 35 import com.genimen.djeneric.repository.DjAssociation; 36 import com.genimen.djeneric.repository.DjExtent; 37 import com.genimen.djeneric.repository.DjList; 38 import com.genimen.djeneric.repository.DjObject; 39 import com.genimen.djeneric.repository.DjProperty; 40 import com.genimen.djeneric.repository.DjRelation; 41 import com.genimen.djeneric.repository.exceptions.DjenericException; 42 import com.genimen.djeneric.structure.ExtentUsage; 43 import com.genimen.djeneric.structure.RelationUsage; 44 45 public class DjObjectCloner 46 { 47 48 public DjObject clone(DjObject source, ExtentUsage usage) throws DjenericException 49 { 50 HashMap source2dest = new HashMap (); 51 ArrayList createdList = new ArrayList (); 52 DjObject result = doClone(source, usage, source2dest, createdList); 53 54 59 Iterator it = createdList.iterator(); 60 while (it.hasNext()) 61 { 62 DjObject created = (DjObject) it.next(); 63 DjProperty props[] = created.getExtent().getProperties(); 64 for (int i = 0; i < props.length; i++) 65 { 66 if (props[i].getType() instanceof DjExtent) 67 { 68 Long newId = (Long ) source2dest.get(new Long (created.getLong(props[i].getName()))); 69 if (newId != null) 70 { 71 created.setLong(props[i].getName(), newId); 72 } 73 } 74 } 75 } 76 77 return result; 78 } 79 80 protected DjObject doClone(DjObject source, ExtentUsage usage, HashMap idMap, ArrayList created) 81 throws DjenericException 82 { 83 DjObject result = source.shallowCopy(); 84 created.add(result); 85 86 idMap.put(new Long (source.getObjectId()), new Long (result.getObjectId())); 87 88 RelationUsage[] rels = usage.getDetailRelations(); 89 for (int i = 0; i < rels.length; i++) 90 { 91 DjRelation rel = rels[i].getRelation(); 92 DjExtent detailExtent = rels[i].getDetail().getExtent(); 93 DjAssociation srcAssoc = source.getDetailAssociationByName(rel.getName()); 94 DjAssociation destAssoc = result.getDetailAssociationByName(rel.getName()); 95 96 DjList details = srcAssoc.getObjects(); 97 for (int d = 0; d < details.size(); d++) 98 { 99 DjObject detailObject = details.getDjenericObjectAt(d); 100 if (!detailObject.isInstanceOf(detailExtent)) continue; 102 destAssoc.add(doClone(detailObject, rels[i].getDetail(), idMap, created)); 103 } 104 } 105 106 return result; 107 } 108 109 } | Popular Tags |