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 (entityCslaObject.IsNotRootType())
                {
                    // re-fill LoadParameters with child criteria
                    child.LoadParameters.Clear();
                    foreach (var property in criteria.Properties)
                    {
                        child.LoadParameters.Add(new Parameter(criteria.Name, property.Name));
                    }
                }
            }
        }
        public void BuildRelationObjects(AssociativeEntity.EntityFacade entity)
        {
            StringBuilder sb;
            var           isManyToMany = (_entity.RelationType == ObjectRelationType.ManyToMany);

            // make property if needed
            ChildProperty child;

            if (FacadeObjectInfo.ChildCollectionProperties.Contains(entity.PropertyName))
            {
                child = FacadeObjectInfo.ChildCollectionProperties.Find(entity.PropertyName);
            }
            else
            {
                child = new ChildProperty {
                    Name = entity.PropertyName
                };
                FacadeObjectInfo.ChildCollectionProperties.Add(child);

                // display message to the user
                sb = new StringBuilder();
                sb.AppendFormat("Successfully added ChildProperty {0} to {1} object.",
                                entity.PropertyName,
                                FacadeObjectInfo.ObjectName);
                OutputWindow.Current.AddOutputInfo(sb.ToString());
            }
            child.ParameterName   = entity.PropertyName;
            child.TypeName        = entity.CollectionTypeName;
            child.DeclarationMode = PropertyDeclaration.Managed;
            child.ReadOnly        = FacadeObjectInfo.IsReadOnlyObject();
            child.Nullable        = false;
            child.LazyLoad        = entity.LazyLoad;
            child.LoadingScheme   = entity.LoadingScheme;

            // LoadParameters might be re-filled by BuildCollectionCriteriaGet
            child.LoadParameters = entity.LoadParameters;
            child.Access         = PropertyAccess.IsPublic;
            child.Undoable       = true;

            // make collection if needed
            var coll = _cslaObjects.Find(entity.CollectionTypeName);

            if (coll == null)
            {
                coll = new CslaObjectInfo(_currentUnit)
                {
                    ObjectType = CslaObjectType.EditableChildCollection,
                    ObjectName = entity.CollectionTypeName
                };
                _currentUnit.CslaObjects.InsertAtTop(coll);

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

            // populate collection
            coll.ParentType       = entity.ObjectName;
            coll.ParentProperties = BuildParentProperties(coll);
            coll.ItemType         = entity.ItemTypeName;

            // handle collection criteria
            BuildCollectionCriteriaGet(coll, entity, child);

            /* handle item */
            var item = _cslaObjects.Find(entity.ItemTypeName);

            if (item == null)
            {
                item = new CslaObjectInfo(_currentUnit)
                {
                    ObjectType = CslaObjectType.EditableChild,
                    ObjectName = entity.ItemTypeName
                };
                _currentUnit.CslaObjects.InsertAtTop(item);

                // display message to the user
                sb = new StringBuilder();
                sb.AppendFormat("Successfully added {0} collection item object.", entity.ItemTypeName);
                OutputWindow.Current.AddOutputInfo(sb.ToString());
            }

            // populate collection item
            if (!isManyToMany)
            {
                item.ParentInsertOnly = true;
            }
            else
            {
                item.ParentInsertOnly = false;
            }
            item.ParentType       = entity.CollectionTypeName;
            item.ParentProperties = BuildParentProperties(item);
            var suffix     = string.Empty;
            var objectName = entity.ItemTypeName;

            if (isManyToMany &&
                !_entity.MainLazyLoad &&
                !_entity.SecondaryLazyLoad &&
                _entity.Parent.Params.ORBItemsUseSingleSP)
            {
                suffix     = _entity.Parent.Params.ORBSingleSPSuffix;
                objectName = _entity.MainItemTypeName;
            }
            if (item.IsNotReadOnlyObject())
            {
                if (item.GenerateDataPortalInsert)
                {
                    item.InsertProcedureName = _entity.Parent.Params.GetAddProcName(objectName) + suffix;
                }
                if (item.GenerateDataPortalUpdate)
                {
                    item.UpdateProcedureName = _entity.Parent.Params.GetUpdateProcName(objectName) + suffix;
                }
            }
            else
            {
                item.InsertProcedureName = string.Empty;
                item.UpdateProcedureName = string.Empty;
            }

            // prepare for deprecate
            item.DeleteProcedureName = string.Empty;

            // handle collection item CriteriaNew
            if (!isManyToMany)
            {
                BuildCollectionItemCriteriaNew(item, null, null, false);
            }
            else
            {
                if (FacadeObjectInfo == MainObjectInfo)
                {
                    BuildCollectionItemCriteriaNew(item, null, SecondaryObjectInfo, true);
                }
                else
                {
                    BuildCollectionItemCriteriaNew(item, null, MainObjectInfo, true);
                }
            }

            // handle collection item Criteria
            if (!isManyToMany)
            {
                BuildCollectionItemCriteriaDelete(item, null, null, false);
            }
            else
            {
                if (FacadeObjectInfo == MainObjectInfo)
                {
                    BuildCollectionItemCriteriaDelete(item, MainObjectInfo, SecondaryObjectInfo, true);
                }
                else
                {
                    BuildCollectionItemCriteriaDelete(item, SecondaryObjectInfo, MainObjectInfo, true);
                }
            }
        }