public Models.BackupPlanPathNode CreateOrUpdatePathNodes(Models.StorageAccount account, Models.BackupPlanFile file) { PathNodes pathNodes = new PathNodes(file.Path); bool nodeExists = true; // Start assuming it exists. Models.BackupPlanPathNode previousNode = null; Models.BackupPlanPathNode planPathNode = null; foreach (var pathNode in pathNodes.Nodes) { // If it does not exist, it does not make sense to lookup inner directories/files. if (nodeExists) { planPathNode = _dao.GetByStorageAccountAndTypeAndPath( account, Models.EntryTypeExtensions.ToEntryType(pathNode.Type), pathNode.Path); // If we couldn't find the current `Models.BackupPlanPathNode`, it's safe to assume the inner // directories/files don't exist either. From now on, all nodes will be created/inserted. if (planPathNode == null) { nodeExists = false; } } if (!nodeExists) { //BackupPlanFile planFile = daoBackupPlanFile.GetByPlanAndPath(Backup.BackupPlan, file.Path); //Assert.NotNull(planFile, string.Format("Required {0} not found in the database.", typeof(BackupPlanFile).Name)) planPathNode = new Models.BackupPlanPathNode(file, Models.EntryTypeExtensions.ToEntryType(pathNode.Type), pathNode.Name, pathNode.Path, previousNode); if (previousNode != null) { planPathNode.Parent = previousNode; previousNode.SubNodes.Add(planPathNode); } _dao.Insert(_tx, planPathNode); _dao.Refresh(planPathNode); } previousNode = planPathNode; //session.Evict(planPathNode); // Force future queries to re-load it and its relationships. } return(previousNode); }
// // Loads or creates `RestorePlanFile`s for each file in `files`. // Returns the complete list of `RestorePlanFile`s that are related to `files`. // NOTE: Does not save to the database because this method is run by a secondary thread. // private LinkedList <Models.RestorePlanFile> DoLoadOrCreateRestorePlanFiles(Models.RestorePlan plan, LinkedList <CustomVersionedFile> files) { Assert.IsNotNull(plan); Assert.IsNotNull(files); Assert.IsNotNull(AllFilesFromPlan); LinkedList <Models.RestorePlanFile> result = new LinkedList <Models.RestorePlanFile>(); BackupPlanPathNodeRepository daoPathNode = new BackupPlanPathNodeRepository(); // Check all files. foreach (CustomVersionedFile file in files) { // Throw if the operation was canceled. CancellationToken.ThrowIfCancellationRequested(); // // Create or update `RestorePlanFile`. // Models.RestorePlanFile restorePlanFile = null; bool backupPlanFileAlreadyExists = AllFilesFromPlan.TryGetValue(file.Path, out restorePlanFile); if (!backupPlanFileAlreadyExists) { restorePlanFile = new Models.RestorePlanFile(plan, file.Path); restorePlanFile.CreatedAt = DateTime.UtcNow; } Models.BackupPlanPathNode pathNode = daoPathNode.GetByStorageAccountAndTypeAndPath(plan.StorageAccount, Models.EntryType.FILE, file.Path); Assert.IsNotNull(pathNode, string.Format("{0} has no corresponding {1}", file.Path, typeof(Models.BackupPlanPathNode).Name)); restorePlanFile.PathNode = pathNode; restorePlanFile.VersionedFile = file; result.AddLast(restorePlanFile); } return(result); }
private Dictionary <string, BackupPlanTreeNodeData> ExpandCheckedDataSource( Dictionary <string, BackupPlanTreeNodeData> dict) { if (dict == null) { return(null); } Dictionary <string, BackupPlanTreeNodeData> expandedDict = new Dictionary <string, BackupPlanTreeNodeData>(dict.Count * 2); bool hasParents = false; // Expand paths into their respective parts. foreach (var obj in dict) { switch (obj.Value.Type) { default: throw new ArgumentException("Unhandled TypeEnum", "obj.Value.Type"); case TypeEnum.FILE_VERSION: case TypeEnum.FILE: case TypeEnum.FOLDER: hasParents = true; break; case TypeEnum.DRIVE: hasParents = false; break; } if (obj.Value.InfoObject == null) { obj.Value.InfoObject = new EntryInfo(obj.Value.Type, obj.Value.Name, obj.Value.Path, obj.Value.Version); } string nodeKey = BuildNodeKey(obj.Value, obj.Value.Version); if (!expandedDict.ContainsKey(nodeKey)) { expandedDict.Add(nodeKey, obj.Value); if (hasParents) { if (obj.Value.Type == TypeEnum.FILE_VERSION) { ExpandCheckedDataSourceFileVersionNode(expandedDict, obj.Value); } ExpandCheckedDataSourceAddParents(expandedDict, obj.Value.Path); } } } // Load all respective `BackupPlanPathNode`s. foreach (var obj in expandedDict) { BackupPlanTreeNodeData nodeData = obj.Value; if (nodeData.UserObject == null) { Models.EntryType nodeType = Models.EntryTypeExtensions.ToEntryType(nodeData.Type); if (nodeData.Type == TypeEnum.FILE_VERSION) { nodeType = Models.EntryType.FILE; } BackupPlanPathNodeRepository daoPathNode = new BackupPlanPathNodeRepository(); nodeData.UserObject = daoPathNode.GetByStorageAccountAndTypeAndPath(StorageAccount, nodeType, nodeData.Path); } } return(expandedDict); }