bug 1833 : Added distinctness checks to controller generator; also transactions.

This commit is contained in:
sb 2008-05-08 17:26:44 +00:00
parent 363dfd3df9
commit 0e392e6bb3

View file

@ -9,8 +9,8 @@
Transform ADL into (partial) controller classes Transform ADL into (partial) controller classes
$Author: sb $ $Author: sb $
$Revision: 1.22 $ $Revision: 1.23 $
$Date: 2008-04-18 09:27:29 $ $Date: 2008-05-08 17:26:44 $
--> -->
<!-- WARNING WARNING WARNING: Do NOT reformat this file! <!-- WARNING WARNING WARNING: Do NOT reformat this file!
@ -71,7 +71,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.22 $', 10)"/> // adl2controllerclasses.xslt version <xsl:value-of select="substring( '$Revision: 1.23 $', 10)"/>
// //
// This file is automatically generated; DO NOT EDIT IT. // This file is automatically generated; DO NOT EDIT IT.
// //
@ -128,7 +128,7 @@ namespace <xsl:value-of select="$controllerns"/> {
// //
// 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.22 $', 10)"/> // adl2controllerclasses.xslt version <xsl:value-of select="substring( '$Revision: 1.23 $', 10)"/>
// //
// This file is automatically generated; DO NOT EDIT IT. // This file is automatically generated; DO NOT EDIT IT.
// //
@ -187,15 +187,24 @@ namespace <xsl:value-of select="$controllerns"/> {
// field values and save the entity. // field values and save the entity.
Boolean isnewborn = false; Boolean isnewborn = false;
<xsl:apply-templates select="descendant::adl:property"/>
<xsl:call-template name="fetch-instance"> <xsl:call-template name="fetch-instance">
<xsl:with-param name="entity" select="."/> <xsl:with-param name="entity" select="."/>
<xsl:with-param name="initialise-locals" select="'true'"/>
</xsl:call-template> </xsl:call-template>
try {
<xsl:if test="adl:property[@distinct='system' or @distinct='all']">
/* declare a list of <xsl:value-of select="@name"/> to use in
* distinctness checks */
IList&lt;<xsl:value-of select="@name"/>&gt; matches = null;
</xsl:if>
<xsl:apply-templates select="descendant::adl:property"/>
if ( record == null) { if ( record == null) {
/* it seems to be new, create persistent object */ /* it seems to be new, create persistent object */
AssertUserCanCreate(); AssertUserCanCreate();
try { try {
record = new <xsl:value-of select="concat($entityns, '.', @name)"/>(<xsl:for-each select="adl:key/adl:property"> record = new <xsl:value-of select="concat($entityns, '.', @name)"/>(<xsl:for-each select="adl:key/adl:property">
<xsl:variable name="basetype"> <xsl:variable name="basetype">
@ -242,6 +251,9 @@ namespace <xsl:value-of select="$controllerns"/> {
} }
if ( record != null) { if ( record != null) {
/* a transaction to ensure our database operations are atomic */
ITransaction tx = null;
if ( ! isnewborn) { if ( ! isnewborn) {
// isnewborn cannot be true unless we've already checked user can create // isnewborn cannot be true unless we've already checked user can create
// so no need to do it again here // so no need to do it again here
@ -348,6 +360,7 @@ namespace <xsl:value-of select="$controllerns"/> {
if ( index.Equals( itemId)) if ( index.Equals( itemId))
{ {
found = true; found = true;
break;
} }
} }
@ -355,7 +368,7 @@ namespace <xsl:value-of select="$controllerns"/> {
{ {
record.<xsl:value-of select="@name"/>.Remove( item); record.<xsl:value-of select="@name"/>.Remove( item);
} }
} } /* foreach ( string index... */
/* then add any on the included list which are not already members */ /* then add any on the included list which are not already members */
foreach ( string index in <xsl:value-of select="concat(@name, 'Values')"/>) foreach ( string index in <xsl:value-of select="concat(@name, 'Values')"/>)
@ -370,19 +383,30 @@ namespace <xsl:value-of select="$controllerns"/> {
{ {
record.<xsl:value-of select="@name"/>.Add( item); record.<xsl:value-of select="@name"/>.Add( item);
} }
} } /* foreach ( string index... */
} } /* if ( Form.GetValues( "<xsl:value-of select="concat( 'instance.', @name)"/>") != null) */
</xsl:for-each> </xsl:for-each>
/* write the record to the database, in order to guarantee we have a valid key */ /* write the record to the database, in order to guarantee we have a valid key */
hibernator.Save(record); hibernator.Save(record);
hibernator.Flush(); hibernator.Flush();
tx.Commit();
messages.Add( "Record saved successfully"); messages.Add( "Record saved successfully");
} /* try actually commit */
catch ( Exception any) {
tx.Rollback();
messages.Add( "Record not saved");
throw any;
} }
} /* if ( record != null) */
else {
throw new ApplicationException( String.Format( "No record of type <xsl:value-of select="@name"/> with the key values supplied was found"));
}
} /* try */
catch ( DataSuitabilityException dse) catch ( DataSuitabilityException dse)
{ {
AddError( dse.Message); AddError( dse);
} }
catch ( ApplicationException axe) catch ( ApplicationException axe)
{ {
@ -395,7 +419,7 @@ namespace <xsl:value-of select="$controllerns"/> {
PropertyBag["username"] = Session[ NHibernateHelper.USERTOKEN]; PropertyBag["username"] = Session[ NHibernateHelper.USERTOKEN];
</xsl:if> </xsl:if>
if ( ! AssertNoErrors()) if ( HasNoErrors())
{ {
/* the session may be polluted; create a new session */ /* the session may be polluted; create a new session */
NHibernateHelper.CloseSession(); NHibernateHelper.CloseSession();
@ -403,19 +427,19 @@ namespace <xsl:value-of select="$controllerns"/> {
Session[ NHibernateHelper.USERTOKEN], Session[ NHibernateHelper.USERTOKEN],
Session[NHibernateHelper.PASSTOKEN] Session[NHibernateHelper.PASSTOKEN]
</xsl:if>); </xsl:if>);
}
<xsl:call-template name="fetch-instance">
<xsl:with-param name="entity" select="."/>
<xsl:with-param name="initialise-locals" select="'false'"/>
</xsl:call-template>
}
<xsl:call-template name="formmenus"> <xsl:call-template name="formmenus">
<xsl:with-param name="form" select="adl:form[position()=1]"/> <xsl:with-param name="form" select="adl:form[position()=1]"/>
</xsl:call-template> </xsl:call-template>
RenderViewWithFailover("<xsl:value-of select="concat( adl:form[position()=1]/@name, '.vm')"/>", RenderViewWithFailover("<xsl:value-of select="concat( adl:form[position()=1]/@name, '.vm')"/>",
"<xsl:value-of select="concat( adl:form[position()=1]/@name, '.auto.vm')"/>"); "<xsl:value-of select="concat( adl:form[position()=1]/@name, '.auto.vm')"/>");
} }
else
{
throw new DataRequiredException( "Record not found");
}
}
</xsl:if> </xsl:if>
<xsl:if test="adl:form"> <xsl:if test="adl:form">
@ -437,6 +461,7 @@ namespace <xsl:value-of select="$controllerns"/> {
{ {
<xsl:call-template name="fetch-instance"> <xsl:call-template name="fetch-instance">
<xsl:with-param name="entity" select="."/> <xsl:with-param name="entity" select="."/>
<xsl:with-param name="initialise-locals" select="'true'"/>
</xsl:call-template> </xsl:call-template>
if ( record != null) if ( record != null)
@ -486,7 +511,7 @@ namespace <xsl:value-of select="$controllerns"/> {
NHibernateHelper.GetCurrentSession( <xsl:if test="$authentication-layer = 'Database'">Session[ NHibernateHelper.USERTOKEN], NHibernateHelper.GetCurrentSession( <xsl:if test="$authentication-layer = 'Database'">Session[ NHibernateHelper.USERTOKEN],
Session[NHibernateHelper.PASSTOKEN]</xsl:if>); Session[NHibernateHelper.PASSTOKEN]</xsl:if>);
IList&lt;<xsl:value-of select="concat( $entityns, '.', @name)"/>&gt; instances = IList&lt;<xsl:value-of select="concat( $entityns, '.', @name)"/>&gt; instances =
hibernator.CreateCriteria(typeof(<xsl:value-of select="concat($entityns, '.', @name)"/>))<xsl:for-each select="property[@distinct='user']"> hibernator.CreateCriteria(typeof(<xsl:value-of select="concat($entityns, '.', @name)"/>))<xsl:for-each select="adl:property[@distinct='user']">
<xsl:value-of select="concat( '.AddOrder( new Order( &#34;', @name, '&#34;, true))')"/> <xsl:value-of select="concat( '.AddOrder( new Order( &#34;', @name, '&#34;, true))')"/>
</xsl:for-each>.List&lt;<xsl:value-of select="concat($entityns, '.', @name)"/>&gt;(); </xsl:for-each>.List&lt;<xsl:value-of select="concat($entityns, '.', @name)"/>&gt;();
@ -515,8 +540,33 @@ namespace <xsl:value-of select="$controllerns"/> {
</xsl:choose>); </xsl:choose>);
} }
<xsl:choose>
<xsl:when test="adl:property[@distinct='system']">
<xsl:call-template name="check-property-value-distinct">
<xsl:with-param name="property" select="."/>
</xsl:call-template>
</xsl:when>
<xsl:when test="adl:property[@distinct='all']">
<xsl:call-template name="check-property-value-distinct">
<xsl:with-param name="property" select="."/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template> </xsl:template>
<xsl:template match="adl:property[@distinct='system']">
<xsl:call-template name="check-property-value-distinct">
<xsl:with-param name="property" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template match="adl:property[@distinct='all']">
<xsl:call-template name="check-property-value-distinct">
<xsl:with-param name="property" select="."/>
</xsl:call-template>
</xsl:template>
<!-- suppress properties otherwise --> <!-- suppress properties otherwise -->
<xsl:template match="adl:property"/> <xsl:template match="adl:property"/>
@ -539,12 +589,10 @@ namespace <xsl:value-of select="$controllerns"/> {
/// Handle the submission of the form named <xsl:value-of select="@name"/> /// Handle the submission of the form named <xsl:value-of select="@name"/>
/// &lt;/summary&gt; /// &lt;/summary&gt;
[AccessibleThrough(Verb.Post)] [AccessibleThrough(Verb.Post)]
public void <xsl:value-of select="concat( @name, 'SubmitHandler')"/>( ) public void <xsl:value-of select="concat( @name, 'SubmitHandler')"/>( ) {
{
string command = Form[ "command"]; string command = Form[ "command"];
if ( command == null) if ( command == null) {
{
throw new Exception( "No command?"); throw new Exception( "No command?");
} }
else else
@ -565,6 +613,7 @@ namespace <xsl:value-of select="$controllerns"/> {
<xsl:call-template name="fetch-instance"> <xsl:call-template name="fetch-instance">
<xsl:with-param name="entity" select="ancestor::adl:entity"/> <xsl:with-param name="entity" select="ancestor::adl:entity"/>
<xsl:with-param name="initialise-locals" select="'true'"/>
</xsl:call-template> </xsl:call-template>
<xsl:if test="$authentication-layer = 'Database'"> <xsl:if test="$authentication-layer = 'Database'">
@ -716,6 +765,78 @@ namespace <xsl:value-of select="$controllerns"/> {
/* <xsl:apply-templates/> */ /* <xsl:apply-templates/> */
</xsl:template> </xsl:template>
<!-- for properties with @distinct='all' or @distinct='system',
check that values are indeed distinct -->
<xsl:template name="check-property-value-distinct">
<xsl:param name="property"/>
<xsl:variable name="basetype">
<xsl:call-template name="base-type">
<xsl:with-param name="property" select="."/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$property/ancestor::adl:key and not( $property/ancestor::adl:entity/adl:key[position()=2])">
<!-- if I'm the property of a key field and it is the only key field of my entity then
it is pointless trying to establish my distinctness -->
</xsl:when>
<xsl:when test="$property/@distinct = 'system' or $property/@distinct='all'">
/* <xsl:value-of select="$property/@name"/> should be distinct: check that it is */
matches =
hibernator.CreateCriteria(typeof(<xsl:value-of select="$property/ancestor::adl:entity/@name"/>))
<xsl:choose>
<xsl:when test="$basetype='string'">
.Add(Expression.Eq("<xsl:value-of select="$property/@name"/>", Form["<xsl:value-of select="concat('instance.', $property/@name)"/>"]))
</xsl:when>
<xsl:when test="$basetype='integer'">
.Add(Expression.Eq("<xsl:value-of select="$property/@name"/>", Int32.Parse( Form["<xsl:value-of select="concat('instance.', $property/@name)"/>"])))
</xsl:when>
<xsl:when test="$basetype='entity'">
.Add(Expression.Eq("<xsl:value-of select="$property/@name"/>",
<xsl:call-template name="fetch-property-instance">
<xsl:with-param name="property" select="."/>
<xsl:with-param name="valuename" select="adl:property[position()=1]/@name"/>
</xsl:call-template>))
</xsl:when>
<xsl:otherwise>
<xsl:message terminate="yes">
ADL: ERROR: don't yet know how to do distinctness checks for properties
with base type <xsl:value-of select="$basetype"/>
</xsl:message>
</xsl:otherwise>
</xsl:choose>
<xsl:for-each select="$property/ancestor::adl:entity/adl:key">
.Add(Expression.Not(Expression.Eq("<xsl:value-of select="adl:property[position()=1]/@name"/>",<xsl:choose>
<xsl:when test="@type='entity'">
<xsl:call-template name="fetch-property-instance">
<!-- recurse -->
<xsl:with-param name="property" select="."/>
<xsl:with-param name="valuename" select="adl:property[position()=1]/@name"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="@type='integer'">
Int32.Parse( Form["<xsl:value-of select="adl:property[position()=1]/@name"/>"])
</xsl:when>
<xsl:otherwise>
Form["<xsl:value-of select="adl:property[position()=1]/@name"/>"]
</xsl:otherwise>
</xsl:choose>)))
</xsl:for-each>
.List&lt;<xsl:value-of select="$property/ancestor::adl:entity/@name"/>&gt;();
if ( matches.Count > 0)
{
throw new
DataNotDistinctException(
String.Format("There is already a {0} with the {1} '{2}'",
"<xsl:value-of select="$property/ancestor::adl:entity/@name"/>", "<xsl:value-of select="$property/@name"/>", Form["<xsl:value-of select="concat('instance.', $property/@name)"/>"]));
}
</xsl:when>
</xsl:choose>
</xsl:template>
<!-- produce all menus for a given form: harder, but more efficient --> <!-- produce all menus for a given form: harder, but more efficient -->
<xsl:template name="formmenus"> <xsl:template name="formmenus">
<xsl:param name="form"/> <xsl:param name="form"/>
@ -876,11 +997,15 @@ namespace <xsl:value-of select="$controllerns"/> {
declared) called 'record' --> declared) called 'record' -->
<xsl:template name="fetch-instance"> <xsl:template name="fetch-instance">
<xsl:param name="entity"/> <xsl:param name="entity"/>
<xsl:value-of select="concat($entityns, '.', $entity/@name)"/> record = null; <xsl:param name="initialise-locals" select="'false'"/>
/* check whether values for all key slots have been passed in; if so, we're probably dealing with an /* check whether values for all key slots have been passed in; if so, we're probably dealing with an
* existing record */ * existing record */
<xsl:if test="$initialise-locals = 'true'">
/* the instance (record) of type <xsl:value-of select="$entity/@name"/> we're dealing with */
<xsl:value-of select="concat($entityns, '.', $entity/@name)"/> record = null;
/* whether we have valid values for all the key fields */
bool allkeys = true; bool allkeys = true;
</xsl:if>
<xsl:for-each select="$entity/adl:key/adl:property"> <xsl:for-each select="$entity/adl:key/adl:property">
if ( String.IsNullOrEmpty( Form["<xsl:value-of select="concat( 'instance.', @name)"/>"])) { if ( String.IsNullOrEmpty( Form["<xsl:value-of select="concat( 'instance.', @name)"/>"])) {
allkeys = false; allkeys = false;
@ -893,7 +1018,6 @@ namespace <xsl:value-of select="$controllerns"/> {
if ( allkeys){ if ( allkeys){
/* it's (probably) existing, retrieve it */ /* it's (probably) existing, retrieve it */
record = hibernator.CreateCriteria(typeof(<xsl:value-of select="concat( $entityns, '.', $entity/@name)"/>)) record = hibernator.CreateCriteria(typeof(<xsl:value-of select="concat( $entityns, '.', $entity/@name)"/>))
<xsl:for-each select="$entity/adl:key/adl:property"> <xsl:for-each select="$entity/adl:key/adl:property">
<xsl:variable name="basetype"> <xsl:variable name="basetype">