private void BindData()
        {
            using (var db = new DataAccess.CSSDataContext())
            {
                DataAccess.Lobby lobby = db.Lobbies.FirstOrDefault(p => p.Id == PublicationID);

                txtBasePath.Text       = lobby.BasePath;
                txtHost.Text           = lobby.Host;
                txtLobbyName.Text      = lobby.Name;
                chkEnabled.Checked     = lobby.IsEnabled;
                chkRestrictive.Checked = lobby.IsRestrictive;
            }


            List <FileInfo> packageInfos = AutoUpdateManager.GetPackages();

            List <Data.EditablePublication> packages = new List <Data.EditablePublication>();

            foreach (FileInfo packageInfo in packageInfos)
            {
                packages.Add(new Data.EditablePublication()
                {
                    IsIncluded = AutoUpdateManager.IsPackageExcludedFromPublication(PublicationID, packageInfo.Name) == false,
                    Name       = packageInfo.Name
                });
            }

            gvPackages.DataSource = packages;
            gvPackages.DataBind();
        }
        private static void GenerateFileListForAutoUpdate(DataAccess.Lobby lobby)
        {
            string filelistFile = Path.Combine(lobby.BasePath, "FileList.txt");

            if (File.Exists(filelistFile) == true)
            {
                File.Delete(filelistFile);
            }

            AddFilesToFileList(filelistFile, String.Empty, lobby.BasePath);
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            using (var db = new DataAccess.CSSDataContext())
            {
                DataAccess.Lobby lobby = db.Lobbies.FirstOrDefault(p => p.Id == PublicationID);

                lobby.Host          = txtHost.Text.Trim();
                lobby.BasePath      = txtBasePath.Text.Trim();
                lobby.IsEnabled     = chkEnabled.Checked;
                lobby.IsRestrictive = chkRestrictive.Checked;

                db.SubmitChanges();
            }

            BindData();
        }
        public static bool DeployPublication(int publicationID)
        {
            using (var db = new DataAccess.CSSDataContext())
            {
                DataAccess.Lobby lobby = db.Lobbies.FirstOrDefault(p => p.Id == publicationID);

                if (lobby == null)
                {
                    throw new Exception("Couldn't get lobby for publication id: " + publicationID);
                }

                if (Directory.Exists(lobby.BasePath) == false)
                {
                    Directory.CreateDirectory(lobby.BasePath);
                }

                List <FileCollision>            fileCollisions     = new List <FileCollision>();
                Dictionary <string, UpdateItem> filesInPublication = new Dictionary <string, UpdateItem>();

                // Remove physical files from the directory. Not doing this with a recursive directory delete because
                // I don't want someone to put in a bad path into the content manager web UI, and then drill the
                // whole drive.
                foreach (DataAccess.AutoUpdateFile_Lobby file in db.AutoUpdateFile_Lobbies.Where(p => p.LobbyId == lobby.Id))
                {
                    string fileToDelete = Path.Combine(lobby.BasePath, file.AutoUpdateFile.Filename);

                    if (File.Exists(fileToDelete) == true)
                    {
                        File.Delete(fileToDelete);
                    }
                }

                // Clear all files for the lobby.
                db.AutoUpdateFile_Lobbies.DeleteAllOnSubmit(db.AutoUpdateFile_Lobbies.Where(p => p.LobbyId == lobby.Id));
                db.SubmitChanges();

                if (AutoUpdateManager.TryGetPublicationFiles(publicationID, out filesInPublication, out fileCollisions) == true)
                {
                    foreach (UpdateItem fileInfo in filesInPublication.Values)
                    {
                        string checksum;
                        using (SHA1 hasher = SHA1.Create())
                        {
                            using (FileStream fs = new FileStream(fileInfo.FileInfo.FullName, FileMode.Open, FileAccess.Read))
                                checksum = Convert.ToBase64String(hasher.ComputeHash(fs));
                        }

                        string          fileVersion     = String.Empty;
                        FileVersionInfo fileVersionInfo = FileVersionInfo.GetVersionInfo(fileInfo.FileInfo.FullName);

                        // Doing it this way, as sometimes there is product or vendor info at the
                        // end of the file version spec. ProductVersion may not correctly reflect the actual
                        // version of the file all the time.
                        if (fileVersionInfo != null && fileVersionInfo.FileVersion != null)
                        {
                            fileVersion = String.Format("{0}.{1}.{2}.{3}", fileVersionInfo.FileMajorPart, fileVersionInfo.FileMinorPart, fileVersionInfo.FileBuildPart, fileVersionInfo.FilePrivatePart);
                        }

                        string relativeFilePath = Path.Combine(fileInfo.RelativeDirectory, fileInfo.Name);

                        DataAccess.AutoUpdateFile autoUpdateFile = db.AutoUpdateFiles.FirstOrDefault(p => p.Filename == relativeFilePath);

                        if (autoUpdateFile == null)
                        {
                            autoUpdateFile = new Allegiance.CommunitySecuritySystem.DataAccess.AutoUpdateFile()
                            {
                                Filename    = relativeFilePath,
                                IsProtected = fileInfo.IsProtected
                            };

                            db.AutoUpdateFiles.InsertOnSubmit(autoUpdateFile);
                            db.SubmitChanges();
                        }
                        else
                        {
                            if (autoUpdateFile.IsProtected != fileInfo.IsProtected)
                            {
                                autoUpdateFile.IsProtected = fileInfo.IsProtected;
                                db.SubmitChanges();
                            }
                        }

                        DataAccess.AutoUpdateFile_Lobby lobbyFile = db.AutoUpdateFile_Lobbies.FirstOrDefault(p => p.AutoUpdateFileId == autoUpdateFile.Id && p.LobbyId == lobby.Id);

                        if (lobbyFile == null)
                        {
                            lobbyFile = new Allegiance.CommunitySecuritySystem.DataAccess.AutoUpdateFile_Lobby()
                            {
                                AutoUpdateFileId = autoUpdateFile.Id,
                                CurrentVersion   = fileVersion,
                                DateCreated      = fileInfo.FileInfo.CreationTime,
                                DateModified     = fileInfo.FileInfo.LastWriteTime,
                                ValidChecksum    = checksum,
                                LobbyId          = lobby.Id
                            };

                            db.AutoUpdateFile_Lobbies.InsertOnSubmit(lobbyFile);
                            db.SubmitChanges();
                        }

                        string targetFilePath      = Path.Combine(lobby.BasePath, relativeFilePath);
                        string targetFileDirectory = Path.GetDirectoryName(targetFilePath);
                        if (Directory.Exists(targetFileDirectory) == false)
                        {
                            Directory.CreateDirectory(targetFileDirectory);
                        }

                        File.Copy(fileInfo.FileInfo.FullName, targetFilePath, true);
                    }

                    GenerateFileListForAutoUpdate(lobby);
                }

                // Clean up any unused AutoUpdateFile records.
                //db.AutoUpdateFiles.DeleteAllOnSubmit(db.AutoUpdateFiles.Where(p => db.AutoUpdateFile_Lobbies.Select(r => r.AutoUpdateFileId).Contains(p.Id) == false));
                //db.SubmitChanges();
            }

            return(true);
        }