Exemple #1
0
 public void AddPropertiesToCriteria(IEnumerable <ValueProperty> primaryKeyProperties, Criteria crit)
 {
     // Add all primary key properties to critera if they dont already exist
     foreach (ValueProperty col in primaryKeyProperties)
     {
         if (!crit.Properties.Contains(col.Name))
         {
             CriteriaProperty p = new CriteriaProperty(col.Name, col.PropertyType);
             p.DbBindColumn = (DbBindColumn)col.DbBindColumn.Clone();
             crit.Properties.Add(p);
         }
     }
 }
Exemple #2
0
        /// <summary>
        /// Should be called after the object has all it's properties (if applicable).
        /// It creates the default criteria classes depending on the object type.
        /// </summary>
        public void AddDefaultCriteriaAndParameters()
        {
            if (_currentCslaObject.CriteriaObjects.Count != 0)
            {
                return;
            }

            if (_currentCslaObject.ObjectType == CslaObjectType.NameValueList ||
                _currentCslaObject.ObjectType == CslaObjectType.ReadOnlyCollection ||
                _currentCslaObject.ObjectType == CslaObjectType.EditableRootCollection ||
                _currentCslaObject.ObjectType == CslaObjectType.DynamicEditableRootCollection)
            {
                if (_currentUnit.Params.AutoCriteria)
                {
                    Criteria crit = CreateEmptyFetchCriteria();
                    _currentCslaObject.CriteriaObjects.Add(crit);
                    crit.SetSprocNames();
                }
                //no need to go through the properties here.
                return;
            }

            List <ValueProperty> primaryKeyProperties = new List <ValueProperty>();
            ValueProperty        timestampProperty    = null;
            bool UseForCreate = false;

            // retrieve all primary key and timestamp properties
            foreach (ValueProperty prop in _currentCslaObject.ValueProperties)
            {
                if (prop.PrimaryKey != ValueProperty.UserDefinedKeyBehaviour.Default)
                {
                    primaryKeyProperties.Add(prop);
                    if (!(prop.DbBindColumn.IsIdentity || prop.PropertyType == TypeCodeEx.Guid))
                    {
                        UseForCreate = true;
                    }
                }
                else if (prop.DbBindColumn.NativeType == "timestamp")
                {
                    timestampProperty = prop;
                }
            }

            if (primaryKeyProperties.Count > 0 || timestampProperty != null)
            {
                // Try to find default Criteria object
                Criteria defaultCriteria   = _currentCslaObject.CriteriaObjects.Find("Criteria");
                Criteria timestampCriteria = _currentCslaObject.CriteriaObjects.Find("CriteriaTS");

                // If criteria objects are not set
                if (_currentCslaObject.CriteriaObjects.Count == 0)
                {
                    // If default criteria doesn't exists, create a new criteria
                    if (defaultCriteria == null)
                    {
                        if (_currentCslaObject.ObjectType != CslaObjectType.ReadOnlyObject)
                        {
                            defaultCriteria      = new Criteria(_currentCslaObject);
                            defaultCriteria.Name = "Criteria";
                            defaultCriteria.GetOptions.Enable();
                            if (_currentCslaObject.ObjectType == CslaObjectType.EditableRoot ||
                                _currentCslaObject.ObjectType == CslaObjectType.EditableSwitchable ||
                                _currentCslaObject.ObjectType == CslaObjectType.EditableChild ||
                                _currentCslaObject.ObjectType == CslaObjectType.DynamicEditableRoot)
                            {
                                defaultCriteria.DeleteOptions.Enable();
                                if (defaultCriteria.Properties.Count > 0 && UseForCreate)
                                {
                                    defaultCriteria.CreateOptions.Factory    = true;
                                    defaultCriteria.CreateOptions.DataPortal = true;
                                    defaultCriteria.CreateOptions.RunLocal   = true;
                                }
                                else
                                {
                                    Criteria createCriteria = _currentCslaObject.CriteriaObjects.Find("CriteriaNew");
                                    if (createCriteria == null)
                                    {
                                        createCriteria      = new Criteria(_currentCslaObject);
                                        createCriteria.Name = "CriteriaNew";
                                        createCriteria.CreateOptions.DataPortal = true;
                                        createCriteria.CreateOptions.Factory    = true;
                                        createCriteria.CreateOptions.RunLocal   = true;
                                        _currentCslaObject.CriteriaObjects.Add(createCriteria);
                                    }
                                }
                            }

                            defaultCriteria.SetSprocNames();

                            if (_currentUnit.GenerationParams.TargetFramework == TargetFramework.CSLA40 &&
                                _currentCslaObject.ObjectType != CslaObjectType.EditableRoot &&
                                _currentCslaObject.ObjectType != CslaObjectType.EditableSwitchable)
                            {
                                defaultCriteria.Name = "CriteriaDelete";
                                defaultCriteria.GetOptions.Factory       = false;
                                defaultCriteria.GetOptions.DataPortal    = false;
                                defaultCriteria.GetOptions.Procedure     = false;
                                defaultCriteria.GetOptions.ProcedureName = string.Empty;
                                defaultCriteria.DeleteOptions.Factory    = false;
                                defaultCriteria.DeleteOptions.DataPortal = false;
                            }

                            _currentCslaObject.CriteriaObjects.Add(defaultCriteria);
                            AddPropertiesToCriteria(primaryKeyProperties, defaultCriteria);

                            if (_currentUnit.Params.AutoTimestampCriteria && timestampProperty != null && timestampCriteria == null)
                            {
                                AddTimestampProperty(defaultCriteria, timestampProperty);
                            }
                        }
                    }
                }
            }
        }
        private void BuildCollectionCriteriaGet(CslaObjectInfo info, AssociativeEntity.EntityFacade entity, ChildProperty child)
        {
            const string critName = "CriteriaGet";

            var selfLoad = CslaTemplateHelperCS.IsChildSelfLoaded(info);

            if (!selfLoad)
            {
                DeleteDefaultCollectionCriteria(info, critName);
                return;
            }

            StringBuilder sb;

            // make collection criteria if needed
            var      collCriteria = info.CriteriaObjects;
            Criteria criteria     = null;

            foreach (var crit in collCriteria)
            {
                if (CheckAndSetCollectionCriteriaGet(crit, info, critName))
                {
                    criteria = crit;
                    break;
                }
            }

            if (criteria == null)
            {
                criteria      = new Criteria(info);
                criteria.Name = critName;
                SetCollectionCriteriaGet(criteria, info, critName);
                info.CriteriaObjects.Add(criteria);

                // display message to the user
                sb = new StringBuilder();
                sb.AppendFormat("Successfully added criteria {0} to {1} collection object.", critName, info.ObjectName);
                OutputWindow.Current.AddOutputInfo(sb.ToString());
            }

            if (criteria.Name != critName)
            {
                return;
            }

            if (criteria.Properties.Count > 0)
            {
                // clear CriteriaGet properties
                criteria.Properties.Clear();

                // display message to the user
                sb = new StringBuilder();
                sb.AppendFormat("Successfully removed all criteria properties of {0} on {1} collection object.",
                                critName, info.ObjectName);
                OutputWindow.Current.AddOutputInfo(sb.ToString());
            }

            // populate collection criteria properties
            var addedProps = new List <string>();

            if (entity.LoadProperties.Count == 0)
            {
                criteria.Properties.AddRange(CriteriaPropertyCollection.Clone(FacadeRootCriteriaProperties));
                foreach (var property in FacadeRootCriteriaProperties)
                {
                    addedProps.Add(property.Name);
                }
            }
            else
            {
                criteria.Properties.AddRange(CriteriaPropertyCollection.Clone(entity.LoadProperties));
                foreach (var property in entity.LoadProperties)
                {
                    addedProps.Add(property.Name);
                }
            }

            if (addedProps.Count > 0)
            {
                // display message to the user
                sb = new StringBuilder();
                sb.AppendFormat("Successfully added the following properties to criteria {0}:" + Environment.NewLine, criteria.Name);
                foreach (var propName in addedProps)
                {
                    sb.AppendFormat("\t{0}.{1}.{2}" + Environment.NewLine, critName, info.ObjectName, propName);
                }
                OutputWindow.Current.AddOutputInfo(sb.ToString());
            }

            // is it non-root?
            var entityCslaObject = _cslaObjects.Find(entity.ObjectName);

            if (entityCslaObject != null)
            {
                if (CslaTemplateHelperCS.IsNotRootType(entityCslaObject))
                {
                    // re-fill LoadParameters with child criteria
                    child.LoadParameters.Clear();
                    foreach (var property in criteria.Properties)
                    {
                        child.LoadParameters.Add(new Parameter(criteria, property));
                    }
                }
            }
        }
        private void BuildCollectionItemCriteriaDelete(CslaObjectInfo info, CslaObjectInfo myRootInfo, CslaObjectInfo otherRootInfo, bool isMultipleToMultiple)
        {
            const string critName = "CriteriaDelete";

            // don't generate CriteriaNew for read-only objects
            if (info.ObjectType == CslaObjectType.ReadOnlyObject)
            {
                RemoveDefaultItemCriteria(info, critName);
                return;
            }

            StringBuilder sb;

            // make collection item criteria if needed
            var      itemCriteria = info.CriteriaObjects;
            Criteria criteria     = null;

            foreach (var crit in itemCriteria)
            {
                if (CheckCollectionItemCriteriaDelete(crit, info, myRootInfo, critName))
                {
                    criteria = crit;
                    break;
                }
            }

            if (criteria == null)
            {
                criteria = new Criteria(info)
                {
                    Name = critName
                };
                SetCollectionItemCriteriaDelete(criteria, info, myRootInfo == MainObjectInfo, critName);
                info.CriteriaObjects.Add(criteria);

                // display message to the user
                sb = new StringBuilder();
                sb.AppendFormat("Successfully added criteria {0} to {1} item object.", critName, info.ObjectName);
                OutputWindow.Current.AddOutputInfo(sb.ToString());
            }

            if (criteria.Name != critName)
            {
                return;
            }

            if (criteria.Properties.Count > 0)
            {
                // clear CriteriaDelete properties
                criteria.Properties.Clear();

                // display message to the user
                sb = new StringBuilder();
                sb.AppendFormat("Successfully removed all criteria properties of {0} on {1} item object.", critName, info.ObjectName);
                OutputWindow.Current.AddOutputInfo(sb.ToString());
            }

            // populate collection Criteria properties
            var addedProps = AddCriteriaProperties(info, criteria, myRootInfo, otherRootInfo, false, isMultipleToMultiple);

            if (addedProps.Count > 0)
            {
                // display message to the user
                sb = new StringBuilder();
                sb.AppendFormat("Successfully added the following properties to criteria {0}:" + Environment.NewLine, criteria.Name);
                foreach (var propName in addedProps)
                {
                    sb.AppendFormat("\t{0}.{1}.{2}" + Environment.NewLine, critName, info.ObjectName, propName);
                }
                OutputWindow.Current.AddOutputInfo(sb.ToString());
            }
        }
        /// <summary>
        /// Filters and merges the unit of work criteria collection.
        /// Collection is filtered according to the type of criteria and type of the Unit of Work Type under processing.
        /// </summary>
        /// <param name="info">The info.</param>
        /// <returns>
        /// A single filtered collection of critera.
        /// </returns>
        public static List <CriteriaCollection> GetAllCriteria(CslaObjectInfo info)
        {
            if (info.IsUpdater)
            {
                return(null);
            }

            var mergeTypes = new List <CriteriaMergeType>();

            if (info.UnitOfWorkType == UnitOfWorkFunction.Creator ||
                info.UnitOfWorkType == UnitOfWorkFunction.CreatorGetter)
            {
                mergeTypes.Add(CriteriaMergeType.Create);
            }

            if (info.UnitOfWorkType == UnitOfWorkFunction.Getter ||
                info.UnitOfWorkType == UnitOfWorkFunction.CreatorGetter)
            {
                mergeTypes.Add(CriteriaMergeType.Get);
            }

            if (info.UnitOfWorkType == UnitOfWorkFunction.Deleter)
            {
                mergeTypes.Add(CriteriaMergeType.Delete);
            }

            var result = new List <CriteriaCollection>();

            foreach (var mergeType in mergeTypes)
            {
                _buffer = new List <CriteriaCollection>();
                foreach (var prop in info.UnitOfWorkProperties)
                {
                    _criteriaCache = new List <Criteria>();
                    var targetInfo = info.Parent.CslaObjects.Find(prop.TypeName);
                    var index      = 0;
                    foreach (var crit in targetInfo.CriteriaObjects)
                    {
                        if (mergeType.HasFlag(CriteriaMergeType.Create) && crit.IsCreator)
                        {
                            var newCriteria = Criteria.Clone(crit);
                            newCriteria.GetOptions.Disable();
                            newCriteria.DeleteOptions.Disable();
                            if (info.UnitOfWorkType == UnitOfWorkFunction.CreatorGetter)
                            {
                                index++;
                                if (newCriteria.Properties.Count == 0)
                                {
                                    newCriteria.Properties.AddRange(GetFakeCreateCriteria(targetInfo, index));
                                }
                            }
                            _criteriaCache.Add(newCriteria);
                        }
                        else if (mergeType.HasFlag(CriteriaMergeType.Create) && crit.IsGetter && !prop.CreatesObject)
                        {
                            _criteriaCache.Add(Criteria.Clone(crit));
                        }
                        else if (mergeType.HasFlag(CriteriaMergeType.Get) && crit.IsGetter)
                        {
                            var newCriteria = Criteria.Clone(crit);
                            newCriteria.CreateOptions.Disable();
                            newCriteria.DeleteOptions.Disable();
                            if (info.UnitOfWorkType == UnitOfWorkFunction.CreatorGetter && prop.CreatesObject)
                            {
                                foreach (var property in newCriteria.Properties)
                                {
                                    property.UnitOfWorkFactoryParameter = "false";
                                }
                            }
                            _criteriaCache.Add(newCriteria);
                        }
                        else if (mergeType.HasFlag(CriteriaMergeType.Delete) && crit.IsDeleter)
                        {
                            var newCriteria = Criteria.Clone(crit);
                            newCriteria.CreateOptions.Disable();
                            newCriteria.GetOptions.Disable();
                            _criteriaCache.Add(newCriteria);
                        }
                    }
                    if (_criteriaCache.Count == 0)
                    {
                        continue;
                    }

                    PrepareCriteriaList();
                    AssignCriteria();
                }
                result.AddRange(_buffer);
            }

            return(result);
        }