protected override void OnExecute(CommandProcessor theProcessor, IUpdateContext updateContext)
        {
            // update FilesystemStudyStorage
            if (Context != null)
            {
                Platform.Log(LogLevel.Info, "Updating database...");
                IFilesystemStudyStorageEntityBroker broker = updateContext.GetBroker<IFilesystemStudyStorageEntityBroker>();

                FilesystemStudyStorageSelectCriteria searchCriteria = new FilesystemStudyStorageSelectCriteria();
                searchCriteria.StudyStorageKey.EqualTo(Context.OriginalStudyLocation.GetKey());
                searchCriteria.FilesystemKey.EqualTo(Context.OriginalStudyLocation.FilesystemKey);
                FilesystemStudyStorage filesystemStudyStorage = broker.FindOne(searchCriteria);
                Debug.Assert(filesystemStudyStorage != null);

                // Update Filesystem for the StudyStorage entry
                filesystemStudyStorage.FilesystemKey = Context.Destination.Filesystem.GetKey();
                broker.Update(filesystemStudyStorage);


                // Update Filesystem for the remaining FilesystemQueue entries
                IFilesystemQueueEntityBroker fsQueueBroker = updateContext.GetBroker<IFilesystemQueueEntityBroker>();                
                FilesystemQueueSelectCriteria fsQueueSearchCriteria = new FilesystemQueueSelectCriteria();
                fsQueueSearchCriteria.StudyStorageKey.EqualTo(Context.OriginalStudyLocation.GetKey());
                fsQueueSearchCriteria.FilesystemKey.EqualTo(Context.OriginalStudyLocation.FilesystemKey);

                FilesystemQueueUpdateColumns fsQueueUpdateColumns = new FilesystemQueueUpdateColumns();
                fsQueueUpdateColumns.FilesystemKey = Context.Destination.Filesystem.GetKey();
                fsQueueBroker.Update(fsQueueSearchCriteria, fsQueueUpdateColumns);
                
                // Insert or update Filesystem Queue table.
                IInsertFilesystemQueue insertFilesystemQueueBroker = updateContext.GetBroker<IInsertFilesystemQueue>();
                FilesystemQueueInsertParameters parms = new FilesystemQueueInsertParameters();
                parms.FilesystemKey = Context.Destination.Filesystem.GetKey();
                parms.FilesystemQueueTypeEnum = FilesystemQueueTypeEnum.TierMigrate;
                parms.ScheduledTime = Platform.Time;
                parms.StudyStorageKey = Context.OriginalStudyLocation.GetKey();
                insertFilesystemQueueBroker.Execute(parms);

                Platform.Log(LogLevel.Info, "Database is updated.");
                
            }
        }
 public FilesystemStudyStorageSelectCriteria(FilesystemStudyStorageSelectCriteria other)
 : base(other)
 {}
        /// <summary>
        /// Updates the status of a study to a new status
        /// </summary>
		protected virtual void UpdateStudyStatus(StudyStorageLocation theStudyStorage, StudyStatusEnum theStatus, TransferSyntax theSyntax)
        {
        	DBUpdateTime.Add(
        		delegate
        			{
        				using (
        					IUpdateContext updateContext =
        						PersistentStoreRegistry.GetDefaultStore().OpenUpdateContext(UpdateContextSyncMode.Flush))
        				{
							// Select the Server Transfer Syntax
							ServerTransferSyntaxSelectCriteria syntaxCriteria = new ServerTransferSyntaxSelectCriteria();
							IServerTransferSyntaxEntityBroker syntaxBroker =
								updateContext.GetBroker<IServerTransferSyntaxEntityBroker>();
							syntaxCriteria.Uid.EqualTo(theSyntax.UidString);

							ServerTransferSyntax serverSyntax = syntaxBroker.FindOne(syntaxCriteria);
							if (serverSyntax == null)
							{
								Platform.Log(LogLevel.Error, "Unable to load ServerTransferSyntax for {0}.  Unable to update study status.", theSyntax.Name);
								return;
							}

							// Get the FilesystemStudyStorage update broker ready
        					IFilesystemStudyStorageEntityBroker filesystemQueueBroker =
								updateContext.GetBroker<IFilesystemStudyStorageEntityBroker>();
							FilesystemStudyStorageUpdateColumns filesystemQueueUpdate = new FilesystemStudyStorageUpdateColumns
							                                                                {
							                                                                    ServerTransferSyntaxKey = serverSyntax.GetKey()
							                                                                };
        				    FilesystemStudyStorageSelectCriteria filesystemQueueCriteria = new FilesystemStudyStorageSelectCriteria();
        					filesystemQueueCriteria.StudyStorageKey.EqualTo(theStudyStorage.GetKey());

							// Get the StudyStorage update broker ready
        					IStudyStorageEntityBroker studyStorageBroker =
        						updateContext.GetBroker<IStudyStorageEntityBroker>();
							StudyStorageUpdateColumns studyStorageUpdate = new StudyStorageUpdateColumns
							                                                   {
							                                                       StudyStatusEnum = theStatus,
							                                                       LastAccessedTime = Platform.Time
							                                                   };

        				    if (!filesystemQueueBroker.Update(filesystemQueueCriteria,filesystemQueueUpdate))
							{
								Platform.Log(LogLevel.Error, "Unable to update FilesystemQueue row: Study {0}, Server Entity {1}",
											 theStudyStorage.StudyInstanceUid, theStudyStorage.ServerPartitionKey);
								
							}
							else if (!studyStorageBroker.Update(theStudyStorage.GetKey(),studyStorageUpdate))
							{
								Platform.Log(LogLevel.Error, "Unable to update StudyStorage row: Study {0}, Server Entity {1}",
											 theStudyStorage.StudyInstanceUid, theStudyStorage.ServerPartitionKey);								
							}
							else
								updateContext.Commit();
        				}
        			}
        		);
        }
        /// <summary>
        /// Returns number of Delete Study, Tier Migrate, and Study Purge work queue items 
        /// that are still Pending or In Progress for the filesystem associated with the
        /// specified <see cref="ServiceLock"/>.
        /// </summary>
        /// <param name="item">The ServiceLock item.</param>
        /// <returns>The number of WorkQueue entries pending.</returns>
        private int CheckWorkQueueCount(Model.ServiceLock item)
        {
			using (ServerExecutionContext context = new ServerExecutionContext())
			{
				IWorkQueueEntityBroker select = context.ReadContext.GetBroker<IWorkQueueEntityBroker>();

				WorkQueueSelectCriteria criteria = new WorkQueueSelectCriteria();

				criteria.WorkQueueTypeEnum.In(new[]
				                              	{
				                              		WorkQueueTypeEnum.DeleteStudy, WorkQueueTypeEnum.MigrateStudy,
				                              		WorkQueueTypeEnum.PurgeStudy
				                              	});

				// Do Pending status, in case there's a Failure status entry, we don't want to 
				// block on that.
				criteria.WorkQueueStatusEnum.In(new[] {WorkQueueStatusEnum.Pending, WorkQueueStatusEnum.InProgress});

				FilesystemStudyStorageSelectCriteria filesystemCriteria = new FilesystemStudyStorageSelectCriteria();

				filesystemCriteria.FilesystemKey.EqualTo(item.FilesystemKey);

				criteria.FilesystemStudyStorageRelatedEntityCondition.Exists(filesystemCriteria);
				int count = select.Count(criteria);

				return count;

			}
        }
		protected override void OnExecute(CommandProcessor theProcessor, IUpdateContext updateContext)
		{
			// Check if the File is the same syntax as the 
			TransferSyntax fileSyntax = _file.TransferSyntax;
			TransferSyntax dbSyntax = TransferSyntax.GetTransferSyntax(_location.TransferSyntaxUid);

			// Check if the syntaxes match the location
			if ((!fileSyntax.Encapsulated && !dbSyntax.Encapsulated)
			  || (fileSyntax.LosslessCompressed && dbSyntax.LosslessCompressed)
			  || (fileSyntax.LossyCompressed && dbSyntax.LossyCompressed))
			{
				// no changes necessary, just return;
				return;
			}

			// Select the Server Transfer Syntax
			var syntaxCriteria = new ServerTransferSyntaxSelectCriteria();
			var syntaxBroker = updateContext.GetBroker<IServerTransferSyntaxEntityBroker>();
			syntaxCriteria.Uid.EqualTo(fileSyntax.UidString);

			ServerTransferSyntax serverSyntax = syntaxBroker.FindOne(syntaxCriteria);
			if (serverSyntax == null)
			{
				Platform.Log(LogLevel.Error, "Unable to load ServerTransferSyntax for {0}.  Unable to update study status.", fileSyntax.Name);
				return;
			}

			// Get the FilesystemStudyStorage update broker ready
			var filesystemStudyStorageEntityBroker = updateContext.GetBroker<IFilesystemStudyStorageEntityBroker>();
			var filesystemStorageUpdate = new FilesystemStudyStorageUpdateColumns();
			var filesystemStorageCritiera = new FilesystemStudyStorageSelectCriteria();

			filesystemStorageUpdate.ServerTransferSyntaxKey = serverSyntax.Key;
			filesystemStorageCritiera.StudyStorageKey.EqualTo(_location.Key);

			// Save a StudyHistory record for the compression that occurred.
			StudyHistoryHelper.CreateStudyHistoryRecord(updateContext, _location, _location, StudyHistoryTypeEnum.SopCompress,
			                                            null, new CompressionStudyHistory
				                                            {
					                                            TimeStamp = Platform.Time,
					                                            OriginalTransferSyntaxUid = dbSyntax.UidString,
					                                            FinalTransferSyntaxUid = fileSyntax.UidString
				                                            });

			// Get the StudyStorage update broker ready
			var studyStorageBroker =
				updateContext.GetBroker<IStudyStorageEntityBroker>();
			var studyStorageUpdate = new StudyStorageUpdateColumns();
			StudyStatusEnum statusEnum = _location.StudyStatusEnum;
			if (fileSyntax.LossyCompressed)
				studyStorageUpdate.StudyStatusEnum = statusEnum = StudyStatusEnum.OnlineLossy;
			else if (fileSyntax.LosslessCompressed)
				studyStorageUpdate.StudyStatusEnum = statusEnum = StudyStatusEnum.OnlineLossless;

			studyStorageUpdate.LastAccessedTime = Platform.Time;

			if (!filesystemStudyStorageEntityBroker.Update(filesystemStorageCritiera, filesystemStorageUpdate))
			{
				Platform.Log(LogLevel.Error, "Unable to update FilesystemQueue row: Study {0}, Server Entity {1}",
							 _location.StudyInstanceUid, _location.ServerPartitionKey);

			}
			else if (!studyStorageBroker.Update(_location.GetKey(), studyStorageUpdate))
			{
				Platform.Log(LogLevel.Error, "Unable to update StudyStorage row: Study {0}, Server Entity {1}",
							 _location.StudyInstanceUid, _location.ServerPartitionKey);
			}
			else
			{
				// Update the location, so the next time we come in here, we don't try and update the database
				// for another sop in the study.
				_location.StudyStatusEnum = statusEnum;
				_location.TransferSyntaxUid = fileSyntax.UidString;
				_location.ServerTransferSyntaxKey = serverSyntax.Key;
			}
		}
 public FilesystemStudyStorageSelectCriteria(FilesystemStudyStorageSelectCriteria other)
     : base(other)
 {
 }