public int RestoreItem(string tempFolder, XmlNode itemNode, int itemId, Type itemType, string itemName, int packageId, int serviceId, ResourceGroupInfo group)
        {
            if (itemType == typeof(SqlDatabase))
            {
                DatabaseServer sql = GetDatabaseServer(serviceId);

                // extract meta item
                XmlSerializer serializer = new XmlSerializer(typeof(SqlDatabase));
                SqlDatabase   db         = (SqlDatabase)serializer.Deserialize(
                    new XmlNodeReader(itemNode.SelectSingleNode("SqlDatabase")));

                // create database if required
                if (!sql.DatabaseExists(itemName))
                {
                    sql.CreateDatabase(db);
                }

                // copy database backup to remote server
                XmlNode fileNode            = itemNode.SelectSingleNode("File[@name='DatabaseBackup']");
                string  backupFileName      = fileNode.Attributes["path"].Value;
                long    backupFileLength    = Int64.Parse(fileNode.Attributes["size"].Value);
                string  localBackupFilePath = Path.Combine(tempFolder, backupFileName);

                if (new FileInfo(localBackupFilePath).Length != backupFileLength)
                {
                    return(-3);
                }

                FileStream stream = new FileStream(localBackupFilePath, FileMode.Open, FileAccess.Read);
                byte[]     buffer = new byte[FILE_BUFFER_LENGTH];

                int    readBytes        = 0;
                long   length           = 0;
                string remoteBackupPath = null;
                do
                {
                    // read package file
                    readBytes = stream.Read(buffer, 0, FILE_BUFFER_LENGTH);
                    length   += readBytes;

                    if (readBytes < FILE_BUFFER_LENGTH)
                    {
                        // resize buffer
                        Array.Resize <byte>(ref buffer, readBytes);
                    }

                    // write remote backup file
                    string tempPath = sql.AppendTempFileBinaryChunk(backupFileName, remoteBackupPath, buffer);
                    if (remoteBackupPath == null)
                    {
                        remoteBackupPath = tempPath;
                    }
                }while (readBytes == FILE_BUFFER_LENGTH);
                stream.Close();

                // restore database
                sql.RestoreDatabase(itemName, new string[] { remoteBackupPath });

                // add meta-item if required
                if (PackageController.GetPackageItemByName(packageId, group.GroupName,
                                                           itemName, typeof(SqlDatabase)) == null)
                {
                    db.PackageId = packageId;
                    db.ServiceId = serviceId;
                    db.GroupName = group.GroupName;
                    PackageController.AddPackageItem(db);
                }
            }
            else if (itemType == typeof(SqlUser))
            {
                DatabaseServer sql = GetDatabaseServer(serviceId);

                // extract meta item
                XmlSerializer serializer = new XmlSerializer(typeof(SqlUser));
                SqlUser       user       = (SqlUser)serializer.Deserialize(
                    new XmlNodeReader(itemNode.SelectSingleNode("SqlUser")));

                // create user if required
                if (!sql.UserExists(itemName))
                {
                    sql.CreateUser(user, CryptoUtils.Decrypt(user.Password));
                }

                // add meta-item if required
                if (PackageController.GetPackageItemByName(packageId, group.GroupName,
                                                           itemName, typeof(SqlUser)) == null)
                {
                    user.PackageId = packageId;
                    user.ServiceId = serviceId;
                    user.GroupName = group.GroupName;
                    PackageController.AddPackageItem(user);
                }
            }

            return(0);
        }
        public static int AddSqlDatabase(SqlDatabase item, string groupName)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);

            if (accountCheck < 0)
            {
                return(accountCheck);
            }

            // check package
            int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive);

            if (packageCheck < 0)
            {
                return(packageCheck);
            }

            // check quota
            QuotaValueInfo quota = PackageController.GetPackageQuota(item.PackageId, groupName + ".Databases");

            if (quota.QuotaExhausted)
            {
                return(BusinessErrorCodes.ERROR_MSSQL_DATABASES_RESOURCE_QUOTA_LIMIT);
            }

            // check if mail resource is available
            int serviceId = PackageController.GetPackageServiceId(item.PackageId, groupName);

            if (serviceId == 0)
            {
                return(BusinessErrorCodes.ERROR_MSSQL_RESOURCE_UNAVAILABLE);
            }

            // check package items
            if (PackageController.GetPackageItemByName(item.PackageId, groupName, item.Name, typeof(SqlDatabase)) != null)
            {
                return(BusinessErrorCodes.ERROR_MSSQL_DATABASES_PACKAGE_ITEM_EXISTS);
            }

            // place log record
            TaskManager.StartTask("SQL_DATABASE", "ADD", item.Name);
            TaskManager.WriteParameter("Provider", groupName);

            int itemId = default(int);

            //
            try
            {
                // check service items
                DatabaseServer sql = GetDatabaseServer(serviceId);
                if (sql.DatabaseExists(item.Name))
                {
                    return(BusinessErrorCodes.ERROR_MSSQL_DATABASES_SERVICE_ITEM_EXISTS);
                }

                // calculate database location
                StringDictionary settings = ServerController.GetServiceSettings(serviceId);
                UserInfo         user     = PackageController.GetPackageOwner(item.PackageId);
                if (settings["UseDefaultDatabaseLocation"] != null &&
                    !Utils.ParseBool(settings["UseDefaultDatabaseLocation"], false))
                {
                    item.Location = Utils.ReplaceStringVariable(settings["DatabaseLocation"], "user_name", user.Username);
                }

                // set database size
                item.DataSize = GetMaxDatabaseSize(item.PackageId, groupName);


                // set log size
                item.LogSize = GetMaxLogSize(item.PackageId, groupName);

                // add service item
                sql.CreateDatabase(item);

                // save item
                item.ServiceId = serviceId;
                itemId         = PackageController.AddPackageItem(item);

                TaskManager.ItemId = itemId;
            }
            catch (Exception ex)
            {
                TaskManager.WriteError(ex);
                //
                if (ex.Message.Contains("INVALID_DATABASE_NAME"))
                {
                    return(BusinessErrorCodes.ERROR_MYSQL_INVALID_DATABASE_NAME);
                }
                // Return a generic error instead of default(int)
                itemId = BusinessErrorCodes.FAILED_EXECUTE_SERVICE_OPERATION;
            }
            finally
            {
                TaskManager.CompleteTask();
            }
            return(itemId);
        }