/// <summary> /// Creates a brand new project. If this completes without any exception, you can /// call <see cref="OpenProject"/> to activate the project. /// </summary> /// <param name="projectName">The user-perceived name for the project.</param> /// <param name="layer">The map layer the project is for (not null)</param> internal void CreateProject(string projectName, ILayer layer) { if (String.IsNullOrWhiteSpace(projectName) || layer == null) { throw new ArgumentNullException(); } // Confirm that the project name is unique if (FindProjectId(projectName) != null) { throw new ArgumentException("Specified project already exists"); } // Define the event data NewProjectEvent e = new NewProjectEvent() { ProjectId = Guid.NewGuid(), ProjectName = projectName, LayerId = layer.Id, DefaultSystem = String.Empty, UserName = System.Environment.UserName, MachineName = System.Environment.MachineName }; // Create the index entry CreateIndexEntry(projectName, e.ProjectId); // Create the data folder string dataFolder = CreateDataFolder(e.ProjectId); // Serialize the event data to the data folder. Specify <Change> so that // the class name will be included in the output file. string s = EditSerializer.GetSerializedString <Change>(DataField.Edit, e); string fileName = Path.Combine(dataFolder, GetDataFileName(e.EditSequence)); File.WriteAllText(fileName, s); // Write initial project settings to the data folder ProjectSettings ps = new ProjectSettings(); IDataServer ds = EditingController.Current.DataServer; if (ds != null) { ps.ConnectionString = ds.ConnectionString; } // Turn off auto-number if there's no database connection string if (String.IsNullOrEmpty(ps.ConnectionString)) { ps.IsAutoNumber = false; } // Remember default entity types for points, lines, text, polygons ps.SetEntityTypeDefaults(layer); // Save the settings string settingsFileName = Path.Combine(dataFolder, "settings.txt"); ps.WriteXML(settingsFileName); }
void LoadDataFiles(string folderName, uint[] fileNums) { Trace.Write("Reading data..."); EditDeserializer ed = new EditDeserializer(this); Session lastSession = null; IdManager idMan = m_MapModel.IdManager; foreach (uint fileNum in fileNums) { string editFile = Path.Combine(folderName, ProjectDatabase.GetDataFileName(fileNum)); using (TextReader tr = File.OpenText(editFile)) { TextEditReader er = new TextEditReader(tr); // Ignore any empty files altogether while (er.HasNext) { ed.SetReader(er); Change edit = Change.Deserialize(ed); if (edit is NewProjectEvent) { m_ProjectInfo = (NewProjectEvent)edit; // If the project settings don't have default entity types, initialize them with // the layer defaults. This covers a case where the settings file has been lost, and // automatically re-created by ProjectSettings.CreateInstance. var layer = EnvironmentContainer.FindLayerById(m_ProjectInfo.LayerId); m_Settings.SetEntityTypeDefaults(layer); } else if (edit is NewSessionEvent) { lastSession = new Session(this, (NewSessionEvent)edit, editFile); m_MapModel.AddSession(lastSession); } else if (edit is EndSessionEvent) { Debug.Assert(lastSession != null); lastSession.EndTime = edit.When; } else if (edit is IdAllocation) { if (idMan != null) { IdAllocation alloc = (IdAllocation)edit; IdGroup g = idMan.FindGroupById(alloc.GroupId); g.AddIdPacket(alloc); // Remember that allocations have been made in the session (bit of a hack // to ensure the session isn't later removed if no edits are actually // performed). lastSession.AddAllocation(alloc); } } else { Debug.Assert(edit is Operation); Debug.Assert(lastSession != null); lastSession.AddOperation((Operation)edit); } } } } if (m_ProjectInfo == null) { throw new ApplicationException("Could not locate the project creation event"); } // Apply any forward references ed.ApplyForwardRefs(); // Remember the highest internal ID used by the project SetLastItem(fileNums[fileNums.Length - 1]); }