Пример #1
0
        /// <summary>
        /// Returns all instances found for the specified type according to the criteria
        /// </summary>
        public static Array FindAll(Type targetType, DetachedCriteria detachedCriteria, params Order[] orders)
        {
            EnsureInitialized(targetType);

            ISession session = holder.CreateSession(targetType);

            try
            {
                ICriteria criteria = detachedCriteria.GetExecutableCriteria(session);

                AddOrdersToCriteria(criteria, orders);

                return(SupportingUtils.BuildArray(targetType, criteria.List()));
            }
            catch (ValidationException)
            {
                holder.FailSession(session);

                throw;
            }
            catch (Exception ex)
            {
                holder.FailSession(session);
                throw ex;
            }
            finally
            {
                holder.ReleaseSession(session);
            }
        }
Пример #2
0
        private object LoadActiveRecord(Type type, object pk, ARFetchAttribute attr, ActiveRecordModel model)
        {
            object obj = null;

            if (pk != null && !String.Empty.Equals(pk))
            {
                PrimaryKeyModel pkModel = (PrimaryKeyModel)model.Ids[0];

                Type pkType = pkModel.Property.PropertyType;

                if (pk.GetType() != pkType)
                {
                    pk = Convert.ChangeType(pk, pkType);
                }

                obj = SupportingUtils.FindByPK(type, pk, attr.Required);
            }

            if (obj == null && attr.Create)
            {
                obj = Activator.CreateInstance(type);
            }

            return(obj);
        }
Пример #3
0
        /// <summary>
        /// Returns all instances found for the specified type according to the criteria
        /// </summary>
        /// <param name="targetType">The target type.</param>
        /// <param name="detachedQuery">The query expression</param>
        /// <returns>The <see cref="Array"/> of results.</returns>
        public static Array FindAll(Type targetType, IDetachedQuery detachedQuery)
        {
            EnsureInitialized(targetType);

            ISession session = holder.CreateSession(targetType);

            try
            {
                IQuery executableQuery = detachedQuery.GetExecutableQuery(session);
                return(SupportingUtils.BuildArray(targetType, executableQuery.List()));
            }
            catch (ValidationException)
            {
                holder.FailSession(session);
                throw;
            }
            catch (Exception exception)
            {
                holder.FailSession(session);
                throw new ActiveRecordException("Could not perform FindAll for " + targetType.Name, exception);
            }
            finally
            {
                holder.ReleaseSession(session);
            }
        }
Пример #4
0
        protected override object CreateInstance(Type instanceType, String paramPrefix, NameValueCollection paramsList)
        {
            object instance = null;

            bool shouldLoad = autoLoad || paramsList[paramPrefix + AutoLoadAttribute] == Yes;

            if (shouldLoad && paramsList[paramPrefix + AutoLoadAttribute] == No)
            {
                shouldLoad = false;
            }

            if (shouldLoad)
            {
                if (instanceType.IsArray)
                {
                    throw new RailsException("ARDataBinder autoload does not support arrays");
                }

                if (!typeof(ActiveRecordBase).IsAssignableFrom(instanceType))
                {
                    throw new RailsException("ARDataBinder autoload only supports classes that inherit from ActiveRecordBase");
                }

                ActiveRecordModel model = ActiveRecordModel.GetModel(instanceType);

                // NOTE: as of right now we only support one PK
                if (model.Ids.Count == 1)
                {
                    PrimaryKeyModel pkModel = model.Ids[0] as PrimaryKeyModel;

                    string propName    = pkModel.Property.Name;
                    string paramListPk = (paramPrefix == String.Empty) ? propName : paramPrefix + "." + propName;
                    string propValue   = paramsList.Get(paramListPk);

                    if (propValue != null)
                    {
                        object id = ConvertUtils.Convert(pkModel.Property.PropertyType, propValue, propName, null, null);
                        instance = SupportingUtils.FindByPK(instanceType, id);
                    }
                    else
                    {
                        throw new RailsException("ARDataBinder autoload failed as element {0} doesn't have a primary key {1} value", paramPrefix, propName);
                    }
                }
            }
            else
            {
                instance = base.CreateInstance(instanceType, paramPrefix, paramsList);
            }

            return(instance);
        }
Пример #5
0
        /// <summary>
        /// Returns a portion of the query results (sliced)
        /// </summary>
        public static Array SlicedFindAll(Type targetType, int firstResult, int maxresults,
                                          Order[] orders, params ICriterion[] criterias)
        {
            EnsureInitialized(targetType);

            ISession session = holder.CreateSession(targetType);

            try
            {
                ICriteria sessionCriteria = session.CreateCriteria(targetType);

                foreach (ICriterion cond in criterias)
                {
                    sessionCriteria.Add(cond);
                }

                if (orders != null)
                {
                    foreach (Order order in orders)
                    {
                        sessionCriteria.AddOrder(order);
                    }
                }

                sessionCriteria.SetFirstResult(firstResult);
                sessionCriteria.SetMaxResults(maxresults);

                return(SupportingUtils.BuildArray(targetType, sessionCriteria.List()));
            }
            catch (ValidationException)
            {
                holder.FailSession(session);

                throw;
            }
            catch (Exception ex)
            {
                holder.FailSession(session);

                throw new ActiveRecordException("Could not perform SlicedFindAll for " + targetType.Name, ex);
            }
            finally
            {
                holder.ReleaseSession(session);
            }
        }
Пример #6
0
        protected override void PerformActionProcess(Controller controller)
        {
            object idVal = CommonOperationUtils.ReadPkFromParams(controller, ObtainPKProperty());

            try
            {
                object instance = SupportingUtils.FindByPK(Model.Type, idVal);

                controller.PropertyBag["armodel"]  = Model;
                controller.PropertyBag["instance"] = instance;
                controller.PropertyBag["id"]       = idVal;
            }
            catch (Exception ex)
            {
                throw new ScaffoldException("Could not obtain instance by using this id", ex);
            }
        }
Пример #7
0
        protected override void PerformActionProcess(Controller controller)
        {
            object idVal = CommonOperationUtils.ReadPkFromParams(controller, ObtainPKProperty());

            controller.PropertyBag["armodel"] = Model;
            controller.PropertyBag["id"]      = idVal;

            try
            {
                object instance = SupportingUtils.FindByPK(Model.Type, idVal);

                controller.PropertyBag["instance"] = instance;

                (instance as ActiveRecordBase).Delete();
            }
            catch (Exception ex)
            {
                controller.PropertyBag["exception"] = ex;
            }
        }
Пример #8
0
        /// <summary>
        /// Returns a portion of the query results (sliced)
        /// </summary>
        public static Array SlicedFindAll(Type targetType, int firstResult, int maxResults,
                                          Order[] orders, DetachedCriteria criteria)
        {
            EnsureInitialized(targetType);

            ISession session = holder.CreateSession(targetType);

            try
            {
                ICriteria executableCriteria = criteria.GetExecutableCriteria(session);
                AddOrdersToCriteria(executableCriteria, orders);
                executableCriteria.SetFirstResult(firstResult);
                executableCriteria.SetMaxResults(maxResults);

                return(SupportingUtils.BuildArray(targetType, executableCriteria.List()));
            }
            catch (ValidationException)
            {
                holder.FailSession(session);

                throw;
            }
            catch (InvalidCastException ex)
            {
                holder.FailSession(session);

                throw new ActiveRecordException("Could not perform SlicedFindAll for " + targetType.Name, ex);
            }
            catch (Exception ex)
            {
                holder.FailSession(session);

                throw new ActiveRecordException("Could not perform SlicedFindAll for " + targetType.Name, ex);
            }
            finally
            {
                holder.ReleaseSession(session);
            }
        }
        internal static object[] FindAll(Type type, String customWhere)
        {
            MethodInfo findAll = type.GetMethod("FindAll",
                                                BindingFlags.Static | BindingFlags.Public, null, new Type[0], null);

            object[] items = null;

            if (findAll != null)
            {
                items = (object[])findAll.Invoke(null, null);
            }
            else
            {
                IList list = SupportingUtils.FindAll(type);

                items = new object[list.Count];

                list.CopyTo(items, 0);
            }

            return(items);
        }
Пример #10
0
        /// <summary>
        /// Searches and returns the first row.
        /// </summary>
        /// <param name="targetType">The target type</param>
        /// <param name="orders">The sort order - used to determine which record is the first one</param>
        /// <param name="criterias">The criteria expression</param>
        /// <returns>A <c>targetType</c> instance or <c>null</c></returns>
        public static object FindFirst(Type targetType, Order[] orders, params ICriterion[] criterias)
        {
            EnsureInitialized(targetType);

            ISession session = holder.CreateSession(targetType);

            try
            {
                ICriteria sessionCriteria = session.CreateCriteria(targetType);

                sessionCriteria.SetMaxResults(1);

                foreach (ICriterion cond in criterias)
                {
                    sessionCriteria.Add(cond);
                }

                AddOrdersToCriteria(sessionCriteria, orders);

                return(SupportingUtils.BuildArray(targetType, sessionCriteria.List()));
            }
            catch (ValidationException)
            {
                holder.FailSession(session);

                throw;
            }
            catch (Exception ex)
            {
                holder.FailSession(session);

                throw new ActiveRecordException("Could not perform FindAll for " + targetType.Name, ex);
            }
            finally
            {
                holder.ReleaseSession(session);
            }
        }
Пример #11
0
        protected override void PerformActionProcess(Controller controller)
        {
            ARDataBinder binder = new ARDataBinder();

            object idVal = CommonOperationUtils.ReadPkFromParams(controller, ObtainPKProperty());

            SessionScope scope = new SessionScope();

            try
            {
                object instance = SupportingUtils.FindByPK(Model.Type, idVal);

                binder.BindObjectInstance(instance, String.Empty, controller.Params, null, null);

                CommonOperationUtils.SaveInstance(instance, controller, errors, prop2Validation);

                scope.Dispose();

                controller.Redirect(controller.AreaName, controller.Name, "list" + Model.Type.Name);
            }
            catch (Exception ex)
            {
                errors.Add("Could not save " + Model.Type.Name + ". " + ex.Message);

                scope.Dispose(true);
            }

            if (errors.Count != 0)
            {
                controller.Context.Flash["errors"] = errors;

                NameValueCollection parameters = new NameValueCollection();
                parameters["id"] = idVal.ToString();

                controller.Redirect(controller.AreaName, controller.Name, "edit" + Model.Type.Name, parameters);
            }
        }
Пример #12
0
        /// <summary>
        /// Executes the query and converts the results into a strongly-typed
        /// array of <typeparamref name="T"/>.
        /// </summary>
        /// <param name="session">The <c>NHibernate</c>'s <see cref="ISession"/></param>
        protected override object InternalExecute(ISession session)
        {
            IList results = (IList)base.InternalExecute(session);

            return(SupportingUtils.BuildArray <T>(results, false));
        }
Пример #13
0
 protected Array GetResultsArray(Type t, IList list, int entityIndex, bool distinct)
 {
     return(SupportingUtils.BuildArray(t, list, entityIndex, distinct));
 }