public override void AddUserToProject(out int id, string username, string projectRole, Project project)
        {
            var user = SEOMembershipManager.GetUser(username);
            var projectRoleObj = GetProjectRoleByName(projectRole);

            if (projectRoleObj == null) throw new ProjectRoleNameMustExistException("the project role must exist " + projectRole);
            if (projectRoleObj.IdRoleType != (Int32)RoleType.ProjectRole) throw new ProjectRoleNameMustExistException("the project role must exist " + projectRole);

            using (var tran = new TransactionScope(_connName))
            {
                var projectUser = new ProjectUser
                                      {
                                          Project = project,
                                          ProjectRole = projectRoleObj,
                                          SEOToolsetUser = user,
                                          Enabled = true,
                                          MonitorEmails = false
                                      };

                var ds = DSProjectUser.Create(_connName);

                ds.Insert(projectUser);
                tran.Commit();

                id = projectUser.Id;
            }
        }
        public override int DeleteInactiveProfiles(ProfileAuthenticationOption authenticationOption,
                                                   DateTime userInactiveSinceDate)
        {
            ProfileType? profileType = null;
            if (authenticationOption == ProfileAuthenticationOption.Anonymous)
                profileType = ProfileType.Anonymous;
            else if (authenticationOption == ProfileAuthenticationOption.Authenticated)
                profileType = ProfileType.Authenticated;

            int profilesDeleted = 0;

            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                ProfileUserDataStore profileStore = new ProfileUserDataStore(transaction);

                IList<ProfileUser> users = profileStore.FindByFields(ApplicationName, null, userInactiveSinceDate, profileType, PagingInfo.All);

                profilesDeleted = users.Count;

                foreach (ProfileUser user in users)
                {
                    profileStore.Delete(user.Id);
                }

                transaction.Commit();
            }

            return profilesDeleted;
        }
Beispiel #3
0
        public static void ExecuteSqlScript(string script, string connectionName, bool throwExceptions)
        {
            var cfgHelper =
               NHibernateConfigurationManager.ConfigurationHelper;

            var session = cfgHelper.GetCurrentSession(connectionName);
            using (var tran = new TransactionScope(connectionName))
            {
                try
                {
                    var cmd = session.Connection.CreateCommand();
                    cmd.CommandText = script;
                    session.Transaction.Enlist(cmd);
                    cmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    LoggerFacade.Log.LogException(typeof(SqlExecutor), ex);
                    if (throwExceptions)
                        throw;
                }

                tran.Commit();
            }
        }
        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
              {
            RoleDataStore roleStore = new RoleDataStore(transaction);

            //Find the roles
            Role[] roles = new Role[roleNames.Length];
            for (int i = 0; i < roleNames.Length; i++)
            {
              //Find the role
              Role role = roleStore.FindByName(ApplicationName, roleNames[i]);
              if (role == null)
            throw new RoleNotFoundException(roleNames[i]);

              roles[i] = role;
            }

            UserInRoleDataStore usersInRolesStore = new UserInRoleDataStore(transaction);
            foreach (string userName in usernames)
            {
              foreach (Role role in roles)
              {
            UserInRole userInRole = new UserInRole(ApplicationName, userName, role.Name);

            usersInRolesStore.Insert(userInRole);
              }
            }

            transaction.Commit();
              }
        }
        public override Article CreateArticle(Category category, string owner,
                                            string name, string title, 
                                            string description, string body)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                ArticleDataStore articleStore = new ArticleDataStore(transaction);
                if (articleStore.FindByName(name) != null)
                    throw new ArticleNameAlreadyExistsException(name);

                CategoryDataStore dataStore = new CategoryDataStore(transaction);
                dataStore.Attach(category);

                Article article = new Article(category, name, owner, title, description, body);
                article.Author = owner;

                if (category.AutoApprove)
                    article.Approved = true;

                articleStore.Insert(article);

                transaction.Commit();

                return article;
            }
        }
        public override int DeleteInactiveProfiles(ProfileAuthenticationOption authenticationOption,
                                                   DateTime userInactiveSinceDate)
        {
            ProfileType? profileType = null;
            if (authenticationOption == ProfileAuthenticationOption.Anonymous)
                profileType = ProfileType.Anonymous;
            else if (authenticationOption == ProfileAuthenticationOption.Authenticated)
                profileType = ProfileType.Authenticated;

            int profilesDeleted;

            using (var transaction = new TransactionScope(_connName))
            {
                var profileStore = DSSEOProfile.Create(_connName);

                IList<SEOProfile> users = profileStore.FindByFields(ApplicationName, null, userInactiveSinceDate,
                                                                    profileType, PagingInfo.All);

                profilesDeleted = users.Count;

                foreach (var user in users)
                {
                    profileStore.Delete(user.Id);
                }

                transaction.Commit();
            }

            return profilesDeleted;
        }
Beispiel #7
0
 public static void ExecuteInTransaction(OnTransaction onTransaction, string connectionName)
 {
     using (var t = new TransactionScope(connectionName))
     {
         if (onTransaction != null)
             if (onTransaction(connectionName))
                 t.Commit();
     }
 }
 public override int CreateSubscriptionLevel(string level, Role role, double price)
 {
     var ds = DSSubscriptionLevel.Create(_connName);
     int id;
     using (var tran = new TransactionScope(_connName))
     {
         id = ds.CreateSubscriptionLevel(level, role, price);
         tran.Commit();
     }
     return id;
 }
        public override void DeleteCategory(Category category)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                CategoryDataStore dataStore = new CategoryDataStore(transaction);

                dataStore.Delete(category.Id);

                transaction.Commit();
            }
        }
 public override int CreateSubscriptionProperty(string propertyName)
 {
     var ds = DSSubscriptionProperty.Create(_connName);
     int id;
     using (var tran = new TransactionScope(_connName))
     {
         id = ds.CreateSubscriptionProperty(propertyName);
         tran.Commit();
     }
     return id;
 }
 public override void CreateSubscriptionDetails(int accountId, IDictionary<int, string> propertyValues)
 {
     var ds = DSSubscriptionDetail.Create(_connName);
     using (var tran = new TransactionScope(_connName))
     {
         foreach (var keyValuePair in propertyValues)
         {
              ds.CreateSubscriptionDetail(accountId, keyValuePair.Key, keyValuePair.Value);
         }
         tran.Commit();
     }
 }
 ///<summary>
 ///If the promotion has times of use, then it is updated
 ///</summary>
 ///<param name="promoCode"></param>
 public override void Consume(string promoCode)
 {
     var ds = DSPromoCode.Create(_connName);
     var promo = ds.FindByCode(promoCode);
     if (!promo.TimesUsed.HasValue || !promo.MaxUse.HasValue)
         return;
     if (promo.TimesUsed.Value >= promo.MaxUse.Value)
         throw new Exceptions.ExceededMaximumUsageException();
     promo.TimesUsed++;
     using (var tran = new TransactionScope(_connName))
     {
         ds.Update(promo);
         tran.Commit();
     }
 }
        public override Category CreateCategory(string name, string displayName)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                CategoryDataStore dataStore = new CategoryDataStore(transaction);

                Category category = new Category(name, displayName);

                dataStore.Insert(category);

                transaction.Commit();

                return category;
            }
        }
        public override void AddPermissionToRole(string role, string permission)
        {
            using (var tran = new TransactionScope(_connName))
            {
                var dsPermission = DSPermission.Create(_connName);
                var dsRole = DSRole.Create(_connName);
                var dsRolePermission = DSPermissionRole.Create(_connName);

                dsRolePermission.Insert(new PermissionRole
                                            {
                                                Permission = dsPermission.FindByName(permission),
                                                Role = dsRole.FindByName(role)
                                            });

                tran.Commit();
            }
        }
        public override Item CreateItem(Category category, string owner,
                                        string title, string description,
                                        string url, string urlName, 
                                        DateTime newsDate)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                ItemDataStore dataStore = new ItemDataStore(transaction);

                Item item = new Item(category, owner, title, description, url, urlName, newsDate);

                dataStore.Insert(item);

                transaction.Commit();

                return item;
            }
        }
        public override void CreateRole(string roleName)
        {
            //Check required for MSDN
              if (roleName == null || roleName == "")
            throw new System.Configuration.Provider.ProviderException("Role name cannot be empty or null.");
              if (roleName.IndexOf(',') > 0)
            throw new ArgumentException("Role names cannot contain commas.");
              if (RoleExists(roleName))
            throw new System.Configuration.Provider.ProviderException("Role name already exists.");

              using (TransactionScope transaction = new TransactionScope(mConfiguration))
              {
            RoleDataStore roleStore = new RoleDataStore(transaction);

            roleStore.Insert(new Role(ApplicationName, roleName));

            transaction.Commit();
              }
        }
        public override void AddUserToRole(string userName, string roleName)
        {
            using (var tran = new TransactionScope(_connName))
            {
                var dsRole = DSRole.Create(_connName);
                var dsUser = DSSEOToolsetUser.Create(_connName);

                var user = dsUser.FindByName(userName);

                if (user == null)
                {
                    tran.Rollback();
                    throw new Exception("User Not Found");
                }

                user.UserRole = dsRole.FindByName(roleName);
                dsUser.Update(user);
                tran.Commit();
            }
        }
        public override void CreateAccount(Account account)
        {
            using (var tran = new TransactionScope(_connName))
            {
                var ds = DSAccount.Create(_connName);

                if (account.Owner == null)
                {
                    account.Owner = GetTopLevelAccount();
                }

                if (ds.FindByName(account.Name) != null)
                {
                    tran.Rollback();
                    throw new DuplicatedEntityException("Account");
                }

                ds.Insert(account);
                tran.Commit();
            }
        }
 public override int CreateRankingMonitor(int idProject, string loginName, int[] idKeywordLists, out int keywordsTotal)
 {
     var dsKeyword = DSKeyword.Create(_connName);
     var keywords = dsKeyword.FindUniqueByKeywordLists(idKeywordLists);
     keywordsTotal = keywords.Count;
     var keywordAnalysis = new List<KeywordAnalysis>();
     foreach (var keyword in keywords)
     {
         var keywordAnalysisItem = new KeywordAnalysis
                                       {
                                           Keyword = keyword,
                                           Status = "P"
                                       };
         var keyword1 = keyword;
         if (keywordAnalysis.Exists(Item => Item.Keyword == keyword1)) continue;
         keywordAnalysis.Add(keywordAnalysisItem);
     }
     var rankingMonitorRun = new RankingMonitorRun
                                 {
                                     AnalysisType = "M",
                                     ExecutionDate = DateTime.Now,
                                     Project = new Project { Id = idProject },
                                     Status = new Status { Name = "P" },
                                     User = loginName
                                 };
     using (var tran = new TransactionScope(_connName))
     {
         var dsRankingMonitorRun = DSRankingMonitorRun.Create(_connName);
         var dsKeywordAnalysis = DSKeywordAnalysis.Create(_connName);
         dsRankingMonitorRun.Insert(rankingMonitorRun);
         keywordAnalysis.ForEach(KeywordAnalysis =>
                                     {
                                         KeywordAnalysis.RankingMonitorRun = rankingMonitorRun;
                                         dsKeywordAnalysis.Insert(KeywordAnalysis);
                                     });
         tran.Commit();
     }
     return rankingMonitorRun.Id;
 }
        public override void AddCompetitor(out int id, string name, string url, string description, Project project)
        {
            using (var tran = new TransactionScope(_connName))
            {
                var ds = DSCompetitor.Create(_connName);

                var ce = new Competitor();

                if (name != null)
                    ce.Name = name;
                if (url != null)
                    ce.Url = url;
                if (description != null)
                    ce.Description = description;
                if (project != null)
                    ce.Project = project;

                ds.Insert(ce);

                tran.Commit();

                id = ce.Id;
            }
        }
        public override void AddUsersToRole(string[] userNames, string roleName)
        {
            Check.Ensure(!String.IsNullOrEmpty(roleName), "roleName must not be null nor Empty");
            Check.Ensure(userNames.Length > 0, "At least one username must be provided");

            using (var tran = new TransactionScope(_connName))
            {
                var dsUser = DSSEOToolsetUser.Create(_connName);
                var dsRole = DSRole.Create(_connName);

                for (var i = 0; i < userNames.Length; i++)
                {
                    var user = dsUser.FindByName(userNames[i]);

                    if (user != null)
                    {
                        user.UserRole = dsRole.FindByName(roleName);
                    }

                    dsUser.Update(user);
                }
                tran.Commit();
            }
        }
        public override void UpdateFileAttachment(FileAttachment attachment)
        {
            //Check attachment
            if (attachment != null)
                Attachment.FileHelper.CheckFile(attachment, attachment.Article.Category.AttachExtensions, attachment.Article.Category.AttachMaxSize);

            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                FileAttachmentDataStore dataStore = new FileAttachmentDataStore(transaction);

                dataStore.Update(attachment);

                transaction.Commit();
            }
        }
        /// <summary>
        /// Update the specified article. Increment the version if required.
        /// </summary>
        /// <param name="article"></param>
        /// <param name="backupVersion">If true the previous article version is saved as a backup in the VersionedArticle and the current version is incremented.</param>
        public override void UpdateArticle(Article article, bool backupVersion)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                ArticleDataStore dataStore = new ArticleDataStore(transaction);

                VersionedArticle versionedArticle = null;

                if (backupVersion)
                {
                    //Retrive the previous version (before saving the new instance) and save a versioned row

                    Article prevVersion = dataStore.FindByKey(article.Id);
                    if (prevVersion == null)
                        throw new ArticleNotFoundException(article.Id);

                    versionedArticle = new VersionedArticle(prevVersion);

                    VersionedArticleDataStore versionedStore = new VersionedArticleDataStore(transaction);
                    versionedStore.Insert(versionedArticle);

                    //Increment the current article version
                    article.IncrementVersion();
                }

                //flag the entity to be updated and attach the entity to the db
                // I must use InsertOrUpdateCopy because if backupVersion = true there is already a
                // persistent entity in the session and I must copy the values to this instance. The Update method in this case throw an exception
                article = dataStore.InsertOrUpdateCopy(article);

                transaction.Commit();
            }
        }
        public override string[] GetFileAttachments(Article article, EnabledStatus enabledStatus)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                FileAttachmentDataStore dataStore = new FileAttachmentDataStore(transaction);

                return dataStore.GetArticleAttachments(article, enabledStatus);
            }
        }
        public override FileAttachment GetFileAttachmentByName(Article article, string name, bool throwIfNotFound)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                FileAttachmentDataStore dataStore = new FileAttachmentDataStore(transaction);

                FileAttachment attachment = dataStore.FindByArticleVersion(article, name);
                if (attachment == null && throwIfNotFound)
                    throw new FileAttachNotFoundException(article.Name + "." + name);
                else if (attachment == null)
                    return null;

                return attachment;
            }
        }
        public override FileAttachment GetFileAttachment(string id)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                FileAttachmentDataStore dataStore = new FileAttachmentDataStore(transaction);

                FileAttachment attachment = dataStore.FindByKey(id);
                if (attachment == null)
                    throw new FileAttachNotFoundException(id);

                return attachment;
            }
        }
        public override Category GetCategoryByName(string name, bool throwIfNotFound)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                CategoryDataStore dataStore = new CategoryDataStore(transaction);

                Category category = dataStore.FindByName(name);
                if (category == null && throwIfNotFound)
                    throw new WikiCategoryNotFoundException(name);
                else if (category == null)
                    return null;

                return category;
            }
        }
        public override Category GetCategory(string id)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                CategoryDataStore dataStore = new CategoryDataStore(transaction);

                Category category = dataStore.FindByKey(id);
                if (category == null)
                    throw new WikiCategoryNotFoundException(id);

                return category;
            }
        }
        public override FileAttachment CreateFileAttachment(Article article, string name, string contentType, byte[] contentData)
        {
            FileAttachment attachment = new FileAttachment(article, name, contentType, contentData);

            //Check attachment
            if (attachment != null)
                Attachment.FileHelper.CheckFile(attachment, article.Category.AttachExtensions, article.Category.AttachMaxSize);

            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                ArticleDataStore dataStore = new ArticleDataStore(transaction);
                dataStore.Attach(article);

                FileAttachmentDataStore attachmentStore = new FileAttachmentDataStore(transaction);
                attachmentStore.Insert(attachment);

                transaction.Commit();

                return attachment;
            }
        }
        /// <summary>
        /// Get a list of article versions (also with the latest version)
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        public override IList<ArticleBase> GetArticleVersions(Article article)
        {
            using (TransactionScope transaction = new TransactionScope(mConfiguration))
            {
                VersionedArticleDataStore dataStore = new VersionedArticleDataStore(transaction);

                IList<VersionedArticle> versionedArticles = dataStore.GetArticleVersions(article);

                List<ArticleBase> list = new List<ArticleBase>();

                //Add the latest version
                list.Add(article);

                //add all the other versions
                foreach (VersionedArticle verArticle in versionedArticles)
                    list.Add(verArticle);

                return list;
            }
        }