Esempio n. 1
0
        public static ImageFormatGroupModel DataReader_to_ImageGroupModel(SqlDataReader reader)
        {
            ImageFormatGroupModel imageGroupModel = new ImageFormatGroupModel();

            imageGroupModel.ImageFormatGroupTypeNameKey = (String)reader["ImageGroupTypeNameKey"];

            imageGroupModel.ImageFormatGroupID      = (Guid)reader["ImageGroupID"];
            imageGroupModel.ImageFormatGroupName    = (String)reader["ImageGroupName"];
            imageGroupModel.ImageFormatGroupNameKey = (String)reader["ImageGroupNameKey"];

            imageGroupModel.AllowDeletion = (bool)reader["AllowDeletion"];

            imageGroupModel.Visible = (bool)reader["Visible"];

            imageGroupModel.ImageFormats = new List <ImageFormatModel>();

            return(imageGroupModel);
        }
        internal static DataAccessResponseType InsertImageGroup(string sqlPartition, string schemaId, ImageFormatGroupModel imageGroup)//, int maxAllowed) //<-- Removed, now checked above AFTER removing non custom types
        {
            DataAccessResponseType response = new DataAccessResponseType();

            StringBuilder SqlStatement = new StringBuilder();


            //newAccountModel.Provisioned = false;

            //SQL Statements =============================================================

            //Check Row Count ===========================================================
            //SqlStatement.Append("DECLARE @ObjectCount INT ");

            /*
             * SqlStatement.Append("SET @ObjectCount = (SELECT COUNT(*) ");
             * SqlStatement.Append("FROM ");
             * SqlStatement.Append(schemaId);
             * SqlStatement.Append(".ImageGroup) ");
             * SqlStatement.Append("IF @ObjectCount < '");
             * SqlStatement.Append(maxAllowed);
             * SqlStatement.Append("' ");
             * SqlStatement.Append("BEGIN ");
             */

            //INSERT =============================================================
            SqlStatement.Append("INSERT INTO  ");
            SqlStatement.Append(schemaId);
            SqlStatement.Append(".ImageGroup (");

            SqlStatement.Append("ImageGroupTypeNameKey,");
            SqlStatement.Append("ImageGroupID,");
            SqlStatement.Append("ImageGroupName,");
            SqlStatement.Append("ImageGroupNameKey, ");
            SqlStatement.Append("CreatedDate");

            SqlStatement.Append(") VALUES (");

            //Using parameterized queries to protect against injection
            SqlStatement.Append("@ImageGroupTypeNameKey, ");
            SqlStatement.Append("@ImageGroupID, ");
            SqlStatement.Append("@ImageGroupName, ");
            SqlStatement.Append("@ImageGroupNameKey, ");
            SqlStatement.Append("@CreatedDate");

            SqlStatement.Append(")");

            //CLOSE: Check Row Count ===========================================================
            //SqlStatement.Append(" END");

            //SqlCommand sqlCommand = new SqlCommand(SqlStatement.ToString(), Sahara.Core.Settings.Azure.Databases.DatabaseConnections.DatabasePartitionSqlConnection(sqlPartition));
            SqlCommand sqlCommand = Sahara.Core.Settings.Azure.Databases.DatabaseConnections.DatabasePartitionSqlConnection(sqlPartition).CreateCommand();

            sqlCommand.CommandText = SqlStatement.ToString();



            //Using parameterized queries to protect against injection
            sqlCommand.Parameters.Add("@ImageGroupTypeNameKey", SqlDbType.Text);
            sqlCommand.Parameters.Add("@ImageGroupID", SqlDbType.UniqueIdentifier);
            sqlCommand.Parameters.Add("@ImageGroupName", SqlDbType.Text);
            sqlCommand.Parameters.Add("@ImageGroupNameKey", SqlDbType.Text);
            sqlCommand.Parameters.Add("@CreatedDate", SqlDbType.DateTime);

            //Assign values
            sqlCommand.Parameters["@ImageGroupTypeNameKey"].Value = imageGroup.ImageFormatGroupTypeNameKey;
            sqlCommand.Parameters["@ImageGroupID"].Value          = imageGroup.ImageFormatGroupID;
            sqlCommand.Parameters["@ImageGroupName"].Value        = imageGroup.ImageFormatGroupName;
            sqlCommand.Parameters["@ImageGroupNameKey"].Value     = imageGroup.ImageFormatGroupNameKey;
            sqlCommand.Parameters["@CreatedDate"].Value           = DateTime.UtcNow;


            // Add output parameters
            //SqlParameter objectCount = sqlCommand.Parameters.Add("@ObjectCount", SqlDbType.Int);
            //objectCount.Direction = ParameterDirection.Output;

            int insertResult = 0;

            sqlCommand.Connection.OpenWithRetry();

            try
            {
                insertResult = sqlCommand.ExecuteNonQueryWithRetry(); // returns Int indicating number of rows affected
                if (insertResult > 0)
                {
                    response.isSuccess = true;
                }
                else
                {
                    /*
                     * if ((int)objectCount.Value >= maxAllowed)
                     * {
                     *  return new DataAccessResponseType
                     *  {
                     *      isSuccess = false,
                     *      ErrorMessage = "Your plan does not allow for more than " + maxAllowed + " image groups. Please upgrade to increase your limits."
                     *      //ErrorMessage = "You have reached the maximum amount of categories for your account. Please upgrade your plan or contact support to increase your limits."
                     *  };
                     * }*/
                }
            }
            catch (Exception e)
            {
                //Log exception and email platform admins
                PlatformExceptionsHelper.LogExceptionAndAlertAdmins(
                    e,
                    "attempting to insert an image group into SQL",
                    System.Reflection.MethodBase.GetCurrentMethod()
                    );

                response.isSuccess    = false;
                response.ErrorMessage = e.Message;
                return(response);
            }

            sqlCommand.Connection.Close();

            return(response);
        }
        /*
         * public static DataAccessResponseType UpdateImageFormatVisibleState(Account account, string imageFormatId, bool isVisible)
         * {
         *  var response = new DataAccessResponseType();
         *
         *  //Make the update
         *  response.isSuccess = Sql.Statements.UpdateStatements.UpdateImageFormatVisibleState(account.SqlPartition, account.SchemaName, imageFormatId, isVisible);
         *
         *  //Clear all associated caches
         *  if (response.isSuccess)
         *  {
         *      Caching.InvalidateImageFormatCaches(account.AccountNameKey);
         *  }
         *
         *  return response;
         * }
         *
         * public static DataAccessResponseType UpdateImageFormatGalleryState(Account account, string imageFormatId, bool isGallery)
         * {
         *  var response = new DataAccessResponseType();
         *
         *  //Make the update
         *  response.isSuccess = Sql.Statements.UpdateStatements.UpdateImageFormatGalleryState(account.SqlPartition, account.SchemaName, imageFormatId, isGallery);
         *
         *  //Clear all associated caches
         *  if (response.isSuccess)
         *  {
         *      Caching.InvalidateImageFormatCaches(account.AccountNameKey);
         *  }
         *
         *  return response;
         * }
         *
         * public static DataAccessResponseType UpdateImageFormatListingState(Account account, string imageFormatId, bool isListing)
         * {
         *  var response = new DataAccessResponseType();
         *
         *  //Make the update
         *  response.isSuccess = Sql.Statements.UpdateStatements.UpdateImageFormatListingState(account.SqlPartition, account.SchemaName, imageFormatId, isListing);
         *
         *  //Clear all associated caches
         *  if (response.isSuccess)
         *  {
         *      Caching.InvalidateImageFormatCaches(account.AccountNameKey);
         *  }
         *
         *  return response;
         * }
         *
         */

        #endregion

        #region Delete

        public static DataAccessResponseType DeleteImageGroup(Account account, string imageGroupTypeNameKey, string imageGroupNameKey)
        {
            var response = new DataAccessResponseType();

            //Get image formats for this grouptype
            var imageFormats = GetImageFormats(account.AccountNameKey, imageGroupTypeNameKey);
            ImageFormatGroupModel imageGroupToDelete = null;

            #region Extract Group from GroupType

            foreach (ImageFormatGroupModel group in imageFormats)
            {
                if (group.ImageFormatGroupNameKey == imageGroupNameKey)
                {
                    imageGroupToDelete = group;
                }
            }

            #endregion

            #region Validate Deletion Is Allowed

            if (imageGroupToDelete == null)
            {
                return(new DataAccessResponseType
                {
                    isSuccess = false,
                    ErrorMessage = "Image group does not exist!"
                });
            }

            if (imageGroupToDelete.ImageFormats.Count > 0)
            {
                return(new DataAccessResponseType
                {
                    isSuccess = false,
                    ErrorMessage = "Cannot delete an image group that has formats attached!"
                });
            }

            //Make sure deletion is allowed
            if (!imageGroupToDelete.AllowDeletion)
            {
                return(new DataAccessResponseType
                {
                    isSuccess = false,
                    ErrorMessage = "Deletion is not allowed on this image group."
                });
            }

            #endregion

            response.isSuccess = Sql.Statements.DeleteStatements.DeleteImageGroup(account.SqlPartition, account.SchemaName, imageGroupToDelete.ImageFormatGroupID.ToString());

            //Clear Cache
            if (response.isSuccess)
            {
                Caching.InvalidateImageFormatCaches(account.AccountNameKey);
            }

            return(response);
        }
        public static DataAccessResponseType CreateImageGroup(Account account, string imageGroupTypeNameKey, string imageGroupName)
        {
            var response = new DataAccessResponseType();

            #region Validate Input

            #region Validate Image Group Name:

            ValidationResponseType ojectNameValidationResponse = ValidationManager.IsValidImageGroupName(imageGroupName);
            if (!ojectNameValidationResponse.isValid)
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = ojectNameValidationResponse.validationMessage
                });
            }

            #endregion


            if (String.IsNullOrEmpty(imageGroupTypeNameKey))
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "imageGroupTypeNameKey cannot be empty"
                });
            }

            if (String.IsNullOrEmpty(imageGroupName))
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "imageGroupName cannot be empty"
                });
            }

            #endregion

            #region Validate Plan Restrictions

            if (GetCustomImageGroupCount(account.AccountNameKey) >= account.PaymentPlan.MaxImageGroups)
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "You have reached your account restriction of " + account.PaymentPlan.MaxImageGroups + " custom image groups"
                });
            }

            #endregion

            //CREATE NAME KEY
            var imageGroupNameKey = Sahara.Core.Common.Methods.ObjectNames.ConvertToObjectNameKey(imageGroupName);

            #region Validate GroupType Exists

            var imageGroupTypes = GetImageFormatGroupTypes();

            bool typeExists = false;

            foreach (ImageFormatGroupTypeModel type in imageGroupTypes)
            {
                if (type.ImageFormatGroupTypeNameKey == imageGroupTypeNameKey)
                {
                    typeExists = true;
                }
            }

            if (!typeExists)
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "Format group type '" + imageGroupTypeNameKey + "' does not exists!"
                });
            }

            #endregion

            #region Validate GroupName is unique to this type

            var imageFormats = GetImageFormats(account.AccountNameKey, imageGroupTypeNameKey);

            bool nameUniqueInType = true;

            foreach (ImageFormatGroupModel group in imageFormats)
            {
                if (group.ImageFormatGroupTypeNameKey == imageGroupTypeNameKey && group.ImageFormatGroupNameKey == imageGroupNameKey)
                {
                    nameUniqueInType = false;
                }
            }

            if (!nameUniqueInType)
            {
                return(new DataAccessResponseType {
                    isSuccess = false, ErrorMessage = "Group name '" + imageGroupNameKey + "' is not unique to the '" + imageGroupTypeNameKey + "' type!"
                });
            }

            #endregion

            #region Create Model & ID

            var imageGroup = new ImageFormatGroupModel
            {
                ImageFormatGroupTypeNameKey = imageGroupTypeNameKey,

                ImageFormatGroupID      = Guid.NewGuid(),
                ImageFormatGroupName    = imageGroupName,
                ImageFormatGroupNameKey = imageGroupNameKey
            };

            imageGroup.ImageFormatGroupID = Guid.NewGuid();

            #endregion

            //INSERT
            response = Sql.Statements.InsertStatements.InsertImageGroup(account.SqlPartition, account.SchemaName, imageGroup); //, account.PaymentPlan.MaxImageGroups);

            //CLear Cache
            if (response.isSuccess)
            {
                Caching.InvalidateImageFormatCaches(account.AccountNameKey);
            }

            return(response);
        }
        public static ImageFormatModel GetImageFormat(string accountNameKey, string groupType, string groupName, string formatName, out ImageFormatGroupModel imageGroup)
        {
            imageGroup = null;
            ImageFormatModel imageFormat = null;

            //Get all formats for this group type
            var imageFormats = GetImageFormats(accountNameKey, groupType);

            //Extract the correct image format for this processing submission
            foreach (ImageFormatGroupModel group in imageFormats)
            {
                if (group.ImageFormatGroupNameKey == groupName)
                {
                    imageGroup = group;

                    foreach (ImageFormatModel format in group.ImageFormats)
                    {
                        if (format.ImageFormatNameKey == formatName)
                        {
                            imageFormat = format;
                        }
                    }
                }
            }



            return(imageFormat);
        }