1 56 package org.objectstyle.cayenne.service; 57 58 import java.util.ArrayList ; 59 import java.util.Iterator ; 60 import java.util.List ; 61 62 import org.apache.commons.beanutils.PropertyUtils; 63 import org.objectstyle.cayenne.CayenneDataObject; 64 import org.objectstyle.cayenne.CayenneRuntimeException; 65 import org.objectstyle.cayenne.Persistent; 66 import org.objectstyle.cayenne.map.EntityResolver; 67 import org.objectstyle.cayenne.map.ObjEntity; 68 69 73 class ClientServerUtils { 76 77 static Object toClientObject(EntityResolver resolver, CayenneDataObject object) 78 throws Exception { 79 ObjEntity entity = object.getObjEntity(); 80 81 if (entity.getClientClassName() == null) { 83 throw new CayenneRuntimeException( 84 "No client-side class defined for ObjEntity: " + entity.getName()); 85 } 86 87 Class clientClass = Class.forName(entity.getClientClassName(), true, Thread 88 .currentThread() 89 .getContextClassLoader()); 90 91 Object clientObject = clientClass.newInstance(); 92 93 if (clientObject instanceof Persistent) { 95 ((Persistent) clientObject).setOid(resolver.convertToGlobalID(object.getObjectId())); 96 } 97 98 101 Iterator it = entity.getAttributeMap().keySet().iterator(); 102 while (it.hasNext()) { 103 String key = (String ) it.next(); 104 PropertyUtils.setProperty(clientObject, key, object.readProperty(key)); 105 } 106 107 return clientObject; 108 } 109 110 113 static List toClientObjects(EntityResolver resolver, List dataObjects) 114 throws Exception { 115 List clientObjects = new ArrayList (dataObjects.size()); 116 117 Iterator it = dataObjects.iterator(); 118 while (it.hasNext()) { 119 CayenneDataObject serverObject = (CayenneDataObject) it.next(); 120 121 124 clientObjects.add(toClientObject(resolver, serverObject)); 125 } 126 127 return clientObjects; 128 } 129 130 private ClientServerUtils() { 131 132 } 133 } | Popular Tags |