bug 1801 : Better understanding of how 'Assert...' methods are supposed to work, leading to simpler code.

This commit is contained in:
sb 2008-04-17 17:33:16 +00:00
parent fd3bfabe3a
commit 4efb909880

View file

@ -9,8 +9,8 @@
Transform ADL into (partial) controller classes Transform ADL into (partial) controller classes
$Author: sb $ $Author: sb $
$Revision: 1.20 $ $Revision: 1.21 $
$Date: 2008-04-17 16:16:19 $ $Date: 2008-04-17 17:33:16 $
--> -->
<!-- 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.20 $', 10)"/> // adl2controllerclasses.xslt version <xsl:value-of select="substring( '$Revision: 1.21 $', 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.20 $', 10)"/> // adl2controllerclasses.xslt version <xsl:value-of select="substring( '$Revision: 1.21 $', 10)"/>
// //
// This file is automatically generated; DO NOT EDIT IT. // This file is automatically generated; DO NOT EDIT IT.
// //
@ -182,11 +182,11 @@ 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>);
List&lt;string&gt; messages = new List&lt;string&gt;(); List&lt;string&gt; messages = new List&lt;string&gt;();
// A 'newborn' instance can be updated even if the current user doesn't have // A 'newborn' instance can be updated even if the current user doesn't have
// update permissions, seeing that we use an update operation to set the // update permissions, seeing that we use an update operation to set the
// 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:apply-templates select="descendant::adl:property"/>
<xsl:call-template name="fetch-instance"> <xsl:call-template name="fetch-instance">
@ -194,18 +194,18 @@ namespace <xsl:value-of select="$controllerns"/> {
</xsl:call-template> </xsl:call-template>
if ( record == null) { if ( record == null) {
if ( AssertUserCanCreate()) { /* it seems to be new, create persistent object */
/* it seems to be new, create persistent object */ 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">
<xsl:call-template name="base-type"> <xsl:call-template name="base-type">
<xsl:with-param name="property" select="."/> <xsl:with-param name="property" select="."/>
</xsl:call-template> </xsl:call-template>
</xsl:variable> </xsl:variable>
<xsl:choose> <xsl:choose>
<xsl:when test="$basetype='integer'">Int32.Parse( Form["<xsl:value-of select="concat( 'instance.', @name)"/>"])</xsl:when> <xsl:when test="$basetype='integer'">Int32.Parse( Form["<xsl:value-of select="concat( 'instance.', @name)"/>"])</xsl:when>
<xsl:when test="$basetype='entity'"> <xsl:when test="$basetype='entity'">
<!-- Maybe TODO: this doesn't work recursively - if an entity has a key which is an entity <!-- Maybe TODO: this doesn't work recursively - if an entity has a key which is an entity
and the key of that entity is an entity, you're on your own, mate! --> and the key of that entity is an entity, you're on your own, mate! -->
<xsl:variable name="keyentity" select="@entity"/> <xsl:variable name="keyentity" select="@entity"/>
@ -214,12 +214,12 @@ namespace <xsl:value-of select="$controllerns"/> {
<xsl:with-param name="entity" select="//adl:entity[@name=$keyentity]"/> <xsl:with-param name="entity" select="//adl:entity[@name=$keyentity]"/>
</xsl:call-template> </xsl:call-template>
</xsl:variable> </xsl:variable>
hibernator.CreateCriteria( typeof( <xsl:value-of select="concat( $entityns, '.', $keyentity)"/>)) hibernator.CreateCriteria( typeof( <xsl:value-of select="concat( $entityns, '.', $keyentity)"/>))
.Add(Expression.Eq( "<xsl:value-of select="//adl:entity[@name=$keyentity]/adl:key/adl:property[position()=1]/@name"/>",<xsl:choose> .Add(Expression.Eq( "<xsl:value-of select="//adl:entity[@name=$keyentity]/adl:key/adl:property[position()=1]/@name"/>",<xsl:choose>
<xsl:when test="$keyenttype = 'int'">Int32.Parse( Form[ "<xsl:value-of select="concat( 'instance.', @name)"/>"])</xsl:when> <xsl:when test="$keyenttype = 'int'">Int32.Parse( Form[ "<xsl:value-of select="concat( 'instance.', @name)"/>"])</xsl:when>
<xsl:otherwise>Form[ "<xsl:value-of select="concat( 'instance.', @name)"/>"]</xsl:otherwise> <xsl:otherwise>Form[ "<xsl:value-of select="concat( 'instance.', @name)"/>"]</xsl:otherwise>
</xsl:choose>)) </xsl:choose>))
.UniqueResult&lt;<xsl:value-of select="concat( $entityns, '.', $keyentity)"/>&gt;()</xsl:when> .UniqueResult&lt;<xsl:value-of select="concat( $entityns, '.', $keyentity)"/>&gt;()</xsl:when>
<xsl:otherwise>Form["<xsl:value-of select="concat( 'instance.', @name)"/>"]</xsl:otherwise> <xsl:otherwise>Form["<xsl:value-of select="concat( 'instance.', @name)"/>"]</xsl:otherwise>
</xsl:choose> </xsl:choose>
<xsl:choose> <xsl:choose>
@ -227,70 +227,69 @@ namespace <xsl:value-of select="$controllerns"/> {
<xsl:otherwise>, </xsl:otherwise> <xsl:otherwise>, </xsl:otherwise>
</xsl:choose> </xsl:choose>
</xsl:for-each>); </xsl:for-each>);
}
catch ( FormatException) {
/* failed to parse a number - not wholly unexpected, since it's most likely
that an empty string was passed in */
record = new <xsl:value-of select="concat($entityns, '.', @name)"/>();
}
catch ( NullReferenceException) {
/* again, probably more normal than otherwise */
record = new <xsl:value-of select="concat($entityns, '.', @name)"/>();
}
messages.Add( "New <xsl:value-of select="@name"/> record created");
isnewborn = true;
} }
else catch ( FormatException) {
{ /* failed to parse a number - not wholly unexpected, since it's most likely
AddError( "You are not authorised to create objects of type <xsl:value-of select="@name"/>"); that an empty string was passed in */
record = new <xsl:value-of select="concat($entityns, '.', @name)"/>();
} }
catch ( NullReferenceException) {
/* again, probably more normal than otherwise */
record = new <xsl:value-of select="concat($entityns, '.', @name)"/>();
}
messages.Add( "New <xsl:value-of select="@name"/> record created");
isnewborn = true;
} }
if ( record != null) { if ( record != null) {
if ( isnewborn || AssertUserCanUpdate()) if ( ! isnewborn) {
{ // isnewborn cannot be true unless we've already checked user can create
try { // so no need to do it again here
/* actually update the record */ AssertUserCanUpdate();
BindObjectInstance( record, ParamStore.Form, "instance"); }
try {
/* actually update the record */
BindObjectInstance( record, ParamStore.Form, "instance");
<xsl:if test="descendant::adl:property[@type='message']"> <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();
<xsl:for-each select="descendant::adl:property[@type='message']"> <xsl:for-each select="descendant::adl:property[@type='message']">
if ( ! String.IsNullOrEmpty( Form["<xsl:value-of select="concat( 'i18n.instance.', @name)"/>"])){ if ( ! String.IsNullOrEmpty( Form["<xsl:value-of select="concat( 'i18n.instance.', @name)"/>"])){
/* there's an uninitialised message for this slot */ /* there's an uninitialised message for this slot */
Message mess = record.<xsl:value-of select="@name"/>; Message mess = record.<xsl:value-of select="@name"/>;
if ( mess == null){ if ( mess == null){
mess = new Message(); mess = new Message();
}
hibernator.Save( mess);
Translation trans = mess.GetTranslationObject( locale, hibernator);
if ( trans == null) {
trans = new Translation( mess, locale);
}
trans.MessageText = Form["<xsl:value-of select="concat( 'i18n.instance.', @name)"/>"];
record.<xsl:value-of select="@name"/> = mess;
hibernator.Save( trans);
} }
hibernator.Save( mess);
Translation trans = mess.GetTranslationObject( locale, hibernator);
if ( trans == null) {
trans = new Translation( mess, locale);
}
trans.MessageText = Form["<xsl:value-of select="concat( 'i18n.instance.', @name)"/>"];
record.<xsl:value-of select="@name"/> = mess;
hibernator.Save( trans);
}
</xsl:for-each> </xsl:for-each>
</xsl:if> </xsl:if>
<xsl:for-each select="descendant::adl:property[@type='entity']"> <xsl:for-each select="descendant::adl:property[@type='entity']">
/* for properties of type 'entity', it should not be necessary to do anything /* for properties of type 'entity', it should not be necessary to do anything
* special - BindObjectInstance /should/ do it all. Unfortunately it sometimes * special - BindObjectInstance /should/ do it all. Unfortunately it sometimes
* doesn't, and I haven't yet characterised why not. */ * doesn't, and I haven't yet characterised why not. */
<xsl:variable name="entityname" select="@entity"/> <xsl:variable name="entityname" select="@entity"/>
<xsl:choose> <xsl:choose>
<xsl:when test="//adl:entity[@name=$entityname]"> <xsl:when test="//adl:entity[@name=$entityname]">
if ( ! String.IsNullOrEmpty( Form["<xsl:value-of select="concat( 'instance.', @name)"/>"])) if ( ! String.IsNullOrEmpty( Form["<xsl:value-of select="concat( 'instance.', @name)"/>"]))
{ {
record.<xsl:value-of select="@name"/> = <xsl:call-template name="fetch-property-instance"> record.<xsl:value-of select="@name"/> = <xsl:call-template name="fetch-property-instance">
<xsl:with-param name="property" select="."/> <xsl:with-param name="property" select="."/>
<xsl:with-param name="valuename" select="concat( 'instance.', @name)"/> <xsl:with-param name="valuename" select="concat( 'instance.', @name)"/>
</xsl:call-template>; </xsl:call-template>;
} }
</xsl:when> </xsl:when>
<xsl:otherwise> <xsl:otherwise>
<xsl:message terminate="yes"> <xsl:message terminate="yes">
@ -302,98 +301,92 @@ namespace <xsl:value-of select="$controllerns"/> {
</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 /* 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! */
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)"/>");
if ( <xsl:value-of select="concat(@name, 'Values')"/> != null) if ( <xsl:value-of select="concat(@name, 'Values')"/> != null)
{
/* update the linking table for my <xsl:value-of select="@name"/>; first smash the old values */
if ( <xsl:value-of select="concat( 'record.', @name)"/> != null)
{ {
/* update the linking table for my <xsl:value-of select="@name"/>; first smash the old values */ <xsl:value-of select="concat( 'record.', @name)"/>.Clear();
if ( <xsl:value-of select="concat( 'record.', @name)"/> != null)
{
<xsl:value-of select="concat( 'record.', @name)"/>.Clear();
}
else
{
<xsl:value-of select="concat( 'record.', @name)"/> = new HashedSet&lt;<xsl:value-of select="@entity"/>&gt;();
}
/* then reinstate the values from the indexes passed */
foreach ( string index in <xsl:value-of select="concat(@name, 'Values')"/>)
{
<xsl:value-of select="concat( 'record.', @name)"/>.Add(
hibernator.CreateCriteria(typeof(<xsl:value-of select="@entity"/>))
.Add(Expression.Eq("<xsl:value-of select="@entity"/>Id", index))
.UniqueResult&lt;<xsl:value-of select="$entityns"/>.<xsl:value-of select="@entity"/>&gt;());
}
} }
else
{
<xsl:value-of select="concat( 'record.', @name)"/> = new HashedSet&lt;<xsl:value-of select="@entity"/>&gt;();
}
/* then reinstate the values from the indexes passed */
foreach ( string index in <xsl:value-of select="concat(@name, 'Values')"/>)
{
<xsl:value-of select="concat( 'record.', @name)"/>.Add(
hibernator.CreateCriteria(typeof(<xsl:value-of select="@entity"/>))
.Add(Expression.Eq("<xsl:value-of select="@entity"/>Id", index))
.UniqueResult&lt;<xsl:value-of select="$entityns"/>.<xsl:value-of select="@entity"/>&gt;());
}
}
</xsl:for-each> </xsl:for-each>
<xsl:for-each select="descendant::adl:property[@type='list']"> <xsl:for-each select="descendant::adl:property[@type='list']">
/* with a list we cannot just smash the old values! Instead we need to check /* with a list we cannot just smash the old values! Instead we need to check
* each one and exclude it if no longer required */ * each one and exclude it if no longer required */
if ( Form.GetValues( "<xsl:value-of select="concat( 'instance.', @name)"/>") != null) if ( Form.GetValues( "<xsl:value-of select="concat( 'instance.', @name)"/>") != null)
{
string[] <xsl:value-of select="concat(@name, 'Values')"/> = Form.GetValues( "<xsl:value-of select="concat( 'instance.', @name)"/>");
/* updating <xsl:value-of select="@name"/> child records; first remove any not on the submitted list */
foreach ( <xsl:value-of select="@entity"/> item in record.<xsl:value-of select="@name"/>)
{ {
string[] <xsl:value-of select="concat(@name, 'Values')"/> = Form.GetValues( "<xsl:value-of select="concat( 'instance.', @name)"/>"); String itemId = item.KeyString;
bool found = false;
/* updating <xsl:value-of select="@name"/> child records; first remove any not on the submitted list */
foreach ( <xsl:value-of select="@entity"/> item in record.<xsl:value-of select="@name"/>)
{
String itemId = item.KeyString;
bool found = false;
foreach ( string index in <xsl:value-of select="concat(@name, 'Values')"/>)
{
<!-- TODO: this could definitely be made more efficient -->
if ( index.Equals( itemId))
{
found = true;
}
}
if ( ! found)
{
record.<xsl:value-of select="@name"/>.Remove( item);
}
}
/* 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')"/>)
{ {
<xsl:variable name="entityname" select="@entity"/> <!-- TODO: this could definitely be made more efficient -->
<xsl:value-of select="@entity"/> item = if ( index.Equals( itemId))
hibernator.CreateCriteria(typeof(<xsl:value-of select="@entity"/>))
.Add(Expression.Eq("<xsl:value-of select="@entity"/>Id", index))
.UniqueResult&lt;<xsl:value-of select="$entityns"/>.<xsl:value-of select="@entity"/>&gt;();
if ( ! record.<xsl:value-of select="@name"/>.Contains( item))
{ {
record.<xsl:value-of select="@name"/>.Add( item); found = true;
} }
} }
if ( ! found)
{
record.<xsl:value-of select="@name"/>.Remove( item);
}
} }
/* then add any on the included list which are not already members */
foreach ( string index in <xsl:value-of select="concat(@name, 'Values')"/>)
{
<xsl:variable name="entityname" select="@entity"/>
<xsl:value-of select="@entity"/> item =
hibernator.CreateCriteria(typeof(<xsl:value-of select="@entity"/>))
.Add(Expression.Eq("<xsl:value-of select="@entity"/>Id", index))
.UniqueResult&lt;<xsl:value-of select="$entityns"/>.<xsl:value-of select="@entity"/>&gt;();
if ( ! record.<xsl:value-of select="@name"/>.Contains( item))
{
record.<xsl:value-of select="@name"/>.Add( item);
}
}
}
</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();
messages.Add( "Record saved successfully"); messages.Add( "Record saved successfully");
}
catch ( DataSuitabilityException dse)
{
AddError( dse.Message);
}
catch ( ApplicationException axe)
{
AddError( axe.Message);
}
} }
else { catch ( DataSuitabilityException dse)
AddError( "You are not authorised to update objects of type <xsl:value-of select="@name"/>"); {
AddError( dse.Message);
}
catch ( ApplicationException axe)
{
AddError( axe.Message);
} }
PropertyBag["messages"] = messages; PropertyBag["messages"] = messages;
@ -433,42 +426,38 @@ namespace <xsl:value-of select="$controllerns"/> {
[AccessibleThrough(Verb.Post)] [AccessibleThrough(Verb.Post)]
public void Delete() public void Delete()
{ {
if ( AssertUserCanDelete()) AssertUserCanDelete();
{ ISession hibernator =
ISession hibernator =
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>);
string reallydelete = Params["reallydelete"]; string reallydelete = Params["reallydelete"];
if ( "true".Equals( reallydelete)) if ( "true".Equals( reallydelete))
{ {
<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:call-template> </xsl:call-template>
if ( record != null) if ( record != null)
{ {
hibernator.Delete( record); hibernator.Delete( record);
hibernator.Flush(); hibernator.Flush();
}
else
{
throw new ApplicationException( "No such record?");
}
} }
else
{
throw new ApplicationException( "No such record?");
}
}
<xsl:choose> <xsl:choose>
<xsl:when test="adl:list"> <xsl:when test="adl:list">
Redirect( "<xsl:value-of select="concat(adl:list[position()=1]/@name, '.rails')"/>"); Redirect( "<xsl:value-of select="concat(adl:list[position()=1]/@name, '.rails')"/>");
</xsl:when> </xsl:when>
<xsl:otherwise> <xsl:otherwise>
Redirect( FormsAuthentication.DefaultUrl); Redirect( FormsAuthentication.DefaultUrl);
</xsl:otherwise> </xsl:otherwise>
</xsl:choose> </xsl:choose>
} else {
AddError( "You are not authorised to delete objects of type <xsl:value-of select="@name"/>");
}
} }
</xsl:if> </xsl:if>
<xsl:apply-templates select="adl:form"/> <xsl:apply-templates select="adl:form"/>
@ -492,25 +481,22 @@ namespace <xsl:value-of select="$controllerns"/> {
/// &lt;param name="view"&gt;The name of the list view to show&lt;/param&gt; /// &lt;param name="view"&gt;The name of the list view to show&lt;/param&gt;
public void InternalShowList( String view) public void InternalShowList( String view)
{ {
if ( AssertUserCanRead()) { AssertUserCanRead();
ISession hibernator = ISession hibernator =
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="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;();
<xsl:if test="$authentication-layer = 'Database'"> <xsl:if test="$authentication-layer = 'Database'">
PropertyBag["username"] = Session[ NHibernateHelper.USERTOKEN]; PropertyBag["username"] = Session[ NHibernateHelper.USERTOKEN];
</xsl:if> </xsl:if>
PropertyBag["instances"] = PropertyBag["instances"] =
PaginationHelper.CreatePagination( this, instances, 25); PaginationHelper.CreatePagination( this, instances, 25);
RenderViewWithFailover(view + ".vm", view + ".auto.vm"); RenderViewWithFailover(view + ".vm", view + ".auto.vm");
} else {
AddError( "You are not authorised to delete view of type <xsl:value-of select="@name"/>");
}
} }
</xsl:if> </xsl:if>
} }
@ -604,6 +590,8 @@ namespace <xsl:value-of select="$controllerns"/> {
[AccessibleThrough(Verb.Get)] [AccessibleThrough(Verb.Get)]
public void <xsl:value-of select="@name"/>( ) public void <xsl:value-of select="@name"/>( )
{ {
AssertUserCanRead();
ISession hibernator = ISession hibernator =
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>);