public void CanCustomRepository()
        {
            PersistentVariableRepository persistencevariableRepository = new PersistentVariableRepository();

            HatCMS.CmsPersistentVariable variable = persistencevariableRepository.FetchbyName("LastPeriodicTaskStartTime_HatCms.Admin.BackgroundTasks.EmailConfigErrors");
            Assert.That(variable.Name, Is.EqualTo("LastPeriodicTaskStartTime_HatCms.Admin.BackgroundTasks.EmailConfigErrors"));
        }
 public void CanCreatePersistenVariable()
 {
     HatCMS.CmsPersistentVariable persistentvariable = new HatCMS.CmsPersistentVariable();
     persistentvariable.Name = "testname";
     Assert.IsNotNull(persistentvariable);
     Assert.That(persistentvariable.Name, Is.EqualTo("testname"));
 }
        private static void SaveLastPeriodicTaskStartTime(CmsBackgroundTask task, DateTime startTime)
        {
            string persistentVariableName = getLastStartPersistentVariableName(task);
            CmsPersistentVariable v       = CmsPersistentVariable.Fetch(persistentVariableName);

            v.Name           = persistentVariableName;
            v.PersistedValue = startTime;

            v.SaveToDatabase();
        }
        /// <summary>
        /// returns DateTime.MinValue if the task has never been run, or if the last run time could not be determined.
        /// </summary>
        /// <param name="task"></param>
        /// <returns></returns>
        private static DateTime GetLastPeriodicTaskStartTime(CmsBackgroundTask task)
        {
            string persistentVariableName = getLastStartPersistentVariableName(task);
            CmsPersistentVariable v       = CmsPersistentVariable.Fetch(persistentVariableName);

            if (v.Name == persistentVariableName)
            {
                return((DateTime)v.PersistedValue);
            }
            return(DateTime.MinValue);
        }
            private CmsPersistentVariable GetFromRow(DataRow dr)
            {
                string Name = (dr["Name"]).ToString();

                byte[] bytes = (byte[])dr["PersistedValue"];

                CmsPersistentVariable item = new CmsPersistentVariable(Name, DeSerialize(bytes));

                item.persistentvariableid = Convert.ToInt32(dr["PersistentVariableId"]);
                return(item);
            } // GetFromRow
        } // constructor

        public virtual bool SaveToDatabase()
        {
            IRepository <CmsPersistentVariable> repository = new Repository <CmsPersistentVariable>();
            CmsPersistentVariable returnPersistenVariable  = repository.SaveOrUpdate(this);

            if (returnPersistenVariable.Id > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        } // SaveToDatabase
            } // Insert

            public bool Update(CmsPersistentVariable item)
            {
                using (MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(ConfigUtils.getConfigValue("ConnectionString", "")))
                {
                    OpenMySqlConnection(conn);

                    MySql.Data.MySqlClient.MySqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "UPDATE persistentvariables set Name = @Name, PersistedValue = @PersistedValue where PersistentVariableId = @PersistentVariableId;";
                    cmd.Parameters.AddWithValue("@Name", item.Name);
                    cmd.Parameters.AddWithValue("@PersistedValue", Serialize(item.PersistedValue));
                    cmd.Parameters.AddWithValue("@PersistentVariableId", item.persistentvariableid);

                    int numUpdated = cmd.ExecuteNonQuery();

                    if (numUpdated < 1)
                    {
                        return(false);
                    }

                    return(true);
                }
            } // Update
            public bool Insert(CmsPersistentVariable item)
            {
                using (MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(ConfigUtils.getConfigValue("ConnectionString", "")))
                {
                    OpenMySqlConnection(conn);
                    MySql.Data.MySqlClient.MySqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "INSERT INTO persistentvariables (Name, PersistedValue) VALUES(@Name, @PersistedValue);";
                    cmd.Parameters.AddWithValue("@Name", item.Name);
                    cmd.Parameters.AddWithValue("@PersistedValue", Serialize(item.PersistedValue));

                    int numInserted = cmd.ExecuteNonQuery();

                    if (numInserted < 1)
                    {
                        return(false);
                    }

                    int newId = Convert.ToInt32(cmd.LastInsertedId);
                    item.persistentvariableid = newId;

                    return(true);
                }
            } // Insert
        public static void Delete(CmsPersistentVariable persistentVariableToDelete)
        {
            PersistentVariableRepository repository = new PersistentVariableRepository();

            repository.Delete(persistentVariableToDelete);
        } // Delete
        } // FetchAll

        public static bool Delete(CmsPersistentVariable persistentVariableToDelete)
        {
            return((new PersistentVariableDB()).Delete(persistentVariableToDelete.persistentvariableid));
        } // Delete