bug 2840 : Major work on adl2controllerclasses to collect up modified entities and call before- and after- update hooks on all of them. It complies and looks convincing but at this stage a great deal more testing is required!

This commit is contained in:
sb 2009-04-28 18:27:59 +00:00
parent 133b53fd23
commit 97fd52777b

View file

@ -9,8 +9,8 @@
Transform ADL into (partial) controller classes Transform ADL into (partial) controller classes
$Author: sb $ $Author: sb $
$Revision: 1.36 $ $Revision: 1.37 $
$Date: 2009-04-28 13:44:51 $ $Date: 2009-04-28 18:27:59 $
--> -->
<!-- WARNING WARNING WARNING: Do NOT reformat this file! <!-- WARNING WARNING WARNING: Do NOT reformat this file!
@ -70,7 +70,7 @@
// //
// Automatically generated abstract super class for controllers for the // Automatically generated abstract super class for controllers for the
// <xsl:value-of select="/adl:application/@name"/> application; generated using // <xsl:value-of select="/adl:application/@name"/> application; generated using
// adl2controllerclasses.xslt version <xsl:value-of select="substring( '$Revision: 1.36 $', 10)"/> // adl2controllerclasses.xslt version <xsl:value-of select="substring( '$Revision: 1.37 $', 10)"/>
// //
// <xsl:value-of select="/adl:application/@revision"/> // <xsl:value-of select="/adl:application/@revision"/>
// //
@ -180,7 +180,7 @@
// //
// Controller for auto-generated forms for editing <xsl:value-of select="@name"/>s // Controller for auto-generated forms for editing <xsl:value-of select="@name"/>s
// Automatically generated from application description using // Automatically generated from application description using
// adl2controllerclasses.xslt version <xsl:value-of select="substring( '$Revision: 1.36 $', 10)"/> // adl2controllerclasses.xslt version <xsl:value-of select="substring( '$Revision: 1.37 $', 10)"/>
// //
// This file is automatically generated; DO NOT EDIT IT. // This file is automatically generated; DO NOT EDIT IT.
// //
@ -197,6 +197,7 @@
using System.Web.UI.WebControls.WebParts; using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls; using System.Web.UI.HtmlControls;
using Castle.MonoRail.Framework.Helpers; using Castle.MonoRail.Framework.Helpers;
using Cygnet.Entities;
using Cygnet.Exceptions; using Cygnet.Exceptions;
using Cygnet.Web.Helpers; using Cygnet.Web.Helpers;
using Cygnet.Web.Controllers; using Cygnet.Web.Controllers;
@ -388,27 +389,31 @@
record = new <xsl:value-of select="concat($entityns, '.', @name)"/>(); record = new <xsl:value-of select="concat($entityns, '.', @name)"/>();
} }
messages.Add( "New <xsl:value-of select="@name"/> record created"); messages.Add( "New <xsl:value-of select="@name"/> record created");
isnewborn = true; isnewborn = true;
} }
if ( record != null) { if ( record != null) {
/* a transaction to ensure our database operations are atomic */ /* a transaction to ensure our database operations are atomic */
ITransaction tx = null; ITransaction tx = null;
if ( ! isnewborn) {
/* isnewborn cannot be true unless we've already checked user can create
* so no need to do it again here */
AssertUserCanUpdate();
}
try { if ( ! isnewborn) {
/* begin our atomic transaction */ /* isnewborn cannot be true unless we've already checked user can create
tx = hibernator.BeginTransaction(); * so no need to do it again here */
AssertUserCanUpdate();
}
/* actually update the record */ try {
BindObjectInstance( record, ParamStore.Form, "instance"); /* begin our atomic transaction */
tx = hibernator.BeginTransaction();
/* list of entities modified in this transaction */
List&lt;Entity&gt; modified = new List&lt;Entity&gt;();
<xsl:if test="descendant::adl:property[@type='message']"> /* actually update the record */
BindObjectInstance( record, ParamStore.Form, "instance");
modified.Add( record);
<xsl:if test="descendant::adl:property[@type='message']">
/* there is at least one slot whose value is an internationalised message; /* there is at least one slot whose value is an internationalised message;
* if these have yet to be initialised they must be handled specially */ * if these have yet to be initialised they must be handled specially */
Locale locale = GetBestLocaleForUser(); Locale locale = GetBestLocaleForUser();
@ -451,6 +456,8 @@
Form[ "<xsl:value-of select="concat( 'instance.', @name)"/>"] Form[ "<xsl:value-of select="concat( 'instance.', @name)"/>"]
</xsl:with-param> </xsl:with-param>
</xsl:call-template>; </xsl:call-template>;
modified.Add( record.<xsl:value-of select="@name"/>);
} }
</xsl:when> </xsl:when>
<xsl:otherwise> <xsl:otherwise>
@ -478,44 +485,57 @@
</xsl:variable> </xsl:variable>
/* to update a link table which has no other data than the near and far keys, it is /* 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! */ * sufficient to smash the existing values and create new ones. It's also a lot easier! */
/* TODO: 20090427: rewrite so we don't do this - also add changes to 'modified' */
<xsl:if test="$authentication-layer = 'Database'"> <xsl:if test="$authentication-layer = 'Database'">
if ( <xsl:for-each select="exsl:node-set( $deletegroups)/*"> if ( <xsl:for-each select="exsl:node-set( $deletegroups)/*">InGroup( "<xsl:value-of select="./@name"/>") ||</xsl:for-each> false) {
InGroup( "<xsl:value-of select="./@name"/>") ||
</xsl:for-each> false) {
/* however, we cannot do anything unless we have delete permissions on the table, so /* however, we cannot do anything unless we have delete permissions on the table, so
* should not try. */ * should not try. */
</xsl:if> </xsl:if>
string[] <xsl:value-of select="concat(@name, 'Values')"/> = Form.GetValues( "<xsl:value-of select="concat( 'instance.', @name)"/>"); string[] <xsl:value-of select="concat(@name, 'Values')"/> = Form.GetValues( "<xsl:value-of select="concat( 'instance.', @name)"/>");
/* update the linking table for my <xsl:value-of select="@name"/>; first smash the old values */ List&lt;Entity&gt; toAdd = new List&lt;Entity&gt;();
if ( <xsl:value-of select="concat( 'record.', @name)"/> != null) List&lt;Entity&gt; toRemove = new List&lt;Entity&gt;();
{
<xsl:value-of select="concat( 'record.', @name)"/>.Clear();
}
else
{
<xsl:value-of select="concat( 'record.', @name)"/> = new HashedSet&lt;<xsl:value-of select="concat( $entityns, '.', @entity)"/>&gt;();
} /* if ( <xsl:value-of select="concat( 'record.', @name)"/> != null) */
if ( <xsl:value-of select="concat(@name, 'Values')"/> != null) foreach (<xsl:value-of select="concat( $entityns, '.', @entity)"/> item in <xsl:value-of select="concat( 'record.', @name)"/>) {
{ foreach (string value in <xsl:value-of select="concat(@name, 'Values')"/>) {
/* then reinstate the values from the indexes passed */ if (item.KeyString.Equals(value)) {
foreach ( string index in <xsl:value-of select="concat(@name, 'Values')"/>) toRemove.Add(item);
{ break;
<xsl:value-of select="concat( 'record.', @name)"/>.Add( }
hibernator.CreateCriteria(typeof(<xsl:value-of select="concat( $entityns, '.', @entity)"/>)) }
<xsl:call-template name="add-hibernate-expression-eq"> }
<xsl:with-param name="property" select="//adl:entity[@name=$farentityname]/adl:key/adl:property[position()=1]"/> foreach (string value in <xsl:value-of select="concat(@name, 'Values')"/>) {
<xsl:with-param name="value" select="'index'"/> Boolean add = true;
</xsl:call-template> foreach (<xsl:value-of select="concat( $entityns, '.', @entity)"/> item in <xsl:value-of select="concat( 'record.', @name)"/>) {
.UniqueResult&lt;<xsl:value-of select="concat( $entityns, '.', @entity)"/>&gt;()); if (item.KeyString.Equals(value)) {
} /* foreach ( string index in <xsl:value-of select="concat(@name, 'Values')"/>) */ add = false;
} /* if ( <xsl:value-of select="concat(@name, 'Values')"/> != null) */ break;
<xsl:if test="$authentication-layer = 'Database'"> }
} /* if ( <xsl:for-each select="exsl:node-set( $deletegroups)/*"> }
InGroup( "<xsl:value-of select="./@name"/>") || if (add) {
</xsl:for-each> false) */ toAdd.Add(hibernator.CreateCriteria(typeof(<xsl:value-of select="concat( $entityns, '.', @entity)"/>))
</xsl:if> <xsl:call-template name="add-hibernate-expression-eq">
<xsl:with-param name="property" select="//adl:entity[@name=$farentityname]/adl:key/adl:property[position()=1]"/>
<xsl:with-param name="value" select="'value'"/>
</xsl:call-template>
.UniqueResult&lt;<xsl:value-of select="concat( $entityns, '.', @entity)"/>&gt;());
}
}
foreach (<xsl:value-of select="concat( $entityns, '.', @entity)"/> item in toRemove) {
modified.Add(item);
<xsl:value-of select="concat( 'record.', @name)"/>.Remove(item);
}
foreach (<xsl:value-of select="concat( $entityns, '.', @entity)"/> item in toAdd) {
modified.Add(item);
<xsl:value-of select="concat( 'record.', @name)"/>.Add(item);
}
<xsl:if test="$authentication-layer = 'Database'">
} /* if ( <xsl:for-each select="exsl:node-set( $deletegroups)/*">
InGroup( "<xsl:value-of select="./@name"/>") ||
</xsl:for-each> false) */
</xsl:if>
</xsl:for-each> </xsl:for-each>
<xsl:for-each select="descendant::adl:property[@type='list']"> <xsl:for-each select="descendant::adl:property[@type='list']">
@ -574,6 +594,7 @@
if ( ! found) if ( ! found)
{ {
record.<xsl:value-of select="@name"/>.Remove( item); record.<xsl:value-of select="@name"/>.Remove( item);
modified.Add( item);
break; break;
} }
} /* foreach ( <xsl:value-of select="@entity"/> item ... */ } /* foreach ( <xsl:value-of select="@entity"/> item ... */
@ -597,17 +618,14 @@
<xsl:value-of select="concat( 'item.', $foreignkey)"/>; <xsl:value-of select="concat( 'item.', $foreignkey)"/>;
if ( oldparent != null) { if ( oldparent != null) {
oldparent.<xsl:value-of select="@name"/>.Remove( item); oldparent.<xsl:value-of select="@name"/>.Remove( item);
oldparent.BeforeUpdateHook( hibernator);
hibernator.SaveOrUpdate( oldparent); modified.Add( oldparent);
oldparent.AfterUpdateHook( hibernator);
} }
/* then add it to my <xsl:value-of select="@name"/> */ /* then add it to my <xsl:value-of select="@name"/> */
record.<xsl:value-of select="@name"/>.Add( item); record.<xsl:value-of select="@name"/>.Add( item);
<xsl:value-of select="concat( 'item.', $foreignkey)"/> = record; <xsl:value-of select="concat( 'item.', $foreignkey)"/> = record;
item.BeforeUpdateHook( hibernator); modified.Add( item);
hibernator.SaveOrUpdate( item);
item.AfterUpdateHook( hibernator);
} }
} /* foreach ( string index... */ } /* foreach ( string index... */
} /* if ( Form.GetValues( "<xsl:value-of select="concat( 'instance.', @name)"/>") != null) */ } /* if ( Form.GetValues( "<xsl:value-of select="concat( 'instance.', @name)"/>") != null) */
@ -617,14 +635,13 @@
</xsl:for-each> </xsl:for-each>
/* write the record to the database, in order to guarantee we have a valid key */ /* write the modified records to the database */
/* 20090416 we have a significant problem in that the ILifecycle interface is not foreach ( Entity item in modified) {
doing what we expect - or, at least, not consistently! item.BeforeUpdateHook( hibernator);
Reinstating the before- and after- update hooks */ hibernator.SaveOrUpdate(item);
record.BeforeUpdateHook( hibernator); item.AfterUpdateHook( hibernator);
hibernator.SaveOrUpdate(record); }
record.AfterUpdateHook( hibernator); /* and if no exceptions, commit */
tx.Commit(); tx.Commit();
messages.Add( "<xsl:call-template name="i18n-record-saved"/>"); messages.Add( "<xsl:call-template name="i18n-record-saved"/>");