From 7f5585d49287d9ae6179925d24acffe19c6ab4fe Mon Sep 17 00:00:00 2001
From: sb <sb>
Date: Wed, 6 Feb 2008 17:24:53 +0000
Subject: [PATCH] Many changes, principally dealing with composite primary keys
 and foreign entities.

---
 transforms01/adl2controllerclasses.xslt | 153 ++++++++++++++++--------
 transforms01/adl2entityclass.xslt       |  11 +-
 transforms01/adl2mssql.xslt             | 102 ++++++++++++++--
 transforms01/adl2views.xslt             |   9 +-
 4 files changed, 205 insertions(+), 70 deletions(-)

diff --git a/transforms01/adl2controllerclasses.xslt b/transforms01/adl2controllerclasses.xslt
index 7ede49a..4ca99ca 100755
--- a/transforms01/adl2controllerclasses.xslt
+++ b/transforms01/adl2controllerclasses.xslt
@@ -9,8 +9,8 @@
     Transform ADL into (partial) controller classes
     
     $Author: sb $
-    $Revision: 1.2 $
-    $Date: 2008-02-01 21:47:15 $
+    $Revision: 1.3 $
+    $Date: 2008-02-06 17:24:53 $
   -->
 
   <!-- WARNING WARNING WARNING: Do NOT reformat this file! 
@@ -38,24 +38,36 @@
     <xsl:param name="entityns" select="Unset"/>
     <!-- Whether to authenticate at application or at database layer. 
     If not 'Application', then 'Database'. -->
-    <xsl:param name="authentication-layer" select="Application"/>  
-  
+    <xsl:param name="authentication-layer" select="Application"/>
+
+    <!-- 
+      The convention to use for naming auto-generated abstract primary keys. Known values are
+      Id - the autogenerated primary key, if any, is called just 'Id'
+      Name - the autogenerated primary key has the same name as the entity
+      NameId - the name of the auto generated primary key is the name of the entity followed by 'Id'
+      Name_Id - the name of the auto generated primary key is the name of the entity followed by '_Id'  
+    -->
+    <xsl:param name="abstract-key-name-convention" select="Id"/>
+
     <xsl:template match="adl:application">
       <xsl:apply-templates select="adl:entity"/>
     </xsl:template>
 
-    <xsl:template match="adl:entity">
+    <!-- Don't bother generating anything for foreign entities -->
+    <xsl:template match="adl:entity[@foreign='true']"/>
+  
+    <xsl:template match="adl:entity[adl:form|adl:page|adl:list]">
       <!-- what's all this about? the objective is to get the revision number of the 
     transform into the output, /without/ getting that revision number overwritten 
     with the revision number of the generated file if the generated file is 
     stored to CVS -->
       <xsl:variable name="transform-rev1"
-                    select="substring( '$Revision: 1.2 $', 11)"/>
+                    select="substring( '$Revision: 1.3 $', 11)"/>
       <xsl:variable name="transform-revision"
                     select="substring( $transform-rev1, 0, string-length( $transform-rev1) - 1)"/>
 
       <xsl:variable name="key">
-        <xsl:call-template name="primary-key">
+        <xsl:call-template name="primary-key-name">
           <xsl:with-param name="entityname" select="@name"/>
         </xsl:call-template>
       </xsl:variable>
@@ -109,6 +121,8 @@ namespace <xsl:value-of select="$controllerns"/> {
   /// &lt;/summary&gt;
   public partial class <xsl:value-of select="@name"/>Controller : BaseController {
 
+      <xsl:if test="adl:form">
+        <!-- unless there's at least one form, we won't generate a 'store' method -->
       /// &lt;summary&gt;
       /// Store the record represented by the parameters passed in an HTTP service
       /// Without Id -&gt; it's new, I create a new persistent object;
@@ -121,17 +135,18 @@ namespace <xsl:value-of select="$controllerns"/> {
           NHibernateHelper.GetCurrentSession( <xsl:if test="$authentication-layer = 'Database'">Session[ NHibernateHelper.USERTOKEN], 
                                               Session[NHibernateHelper.PASSTOKEN]</xsl:if>);
           List&lt;string&gt; messages = new List&lt;string&gt;();
-      
-        <xsl:value-of select="$entityns"/>.<xsl:value-of select="@name"/> record;
-        
-        <xsl:apply-templates select="property"/>
 
+        <xsl:value-of select="$entityns"/>.<xsl:value-of select="@name"/> record;
+
+        <xsl:apply-templates select="adl:property"/>
+
+        <!-- TODO: this does not corectly handle entities with composite primary keys -->
         string id = Form["<xsl:value-of select="concat( 'instance.', $key)"/>"];
 
         if ( String.IsNullOrEmpty( id))
         {
           /* it's new, create persistent object */
-          record = new <xsl:value-of select="$entityns"/>.<xsl:value-of select="@name"/>(<xsl:for-each select="property[@distinct='system']">Form[<xsl:value-of select="concat( 'instance.', @name)"/>]<xsl:choose>
+          record = new <xsl:value-of select="$entityns"/>.<xsl:value-of select="@name"/>(<xsl:for-each select="adl:property[@distinct='system']">Form[<xsl:value-of select="concat( 'instance.', @name)"/>]<xsl:choose>
                 <xsl:when test="position() = last()"/>
                 <xsl:otherwise>, </xsl:otherwise>
               </xsl:choose>
@@ -145,6 +160,7 @@ namespace <xsl:value-of select="$controllerns"/> {
         else
         {
           /* it's existing, retrieve it */
+        <!-- TODO: this does not corectly handle entities with composite primary keys -->
           record =
             hibernator.CreateCriteria(typeof(<xsl:value-of select="concat( $entityns, '.', @name)"/>))
               .Add(Expression.Eq("<xsl:value-of select="$key"/>", Int32.Parse(id)))
@@ -158,19 +174,20 @@ namespace <xsl:value-of select="$controllerns"/> {
             /* actually update the record */
             BindObjectInstance( record, ParamStore.Form, "instance");
             
-      <xsl:for-each select="property[@type='entity']">
+            <xsl:for-each select="adl:property[@type='entity']">
             /* for properties of type 'entity', it should not be necessary to do anything 
              * special - BindObjectInstance /should/ do it all. Unfortunately it sometimes 
-             * doesn't, and I haven't yet characterised why not. TODO: Fix this! */
+             * doesn't, and I haven't yet characterised why not. */
+             <!-- TODO: Fix this! -->
             record.<xsl:value-of select="@name"/> = 
               hibernator.CreateCriteria(typeof(<xsl:value-of select="concat( $entityns, '.', @entity)"/>))
-                .Add(Expression.Eq("<xsl:call-template name="primary-key">
+                .Add(Expression.Eq("<xsl:call-template name="primary-key-name">
                   <xsl:with-param name="entityname" select="@entity"/>
                 </xsl:call-template>", Int32.Parse(Form["<xsl:value-of select="concat( $entityns, '.', @entity)"/>"])))
                 .UniqueResult&lt;<xsl:value-of select="concat( $entityns, '.', @entity)"/>&gt;();
-      </xsl:for-each>
+            </xsl:for-each>
           
-      <xsl:for-each select="property[@type='link']">  
+            <xsl:for-each select="property[@type='link']">  
             /* to update a link table which has no other data than the near and far keys, it is
              * sufficient to smash the existing values and create new ones. It's also a lot easier! */
         
@@ -198,9 +215,9 @@ namespace <xsl:value-of select="$controllerns"/> {
                     .UniqueResult&lt;<xsl:value-of select="$entityns"/>.<xsl:value-of select="@entity"/>&gt;());
               }
             }
-      </xsl:for-each>
+            </xsl:for-each>
 
-      <xsl:for-each select="property[@type='list']">
+            <xsl:for-each select="adl:property[@type='list']">
             /* with a list we cannot just smash the old values! Instead we need to check 
              * each one and exclude it if no longer required */
             if ( Form.GetValues( "<xsl:value-of select="concat( 'instance.', @name)"/>") != null)
@@ -242,7 +259,7 @@ namespace <xsl:value-of select="$controllerns"/> {
                 }
               }
             }
-      </xsl:for-each>
+            </xsl:for-each>
 
             /* perform any domain knowledge behaviour on the record prior to updating */
             record.BeforeUpdateHook( hibernator);
@@ -267,21 +284,25 @@ namespace <xsl:value-of select="$controllerns"/> {
 
           PropertyBag["messages"] = messages;
           PropertyBag["instance"] = record;    
-      <xsl:if test="$authentication-layer = 'Database'">   
+            <xsl:if test="$authentication-layer = 'Database'">   
           PropertyBag["username"] = Session[ NHibernateHelper.USERTOKEN];
-      </xsl:if>
+            </xsl:if>
       
-      <xsl:call-template name="menus">
-        <xsl:with-param name="entity" select="."/>
-      </xsl:call-template>
-          RenderViewWithFailover("<xsl:value-of select="concat( form[position()=1]/@name, '.vm')"/>", "<xsl:value-of select="concat( form[position()=1]/@name, '.auto.vm')"/>");
+            <xsl:call-template name="menus">
+              <xsl:with-param name="entity" select="."/>
+            </xsl:call-template>
+          RenderViewWithFailover("<xsl:value-of select="concat( adl:form[position()=1]/@name, '.vm')"/>", 
+              "<xsl:value-of select="concat( adl:form[position()=1]/@name, '.auto.vm')"/>");
         }
         else
         {
           throw new Exception( String.Format( "No record of type <xsl:value-of select="@name"/> with key value {0} found", id));
         }
       }
+      </xsl:if>
 
+      <xsl:if test="adl:form">
+        <!-- unless there's at least one form, we won't generate a 'delete' method -->
       /// &lt;summary&gt;
       /// Actually delete the selected record
       /// &lt;/summary&gt;
@@ -317,21 +338,21 @@ namespace <xsl:value-of select="$controllerns"/> {
             throw new ApplicationException( "No such record?");
           }
         }
-      <xsl:choose>
-        <xsl:when test="list">
+        <xsl:choose>
+          <xsl:when test="adl:list">
         InternalShowList();
-        </xsl:when>
-        <xsl:otherwise>
+          </xsl:when>
+          <xsl:otherwise>
         Redirect( FormsAuthentication.DefaultUrl);
-        </xsl:otherwise>
-      </xsl:choose>
+          </xsl:otherwise>
+        </xsl:choose>
       }
+      </xsl:if>
+      <xsl:apply-templates select="adl:form"/>
 
-      <xsl:apply-templates select="form"/>
-
-      <xsl:if test="list">
-        <xsl:variable name="listname" select="list[position()=1]/@name"/>
-        <xsl:apply-templates select="list"/>
+      <xsl:if test="adl:list">
+        <xsl:variable name="listname" select="adl:list[position()=1]/@name"/>
+        <xsl:apply-templates select="adl:list"/>
       /// &lt;summary&gt;
       /// list all instances of this entity to allow the user to select one for editing
       /// this method invokes the default list view - which is probably what you want unless
@@ -363,13 +384,13 @@ namespace <xsl:value-of select="$controllerns"/> {
         PaginationHelper.CreatePagination( this, instances, 25);
 
         RenderViewWithFailover(view + ".vm", view + ".auto.vm");
-        }
+      }
       </xsl:if>
   }
 }
     </xsl:template>
 
-    <xsl:template match="property[@required='true']">
+    <xsl:template match="adl:property[@required='true']">
         if ( Form[ "<xsl:value-of select="concat( 'instance.', @name)"/>" ] == null)
         {
           AddError( <xsl:choose>
@@ -383,13 +404,13 @@ namespace <xsl:value-of select="$controllerns"/> {
     </xsl:template>
 
     <!-- suppress properties otherwise -->
-    <xsl:template match="property"/>
+    <xsl:template match="adl:property"/>
         
-    <xsl:template match="ifmissing">
+    <xsl:template match="adl:ifmissing">
       "<xsl:value-of select="normalize-space(.)"/>"
     </xsl:template>
 
-    <xsl:template match="form">
+    <xsl:template match="adl:form">
       <xsl:variable name="key">
         <xsl:choose>
           <xsl:when test="../@natural-key">
@@ -496,7 +517,7 @@ namespace <xsl:value-of select="$controllerns"/> {
 
     </xsl:template>
 
-    <xsl:template match="list">
+    <xsl:template match="adl:list">
       /// &lt;summary&gt;
       /// list all instances of this entity to allow the user to select one
       /// this method invokes the named view.
@@ -508,22 +529,26 @@ namespace <xsl:value-of select="$controllerns"/> {
 
     </xsl:template>
 
+    <xsl:template match="adl:documentation">
+      /* <xsl:apply-templates/> */
+    </xsl:template>
+
     <xsl:template name="menus">
       <xsl:param name="entity"/>
-      <xsl:for-each select="$entity/property[@type='entity']">
+      <xsl:for-each select="$entity/adl:property[@type='entity']">
           /* produce a list of <xsl:value-of select="@entity"/> to populate the menu for <xsl:value-of select="@name"/> */
         <xsl:call-template name="menu">
           <xsl:with-param name="property" select="."/>
         </xsl:call-template>
 
       </xsl:for-each>
-      <xsl:for-each select="$entity/property[@type='link']">
+      <xsl:for-each select="$entity/adl:property[@type='link']">
           /* produce a list of <xsl:value-of select="@entity"/> to populate the LHS of the shuffle for <xsl:value-of select="@name"/> */
         <xsl:call-template name="menu">
           <xsl:with-param name="property" select="."/>
         </xsl:call-template>
       </xsl:for-each>
-      <xsl:for-each select="$entity/property[@type='list']">
+      <xsl:for-each select="$entity/adl:property[@type='list']">
           /* produce a list of <xsl:value-of select="@entity"/> to populate the multi-select for <xsl:value-of select="@name"/> */
         <xsl:call-template name="menu">
           <xsl:with-param name="property" select="."/>
@@ -535,22 +560,48 @@ namespace <xsl:value-of select="$controllerns"/> {
     <xsl:template name="menu">
       <xsl:param name="property"/>
       <xsl:variable name="ename" select="$property/@entity"/>
-      <xsl:variable name="entity" select="//entity[@name=$ename]"/>
+      <xsl:variable name="entity" select="//adl:entity[@name=$ename]"/>
           PropertyBag["<xsl:value-of select="concat('all_', $property/@name)"/>"] =
             hibernator.CreateCriteria(typeof(<xsl:value-of select="concat( $entityns, '.', $property/@entity)"/>))<xsl:for-each select="$entity/property[@distinct='user']">
               <xsl:value-of select="concat('.AddOrder( new Order( &#34;', @name, '&#34;, true))')"/>
             </xsl:for-each>.List&lt;<xsl:value-of select="concat( $entityns, '.', $property/@entity)"/>&gt;();
     </xsl:template>
 
-    <xsl:template name="primary-key">
+    <xsl:template name="primary-key-name">
       <!-- return the name of the primary key of the entity with this name -->
       <xsl:param name="entityname"/>
       <xsl:choose>
-        <xsl:when test="//entity[@name=$entityname]/@natural-key">
-          <xsl:value-of select="//entity[@name=$entityname]/@natural-key"/>
+        <xsl:when test="//adl:entity[@name=$entityname]/@natural-key">
+          <xsl:value-of select="//adl:entity[@name=$entityname]/@natural-key"/>
+        </xsl:when>
+        <xsl:when test="//adl:entity[@name=$entityname]/key">
+          <xsl:choose>
+            <xsl:when test="count(//adl:entity[@name=$entityname]/adl:key/adl:property) &gt; 1">
+              <xsl:message terminate="no">
+                ADL: WARNING: entity '<xsl:value-of select="$entityname"/>' has a compound primary key;
+                adl2controllerclasses is not yet clever enough to generate appropriate code
+              </xsl:message>
+            </xsl:when>
+            <xsl:otherwise>
+              <xsl:value-of select="//adl:entity[@name=$entityname]/adl:key/adl:property[position()=1]"/>
+            </xsl:otherwise>
+          </xsl:choose>
         </xsl:when>
         <xsl:otherwise>
-          <xsl:value-of select="concat( $entityname, 'Id')" />
+          <xsl:choose>
+            <xsl:when test="$abstract-key-name-convention='Name'">
+              <xsl:value-of select="@name"/>
+            </xsl:when>
+            <xsl:when test="$abstract-key-name-convention = 'NameId'">
+              <xsl:value-of select="concat( $entityname, 'Id')"/>
+            </xsl:when>
+            <xsl:when test="$abstract-key-name-convention = 'Name_Id'">
+              <xsl:value-of select="concat( $entityname, '_Id')"/>
+            </xsl:when>
+            <xsl:otherwise>
+              <xsl:value-of select="'Id'"/>
+            </xsl:otherwise>
+          </xsl:choose>
         </xsl:otherwise>
       </xsl:choose>
     </xsl:template>
diff --git a/transforms01/adl2entityclass.xslt b/transforms01/adl2entityclass.xslt
index b5c40c9..56e6cb9 100755
--- a/transforms01/adl2entityclass.xslt
+++ b/transforms01/adl2entityclass.xslt
@@ -8,8 +8,8 @@
     Transform ADL into entity classes
     
     $Author: sb $
-    $Revision: 1.3 $
-    $Date: 2008-02-01 21:47:15 $
+    $Revision: 1.4 $
+    $Date: 2008-02-06 17:24:53 $
   -->
 
   <!-- WARNING WARNING WARNING: Do NOT reformat this file! 
@@ -41,6 +41,9 @@
     <xsl:apply-templates select="adl:entity"/>
   </xsl:template>
 
+  <!-- Don't bother generating anything for foreign entities -->
+  <xsl:template match="adl:entity[@foreign='true']"/>
+
   <xsl:template match="adl:entity">
     <xsl:message terminate="no">Matched entity with name <xsl:value-of select="@name"/>
   </xsl:message>
@@ -50,7 +53,7 @@
     stored to CVS -->
 
     <xsl:variable name="transform-rev1"
-                  select="substring( '$Revision: 1.3 $', 11)"/>
+                  select="substring( '$Revision: 1.4 $', 11)"/>
     <xsl:variable name="transform-revision"
                   select="substring( $transform-rev1, 0, string-length( $transform-rev1) - 1)"/>
 
@@ -108,7 +111,7 @@
         /* natural primary key exists - not generating abstract key */
           </xsl:when>
           <xsl:when test="adl:key">
-        /* composite promary key exists - not generating abstract key */
+        /* primary key exists - not generating abstract key */
         
         /// &lt;summary&gt;
         /// Auto-generated constructor; initialises each of the slots within 
diff --git a/transforms01/adl2mssql.xslt b/transforms01/adl2mssql.xslt
index b8192c6..d4bffbc 100755
--- a/transforms01/adl2mssql.xslt
+++ b/transforms01/adl2mssql.xslt
@@ -12,17 +12,26 @@
       Convert ADL to MS-SQL
       
       $Author: sb $
-      $Revision: 1.3 $
+      $Revision: 1.4 $
   -->
     
   <xsl:output indent="no" encoding="utf-8" method="text"/>
   <xsl:include href="base-type-include.xslt"/>
-    
+
+  <!-- 
+      The convention to use for naming auto-generated abstract primary keys. Known values are
+      Id - the autogenerated primary key, if any, is called just 'Id'
+      Name - the autogenerated primary key has the same name as the entity
+      NameId - the name of the auto generated primary key is the name of the entity followed by 'Id'
+      Name_Id - the name of the auto generated primary key is the name of the entity followed by '_Id'  
+    -->
+  <xsl:param name="abstract-key-name-convention" select="Id"/>
+
   <xsl:template match="adl:application"> 
         -------------------------------------------------------------------------------------------------
         --
         --    Database for application <xsl:value-of select="@name"/> version <xsl:value-of select="@version"/>
-        --    Generated for MS-SQL 2000+ using adl2mssql.xsl $Revision: 1.3 $
+        --    Generated for MS-SQL 2000+ using adl2mssql.xsl $Revision: 1.4 $
         --
         --    Code generator (c) 2007 Cygnet Solutions Ltd
         --
@@ -46,7 +55,7 @@
         -------------------------------------------------------------------------------------------------
         --    primary referential integrity constraints
         -------------------------------------------------------------------------------------------------
-      <xsl:for-each select="adl:entity">
+      <xsl:for-each select="adl:entity[ not(@foreign='true')]">
         <xsl:variable name="nearside" select="@name"/>
         <xsl:for-each select="property[@type='entity']">
           <xsl:variable name="farside" select="@entity"/>
@@ -116,10 +125,12 @@
         GO
     </xsl:template>
 
+  <!-- don't generate foreign tables - although we will generate ref integ constraints for them -->
+  <xsl:template match="adl:entity[@foreign='true']" mode="table"/> 
 
   <xsl:template match="adl:entity" mode="table">
-    <xsl:variable name="table" select="@name"/>
-      <!-- xsl:choose>
+    <xsl:variable name="table">
+      <xsl:choose>
         <xsl:when test="@table">
           <xsl:value-of select="@table"/>
         </xsl:when>
@@ -127,12 +138,12 @@
           <xsl:value-of select="@name"/>
         </xsl:otherwise>
       </xsl:choose>
-    </xsl:variable -->
+    </xsl:variable>
 
         -------------------------------------------------------------------------------------------------
-        --    primary table <xsl:value-of select="@name"/>
+        --    primary table <xsl:value-of select="$table"/>
         -------------------------------------------------------------------------------------------------
-        CREATE TABLE  "<xsl:value-of select="@name"/>"
+        CREATE TABLE  "<xsl:value-of select="$table"/>"
         (
           <xsl:for-each select="descendant::adl:property[@type!='link' and @type != 'list']">
             <xsl:apply-templates select="."/><xsl:if test="position() != last()">,</xsl:if>
@@ -376,7 +387,12 @@
   </xsl:template>
 
   <xsl:template match="adl:property[@type='serial']">
-            <xsl:value-of select="@name"/><xsl:text> INT IDENTITY( 1, 1)</xsl:text>
+            <xsl:call-template name="property-name">
+      <xsl:with-param name="property" select="."/>
+    </xsl:call-template><xsl:text> INT IDENTITY( 1, 1)</xsl:text>
+    <xsl:message terminate="no">
+      ADL: WARNING: type='serial' is deprecated; add a generator with type='native' instead
+    </xsl:message>
   </xsl:template>
 
   <xsl:template match="adl:generator[@action='native']">
@@ -387,6 +403,11 @@
   <!-- the grand unified property handler, using the sql-type template to 
   generate the correct types for each field -->
   <xsl:template match="adl:property">
+    <xsl:variable name="column">
+      <xsl:call-template name="property-name">
+        <xsl:with-param name="property" select="."/>
+      </xsl:call-template>
+    </xsl:variable>
     <xsl:variable name="type">
       <xsl:call-template name="sql-type">
         <xsl:with-param name="property" select="."/>
@@ -400,7 +421,7 @@
     <xsl:variable name="generator">
       <xsl:apply-templates select="adl:generator"/>
     </xsl:variable>
-          "<xsl:value-of select="@name"/>" <xsl:value-of 
+          "<xsl:value-of select="$column"/>" <xsl:value-of 
                 select="concat( normalize-space( $type), ' ', normalize-space( $generator))"/><xsl:if
                 test="@required='true'"> NOT NULL</xsl:if><xsl:if
             test="string(@default)"> DEFAULT <xsl:choose>
@@ -418,17 +439,74 @@
   sure why. So temporarily this special case template fixes the problem. TODO: 
   work out what's wrong with the grand unified version -->
   <xsl:template match="adl:property[@type='entity']">
+    <xsl:variable name="column">
+      <xsl:call-template name="property-name">
+        <xsl:with-param name="property" select="."/>
+      </xsl:call-template>
+    </xsl:variable>
     <xsl:variable name="type">
       <xsl:call-template name="sql-type">
         <xsl:with-param name="property" select="."/>
       </xsl:call-template>
     </xsl:variable>
-          "<xsl:value-of select="@name"/>" <xsl:value-of select="$type"/><xsl:if 
+          "<xsl:value-of select="$column"/>" <xsl:value-of select="$type"/><xsl:if 
             test="string(@default)"> DEFAULT <xsl:value-of select="@default"/></xsl:if><xsl:if 
                 test="@required='true'"> NOT NULL</xsl:if>
   </xsl:template>
 
 
+  <!-- consistent, repeatable way of getting the column name for a given property -->
+  <xsl:template name="property-name">
+    <xsl:param name="property"/>
+    <xsl:choose>
+      <xsl:when test="$property/@column">
+        <xsl:value-of select="$property/@column"/>
+      </xsl:when>
+      <xsl:otherwise>
+        <xsl:value-of select="$property/@name"/>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
+
+  <xsl:template name="primary-key-name">
+    <!-- return the name of the primary key of the entity with this name -->
+    <xsl:param name="entityname"/>
+    <xsl:choose>
+      <xsl:when test="//adl:entity[@name=$entityname]/@natural-key">
+        <xsl:value-of select="//adl:entity[@name=$entityname]/@natural-key"/>
+      </xsl:when>
+      <xsl:when test="//adl:entity[@name=$entityname]/key">
+        <xsl:choose>
+          <xsl:when test="count(//adl:entity[@name=$entityname]/adl:key/adl:property) &gt; 1">
+            <xsl:message terminate="no">
+              ADL: WARNING: entity '<xsl:value-of select="$entityname"/>' has a compound primary key;
+              adl2mssql is not yet clever enough to generate appropriate code
+            </xsl:message>
+          </xsl:when>
+          <xsl:otherwise>
+            <xsl:value-of select="//adl:entity[@name=$entityname]/adl:key/adl:property[position()=1]"/>
+          </xsl:otherwise>
+        </xsl:choose>
+      </xsl:when>
+      <xsl:otherwise>
+        <xsl:choose>
+          <xsl:when test="$abstract-key-name-convention='Name'">
+            <xsl:value-of select="@name"/>
+          </xsl:when>
+          <xsl:when test="$abstract-key-name-convention = 'NameId'">
+            <xsl:value-of select="concat( $entityname, 'Id')"/>
+          </xsl:when>
+          <xsl:when test="$abstract-key-name-convention = 'Name_Id'">
+            <xsl:value-of select="concat( $entityname, '_Id')"/>
+          </xsl:when>
+          <xsl:otherwise>
+            <xsl:value-of select="'Id'"/>
+          </xsl:otherwise>
+        </xsl:choose>
+      </xsl:otherwise>
+    </xsl:choose>
+  </xsl:template>
+
   <!-- return the SQL type of the property which is passed as a parameter -->
   <xsl:template name="sql-type">
     <xsl:param name="property"/>
diff --git a/transforms01/adl2views.xslt b/transforms01/adl2views.xslt
index ddd7f92..7c9045e 100755
--- a/transforms01/adl2views.xslt
+++ b/transforms01/adl2views.xslt
@@ -9,8 +9,8 @@
     Transform ADL into velocity view templates
     
     $Author: sb $
-    $Revision: 1.1 $
-    $Date: 2008-01-31 17:06:35 $
+    $Revision: 1.2 $
+    $Date: 2008-02-06 17:24:53 $
   -->
   <!-- WARNING WARNING WARNING: Do NOT reformat this file! 
      Whitespace (or lack of it) is significant! -->
@@ -31,7 +31,7 @@
     stored to CVS -->
 
   <xsl:variable name="transform-rev1"
-                select="substring( '$Revision: 1.1 $', 11)"/>
+                select="substring( '$Revision: 1.2 $', 11)"/>
   <xsl:variable name="transform-revision"
                 select="substring( $transform-rev1, 0, string-length( $transform-rev1) - 1)"/>
 
@@ -45,6 +45,9 @@
     </output>
   </xsl:template>
 
+  <xsl:template match="entity[@foreign='true']"/>
+  <!-- Don't bother generating anything for foreign entities -->
+
   <xsl:template match="entity">
     <xsl:apply-templates select="form"/>
     <xsl:apply-templates select="list"/>