context: #region License
  
  // Copyright (c) 2013, ClearCanvas Inc.
  // All rights reserved.
  // http://www.ClearCanvas.ca
  //
  // This file is part of the ClearCanvas RIS/PACS open source project.
  //
  // The ClearCanvas RIS/PACS open source project is free software: you can
  // redistribute it and/or modify it under the terms of the GNU General Public
  // License as published by the Free Software Foundation, either version 3 of the
  // License, or (at your option) any later version.
  //
  // The ClearCanvas RIS/PACS open source project is distributed in the hope that it
  // will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
  // Public License for more details.
  //
  // You should have received a copy of the GNU General Public License along with
  // the ClearCanvas RIS/PACS open source project.  If not, see
  // <http://www.gnu.org/licenses/>.
  
  #endregion
  
  using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Threading;
  using System.Xml;
  using Macro.Common;
  using Macro.Common.Utilities;
  using Macro.Dicom;
  using Macro.Dicom.Utilities.Xml;
  using Macro.Enterprise.Core;
  using Macro.ImageServer.Enterprise;
  using Macro.ImageServer.Model.Brokers;
  using Macro.ImageServer.Model.EntityBrokers;
  using Macro.ImageServer.Model.Parameters;
  
  namespace Macro.ImageServer.Model
  {
      public class StudyStorageLocation : ServerEntity, ICloneable, IEquatable<StudyStorageLocation>
      {
          private const string StudyXmlExtension = "xml";
          private const string StudyXmlGzipExtension = "xml.gz";
          
          #region Constructors
          public StudyStorageLocation()
              : base("StudyStorageLocation")
          {
          }
  
          /// <summary>
          /// Create a copy of the specified <see cref="StudyStorageLocation"/>
          /// </summary>
          /// <param name="original"></param>
          public StudyStorageLocation(StudyStorageLocation original)
              : base("StudyStorageLocation")
          {
              SetKey(original.Key);
              Enabled = original.Enabled;
              FilesystemKey = original.FilesystemKey;
              FilesystemPath = original.FilesystemPath;
              FilesystemStudyStorageKey = original.FilesystemStudyStorageKey;
              FilesystemTierEnum = original.FilesystemTierEnum;
              InsertTime = original.InsertTime;
              IsReconcileRequired = original.IsReconcileRequired;
              LastAccessedTime = original.LastAccessedTime;
              WriteLock = original.WriteLock;
              PartitionFolder = original.PartitionFolder;
              QueueStudyStateEnum = original.QueueStudyStateEnum;
              ReadOnly = original.ReadOnly;
              ServerPartitionKey = original.ServerPartitionKey;
              ServerTransferSyntaxKey = original.ServerTransferSyntaxKey;
              StudyFolder = original.StudyFolder;
              StudyInstanceUid = original.StudyInstanceUid;
              StudyStatusEnum = original.StudyStatusEnum;
              StudyUidFolder = original.StudyUidFolder;
              TransferSyntaxUid = original.TransferSyntaxUid;
              WriteOnly = original.WriteOnly;
          }
          #endregion
  
          #region Private Members
          static private readonly IPersistentStore _store = PersistentStoreRegistry.GetDefaultStore();
      	private IList<StudyIntegrityQueue> _integrityQueueItems;
  
          private ServerPartition _partition;
          private Study _study;
          private StudyXml _studyXml;
          private string _studyUidFolder;
          private string _studyFolderRelativePath;
          private StudyStorage _studyStorage;
          private Patient _patient;
          private IList<ArchiveStudyStorage> _archives;
  
          #endregion
  
          #region Public Properties
  
      	[EntityFieldDatabaseMappingAttribute(TableName = "StudyStorageLocation", ColumnName = "ServerPartitionGUID")]
      	public ServerEntityKey ServerPartitionKey { get; set; }
  
      	[EntityFieldDatabaseMappingAttribute(TableName = "StudyStorageLocation", ColumnName = "FilesystemGUID")]
      	public ServerEntityKey FilesystemKey { get; set; }
  
      	[EntityFieldDatabaseMappingAttribute(TableName = "StudyStorageLocation", ColumnName = "ServerTransferSyntaxGUID")]
      	public ServerEntityKey ServerTransferSyntaxKey { get; set; }
  
      	[EntityFieldDatabaseMappingAttribute(TableName = "StudyStorageLocation", ColumnName = "StudyInstanceUid")]
      	public string StudyInstanceUid { get; set; }
  
      	[EntityFieldDatabaseMappingAttribute(TableName = "StudyStorageLocation", ColumnName = "LastAccessedTime")]
      	public DateTime LastAccessedTime { get; set; }
  
      	[EntityFieldDatabaseMappingAttribute(TableName = "StudyStorageLocation", ColumnName = "InsertTime")]
      	public DateTime InsertTime { get; set; }
  
      	[EntityFieldDatabaseMappingAttribute(TableName = "StudyStorageLocation", ColumnName = "WriteLock")]
      	public bool WriteLock { get; set; }
  
  		[EntityFieldDatabaseMappingAttribute(TableName = "StudyStorageLocation", ColumnName = "ReadLock")]
  		public short ReadLock { get; set; }
  
      	[EntityFieldDatabaseMappingAttribute(TableName = "StudyStorageLocation", ColumnName = "StudyStatusEnum")]
      	public StudyStatusEnum StudyStatusEnum { get; set; }
  
      	[EntityFieldDatabaseMappingAttribute(TableName = "StudyStorageLocation", ColumnName = "FilesystemPath")]
      	public string FilesystemPath { get; set; }
  
      	[EntityFieldDatabaseMappingAttribute(TableName = "StudyStorageLocation", ColumnName = "PartitionFolder")]
      	public string PartitionFolder { get; set; }
  
      	[EntityFieldDatabaseMappingAttribute(TableName = "StudyStorageLocation", ColumnName = "StudyFolder")]
      	public string StudyFolder { get; set; }
  
      	[EntityFieldDatabaseMappingAttribute(TableName = "StudyStorageLocation", ColumnName = "Enabled")]
      	public bool Enabled { get; set; }
  
      	[EntityFieldDatabaseMappingAttribute(TableName = "StudyStorageLocation", ColumnName = "ReadOnly")]
      	public bool ReadOnly { get; set; }
  
      	[EntityFieldDatabaseMappingAttribute(TableName = "StudyStorageLocation", ColumnName = "WriteOnly")]
      	public bool WriteOnly { get; set; }
  
      	[EntityFieldDatabaseMappingAttribute(TableName = "StudyStorageLocation", ColumnName = "FilesystemTierEnum")]
      	public FilesystemTierEnum FilesystemTierEnum { get; set; }
  
      	[EntityFieldDatabaseMappingAttribute(TableName = "StudyStorageLocation", ColumnName = "TransferSyntaxUid")]
      	public string TransferSyntaxUid { get; set; }
  
      	[EntityFieldDatabaseMappingAttribute(TableName = "StudyStorageLocation", ColumnName = "FilesystemStudyStorageGUID")
      	]
      	public ServerEntityKey FilesystemStudyStorageKey { get; set; }
  
      	[EntityFieldDatabaseMappingAttribute(TableName = "StudyStorageLocation", ColumnName = "QueueStudyStateEnum")]
      	public QueueStudyStateEnum QueueStudyStateEnum { get; set; }
  
      	[EntityFieldDatabaseMappingAttribute(TableName = "StudyStorageLocation", ColumnName = "IsReconcileRequired")]
      	public bool IsReconcileRequired { get; set; }
  
          public string StudyUidFolder
          {
              get
              {
              	if (String.IsNullOrEmpty(_studyUidFolder))
                      return StudyInstanceUid;
              	return _studyUidFolder;
              }
          	set { _studyUidFolder = value; }
          }
  
          /// <summary>
          /// Gets the <see cref="ServerPartition"/> of the study.
          /// </summary>
          public ServerPartition ServerPartition
          {
              get
              {
                  lock (SyncRoot)
                  {
                      if (_partition == null)
                      {
                          _partition = ServerPartition.Load(ServerPartitionKey);
                      }
                  }
                  return _partition;
              }
          }
  
          /// <summary>
          /// Gets the related <see cref="ArchiveStudyStorage"/>.
          /// </summary>
          public IList<ArchiveStudyStorage> ArchiveLocations
          {
              get
              {
                  if (_archives == null)
                  {
                      lock (SyncRoot)
                      {
                          _archives = GetArchiveLocations(Key);
                      }
                  }
                  return _archives;
              }
          }
  
          /// <summary>
          /// Gets the related <see cref="Study"/> entity.
          /// </summary>
          /// <remarks>
          /// 
          /// </remarks>
          public Study Study
          {
              get
              {
                  if (_study==null)
                  {
  
gt:                     using(IPersistenceContext context = _store.OpenReadContext())