Exemplo n.º 1
0
        /// <summary>
        /// Process the entries.
        /// </summary>
        /// <param name="itemsToProcess"></param>
        public static void ProcessQueue(IList <TagsEntry> itemsToProcess)
        {
            if (itemsToProcess == null || itemsToProcess.Count == 0)
            {
                return;
            }

            foreach (var entry in itemsToProcess)
            {
                string error = string.Format("Unable to apply tags for GroupId[{0}], RefId[{1}], Tags[{2}]", entry.GroupId, entry.RefId, entry.Tags);
                Try.CatchLog(error, () =>
                {
                    // Delete existing tags for the associated group/ref id combination.
                    Tag.Repository.Delete(Query <Tag> .New().Where(t => t.GroupId).Is(entry.GroupId)
                                          .And(t => t.RefId).Is(entry.RefId));

                    // Convert string to Tag objects.
                    List <Tag> tags = TagHelper.ConvertToTags(entry.Tags, entry.GroupId, entry.RefId);
                    if (tags != null && tags.Count > 0)
                    {
                        foreach (var tag in tags)
                        {
                            Tag.Create(tag);
                        }
                    }
                });
            }
        }
        /// <summary>
        /// Create the user.
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="email"></param>
        /// <param name="roles"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public BoolMessageItem CreateUser(string userName, string password, string email, string roles, ref MembershipCreateStatus status)
        {
            // Create the User and Profile.
            BoolMessageItem <User>    result      = User.Create(userName, email, password, roles, ref status);
            BoolMessageItem <Profile> finalResult = new BoolMessageItem <Profile>(null, result.Success, result.Message);

            if (result.Success)
            {
                finalResult = Profile.Create(result.Item);

                // Rollback the user
                if (!finalResult.Success)
                {
                    Try.CatchLog("Unable to delete user : "******" after error while creating profile.", () => User.Delete(result.Item.Id));
                    status = MembershipCreateStatus.UserRejected;
                }
                else // Send user welcome email.
                {
                    Profile profile   = finalResult.Item;
                    string  sitename  = Configuration.Config.Get <string>("Notifications", "WebSite.Name");
                    string  firstname = string.IsNullOrEmpty(profile.FirstName) ? profile.UserName : profile.FirstName;
                    if (EnableNotifications)
                    {
                        Notifications.Notifier.WelcomeNewUser(result.Item.Email, "Welcome to " + sitename, firstname, profile.UserName, password);
                    }
                }
            }
            return(finalResult);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Create an install sql file specifically for creating the table for this model.
        /// The location of the file is obtained from the settings and the model name.
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="currentModel"></param>
        /// <param name="sql"></param>
        public virtual void CreateInstallSqlFile(ModelContext ctx, Model currentModel, string sql)
        {
            string fileName = currentModel.InstallSqlFile;

            fileName = ctx.AllModels.Settings.ModelInstallLocation + "install_model_" + fileName;
            string error = string.Format("Error creating sql install file {0} for model {1}", fileName, currentModel.Name);

            Try.CatchLog(error, () => File.WriteAllText(fileName, sql));
        }
        /// <summary>
        /// Determines whether this instance can import the specified items.
        /// </summary>
        /// <param name="data"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public BoolErrorsItem <IList <T> > Load(IDictionary data, string format)
        {
            var errors = new ValidationResults();
            var result = new BoolErrorsItem <IList <T> >(null, false, string.Empty, errors);

            // For each section.
            Try.CatchLog(() =>
            {
                IMapper <T> mapper = _mappers[format];
                IList <T> results  = mapper.Map(data, errors);
                result             = new BoolErrorsItem <IList <T> >(results, results != null && results.Count > 0, errors.Message(), errors);
            });
            return(result);
        }
Exemplo n.º 5
0
 /// <summary>
 /// Executes the method action by wrapping it with
 /// 1. Logging of start / end time.
 /// 2. Status updates.
 /// </summary>
 /// <param name="methodName">The name of the method being executed.</param>
 /// <param name="taskName">Option name to use for the TaskName for StatusUpdates.</param>
 /// <param name="wrapTryCatch">Whether or not to wrap the call inside a try catch.</param>
 /// <param name="action">Action to execute.</param>
 public void Execute(string methodName, string taskName, bool wrapTryCatch, Action action)
 {
     Execute(methodName, taskName, () =>
     {
         if (!wrapTryCatch)
         {
             action();
         }
         else
         {
             Try.CatchLog(action);
         }
     });
 }
Exemplo n.º 6
0
        /// <summary>
        /// Create table in the database.
        /// </summary>
        /// <param name="sqls"></param>
        /// <param name="ctx"></param>
        /// <param name="currentModel"></param>
        public virtual void ExecuteSqlInDb(List <string> sqls, ModelContext ctx, Model currentModel)
        {
            DbCreateType createType = ctx.AllModels.Settings.DbAction_Create;
            DBSchema     helper     = new DBSchema(_conn);
            string       error      = "Error executing sql for model : " + currentModel.Name + " table name : " + currentModel.TableName;

            Try.CatchLog(error, () =>
            {
                foreach (string sql in sqls)
                {
                    helper.ExecuteNonQuery(sql, CommandType.Text, null);
                }
            });
        }
        /// <summary>
        /// Determines whether this instance can import the specified items.
        /// </summary>
        /// <param name="text">The text to import( as csv, xml, ini)</param>
        /// <param name="format">csv, xml, ini, json</param>
        /// <returns></returns>
        public virtual BoolErrorsItem <IList <T> > LoadText(string text, string format)
        {
            if (!_mappers.ContainsKey(format.ToLower()))
            {
                return(new BoolErrorsItem <IList <T> >(null, false, "Format : " + format + " not supported.", null));
            }

            BoolErrorsItem <IList <T> > canImport = new BoolErrorsItem <IList <T> >(null, false, "", null);

            // For each section.
            Try.CatchLog(() =>
            {
                var mapper      = _mappers[format.ToLower()];
                var errors      = new ValidationResults();
                IList <T> items = mapper.MapFromText(text, errors);
                bool success    = items != null && items.Count > 0;
                canImport       = new BoolErrorsItem <IList <T> >(items, success, string.Empty, errors);
            });
            return(canImport);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Creates the models in the database.
        /// </summary>
        /// <param name="ctx"></param>
        /// <returns></returns>
        public BoolMessageItem <ModelContainer> Process(ModelContext ctx)
        {
            var      bufferTables = new StringBuilder();
            var      bufferProcs  = new StringBuilder();
            var      bufferBoth   = new StringBuilder();
            var      bufferDrop   = new StringBuilder();
            DBSchema schema       = new DBSchema(_conn);

            ctx.AllModels.Iterate(m => ctx.CanProcess(m, (model) => model.GenerateTable), currentModel =>
            {
                // Create the database table for all the models.
                List <Model> modelInheritanceChain = ctx.AllModels.InheritanceFor(currentModel.Name);
                Validate(ctx, currentModel);

                // Create table schema for model & create in database.
                DataTable modelTable  = ConvertModelChainToTable(currentModel, modelInheritanceChain, ctx);
                string sqlTableSchema = string.Empty, sqlProcs = string.Empty, sqlModel = string.Empty;
                string sqlDrop        = string.Empty, sqlDropProcs = string.Empty, sqlDropTable = string.Empty;

                // Build sql for
                // 1. create table
                // 2. create procs
                // 3. create table & procs
                // 4. drop procs & table
                sqlDropTable           = schema.GetDropTable(currentModel.TableName, true);
                var sqlDropTableNoGo   = schema.GetDropTable(currentModel.TableName, false);
                sqlTableSchema         = BuildTableSchema(ctx, modelInheritanceChain, currentModel, true);
                var sqlTableSchemaNoGo = BuildTableSchema(ctx, modelInheritanceChain, currentModel, false);
                List <string> sqlTable = new List <string>()
                {
                    sqlDropTableNoGo, sqlTableSchemaNoGo
                };
                List <string> procNames = CodeFileHelper.GetProcNames(ctx.AllModels.Settings.ModelDbStoredProcTemplates);
                BoolMessageItem <List <string> > storedProcSql     = CreateStoredProcs(ctx, currentModel, true);
                BoolMessageItem <List <string> > storedProcSqlNoGo = CreateStoredProcs(ctx, currentModel, false);
                if (storedProcSql.Success)
                {
                    storedProcSql.Item.ForEach(proc => sqlProcs += proc + Environment.NewLine);
                }
                procNames.ForEach(procName => sqlDropProcs += schema.GetDropProc(currentModel.TableName, procName) + Environment.NewLine);

                sqlModel = sqlDropTable + Environment.NewLine + sqlTableSchema + Environment.NewLine + sqlProcs;
                sqlDrop  = sqlDropProcs + sqlDropTable + Environment.NewLine;

                // Create in the database.
                ExecuteSqlInDb(sqlTable, ctx, currentModel);
                ExecuteSqlInDb(storedProcSqlNoGo.Item, ctx, currentModel);

                // Create model install file containing both the table/procs sql.
                CreateInstallSqlFile(ctx, currentModel, sqlModel);

                bufferTables.Append(sqlTable + Environment.NewLine);
                bufferProcs.Append(sqlProcs + Environment.NewLine + Environment.NewLine);
                bufferBoth.Append(sqlModel + Environment.NewLine);
                bufferDrop.Append(sqlDrop + Environment.NewLine);
            });

            // Create the files.
            string installPath = ctx.AllModels.Settings.ModelInstallLocation;

            Try.CatchLog(() => File.WriteAllText(installPath + "_install_models_tables.sql", bufferTables.ToString()));
            Try.CatchLog(() => File.WriteAllText(installPath + "_install_models_tables.sql", bufferTables.ToString()));
            Try.CatchLog(() => File.WriteAllText(installPath + "_install_models_procs.sql", bufferProcs.ToString()));
            Try.CatchLog(() => File.WriteAllText(installPath + "_install_models_all.sql", bufferBoth.ToString()));
            Try.CatchLog(() => File.WriteAllText(installPath + "_uninstall_models.sql", bufferDrop.ToString()));
            return(null);
        }