ClearCanvas.ImageServer.Core.Edit.UpdateStudyCommand.UpdateFilesystem C# (CSharp) Method

UpdateFilesystem() private method

private UpdateFilesystem ( ) : void
return void
		private void UpdateFilesystem()
		{
			Platform.Log(LogLevel.Info, "Updating filesystem...");
			StudyXml studyXml = _oldStudyLocation.LoadStudyXml();
			StudyXmlOutputSettings outputSettings = ImageServerCommonConfiguration.DefaultStudyXmlOutputSettings;

			var newStudyXml = new StudyXml();
            foreach (SeriesXml seriesXml in studyXml)
			{
				foreach (InstanceXml instanceXml in seriesXml)
				{
					string path = Path.Combine(_oldStudyPath, seriesXml.SeriesInstanceUid);
					path = Path.Combine(path, instanceXml.SopInstanceUid);
					path += ServerPlatform.DicomFileExtension;

                    if (!File.Exists(path))
                    {
                        Platform.Log(LogLevel.Info, "SOP {0} is referenced in study xml but does not exist. It will be removed");
                        continue; // file was removed but xml was not updated?
                    }

                    try
                    {                        
						var file = new DicomFile(path);
                        file.Load();

						var instance = new InstanceInfo
                        {
                            SeriesInstanceUid = file.DataSet[DicomTags.SeriesInstanceUid].GetString(0, String.Empty),
                            SopInstanceUid = file.DataSet[DicomTags.SopInstanceUid].GetString(0, String.Empty)
                        };
                        
                        UpdateDicomFile(file);

                        // Add into the temporary study xml
                        long fileSize = 0;
						var finfo = new FileInfo(file.Filename);
						if (finfo.Exists)
                            fileSize = finfo.Length;
                        newStudyXml.AddFile(file, fileSize, outputSettings);

                        
                        _updatedSopList.Add(instance);
						Platform.Log(ServerPlatform.InstanceLogLevel, "SOP {0} has been updated [{1} of {2}].", instance.SopInstanceUid,
						             _updatedSopList.Count, _totalSopCount);

						EventManager.FireEvent(this,
						                       new UpdateSopEventArgs
							                       {
								                       File = file,
								                       ServerPartitionEntry = _partition,
								                       WorkQueueUidEntry = null,
								                       WorkQueueEntry = _workQueue,
								                       FileLength = (ulong) fileSize
							                       });

                    }
                    catch (Exception)
                    {
						FileUtils.Delete(Path.Combine(_backupDir, instanceXml.SopInstanceUid) + ".bak"); //dont' need to restore this file
                        throw;
                    }
                }                
			}

            // Log any study-level warnings
			if (_updatedSopList.Count != _totalSopCount)
			{
				Platform.Log(LogLevel.Warn, "Inconsistent data: expected {0} instances to be updated / Found {1}.", _totalSopCount,
				             _updatedSopList.Count);
			}

            // update the header
			Platform.Log(LogLevel.Info, "Generating new study header...");
			string newStudyXmlPath = Path.Combine(NewStudyPath, _newStudyInstanceUid + ".xml");
			string gzipStudyXmlPath = Path.Combine(NewStudyPath, _newStudyInstanceUid + ".xml.gz");
			using (FileStream xmlStream = FileStreamOpener.OpenForSoleUpdate(newStudyXmlPath, FileMode.Create),
			                  gzipStream = FileStreamOpener.OpenForSoleUpdate(gzipStudyXmlPath, FileMode.Create))
			{
				StudyXmlIo.WriteXmlAndGzip(newStudyXml.GetMemento(outputSettings), xmlStream, gzipStream);
				xmlStream.Close();
				gzipStream.Close();
			}
		}