/// <summary>
        /// Import authority groups.
        /// </summary>
        /// <remarks>
        /// Creates any authority groups that do not already exist.
        /// This method performs an additive import.  It will never remove an existing authority group or
        /// remove authority tokens from an existing group.
        /// </remarks>
        /// <param name="groupDefs"></param>
        /// <param name="context"></param>
        public IList <AuthorityGroup> Import(IEnumerable <AuthorityGroupDefinition> groupDefs, IUpdateContext context)
        {
            // first load all the existing tokens into memory
            // there should not be that many tokens ( < 500), so this should not be a problem
            IAuthorityTokenBroker  tokenBroker    = context.GetBroker <IAuthorityTokenBroker>();
            IList <AuthorityToken> existingTokens = tokenBroker.FindAll();

            // load existing groups
            IAuthorityGroupBroker  groupBroker    = context.GetBroker <IAuthorityGroupBroker>();
            IList <AuthorityGroup> existingGroups = groupBroker.FindAll();

            foreach (AuthorityGroupDefinition groupDef in groupDefs)
            {
                AuthorityGroup group = CollectionUtils.SelectFirst(existingGroups,
                                                                   g => g.Name == groupDef.Name);

                // if group does not exist, create it
                if (group == null)
                {
                    group = new AuthorityGroup
                    {
                        Name        = groupDef.Name,
                        Description = groupDef.Description,
                        DataGroup   = groupDef.DataGroup
                    };
                    context.Lock(group, DirtyState.New);
                    existingGroups.Add(group);
                }

                // process all token nodes contained in group
                foreach (string tokenName in groupDef.Tokens)
                {
                    AuthorityToken token = CollectionUtils.SelectFirst(existingTokens,
                                                                       t => t.Name == tokenName);

                    // ignore non-existent tokens
                    if (token == null)
                    {
                        continue;
                    }

                    // add the token to the group
                    group.AuthorityTokens.Add(token);
                }
            }

            return(existingGroups);
        }
Beispiel #2
0
        private AuthorityGroup LoadOrCreateGroup(string name, IPersistenceContext context)
        {
            AuthorityGroup group = null;

            try
            {
                AuthorityGroupSearchCriteria criteria = new AuthorityGroupSearchCriteria();
                criteria.Name.EqualTo(name);

                IAuthorityGroupBroker broker = context.GetBroker <IAuthorityGroupBroker>();
                group = broker.FindOne(criteria);
            }
            catch (EntityNotFoundException)
            {
                group      = new AuthorityGroup();
                group.Name = name;
                context.Lock(group, DirtyState.New);
            }
            return(group);
        }