Пример #1
0
        /// <summary>
        /// Saves project information
        /// </summary>
        protected virtual bool CreateProject()
        {
            BeginBusy(SharedStrings.CREATING_PROJECT);

            project      = new Project();
            project.Name = txtProjectName.Text;

            project.Location = Path.Combine(txtNewProjectLocation.Text, txtProjectName.Text);

            if (collectedDataDBInfo.DBCnnStringBuilder.ContainsKey("Provider") && collectedDataDBInfo.DBCnnStringBuilder["Provider"] == "Microsoft.Jet.OLEDB.4.0")
            {
                collectedDataDBInfo.DBCnnStringBuilder["Data Source"] = project.FilePath.Substring(0, project.FilePath.Length - 4) + ".mdb";
            }

            project.Id = project.GetProjectId();
            if (File.Exists(project.FilePath))
            {
                DialogResult dr = MsgBox.Show(string.Format(SharedStrings.PROJECT_ALREADY_EXISTS, project.FilePath), SharedStrings.PROJECT_ALREADY_EXISTS_TITLE, MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                switch (dr)
                {
                case DialogResult.Yes:
                    break;

                case DialogResult.No:
                    return(false);
                }
            }

            // Collected data ...
            project.CollectedDataDbInfo           = this.collectedDataDBInfo;
            project.CollectedDataDriver           = cbxCollectedDataDriver.SelectedValue.ToString();
            project.CollectedDataConnectionString = this.collectedDataDBInfo.DBCnnStringBuilder.ToString();
            project.CollectedData.Initialize(collectedDataDBInfo, cbxCollectedDataDriver.SelectedValue.ToString(), true);


            // Check that there isn't an Epi 7 project already here.
            if (project.CollectedDataDriver != "Epi.Data.Office.AccessDBFactory, Epi.Data.Office")
            {
                List <string> tableNames = new List <string>();
                tableNames.Add("metaBackgrounds");
                tableNames.Add("metaDataTypes");
                tableNames.Add("metaDbInfo");
                tableNames.Add("metaFields");
                tableNames.Add("metaFieldTypes");
                tableNames.Add("metaGridColumns");
                tableNames.Add("metaImages");
                tableNames.Add("metaLayerRenderTypes");
                tableNames.Add("metaLayers");
                tableNames.Add("metaMapLayers");
                tableNames.Add("metaMapPoints");
                tableNames.Add("metaMaps");
                tableNames.Add("metaPages");
                tableNames.Add("metaPatterns");
                tableNames.Add("metaPrograms");
                tableNames.Add("metaViews");

                bool projectExists = false;
                foreach (string s in tableNames)
                {
                    if (project.CollectedData.TableExists(s))
                    {
                        projectExists = true;
                        break;
                    }
                }

                if (projectExists)
                {
                    DialogResult result = Epi.Windows.MsgBox.Show(string.Format(SharedStrings.PROJECT_ALREADY_EXISTS, project.Location), SharedStrings.PROJECT_ALREADY_EXISTS_TITLE, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                    if (result == DialogResult.Cancel)
                    {
                        Logger.Log(DateTime.Now + ":  " + SharedStrings.PROJECT_CREATION_STOPPED + " [" + System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString() + "]");
                        return(false);
                    }
                    else
                    {
                        Logger.Log(DateTime.Now + ":  " + SharedStrings.PROJECT_OVERWRITTEN + " [" + System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString() + "]");
                    }
                }
            }

            //Logger.Log(DateTime.Now + ":  " + string.Format("Project [{0}] created in {1} by user [{2}].", project.Name, project.Location, System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString()));

            // Metadata ..
            project.MetadataSource = GetSelectedMetadataSource();
            MetadataDbProvider typedMetadata = project.Metadata as MetadataDbProvider;

            typedMetadata.AttachDbDriver(project.CollectedData.GetDbDriver());
            typedMetadata.CreateMetadataTables();

            try
            {
                project.Save();
                return(true);
            }
            catch (UnauthorizedAccessException ex)
            {
                MsgBox.ShowException(ex);
                return(false);
            }
        }
        /// <summary>
        /// Saves project information
        /// </summary>
        protected virtual bool CreateProject()
        {
            BeginBusy(SharedStrings.CREATING_PROJECT);

            project      = new Project();
            project.Name = txtProjectName.Text;

            project.Location = Path.Combine(txtProjectLocation.Text, txtProjectName.Text);

            if (collectedDataDBInfo.DBCnnStringBuilder.ContainsKey("Provider") && collectedDataDBInfo.DBCnnStringBuilder["Provider"] == "Microsoft.Jet.OLEDB.4.0")
            {
                collectedDataDBInfo.DBCnnStringBuilder["Data Source"] = project.FilePath.Substring(0, project.FilePath.Length - 4) + ".mdb";
            }

            if (!Directory.Exists(project.Location))
            {
                Directory.CreateDirectory(project.Location);
            }

            project.Id = project.GetProjectId();
            if (File.Exists(project.FilePath))
            {
                DialogResult dr = MsgBox.Show(string.Format(SharedStrings.PROJECT_ALREADY_EXISTS, project.FilePath), SharedStrings.PROJECT_ALREADY_EXISTS_TITLE, MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                switch (dr)
                {
                case DialogResult.Yes:
                    break;

                case DialogResult.No:
                    return(false);
                }
            }

            project.Description = txtProjectDescription.Text;

            // Collected data ...
            project.CollectedDataDbInfo           = this.collectedDataDBInfo;
            project.CollectedDataDriver           = cbxCollectedDataDriver.SelectedValue.ToString();
            project.CollectedDataConnectionString = this.collectedDataDBInfo.DBCnnStringBuilder.ToString();
            try
            {
                project.CollectedData.Initialize(collectedDataDBInfo, cbxCollectedDataDriver.SelectedValue.ToString(), true);
            }
            catch (Exception ex)
            {
                MsgBox.Show(string.Format("{0}{1}{1}{2}", SharedStrings.UNABLE_TO_INITIALIZE_PROJECT, Environment.NewLine, ex.Message), SharedStrings.UNABLE_TO_INITIALIZE_PROJECT, MessageBoxButtons.OK, MessageBoxIcon.None);
                return(false);
            }

            // Check that there isn't an Epi 7 project already here.
            if (project.CollectedDataDriver != "Epi.Data.Office.AccessDBFactory, Epi.Data.Office")
            {
                List <string> tableNames = new List <string>();
                tableNames.Add("metaBackgrounds");
                tableNames.Add("metaDataTypes");
                tableNames.Add("metaDbInfo");
                tableNames.Add("metaFields");
                tableNames.Add("metaFieldTypes");
                tableNames.Add("metaGridColumns");
                tableNames.Add("metaImages");
                tableNames.Add("metaLayerRenderTypes");
                tableNames.Add("metaLayers");
                tableNames.Add("metaMapLayers");
                tableNames.Add("metaMapPoints");
                tableNames.Add("metaMaps");
                tableNames.Add("metaPages");
                tableNames.Add("metaPatterns");
                tableNames.Add("metaPrograms");
                tableNames.Add("metaViews");

                bool projectExists = false;
                try
                {
                    foreach (string s in tableNames)
                    {
                        if (project.CollectedData.TableExists(s))
                        {
                            projectExists = true;
                            break;
                        }
                    }
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    Epi.Windows.MsgBox.ShowError("There was a problem accessing the database. Check to make sure you have adequate permissions to access the database and try again.", ex);
                    return(false);
                }

                if (projectExists)
                {
                    DialogResult result = Epi.Windows.MsgBox.Show(SharedStrings.WARNING_PROJECT_MAY_ALREADY_EXIST, SharedStrings.WARNING_PROJECT_MAY_ALREADY_EXIST_SHORT, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                    if (result == DialogResult.Cancel)
                    {
                        Logger.Log(DateTime.Now + ":  " + "Project creation aborted by user [" + System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString() + "] after being prompted to overwrite existing Epi Info 7 project metadata.");
                        return(false);
                    }
                    else
                    {
                        Logger.Log(DateTime.Now + ":  " + "Project creation proceeded by user [" + System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString() + "] after being prompted to overwrite existing Epi Info 7 project metadata.");
                    }
                }
            }

            Logger.Log(DateTime.Now + ":  " + string.Format("Project [{0}] created in {1} by user [{2}].", project.Name, project.Location, System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString()));

            // Metadata ..
            project.MetadataSource = GetSelectedMetadataSource();

            switch (project.MetadataSource)
            {
            case MetadataSource.SameDb:     // Metadata is stored in the same db.
                MetadataDbProvider typedMetadata = project.Metadata as MetadataDbProvider;
                // Get Collected data's Db driver and attach it to Metadata.
                typedMetadata.AttachDbDriver(project.CollectedData.GetDbDriver());
                typedMetadata.CreateMetadataTables();
                break;

            case MetadataSource.DifferentDb:     // Metadata to be stored in a different database
                // Get Metadata driver and connection string from the dialog and initialize metadata object.
                project.MetadataDriver           = cbxMetadataDriver.SelectedValue.ToString();
                project.MetadataConnectionString = metadataConnectionString;
                project.MetaDbInfo = metaDbInfo;

                typedMetadata = project.Metadata as MetadataDbProvider;
                typedMetadata.Initialize(metaDbInfo, cbxMetadataDriver.SelectedValue.ToString(), true);
                break;
            }

            // KKM4 TODO: Temporary band-aid. Revisit ASAP.
            if (!string.IsNullOrEmpty(txtViewName.Text))
            {
                // Creation of project complete. Now create the view.
                project.CreateView(txtViewName.Text);
            }

            try
            {
                project.Save();
                return(true);
            }
            catch (UnauthorizedAccessException ex)
            {
                MsgBox.ShowException(ex);
                return(false);
            }
        }