private void loadPolicySet(int idx,string title,PolicyLinkEntity ple, XmlNode node)
        {
            ple.Policy.Set = true;

            loadPolicyBase(idx,title,ple.Policy, node);

            int policyCount = 0;
            int policySetCount = 0;
            foreach (XmlNode kid in node.ChildNodes)
            {
                if (kid.LocalName != "policy" && kid.LocalName != "policy-set")
                    continue;

                PolicyLinkEntity pleKid = new PolicyLinkEntity();
                pleKid.Order = policyCount + policySetCount;
                pleKid.Parent = ple;
                pleKid.Policy = new PolicyEntity();
                pleKid.Policy.LibraryId = ple.Policy.LibraryId;

                if (kid.NodeType == XmlNodeType.Element && kid.LocalName == "policy")
                {
                    loadPolicy(++policyCount, ple.Policy.Description, pleKid, kid);
                }
                else if (kid.NodeType == XmlNodeType.Element && kid.LocalName == "policy-set")
                {
                    loadPolicySet(++policySetCount, ple.Policy.Description,pleKid, kid);
                }
            }
        }
        private PolicyDocumentEntity importFile(baseData vData,string title, string filepath)
        {
            AttributeCollection acoll = new AttributeCollection();
            acoll.GetMulti(AttributeFields.Name == "Literal");
            if (acoll.Count == 0)
                throw new Exception("can't find literal attribute");
            m_literalAttribute = acoll[0];

            XmlDocument doc = new XmlDocument();
            doc.Load(filepath);

            PolicyDocumentEntity pde = new PolicyDocumentEntity();
            pde.LibraryId = vData.Library.Id;
            pde.Name = title;

            PolicyLinkEntity ple = new PolicyLinkEntity();
            ple.Policy = new PolicyEntity();
            ple.Policy.LibraryId = pde.LibraryId;
            pde.PolicyLink = ple;

            XmlNode policySet = doc.SelectSingleNode("policy-set");
            if (policySet != null)
                loadPolicySet(1,title,ple,policySet);
            else
            {
                XmlNode policy = doc.SelectSingleNode("policy");
                loadPolicy(1,title,ple,policy);
            }

            pde.Save(true);

            return pde;
        }
        private static void DeletePolicyLink(PolicyLinkEntity ple)
        {
            foreach (PolicyLinkEntity child in ple.Children)
            {
                DeletePolicyLink(child);
            }

            PolicyEntity delPolicy = ple.Policy;

            ple.Delete();

            DeletePolicy(delPolicy);
        }
        private void loadPolicy(int idx,string title,PolicyLinkEntity ple, XmlNode node)
        {
            loadPolicyBase(idx,title,ple.Policy, node);

            XmlNodeList rules = node.SelectNodes("rule");
            int ruleIdx = 0;
            foreach (XmlNode rule in rules)
            {
                loadRule(ruleIdx, ple.Policy, rule);
                ruleIdx++;
            }
        }
        /// <summary> Retrieves the related entity of type 'PolicyLinkEntity', using a relation of type 'n:1'</summary>
        /// <param name="forceFetch">if true, it will discard any changes currently in the currently loaded related entity and will refetch the entity from the persistent storage</param>
        /// <returns>A fetched entity of type 'PolicyLinkEntity' which is related to this entity.</returns>
        public virtual PolicyLinkEntity GetSingleParent(bool forceFetch)
        {
            if( ( !_alreadyFetchedParent || forceFetch || _alwaysFetchParent) && !base.IsSerializing && !base.IsDeserializing  && !base.InDesignMode)
            {
                bool performLazyLoading = base.CheckIfLazyLoadingShouldOccur(PolicyLinkEntity.Relations.PolicyLinkEntityUsingIdParentId);

                PolicyLinkEntity newEntity = new PolicyLinkEntity();
                if(base.ParticipatesInTransaction)
                {
                    base.Transaction.Add(newEntity);
                }
                bool fetchResult = false;
                if(performLazyLoading)
                {
                    fetchResult = newEntity.FetchUsingPK(this.ParentId.GetValueOrDefault());
                }
                if(fetchResult)
                {
                    if(base.ActiveContext!=null)
                    {
                        newEntity = (PolicyLinkEntity)base.ActiveContext.Get(newEntity);
                    }
                    this.Parent = newEntity;
                }
                else
                {
                    if(_parentReturnsNewIfNotFound)
                    {
                        if(performLazyLoading || (!performLazyLoading && (_parent == null)))
                        {
                            this.Parent = newEntity;
                        }
                    }
                    else
                    {
                        this.Parent = null;
                    }
                }
                _alreadyFetchedParent = fetchResult;
                if(base.ParticipatesInTransaction && !fetchResult)
                {
                    base.Transaction.Remove(newEntity);
                }
            }
            return _parent;
        }
        public ActionResult PolicyOrder(policyData vData, int id, FormCollection collection)
        {
            PolicyLinkEntity policyLink = new PolicyLinkEntity(id);
            PolicyLinkCollection coll = new PolicyLinkCollection();

            PredicateExpression pe = new PredicateExpression(PolicyLinkFields.Id != policyLink.Id);
            pe.Add(PolicyLinkFields.ParentId == policyLink.ParentId);
            SortExpression se = null;

            if (collection["up"] != null)
            {
                // Find all categories with display index less than ours.
                pe.Add(PolicyLinkFields.Order <= policyLink.Order);

                // Order by display index, highest first.
                se = new SortExpression(PolicyLinkFields.Order | SortOperator.Descending);
            }
            else
            {
                // Find all categories with display index greater than ours.
                pe.Add(PolicyLinkFields.Order >= policyLink.Order);

                // Order by display index, lowest first.
                se = new SortExpression(PolicyLinkFields.Order | SortOperator.Ascending);
            }

            // Swap with closest one.
            if (coll.GetMulti(pe, 1, se) && coll.Count > 0)
            {
                int temp = coll[0].Order;
                coll[0].Order = policyLink.Order;
                policyLink.Order = temp;

                policyLink.Save();
                coll.SaveMulti();
            }

            return RedirectToAction("EditPolicySet", new { id = policyLink.ParentId });
        }
 /// <summary> setups the sync logic for member _parent</summary>
 /// <param name="relatedEntity">Instance to set as the related entity of type entityType</param>
 private void SetupSyncParent(IEntity relatedEntity)
 {
     if(_parent!=relatedEntity)
     {
         DesetupSyncParent(true, true);
         _parent = (PolicyLinkEntity)relatedEntity;
         base.PerformSetupSyncRelatedEntity( _parent, new PropertyChangedEventHandler( OnParentPropertyChanged ), "Parent", PolicyLinkEntity.Relations.PolicyLinkEntityUsingIdParentId, true, ref _alreadyFetchedParent, new string[] {  } );
     }
 }
        /// <summary> Initializes the class members</summary>
        private void InitClassMembers()
        {
            _policyDocument = new policyDB.CollectionClasses.PolicyDocumentCollection(new PolicyDocumentEntityFactory());
            _policyDocument.SetContainingEntityInfo(this, "PolicyLink");
            _alwaysFetchPolicyDocument = false;
            _alreadyFetchedPolicyDocument = false;
            _children = new policyDB.CollectionClasses.PolicyLinkCollection(new PolicyLinkEntityFactory());
            _children.SetContainingEntityInfo(this, "Parent");
            _alwaysFetchChildren = false;
            _alreadyFetchedChildren = false;
            _libraryCollectionViaPolicyDocument = new policyDB.CollectionClasses.LibraryCollection(new LibraryEntityFactory());
            _alwaysFetchLibraryCollectionViaPolicyDocument = false;
            _alreadyFetchedLibraryCollectionViaPolicyDocument = false;
            _policyCollectionViaPolicyLink = new policyDB.CollectionClasses.PolicyCollection(new PolicyEntityFactory());
            _alwaysFetchPolicyCollectionViaPolicyLink = false;
            _alreadyFetchedPolicyCollectionViaPolicyLink = false;
            _policy = null;
            _policyReturnsNewIfNotFound = true;
            _alwaysFetchPolicy = false;
            _alreadyFetchedPolicy = false;
            _parent = null;
            _parentReturnsNewIfNotFound = true;
            _alwaysFetchParent = false;
            _alreadyFetchedParent = false;

            PerformDependencyInjection();

            // __LLBLGENPRO_USER_CODE_REGION_START InitClassMembers
            // __LLBLGENPRO_USER_CODE_REGION_END
            OnInitClassMembersComplete();
        }
 /// <summary> Removes the sync logic for member _parent</summary>
 /// <param name="signalRelatedEntity">If set to true, it will call the related entity's UnsetRelatedEntity method</param>
 /// <param name="resetFKFields">if set to true it will also reset the FK fields pointing to the related entity</param>
 private void DesetupSyncParent(bool signalRelatedEntity, bool resetFKFields)
 {
     base.PerformDesetupSyncRelatedEntity( _parent, new PropertyChangedEventHandler( OnParentPropertyChanged ), "Parent", PolicyLinkEntity.Relations.PolicyLinkEntityUsingIdParentId, true, signalRelatedEntity, "Children", resetFKFields, new int[] { (int)PolicyLinkFieldIndex.ParentId } );
     _parent = null;
 }
        /// <summary>Private CTor for deserialization</summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        protected PolicyLinkEntity(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            _policyDocument = (policyDB.CollectionClasses.PolicyDocumentCollection)info.GetValue("_policyDocument", typeof(policyDB.CollectionClasses.PolicyDocumentCollection));
            _alwaysFetchPolicyDocument = info.GetBoolean("_alwaysFetchPolicyDocument");
            _alreadyFetchedPolicyDocument = info.GetBoolean("_alreadyFetchedPolicyDocument");
            _children = (policyDB.CollectionClasses.PolicyLinkCollection)info.GetValue("_children", typeof(policyDB.CollectionClasses.PolicyLinkCollection));
            _alwaysFetchChildren = info.GetBoolean("_alwaysFetchChildren");
            _alreadyFetchedChildren = info.GetBoolean("_alreadyFetchedChildren");
            _libraryCollectionViaPolicyDocument = (policyDB.CollectionClasses.LibraryCollection)info.GetValue("_libraryCollectionViaPolicyDocument", typeof(policyDB.CollectionClasses.LibraryCollection));
            _alwaysFetchLibraryCollectionViaPolicyDocument = info.GetBoolean("_alwaysFetchLibraryCollectionViaPolicyDocument");
            _alreadyFetchedLibraryCollectionViaPolicyDocument = info.GetBoolean("_alreadyFetchedLibraryCollectionViaPolicyDocument");
            _policyCollectionViaPolicyLink = (policyDB.CollectionClasses.PolicyCollection)info.GetValue("_policyCollectionViaPolicyLink", typeof(policyDB.CollectionClasses.PolicyCollection));
            _alwaysFetchPolicyCollectionViaPolicyLink = info.GetBoolean("_alwaysFetchPolicyCollectionViaPolicyLink");
            _alreadyFetchedPolicyCollectionViaPolicyLink = info.GetBoolean("_alreadyFetchedPolicyCollectionViaPolicyLink");
            _policy = (PolicyEntity)info.GetValue("_policy", typeof(PolicyEntity));
            if(_policy!=null)
            {
                _policy.AfterSave+=new EventHandler(OnEntityAfterSave);
            }
            _policyReturnsNewIfNotFound = info.GetBoolean("_policyReturnsNewIfNotFound");
            _alwaysFetchPolicy = info.GetBoolean("_alwaysFetchPolicy");
            _alreadyFetchedPolicy = info.GetBoolean("_alreadyFetchedPolicy");
            _parent = (PolicyLinkEntity)info.GetValue("_parent", typeof(PolicyLinkEntity));
            if(_parent!=null)
            {
                _parent.AfterSave+=new EventHandler(OnEntityAfterSave);
            }
            _parentReturnsNewIfNotFound = info.GetBoolean("_parentReturnsNewIfNotFound");
            _alwaysFetchParent = info.GetBoolean("_alwaysFetchParent");
            _alreadyFetchedParent = info.GetBoolean("_alreadyFetchedParent");

            base.FixupDeserialization(FieldInfoProviderSingleton.GetInstance(), PersistenceInfoProviderSingleton.GetInstance());

            // __LLBLGENPRO_USER_CODE_REGION_START DeserializationConstructor
            // __LLBLGENPRO_USER_CODE_REGION_END
        }
        /// <summary>Creates a new, empty PolicyLinkEntity object.</summary>
        /// <returns>A new, empty PolicyLinkEntity object.</returns>
        public override IEntity Create()
        {
            IEntity toReturn = new PolicyLinkEntity();

            // __LLBLGENPRO_USER_CODE_REGION_START CreateNewPolicyLink
            // __LLBLGENPRO_USER_CODE_REGION_END
            return toReturn;
        }