Example #1
0
        public override object Execute(IEngineContext context, IController controller, IControllerContext controllerContext)
        {
            base.Execute(context, controller, controllerContext);
            OnPreList(controller);
            OnPreView(controller);

            try {
                var match = context.Items[RouteMatch.RouteMatchKey] as RouteMatch;
                if (match == null)
                {
                    throw new MonoRailException("Cannot find 'id' from the url.");
                }

                var instance = AR.Find <T>(match.Parameters["id"]);
                if (instance == null)
                {
                    context.Handle404();
                    return(null);
                }

                controllerContext.PropertyBag[TemplateObjectName] = instance;
                var id = AR.Holder.GetModel(Type).Metadata.GetIdentifier(instance, EntityMode.Poco);
                controllerContext.PropertyBag["objectid"] = id;

                var loader = DetachedCriteria.For <T>()
                             .Add(Restrictions.Eq(Projections.Id(), id));
                foreach (var b in AR.Holder.GetModel(typeof(T)).BelongsTos)
                {
                    loader.CreateCriteria(b.Key, JoinType.LeftOuterJoin);
                }
                loader.SetResultTransformer(Transformers.DistinctRootEntity).Future <T>();

                if (controller.GetType().GetAttr <DontDoManyFetch>() == null)
                {
                    foreach (var hasmany in AR.Holder.GetModel(typeof(T)).HasManys)
                    {
                        DetachedCriteria.For <T>()
                        .CreateCriteria(hasmany.Value.Type.Role.Replace(typeof(T) + ".", ""), JoinType.LeftOuterJoin)
                        .Future <T>();
                    }
                    foreach (var hasmany in AR.Holder.GetModel(typeof(T)).HasAndBelongsToManys)
                    {
                        DetachedCriteria.For <T>()
                        .CreateCriteria(hasmany.Value.Type.Role.Replace(typeof(T) + ".", ""), JoinType.LeftOuterJoin)
                        .Future <T>();
                    }
                }
                OnPostView(controller, instance);

                return(null);
            } catch (Exception ex) {
                context.ErrorMessages(ex);
            }
            context.Handle404();
            return(null);
        }
Example #2
0
        public object ExecuteUpdate(IEngineContext context, IController controller, IControllerContext controllerContext)
        {
            controllerContext.Action = "update";
            using (var transaction = new TransactionScope()) {
                var instance = AR.Find <T>(context.GetParameter(IdParameter));
                if (instance == null)
                {
                    context.Handle404();
                    return(null);
                }

                var pk = AR.Holder.GetModel(Type).Metadata.GetIdentifier(instance, EntityMode.Poco).ToString();

                try {
                    var excludeattr  = controller.GetType().GetAttr <UpdateExcludeAttribute>();
                    var includeattr  = controller.GetType().GetAttr <UpdateIncludeAttribute>();
                    var includeprops = includeattr == null ? null : includeattr.Properties;
                    var excludeprops = excludeattr == null ? null : excludeattr.Properties;

                    var form = BuildCompositeNode(context.Request.Form, Queryfilters.Where(qf => qf.ApplyOnSave), true);

                    OnPreBind(controller, instance);
                    Binder.AutoLoad = AutoLoadBehavior.NullIfInvalidKey;
                    Binder.BindObjectInstance(instance, TemplateObjectName, excludeprops, includeprops, form);
                    OnPostBind(controller, instance);

                    OnPreSave(controller, instance);
                    OnPreUpdate(controller, instance);
                    if (Validator.IsValid(instance))
                    {
                        AR.Save <T>(instance);
                        OnPostUpdate(controller, instance);
                        OnPostSave(controller, instance);

                        transaction.Flush();
                        transaction.VoteCommit();

                        pk = AR.Holder.GetClassMetadata(Type).GetIdentifier(instance, EntityMode.Poco).ToString();
                        controllerContext.PropertyBag[TemplateObjectName] = instance;
                        context.SuccessMessage(instance + " has been updated");
                    }
                    else
                    {
                        transaction.VoteRollBack();
                        context.ErrorMessages(Validator.GetErrorSummary(instance).ErrorMessages);
                    }
                } catch (Exception ex) {
                    transaction.VoteRollBack();
                    context.ErrorMessages(ex);
                } finally {
                    context.Flash[TemplateObjectName]         = instance;
                    controllerContext.PropertyBag["objectid"] = pk;
                    context.CurrentControllerContext.PropertyBag[TemplateObjectName] = instance;
                }
            }

            return(null);
        }
Example #3
0
        public object ExecuteView(IEngineContext context, IController controller, IControllerContext controllerContext)
        {
            OnPreView(controller);

            try {
                var instance = AR.Find <T>(context.GetParameter(IdParameter));
                if (instance == null)
                {
                    context.Handle404();
                    return(null);
                }

                var id = AR.Holder.GetModel(Type).Metadata.GetIdentifier(instance, EntityMode.Poco);

                var loader = DetachedCriteria.For <T>()
                             .Add(Restrictions.Eq(Projections.Id(), id));

                foreach (var b in AR.Holder.GetModel(typeof(T)).BelongsTos)
                {
                    loader.CreateCriteria(b.Key, JoinType.LeftOuterJoin);
                }

                loader.SetResultTransformer(Transformers.DistinctRootEntity);
                if (!DontDoManyFetch)
                {
                    foreach (var hasmany in AR.Holder.GetModel(typeof(T)).HasManys)
                    {
                        DetachedCriteria.For <T>()
                        .Add(Restrictions.Eq(Projections.Id(), id))
                        .CreateCriteria(hasmany.Value.Type.Role.Replace(typeof(T).FullName + ".", ""), JoinType.LeftOuterJoin)
                        .Future <T>();
                    }
                    foreach (var hasmany in AR.Holder.GetModel(typeof(T)).HasAndBelongsToManys)
                    {
                        DetachedCriteria.For <T>()
                        .Add(Restrictions.Eq(Projections.Id(), id))
                        .CreateCriteria(hasmany.Value.Type.Role.Replace(typeof(T).FullName + ".", ""), JoinType.LeftOuterJoin)
                        .Future <T>();
                    }
                }

                controllerContext.PropertyBag[TemplateObjectName] = instance;

                OnPostView(controller, instance);
            }
            catch (Exception e) {
                context.ErrorMessages(e);
            }
            return(null);
        }