From 4efb9098808df8eda0a9ef795fed0f791789ce76 Mon Sep 17 00:00:00 2001 From: sb Date: Thu, 17 Apr 2008 17:33:16 +0000 Subject: [PATCH] bug 1801 : Better understanding of how 'Assert...' methods are supposed to work, leading to simpler code. --- transforms01/adl2controllerclasses.xslt | 334 ++++++++++++------------ 1 file changed, 161 insertions(+), 173 deletions(-) diff --git a/transforms01/adl2controllerclasses.xslt b/transforms01/adl2controllerclasses.xslt index b45a080..6c776cd 100755 --- a/transforms01/adl2controllerclasses.xslt +++ b/transforms01/adl2controllerclasses.xslt @@ -9,8 +9,8 @@ Transform ADL into (partial) controller classes $Author: sb $ - $Revision: 1.20 $ - $Date: 2008-04-17 16:16:19 $ + $Revision: 1.21 $ + $Date: 2008-04-17 17:33:16 $ --> @@ -214,12 +214,12 @@ namespace { - hibernator.CreateCriteria( typeof( )) - .Add(Expression.Eq( "", + hibernator.CreateCriteria( typeof( )) + .Add(Expression.Eq( "", Int32.Parse( Form[ ""]) Form[ ""] )) - .UniqueResult<>() + .UniqueResult<>() Form[""] @@ -227,70 +227,69 @@ namespace { , ); - } - catch ( FormatException) { - /* failed to parse a number - not wholly unexpected, since it's most likely - that an empty string was passed in */ - record = new (); - } - catch ( NullReferenceException) { - /* again, probably more normal than otherwise */ - record = new (); - } - messages.Add( "New record created"); - isnewborn = true; } - else - { - AddError( "You are not authorised to create objects of type "); + catch ( FormatException) { + /* failed to parse a number - not wholly unexpected, since it's most likely + that an empty string was passed in */ + record = new (); } + catch ( NullReferenceException) { + /* again, probably more normal than otherwise */ + record = new (); + } + messages.Add( "New record created"); + isnewborn = true; } if ( record != null) { - if ( isnewborn || AssertUserCanUpdate()) - { - try { - /* actually update the record */ - BindObjectInstance( record, ParamStore.Form, "instance"); + if ( ! isnewborn) { + // isnewborn cannot be true unless we've already checked user can create + // so no need to do it again here + AssertUserCanUpdate(); + } + + try { + /* actually update the record */ + BindObjectInstance( record, ParamStore.Form, "instance"); - /* there is at least one slot whose value is an internationalised message; - * if these have yet to be initialised they must be handled specially */ - Locale locale = GetBestLocaleForUser(); + /* there is at least one slot whose value is an internationalised message; + * if these have yet to be initialised they must be handled specially */ + Locale locale = GetBestLocaleForUser(); - if ( ! String.IsNullOrEmpty( Form[""])){ - /* there's an uninitialised message for this slot */ - Message mess = record.; - if ( mess == null){ - mess = new Message(); - } - hibernator.Save( mess); - - Translation trans = mess.GetTranslationObject( locale, hibernator); - if ( trans == null) { - trans = new Translation( mess, locale); - } - trans.MessageText = Form[""]; - record. = mess; - hibernator.Save( trans); + if ( ! String.IsNullOrEmpty( Form[""])){ + /* there's an uninitialised message for this slot */ + Message mess = record.; + if ( mess == null){ + mess = new Message(); } + hibernator.Save( mess); + + Translation trans = mess.GetTranslationObject( locale, hibernator); + if ( trans == null) { + trans = new Translation( mess, locale); + } + trans.MessageText = Form[""]; + record. = mess; + hibernator.Save( trans); + } - /* 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. */ + /* 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. */ - if ( ! String.IsNullOrEmpty( Form[""])) - { - record. = - - - ; - } + if ( ! String.IsNullOrEmpty( Form[""])) + { + record. = + + + ; + } @@ -302,98 +301,92 @@ namespace { - /* 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! */ + /* 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! */ - string[] = Form.GetValues( ""); + string[] = Form.GetValues( ""); - if ( != null) + if ( != null) + { + /* update the linking table for my ; first smash the old values */ + if ( != null) { - /* update the linking table for my ; first smash the old values */ - if ( != null) - { - .Clear(); - } - else - { - = new HashedSet<>(); - } - - /* then reinstate the values from the indexes passed */ - foreach ( string index in ) - { - .Add( - hibernator.CreateCriteria(typeof()) - .Add(Expression.Eq("Id", index)) - .UniqueResult<.>()); - } + .Clear(); } + else + { + = new HashedSet<>(); + } + + /* then reinstate the values from the indexes passed */ + foreach ( string index in ) + { + .Add( + hibernator.CreateCriteria(typeof()) + .Add(Expression.Eq("Id", index)) + .UniqueResult<.>()); + } + } - /* 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( "") != null) + /* 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( "") != null) + { + string[] = Form.GetValues( ""); + + /* updating child records; first remove any not on the submitted list */ + foreach ( item in record.) { - string[] = Form.GetValues( ""); + String itemId = item.KeyString; + bool found = false; - /* updating child records; first remove any not on the submitted list */ - foreach ( item in record.) - { - String itemId = item.KeyString; - bool found = false; - - foreach ( string index in ) - { - - if ( index.Equals( itemId)) - { - found = true; - } - } - - if ( ! found) - { - record..Remove( item); - } - } - - /* then add any on the included list which are not already members */ foreach ( string index in ) { - - item = - hibernator.CreateCriteria(typeof()) - .Add(Expression.Eq("Id", index)) - .UniqueResult<.>(); - - if ( ! record..Contains( item)) + + if ( index.Equals( itemId)) { - record..Add( item); + found = true; } } + + if ( ! found) + { + record..Remove( item); + } } + + /* then add any on the included list which are not already members */ + foreach ( string index in ) + { + + item = + hibernator.CreateCriteria(typeof()) + .Add(Expression.Eq("Id", index)) + .UniqueResult<.>(); + + if ( ! record..Contains( item)) + { + record..Add( item); + } + } + } - /* write the record to the database, in order to guarantee we have a valid key */ - hibernator.Save(record); - hibernator.Flush(); + /* write the record to the database, in order to guarantee we have a valid key */ + hibernator.Save(record); + hibernator.Flush(); - messages.Add( "Record saved successfully"); - - - } - catch ( DataSuitabilityException dse) - { - AddError( dse.Message); - } - catch ( ApplicationException axe) - { - AddError( axe.Message); - } + messages.Add( "Record saved successfully"); } - else { - AddError( "You are not authorised to update objects of type "); + catch ( DataSuitabilityException dse) + { + AddError( dse.Message); + } + catch ( ApplicationException axe) + { + AddError( axe.Message); } PropertyBag["messages"] = messages; @@ -433,42 +426,38 @@ namespace { [AccessibleThrough(Verb.Post)] public void Delete() { - if ( AssertUserCanDelete()) - { - ISession hibernator = + AssertUserCanDelete(); + ISession hibernator = NHibernateHelper.GetCurrentSession( Session[ NHibernateHelper.USERTOKEN], Session[NHibernateHelper.PASSTOKEN]); - string reallydelete = Params["reallydelete"]; + string reallydelete = Params["reallydelete"]; - if ( "true".Equals( reallydelete)) - { + if ( "true".Equals( reallydelete)) + { - if ( record != null) - { - hibernator.Delete( record); + if ( record != null) + { + hibernator.Delete( record); - hibernator.Flush(); - } - else - { - throw new ApplicationException( "No such record?"); - } + hibernator.Flush(); } + else + { + throw new ApplicationException( "No such record?"); + } + } - Redirect( ""); + Redirect( ""); - Redirect( FormsAuthentication.DefaultUrl); + Redirect( FormsAuthentication.DefaultUrl); - } else { - AddError( "You are not authorised to delete objects of type "); - } } @@ -492,25 +481,22 @@ namespace { /// <param name="view">The name of the list view to show</param> public void InternalShowList( String view) { - if ( AssertUserCanRead()) { - ISession hibernator = - NHibernateHelper.GetCurrentSession( Session[ NHibernateHelper.USERTOKEN], + AssertUserCanRead(); + ISession hibernator = + NHibernateHelper.GetCurrentSession( Session[ NHibernateHelper.USERTOKEN], Session[NHibernateHelper.PASSTOKEN]); - IList<> instances = - hibernator.CreateCriteria(typeof()) - - .List<>(); + IList<> instances = + hibernator.CreateCriteria(typeof()) + + .List<>(); - - PropertyBag["username"] = Session[ NHibernateHelper.USERTOKEN]; - - PropertyBag["instances"] = - PaginationHelper.CreatePagination( this, instances, 25); + + PropertyBag["username"] = Session[ NHibernateHelper.USERTOKEN]; + + PropertyBag["instances"] = + PaginationHelper.CreatePagination( this, instances, 25); - RenderViewWithFailover(view + ".vm", view + ".auto.vm"); - } else { - AddError( "You are not authorised to delete view of type "); - } + RenderViewWithFailover(view + ".vm", view + ".auto.vm"); } } @@ -604,6 +590,8 @@ namespace { [AccessibleThrough(Verb.Get)] public void ( ) { + AssertUserCanRead(); + ISession hibernator = NHibernateHelper.GetCurrentSession( Session[ NHibernateHelper.USERTOKEN], Session[NHibernateHelper.PASSTOKEN]);