1 56 package org.objectstyle.cayenne.access; 57 58 import java.util.ArrayList ; 59 import java.util.Iterator ; 60 import java.util.List ; 61 62 import org.objectstyle.cayenne.CayenneRuntimeException; 63 import org.objectstyle.cayenne.DataObject; 64 import org.objectstyle.cayenne.DataRow; 65 import org.objectstyle.cayenne.ObjectFactory; 66 import org.objectstyle.cayenne.ObjectId; 67 import org.objectstyle.cayenne.PersistenceState; 68 import org.objectstyle.cayenne.map.DbEntity; 69 import org.objectstyle.cayenne.map.EntityInheritanceTree; 70 import org.objectstyle.cayenne.map.ObjEntity; 71 72 78 public class DataContextObjectFactory implements ObjectFactory { 81 82 DataContext context; 83 boolean refreshObjects; 84 boolean resolveInheritance; 85 86 public DataContextObjectFactory(DataContext context, boolean refreshObjects, 87 boolean resolveInheritance) { 88 this.context = context; 89 this.refreshObjects = refreshObjects; 90 this.resolveInheritance = resolveInheritance; 91 } 92 93 public List objectsFromDataRows(ObjEntity entity, List rows) { 94 if (rows == null || rows.size() == 0) { 95 return new ArrayList (1); 96 } 97 98 DbEntity dbEntity = entity.getDbEntity(); 101 if (dbEntity == null) { 102 throw new CayenneRuntimeException("ObjEntity '" 103 + entity.getName() 104 + "' has no DbEntity."); 105 } 106 107 if (dbEntity.getPrimaryKey().size() == 0) { 108 throw new CayenneRuntimeException("Can't create ObjectId for '" 109 + entity.getName() 110 + "'. Reason: DbEntity '" 111 + dbEntity.getName() 112 + "' has no Primary Key defined."); 113 } 114 115 EntityInheritanceTree tree = null; 117 if (resolveInheritance) { 118 tree = context.getEntityResolver().lookupInheritanceTree(entity); 119 } 120 121 List results = new ArrayList (rows.size()); 122 Iterator it = rows.iterator(); 123 124 synchronized (context.getObjectStore()) { 126 synchronized (context.getObjectStore().getDataRowCache()) { 127 while (it.hasNext()) { 128 DataRow dataRow = (DataRow) it.next(); 129 130 ObjEntity objectEntity = entity; 132 133 if (tree != null) { 134 objectEntity = tree.entityMatchingRow(dataRow); 135 136 if (objectEntity == null) { 138 objectEntity = entity; 139 } 140 } 141 142 ObjectId anId = dataRow.createObjectId(objectEntity); 143 144 DataObject object = context.registeredObject(anId); 146 147 int state = object.getPersistenceState(); 149 switch (state) { 150 case PersistenceState.COMMITTED: 151 case PersistenceState.MODIFIED: 152 case PersistenceState.DELETED: 153 if (!refreshObjects) { 155 break; 156 } 157 case PersistenceState.HOLLOW: 158 DataRowUtils.mergeObjectWithSnapshot(objectEntity, 159 object, 160 dataRow); 161 default: 162 break; 163 } 164 165 object.setSnapshotVersion(dataRow.getVersion()); 166 object.fetchFinished(); 167 results.add(object); 168 } 169 170 context.getObjectStore().snapshotsUpdatedForObjects(results, 172 rows, 173 refreshObjects); 174 } 175 } 176 177 return results; 178 } 179 180 } 181 | Popular Tags |