/* ---- [ cut here: next file 'Controller.auto.cs'] ---------------- */ //------------------------------------------------------------------ // // Application Description Language framework // Controller.auto.cs // // (c) 2007 Cygnet Solutions Ltd // // Controller for auto-generated forms for editing s // Automatically generated from application description using // adl2controllerclasses.xslt version // // This file is automatically generated; DO NOT EDIT IT. // //------------------------------------------------------------------ using System; using System.Data; using System.Collections.Generic; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using Castle.MonoRail.Framework.Helpers; using Cygnet.Exceptions; using Cygnet.Web.Helpers; using Cygnet.Web.Controllers; using NHibernate; using NHibernate.Expression; using Castle.MonoRail.Framework; using Iesi.Collections.Generic; using ; namespace { /// <summary> /// Automatically generated partial controller class following 'thin controller' /// strategy, for entity . Note that part of this /// class may be defined in a separate file called /// Controller.manual.cs, q.v. /// /// DO NOT EDIT THIS FILE! /// </summary> [ Layout("empty"), Rescue("generalerror"), ControllerDetails("", Area = "auto")] public partial class Controller : BaseController { /// <summary> /// Store the record represented by the parameters passed in an HTTP service /// Without Id -> it's new, I create a new persistent object; /// With Id -> it's existing, I update the existing persistent object. /// NOTE: Should only be called from a handler for method 'POST', never 'GET'. /// </summary> private void Store() { ISession hibernator = NHibernateHelper.GetCurrentSession( Session[ NHibernateHelper.USERTOKEN], Session[NHibernateHelper.PASSTOKEN]); List<string> messages = new List<string>(); if ( record == null) { /* it seems to be new, create persistent object */ try { record = new ( Int32.Parse( Form[""]) hibernator.CreateCriteria( typeof( )) .Add(Expression.Eq( "", Int32.Parse( Form[ ""]) Form[ ""] )) .UniqueResult<>() Form[""] , ); } 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 (); } /* perform any domain knowledge behaviour on the new record * after instantiation */ record.AfterCreationHook( hibernator); messages.Add( "New record created"); } if ( record != null) { 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(); 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. */ if ( ! String.IsNullOrEmpty( Form[""])) { record. = ; } ADL: ERROR: Could not fix up value of , because no entity was found called /* 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( ""); 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<.>()); } } /* 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 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)) { record..Add( item); } } } /* perform any domain knowledge behaviour on the record prior to updating */ record.BeforeUpdateHook( hibernator); /* write the record to the database, in order to guarantee we have a valid key */ hibernator.Save(record); hibernator.Flush(); /* perform any domain knowledge behaviour on the record after updating */ record.AfterUpdateHook( hibernator); messages.Add( "Record saved successfully"); } catch ( DataSuitabilityException dse) { AddError( dse.Message); } catch ( ApplicationException axe) { AddError( axe.Message); } PropertyBag["messages"] = messages; PropertyBag["instance"] = record; PropertyBag["username"] = Session[ NHibernateHelper.USERTOKEN]; if ( ! AssertNoErrors()) { /* the session may be polluted; create a new session */ NHibernateHelper.CloseSession(); hibernator = NHibernateHelper.GetCurrentSession( Session[ NHibernateHelper.USERTOKEN], Session[NHibernateHelper.PASSTOKEN] ); } RenderViewWithFailover("", ""); } else { throw new DataRequiredException( "Record not found"); } } /// <summary> /// Actually delete the selected record /// </summary> [AccessibleThrough(Verb.Post)] public void Delete() { ISession hibernator = NHibernateHelper.GetCurrentSession( Session[ NHibernateHelper.USERTOKEN], Session[NHibernateHelper.PASSTOKEN]); string reallydelete = Params["reallydelete"]; if ( "true".Equals( reallydelete)) { if ( record != null) { record.BeforeDeleteHook( hibernator); hibernator.Delete( record); hibernator.Flush(); } else { throw new ApplicationException( "No such record?"); } } InternalShowList(); Redirect( FormsAuthentication.DefaultUrl); } /// <summary> /// 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 /// you've a special reason for doing something different /// </summary> public void InternalShowList() { InternalShowList( ""); } /// <summary> /// list all instances of this entity to allow the user to select one for editing /// </summary> /// <param name="view">The name of the list view to show</param> public void InternalShowList( String view) { ISession hibernator = NHibernateHelper.GetCurrentSession( Session[ NHibernateHelper.USERTOKEN], Session[NHibernateHelper.PASSTOKEN]); IList<> instances = hibernator.CreateCriteria(typeof()) .List<>(); PropertyBag["username"] = Session[ NHibernateHelper.USERTOKEN]; PropertyBag["instances"] = PaginationHelper.CreatePagination( this, instances, 25); RenderViewWithFailover(view + ".vm", view + ".auto.vm"); } } } /* ---- [ cut here: next file 'junk'] ------------------------- */ if ( Form[ "" ] == null) { AddError( "You must supply a value for " ); } "" /// <summary> /// Handle the submission of the form named /// </summary> [AccessibleThrough(Verb.Post)] public void ( ) { string command = Form[ "command"]; if ( command == null) { throw new Exception( "No command?"); } else if ( command.Equals( "")) { /* NOTE: You must write an implementation of this verb in a manually maintained partial class file for this class */ (); } else if ( command.Equals( "delete")) { ISession hibernator = NHibernateHelper.GetCurrentSession( Session[ NHibernateHelper.USERTOKEN], Session[NHibernateHelper.PASSTOKEN]); PropertyBag["username"] = Session[ NHibernateHelper.USERTOKEN]; PropertyBag["instance"] = record; RenderViewWithFailover( "maybedelete.vm", "maybedelete.auto.vm"); } else if ( command.Equals( "store")) { Store(); } else { throw new Exception( String.Format("Unrecognised command '{0}'", command)); } } /// <summary> /// Show the form named , with no content /// </summary> [AccessibleThrough(Verb.Get)] public void ( ) { ISession hibernator = NHibernateHelper.GetCurrentSession( Session[ NHibernateHelper.USERTOKEN], Session[NHibernateHelper.PASSTOKEN]); PropertyBag["username"] = Session[ NHibernateHelper.USERTOKEN]; RenderViewWithFailover("", ""); } /// <summary> /// Show the form named , containing the indicated record. As /// the primary key of the record is itself an entity, we need to first fetch that entity /// </summary> /// <param name="">the key value of the key value of the record to show</param> /// <param name="">the key value of the record to show</param> [AccessibleThrough(Verb.Get)] public void ( , ) { ISession hibernator = NHibernateHelper.GetCurrentSession( Session[ NHibernateHelper.USERTOKEN], Session[NHibernateHelper.PASSTOKEN]); this.( hibernator.CreateCriteria(typeof()) .Add( Expression.Eq( "", )) .UniqueResult<>() , ); } /// <summary> /// Show the form named , containing the indicated record /// </summary> /// <param name="">the key value of the record to show</param> [AccessibleThrough(Verb.Get)] public void ( , ) { ISession hibernator = NHibernateHelper.GetCurrentSession( Session[ NHibernateHelper.USERTOKEN], Session[NHibernateHelper.PASSTOKEN]); record = hibernator.CreateCriteria(typeof()) .Add(Expression.Eq("", )) .UniqueResult<>(); PropertyBag["username"] = Session[ NHibernateHelper.USERTOKEN]; PropertyBag["instance"] = record; RenderViewWithFailover("", ""); } /// <summary> /// list all instances of this entity to allow the user to select one /// this method invokes the named view. /// </summary> public void () { InternalShowList( ""); } /* */ /* produce a list of to populate the menu for */ /* produce a list of to populate the LHS of the shuffle for */ /* produce a list of to populate the multi-select for */ PropertyBag[""] = hibernator.CreateCriteria(typeof()) .List<>(); ADL: ERROR: No entity passed to template primary-key-csharp-type ADL: WARNING: entity '' has no primary key. You will have to manually edit ADL: ERROR: No key for entity: ADL: WARNING: Entity has a composite primary key. You will need to manually edit ADL: WARNING: Entity has no primary key. You will need to manually edit ADL: ERROR: property passed to fetch-property-instance whose type is not 'entity' hibernator.CreateCriteria(typeof()) .Add(Expression.Eq("", Int32.Parse( Form[""]) Form[""] )) .UniqueResult<>() record = null; /* check whether values for all key slots have been passed in; if so, we're probably dealing with an * existing record */ bool allkeys = true; if ( String.IsNullOrEmpty( Form[""])) { allkeys = false; } else if ( "".Equals( Form[""])) { /* nasty artefact of NVelocity forms - default 'null value' is dollar followed by fieldname */ allkeys = false; } if ( allkeys){ /* it's (probably) existing, retrieve it */ record = hibernator.CreateCriteria(typeof()) .Add( Expression.Eq( "", Int32.Parse( Form[ ""]) hibernator.CreateCriteria( typeof( )) .Add(Expression.Eq( "", Int32.Parse( Form[ ""]) Form[ ""])) .UniqueResult<>() Form[ ""])) .UniqueResult<>(); }