cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r630462 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/map/ test/java/org/apache/cayenne/map/
Date Sat, 23 Feb 2008 16:09:46 GMT
Author: aadamchik
Date: Sat Feb 23 08:09:45 2008
New Revision: 630462

URL: http://svn.apache.org/viewvc?rev=630462&view=rev
Log:
CAY-809 Support for explicit mapping of SQL results with SQLResultSetMapping
(DataMap support)

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/DataMap.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/EntityResolver.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/MappingNamespace.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/MockMappingNamespace.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/DataMap.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/DataMap.java?rev=630462&r1=630461&r2=630462&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/DataMap.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/DataMap.java
Sat Feb 23 08:09:45 2008
@@ -42,6 +42,7 @@
 import org.apache.cayenne.project.Project;
 import org.apache.cayenne.query.NamedQuery;
 import org.apache.cayenne.query.Query;
+import org.apache.cayenne.query.SQLResultSetMapping;
 import org.apache.cayenne.util.Util;
 import org.apache.cayenne.util.XMLEncoder;
 import org.apache.cayenne.util.XMLSerializable;
@@ -76,7 +77,7 @@
 
     /**
      * Defines the name of the property for default client Java superclass.
-     *
+     * 
      * @since 3.0
      */
     public static final String DEFAULT_CLIENT_SUPERCLASS_PROPERTY = "defaultClientSuperclass";
@@ -127,6 +128,7 @@
     private SortedMap<String, DbEntity> dbEntityMap;
     private SortedMap<String, Procedure> procedureMap;
     private SortedMap<String, Query> queryMap;
+    private SortedMap<String, SQLResultSetMapping> resultSetMappings;
 
     private List<EntityListener> defaultEntityListeners;
 
@@ -151,6 +153,7 @@
         procedureMap = new TreeMap<String, Procedure>();
         queryMap = new TreeMap<String, Query>();
         defaultEntityListeners = new ArrayList<EntityListener>(3);
+        resultSetMappings = new TreeMap<String, SQLResultSetMapping>();
 
         setName(mapName);
         initWithProperties(properties);
@@ -187,7 +190,8 @@
                 .equalsIgnoreCase(clientEntities.toString()) : false;
         this.defaultClientPackage = (clientPackageName != null) ? clientPackageName
                 .toString() : null;
-        this.defaultClientSuperclass = (clientSuperclass != null) ? clientSuperclass.toString()
: null;
+        this.defaultClientSuperclass = (clientSuperclass != null) ? clientSuperclass
+                .toString() : null;
     }
 
     /**
@@ -276,7 +280,9 @@
         }
 
         if (!Util.isEmptyString(defaultClientSuperclass)) {
-            encoder.printProperty(DEFAULT_CLIENT_SUPERCLASS_PROPERTY, defaultClientSuperclass);
+            encoder.printProperty(
+                    DEFAULT_CLIENT_SUPERCLASS_PROPERTY,
+                    defaultClientSuperclass);
         }
 
         // embeddables
@@ -306,7 +312,9 @@
     }
 
     // stores relationships for the map of entities
-    private final void encodeDBRelationshipsAsXML(Map<String, DbEntity> entityMap,
XMLEncoder encoder) {
+    private final void encodeDBRelationshipsAsXML(
+            Map<String, DbEntity> entityMap,
+            XMLEncoder encoder) {
         for (Entity entity : entityMap.values()) {
             for (Relationship relationship : entity.getRelationships()) {
                 // filter out synthetic
@@ -318,7 +326,9 @@
     }
 
     // stores relationships for the map of entities
-    private final void encodeOBJRelationshipsAsXML(Map<String, ObjEntity> entityMap,
XMLEncoder encoder) {
+    private final void encodeOBJRelationshipsAsXML(
+            Map<String, ObjEntity> entityMap,
+            XMLEncoder encoder) {
         for (ObjEntity entity : entityMap.values()) {
             for (Relationship relationship : entity.getDeclaredRelationships()) {
                 // filter out synthetic
@@ -468,6 +478,13 @@
     }
 
     /**
+     * @since 3.0
+     */
+    public void clearResultSetMappings() {
+        resultSetMappings.clear();
+    }
+
+    /**
      * @since 1.1
      */
     public void clearQueries() {
@@ -543,6 +560,34 @@
     }
 
     /**
+     * @since 3.0
+     */
+    public void addResultSetMapping(SQLResultSetMapping resultSetMapping) {
+        if (resultSetMapping == null) {
+            throw new NullPointerException("Null resultSetMapping");
+        }
+
+        if (resultSetMapping.getName() == null) {
+            throw new NullPointerException(
+                    "Attempt to add resultSetMapping with no name.");
+        }
+
+        Object existing = resultSetMappings.get(resultSetMapping.getName());
+        if (existing != null) {
+            if (existing == resultSetMapping) {
+                return;
+            }
+            else {
+                throw new IllegalArgumentException(
+                        "An attempt to override resultSetMapping '"
+                                + resultSetMapping.getName());
+            }
+        }
+
+        resultSetMappings.put(resultSetMapping.getName(), resultSetMapping);
+    }
+
+    /**
      * Adds a new ObjEntity to this DataMap.
      */
     public void addObjEntity(ObjEntity entity) {
@@ -614,6 +659,20 @@
     public Collection<Embeddable> getEmbeddables() {
         return Collections.unmodifiableCollection(embeddablesMap.values());
     }
+    
+    /**
+     * @since 3.0
+     */
+    public Map<String, SQLResultSetMapping> getResultSetMappingsMap() {
+        return Collections.unmodifiableMap(resultSetMappings);
+    }
+    
+    /**
+     * @since 3.0
+     */
+    public Collection<SQLResultSetMapping> getResultSetMappings() {
+        return Collections.unmodifiableCollection(resultSetMappings.values());
+    }
 
     /**
      * @since 3.0
@@ -628,6 +687,18 @@
     }
 
     /**
+     * @since 3.0
+     */
+    public SQLResultSetMapping getResultSetMapping(String name) {
+        SQLResultSetMapping rsMapping = resultSetMappings.get(name);
+        if (rsMapping != null) {
+            return rsMapping;
+        }
+
+        return namespace != null ? namespace.getResultSetMapping(name) : null;
+    }
+
+    /**
      * Returns an unmodifiable list of default {@link EntityListener} objects. Note that
      * since the order of listeners is significant a list, not just a generic Collection
      * is returned.
@@ -646,7 +717,7 @@
      *             registered.
      */
     public void addDefaultEntityListener(EntityListener listener) {
-         for (EntityListener next : defaultEntityListeners) {
+        for (EntityListener next : defaultEntityListeners) {
             if (listener.getClassName().equals(next.getClassName())) {
                 throw new IllegalArgumentException("Duplicate default listener for "
                         + next.getClassName());
@@ -772,6 +843,13 @@
         // TODO: andrus, 1/25/2007 - clean up references like removeDbEntity does.
         embeddablesMap.remove(className);
     }
+    
+    /**
+     * @since 3.0
+     */
+    public void removeResultSetMapping(String name) {
+        resultSetMappings.remove(name);
+    }
 
     /**
      * "Dirty" remove of the DbEntity from the data map.
@@ -793,7 +871,8 @@
         if (dbEntityToDelete != null && clearDependencies) {
             for (DbEntity dbEnt : this.getDbEntities()) {
                 // take a copy since we're going to modify the entity
-                for (Relationship rel : new ArrayList<Relationship>(dbEnt.getRelationships()))
{
+                for (Relationship rel : new ArrayList<Relationship>(dbEnt
+                        .getRelationships())) {
                     if (dbEntityName.equals(rel.getTargetEntityName())) {
                         dbEnt.removeRelationship(rel.getName());
                     }
@@ -807,9 +886,10 @@
                 }
                 else {
                     for (Relationship rel : objEnt.getRelationships()) {
-                        for (DbRelationship dbRel : ((ObjRelationship)rel).getDbRelationships())
{
+                        for (DbRelationship dbRel : ((ObjRelationship) rel)
+                                .getDbRelationships()) {
                             if (dbRel.getTargetEntity() == dbEntityToDelete) {
-                                ((ObjRelationship)rel).clearDbRelationships();
+                                ((ObjRelationship) rel).clearDbRelationships();
                                 break;
                             }
                         }
@@ -840,7 +920,8 @@
             // remove relationships that point to this entity
             for (ObjEntity ent : getObjEntities()) {
                 // take a copy since we're going to modify the entity
-                for (Relationship relationship : new ArrayList<Relationship>(ent.getRelationships()))
{
+                for (Relationship relationship : new ArrayList<Relationship>(ent
+                        .getRelationships())) {
                     if (objEntityName.equals(relationship.getTargetEntityName())
                             || objEntityName.equals(relationship.getTargetEntityName()))
{
                         ent.removeRelationship(relationship.getName());
@@ -973,7 +1054,7 @@
 
     /**
      * Returns default client superclass.
-     *
+     * 
      * @since 3.0
      */
     public String getDefaultClientSuperclass() {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/EntityResolver.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/EntityResolver.java?rev=630462&r1=630461&r2=630462&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/EntityResolver.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/EntityResolver.java
Sat Feb 23 08:09:45 2008
@@ -30,6 +30,7 @@
 import org.apache.cayenne.ObjectId;
 import org.apache.cayenne.Persistent;
 import org.apache.cayenne.query.Query;
+import org.apache.cayenne.query.SQLResultSetMapping;
 import org.apache.cayenne.reflect.ClassDescriptor;
 import org.apache.cayenne.reflect.ClassDescriptorMap;
 import org.apache.cayenne.reflect.FaultFactory;
@@ -66,6 +67,7 @@
 
     protected transient Map<String, Query> queryCache;
     protected transient Map<String, Embeddable> embeddableCache;
+    protected transient Map<String, SQLResultSetMapping> resultSetMappingCache;
     protected transient Map<String, DbEntity> dbEntityCache;
     protected transient Map<String, ObjEntity> objEntityCache;
     protected transient Map<String, Procedure> procedureCache;
@@ -93,6 +95,7 @@
         this.objEntityCache = new HashMap<String, ObjEntity>();
         this.procedureCache = new HashMap<String, Procedure>();
         this.entityInheritanceCache = new HashMap<String, EntityInheritanceTree>();
+        this.resultSetMappingCache = new HashMap<String, SQLResultSetMapping>();
     }
 
     /**
@@ -384,6 +387,18 @@
 
         return c;
     }
+    
+    /**
+     * @since 3.0
+     */
+    public Collection<SQLResultSetMapping> getResultSetMappings() {
+        CompositeCollection c = new CompositeCollection();
+        for (DataMap map : getDataMaps()) {
+            c.addComposited(map.getResultSetMappings());
+        }
+        
+        return c;
+    }
 
     public Collection<Procedure> getProcedures() {
         CompositeCollection c = new CompositeCollection();
@@ -434,6 +449,22 @@
 
         return result;
     }
+    
+    /**
+     * @since 3.0
+     */
+    public SQLResultSetMapping getResultSetMapping(String name) {
+        SQLResultSetMapping result = resultSetMappingCache.get(name);
+
+        if (result == null) {
+            // reconstruct cache just in case some of the datamaps
+            // have changed and now contain the required information
+            constructCache();
+            result = resultSetMappingCache.get(name);
+        }
+
+        return result;
+    }
 
     /**
      * Returns ClassDescriptor for the ObjEntity matching the name. Returns null if no
@@ -468,6 +499,8 @@
         objEntityCache.clear();
         procedureCache.clear();
         entityInheritanceCache.clear();
+        resultSetMappingCache.clear();
+        embeddableCache.clear();
         clientEntityResolver = null;
     }
 
@@ -480,7 +513,7 @@
 
         // rebuild index
 
-        // index DbEntities separatly and before ObjEntities to avoid infinite loops when
+        // index DbEntities separately and before ObjEntities to avoid infinite loops when
         // looking up DbEntities during ObjEntity index op
 
         for (DataMap map : maps) {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/MappingNamespace.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/MappingNamespace.java?rev=630462&r1=630461&r2=630462&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/MappingNamespace.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/map/MappingNamespace.java
Sat Feb 23 08:09:45 2008
@@ -22,6 +22,7 @@
 import java.util.Collection;
 
 import org.apache.cayenne.query.Query;
+import org.apache.cayenne.query.SQLResultSetMapping;
 
 /**
  * Defines API of a container of DbEntities, ObjEntities, Procedures, Queries and other
@@ -39,6 +40,13 @@
      * @since 3.0
      */
     Embeddable getEmbeddable(String className);
+
+    /**
+     * Returns a named result set mapping.
+     * 
+     * @since 3.0
+     */
+    SQLResultSetMapping getResultSetMapping(String name);
 
     /**
      * Returns DbEntity for a given name, or null if no such DbEntity is found in the

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/MockMappingNamespace.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/MockMappingNamespace.java?rev=630462&r1=630461&r2=630462&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/MockMappingNamespace.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/map/MockMappingNamespace.java
Sat Feb 23 08:09:45 2008
@@ -28,20 +28,26 @@
 import org.apache.cayenne.map.ObjEntity;
 import org.apache.cayenne.map.Procedure;
 import org.apache.cayenne.query.Query;
+import org.apache.cayenne.query.SQLResultSetMapping;
 
 /**
  * @author Andrus Adamchik
  */
 public class MockMappingNamespace implements MappingNamespace {
-    private Map dbEntities = new HashMap();
-    private Map objEntities = new HashMap();
-    private Map queries = new HashMap();
-    private Map procedures = new HashMap();
-    
+
+    private Map<String, DbEntity> dbEntities = new HashMap<String, DbEntity>();
+    private Map<String, ObjEntity> objEntities = new HashMap<String, ObjEntity>();
+    private Map<String, Query> queries = new HashMap<String, Query>();
+    private Map<String, Procedure> procedures = new HashMap<String, Procedure>();
+
     public Embeddable getEmbeddable(String className) {
         return null;
     }
-    
+
+    public SQLResultSetMapping getResultSetMapping(String name) {
+        return null;
+    }
+
     public EntityListener getEntityListener(String className) {
         return null;
     }
@@ -63,34 +69,34 @@
     }
 
     public DbEntity getDbEntity(String name) {
-        return (DbEntity) dbEntities.get(name);
+        return dbEntities.get(name);
     }
 
     public ObjEntity getObjEntity(String name) {
-        return (ObjEntity) objEntities.get(name);
+        return objEntities.get(name);
     }
 
     public Procedure getProcedure(String name) {
-        return (Procedure) procedures.get(name);
+        return procedures.get(name);
     }
 
     public Query getQuery(String name) {
-        return (Query) queries.get(name);
+        return queries.get(name);
     }
 
-    public Collection getDbEntities() {
+    public Collection<DbEntity> getDbEntities() {
         return dbEntities.values();
     }
 
-    public Collection getObjEntities() {
+    public Collection<ObjEntity> getObjEntities() {
         return objEntities.values();
     }
 
-    public Collection getProcedures() {
+    public Collection<Procedure> getProcedures() {
         return procedures.values();
     }
 
-    public Collection getQueries() {
+    public Collection<Query> getQueries() {
         return queries.values();
     }
 }



Mime
View raw message