public NippsPackageResponse UpgradeApplication(NippsPackageRequest request)
        {
            NippsPackageResponse response = new NippsPackageResponse();

            response.Result = Result.OK;
            response.ResultMessages = new List<string>();

            _Logger.Info("Checking for upgrade {0}", request);

            if (request == null || request.NippsPackages == null || request.NippsPackages.Count == 0)
            {
                response.ResultMessages.Add("ArgumentNullException: NippsPackages is null or empty.");
                return response;
            }
            
            //backup the current version,
            NippsPackageResponse backupResponse = BackupApplication(request);

            _Logger.Info("Starting to upgrade {0} with {1}", request, request.NippsPackages[0].PackageZIP);
            //restore the upgraded version
            NippsPackageResponse restoreResponse = RestoreApplication(request);

            _Logger.Info("Starting to execute upgrade.sql {0}", request);
            //execute upgrade.sql, if exist in the zip

            return response;
        }
        public NippsPackageResponse UpgradeApplicationList(NippsPackageRequest request)
        {
            NippsPackageResponse response = new NippsPackageResponse();
            string BackupTargetPath = GetBackupTargetPath();

            response.Result = Result.OK;
            response.ResultMessages = new List<string>();

            if (request != null && request.NippsPackages != null && request.NippsPackages.Count > 0)
                foreach (string fileName in Directory.GetFiles(BackupTargetPath, "Upgr_" + request.NippsPackages[0].ApplicationName.Replace("/", "") + "*.zip", SearchOption.TopDirectoryOnly))
                    response.ResultMessages.Add(fileName.Substring(BackupTargetPath.Length));
            else
                foreach (string fileName in Directory.GetFiles(BackupTargetPath, "Upgr_*.zip", SearchOption.TopDirectoryOnly))
                    response.ResultMessages.Add(fileName.Substring(BackupTargetPath.Length));

            return response;
        }
        public BaseResponse BackupConfiguration()
        {
            BaseResponse response = new NippsPackageResponse();
            response.ResultMessages = new List<string>(); 

            try
            {
                string targetPath = GetBackupTargetPath();
                string bcpExe = GetBcpExe();
                string connectionString = ConfigurationManager.ConnectionStrings["UpgradeDb"].ConnectionString;
                
                string dataBase = Regex.Match(connectionString, @"(Initial Catalog=)(\w+)").Value.Replace("Initial Catalog=", "");
                string userName = Regex.Match(connectionString, @"(User ID=)(\w+)").Value.Replace("User ID=", "");
                string passWord = Regex.Match(connectionString, @"(Password=)(\w+)").Value.Replace("Password="******"");
                string host = Regex.Match(connectionString, @"(Data Source=)(\w+)").Value.Replace("Data Source=", "");
                string sqlArgs = string.Format("-c -C RAW -S {0} -d {1} -U {2} -P {3}", host, dataBase, userName, passWord);

                //take the schema&table name from system parameter service, 
                response.ResultMessages.Add(string.Format("auth.Users exit code -> {0}", ConfigurationBackup(bcpExe, sqlArgs, "auth.Users", targetPath)));

                response.ResultMessages.Add(string.Format("log.NippsModules exit code -> {0}", ConfigurationBackup(bcpExe, sqlArgs, "log.NippsModules", targetPath)));

                response.ResultMessages.Add(string.Format("conf.ParameterCategories exit code -> {0}", ConfigurationBackup(bcpExe, sqlArgs, "conf.ParameterCategories", targetPath)));

                response.ResultMessages.Add(string.Format("conf.SystemParameters exit code -> {0}", ConfigurationBackup(bcpExe, sqlArgs, "conf.SystemParameters", targetPath)));

                response.Result = Result.OK;
                
            }
            catch (Exception ex)
            {
                response.Result = Result.FAIL;
                response.ResultMessages.Add(ex.ToString());
            }

            return response;
        }
        public NippsPackageResponse RestoreApplicationList(NippsPackageRequest request)
        {
            NippsPackageResponse response = new NippsPackageResponse();
            string BackupTargetPath = GetBackupTargetPath();
            
            response.Result = Result.OK;
            response.ResultMessages = new List<string>();

            if (request != null && request.NippsPackages != null && request.NippsPackages.Count > 0)
                foreach (NippsPackage np in request.NippsPackages)
                    foreach (string fileName in Directory.GetFiles(BackupTargetPath, "Appl_" + ZipFileName(np, false).Replace(".zip", "*.zip"), SearchOption.TopDirectoryOnly))
                        response.ResultMessages.Add(fileName.Substring(BackupTargetPath.Length));
            else
                foreach (string fileName in Directory.GetFiles(BackupTargetPath, "Site_*.zip", SearchOption.TopDirectoryOnly))
                    response.ResultMessages.Add(fileName.Substring(BackupTargetPath.Length));

            return response;
        }
        public NippsPackageResponse RestoreApplication(NippsPackageRequest request)
        {
            NippsPackageResponse response = new NippsPackageResponse();

            response.Result = Result.OK;
            response.ResultMessages = new List<string>();

            _Logger.Debug(request);

            foreach (NippsPackage np in request.NippsPackages)
            {
                if (String.IsNullOrEmpty(np.ApplicationName))
                    np.ApplicationName = "/"; //site restore

                _Logger.Debug(np);

                response.ResultMessages
                    .Add(ApplicationRestore(np));
            }
            
            return response;
        }
        public NippsPackageResponse BackupApplication(NippsPackageRequest request)
        {
            NippsPackageResponse response = new NippsPackageResponse();
            bool succeededOne = false;
            string BackupTargetPath = GetBackupTargetPath();

            response.ResultMessages = new List<string>();
            response.Result = Result.OK;

            _Logger.Debug(request.ToString());

            foreach (NippsPackage np in request.NippsPackages)
            {
                
                try
                {
                    if (String.IsNullOrEmpty(np.ApplicationName))
                        np.ApplicationName = "/"; //site backup

                    response.ResultMessages
                        .Add(ApplicationBackup(np));
                    
                    succeededOne = true;
                }
                catch (Exception ex)
                {
                    if (succeededOne)
                        response.Result = Result.SUCCESSWITHWARN;

                    response.ResultMessages.Add(ex.ToString());
                }
            }

            return response;
        }
        public BaseResponse RestoreConfiguration(NippsPackageRequest request)
        {

            BaseResponse response = new NippsPackageResponse();
            response.ResultMessages = new List<string>();

            try
            {

                if (request != null && request.NippsPackages != null && request.NippsPackages.Count > 0)
                {
                    string bcpExe = GetBcpExe();
                    string connectionString = ConfigurationManager.ConnectionStrings["UpgradeDb"].ConnectionString;

                    string dataBase = Regex.Match(connectionString, @"(Initial Catalog=)(\w+)").Value.Replace("Initial Catalog=", "");
                    string userName = Regex.Match(connectionString, @"(User ID=)(\w+)").Value.Replace("User ID=", "");
                    string passWord = Regex.Match(connectionString, @"(Password=)(\w+)").Value.Replace("Password="******"");
                    string host = Regex.Match(connectionString, @"(Data Source=)(\w+)").Value.Replace("Data Source=", "");
                    string sqlArgs = string.Format("-c -C RAW -S {0} -d {1} -U {2} -P {3}", host, dataBase, userName, passWord);

                    foreach (NippsPackage np in request.NippsPackages)
                    {
                        response.ResultMessages.Add(
                            string.Format(
                                "restore {0} exit code -> {1}",
                                np.PackageZIP,
                                ConfigurationRestore(bcpExe, sqlArgs, Regex.Match(np.PackageZIP, "(_)(.*)(_)").Value.Replace("_", ""), np.PackageZIP)
                                )
                            );
                    }
                        
                }
                response.Result = Result.OK;

            }
            catch (Exception ex)
            {
                response.Result = Result.FAIL;
                response.ResultMessages.Add(ex.ToString());
            }

            return response;
        }
        public BaseResponse RestoreConfigurationList()
        {
            BaseResponse response = new NippsPackageResponse();
            response.ResultMessages = new List<string>();

            try
            {
                string targetPath = GetBackupTargetPath();
                foreach (string fileName in Directory.GetFiles(targetPath, "DB_*.txt", SearchOption.TopDirectoryOnly))
                    response.ResultMessages.Add(fileName);

                response.Result = Result.OK;

            }
            catch (Exception ex)
            {
                response.Result = Result.FAIL;
                response.ResultMessages.Add(ex.ToString());
            }

            return response;
        }
        public NippsPackageResponse AddSite(NippsPackageRequest nippsPackageRequest)
        {
            bool isSucceededOne = false;
            Logger logger = NLog.LogManager.GetCurrentClassLogger();
            NippsPackageResponse response = new NippsPackageResponse();
            response.ResultMessages = new List<string>();
            response.Result = Result.OK;

            try
            {
                using (ServerManager serverManager = ServerManager.OpenRemote("localhost"))
                {
                    foreach (NippsPackage nippsPackage in nippsPackageRequest.NippsPackages) 
                    {
                        try
                        {
                            
                            Site site = serverManager.Sites
                                .Where(s => s.Name.Equals(nippsPackage.SiteName))
                                .Single();
                        
                            if (site != null)
                            {
                                string basePath = SavePackage(nippsPackage, site);
                                string appPath = basePath + nippsPackage.ApplicationName;

                                logger.Debug(string.Format("Application>{0}", appPath));

                                //execute SQL if exist
                                BaseDaoHelper.ExecuteUpgradeScript(appPath);
                                
                                //add site
                                Application app = site.Applications.Add("/" + nippsPackage.ApplicationName, appPath.Replace("/","\\"));
                                app.ApplicationPoolName = nippsPackage.ApplicationPoolName;

                                response.ResultMessages.Add(
                                    string.Format("[{0} - {1} - {2}]", nippsPackage.SiteName, nippsPackage.ApplicationPoolName, nippsPackage.ApplicationName));

                                isSucceededOne = true;
                            }

                            
                        }
                        catch (Exception ex)
                        {
                            logger.Error(ex.ToString());
                            if (isSucceededOne)
                                response.Result = Result.SUCCESSWITHWARN;
                            response.ResultMessages.Add(ex.ToString());
                            
                        }

                    }

                    if (isSucceededOne)
                        serverManager.CommitChanges();
                }

            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                response.Result = Result.FAIL;
                response.ResultMessages.Add(ex.ToString());
            }

            return response;
        }