/// <summary> /// Creates a project in memory that will represent the destination data store. /// </summary> /// <returns>Project; represents the destination data store.</returns> protected Project CreateProject() { OnSetStatusMessage(ImportExportSharedStrings.PRJ_FILE_COPY_START); DbDriverInfo collectedDataDBInfo = new DbDriverInfo(); IDbDriverFactory collectedDBFactory = DbDriverFactoryCreator.GetDbDriverFactory("Epi.Data.Office.AccessDBFactory, Epi.Data.Office");// GetSelectedCollectedDataDriver(); string GUID = System.Guid.NewGuid().ToString(); collectedDataDBInfo.DBCnnStringBuilder = collectedDBFactory.RequestDefaultConnection(GUID, GUID); //collectedDataDBInfo.DBCnnStringBuilder = collectedDBFactory.RequestNewConnection(txtCollectedData.Text.Trim()); Project project = new Project(); project.Name = SourceProject.Name; project.Location = PackagePath; //Path.Combine(txtProjectLocation.Text, txtProjectName.Text); if (collectedDataDBInfo.DBCnnStringBuilder.ContainsKey("Provider") && collectedDataDBInfo.DBCnnStringBuilder["Provider"].ToString().ToLower().Equals("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(); project.Description = SourceProject.Description; // Collected data ... project.CollectedDataDbInfo = collectedDataDBInfo; project.CollectedDataDriver = "Epi.Data.Office.AccessDBFactory, Epi.Data.Office"; project.CollectedDataConnectionString = collectedDataDBInfo.DBCnnStringBuilder.ToString(); try { project.CollectedData.Initialize(collectedDataDBInfo, "Epi.Data.Office.AccessDBFactory, Epi.Data.Office", true); } catch (Exception) { //MsgBox.ShowException(ex); return null; } Logger.Log(DateTime.Now + ": " + string.Format(ImportExportSharedStrings.PROJECT_PACKAGE_CREATED_BY_USER, project.Name, project.Location, System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString())); // Metadata .. project.MetadataSource = MetadataSource.SameDb; try { //project.Save(); OnSetStatusMessage(ImportExportSharedStrings.PRJ_FILE_COPY_END); return project; } catch (UnauthorizedAccessException) { return null; } }
/// <summary> /// Creates a project file and project object for a given database. /// </summary> /// <param name="connectionString">The database file name or connection string.</param> /// <param name="createPRJFile">Whether or not to create the PRJ file on disk.</param> /// <param name="prjFileName">The name of the desired PRJ file</param> /// <param name="prjFileLocation">The directory to the desired PRJ file</param> /// <returns>An Epi Info project object</returns> public static Project CreateProjectFileFromDatabase(string connectionString, bool createPRJFile, string prjFileLocation = "", string prjFileName = "") { if (IsDatabaseEpiProject(connectionString, true) == false) { return null; } Project project = new Project(); DbDriverInfo collectedDataDBInfo = new DbDriverInfo(); IDbDriverFactory collectedDBFactory = DbDriverFactoryCreator.GetDbDriverFactory("Epi.Data.SqlServer.SqlDBFactory, Epi.Data.SqlServer"); string GUID = System.Guid.NewGuid().ToString(); //collectedDataDBInfo.DBCnnStringBuilder = collectedDBFactory.RequestNewConnection(connectionInfo); string[] pieces = connectionString.Split(';'); foreach (string piece in pieces) { if (!piece.ToLower().StartsWith("password")) { string[] parts = piece.Split('='); collectedDataDBInfo.DBCnnStringBuilder[parts[0]] = parts[1]; } else { string[] parts = piece.Split('='); int indexOf = piece.IndexOf("="); string password = piece.Substring(indexOf + 1); password = password.TrimStart('\"').TrimEnd('\"'); ; collectedDataDBInfo.DBCnnStringBuilder[parts[0]] = password; } } IDbDriver driver = collectedDBFactory.CreateDatabaseObject(collectedDataDBInfo.DBCnnStringBuilder); project.CollectedData.Initialize(collectedDataDBInfo, "Epi.Data.SqlServer.SqlDBFactory, Epi.Data.SqlServer", false); project.Name = prjFileName; //fi.Name.Substring(0, fi.Name.Length - 4); project.Location = prjFileLocation; //directory; project.Id = project.GetProjectId(); project.Description = string.Empty; project.CollectedDataDbInfo = collectedDataDBInfo; project.CollectedDataDriver = "Epi.Data.SqlServer.SqlDBFactory, Epi.Data.SqlServer"; project.CollectedDataConnectionString = connectionString; project.MetadataSource = MetadataSource.SameDb; project.Metadata.AttachDbDriver(project.CollectedData.GetDbDriver()); if (createPRJFile) { try { project.Save(); } catch (UnauthorizedAccessException ex) { return null; } } return project; }
/// <summary> /// Saves project information /// </summary> public virtual Project CreateProject(string projectName, string projectDescription, string projectLocation, string collectedDataDriver, DbDriverInfo collectedDataDBInfo) { Project newProject = new Project(); newProject.Name = projectName; newProject.Location = Path.Combine(projectLocation, projectName); if (collectedDataDBInfo.DBCnnStringBuilder.ContainsKey("Provider") && (collectedDataDBInfo.DBCnnStringBuilder["Provider"].ToString() == "Microsoft.Jet.OLEDB.4.0")) { collectedDataDBInfo.DBCnnStringBuilder["Data Source"] = newProject.FilePath.Substring(0, newProject.FilePath.Length - 4) + ".mdb"; } if (!Directory.Exists(newProject.Location)) { Directory.CreateDirectory(newProject.Location); } newProject.Id = newProject.GetProjectId(); if (File.Exists(newProject.FilePath)) { DialogResult dr = MessageBox.Show(string.Format(SharedStrings.PROJECT_ALREADY_EXISTS, newProject.FilePath), SharedStrings.PROJECT_ALREADY_EXISTS_TITLE, MessageBoxButtons.YesNo, MessageBoxIcon.Warning); switch (dr) { case DialogResult.Yes: break; case DialogResult.No: return null; } } newProject.Description = projectDescription; // Collected data ... newProject.CollectedDataDbInfo = collectedDataDBInfo; newProject.CollectedDataConnectionString = collectedDataDBInfo.DBCnnStringBuilder.ToString(); newProject.CollectedDataDriver = collectedDataDriver; newProject.CollectedData.Initialize(collectedDataDBInfo, collectedDataDriver, true); // Check that there isn't an Epi 7 project already here. if (newProject.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 (newProject.CollectedData.TableExists(s)) { projectExists = true; break; } } if (projectExists) { DialogResult result = MessageBox.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 null; } 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}].", newProject.Name, newProject.Location, System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString())); // Metadata .. newProject.MetadataSource = MetadataSource.SameDb; MetadataDbProvider typedMetadata = newProject.Metadata as MetadataDbProvider; typedMetadata.AttachDbDriver(newProject.CollectedData.GetDbDriver()); typedMetadata.CreateMetadataTables(); try { newProject.Save(); return newProject; } catch (UnauthorizedAccessException ex) { MessageBox.Show(ex.Message); return newProject; } }
/// <summary> /// Creates a project file and project object for a database file. /// </summary> /// <param name="databaseFileName">The database file name.</param> /// <param name="createPRJFile">Whether or not to create the PRJ file on disk.</param> /// <returns>An Epi Info project object</returns> public static Project CreateProjectFileFromDatabase(string databaseFileName, bool createPRJFile) { if (IsDatabaseEpiProject(databaseFileName, false) == false) { return null; } Project project = new Project(); string fileName = databaseFileName; string directory = Path.GetDirectoryName(fileName); if (fileName.ToLower().EndsWith(".mdb")) { DbDriverInfo collectedDataDBInfo = new DbDriverInfo(); IDbDriverFactory collectedDBFactory = DbDriverFactoryCreator.GetDbDriverFactory("Epi.Data.Office.AccessDBFactory, Epi.Data.Office");// GetSelectedCollectedDataDriver(); string GUID = System.Guid.NewGuid().ToString(); collectedDataDBInfo.DBCnnStringBuilder = collectedDBFactory.RequestDefaultConnection(GUID, GUID); FileInfo fi = new FileInfo(fileName); project.Name = fi.Name.Substring(0, fi.Name.Length - 4); project.Location = directory; if (collectedDataDBInfo.DBCnnStringBuilder.ContainsKey("Provider")) { 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(); project.Description = string.Empty; project.CollectedDataDbInfo = collectedDataDBInfo; project.CollectedDataDriver = "Epi.Data.Office.AccessDBFactory, Epi.Data.Office"; project.CollectedDataConnectionString = collectedDataDBInfo.DBCnnStringBuilder.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())); project.MetadataSource = MetadataSource.SameDb; if (createPRJFile) { try { project.Save(); } catch (UnauthorizedAccessException ex) { return null; } } } return project; }