private void FillMissingInfos(PatientFilesGroupViewModel formModel, IObjectSet<FileMetadata> dbFileMetadataSet)
        {
            var ids = formModel.Files.Select(f => f.MetadataId).ToArray();
            var filesInGroup = dbFileMetadataSet
                .Where(f => ids.Contains(f.Id))
                .ToDictionary(f => f.Id);

            foreach (var eachModelFile in formModel.Files)
            {
                FileMetadata fileMetadata;
                if (filesInGroup.TryGetValue(eachModelFile.MetadataId, out fileMetadata))
                {
                    eachModelFile.BlobName = fileMetadata.BlobName;
                    eachModelFile.ContainerName = fileMetadata.ContainerName;
                    eachModelFile.ExpirationDate = fileMetadata.ExpirationDate;
                    eachModelFile.SourceFileName = fileMetadata.SourceFileName;
                }
            }
        }
        private static void FillFileLengths(IBlobStorageManager storage, PatientFilesGroupViewModel viewModel, int? dbUserId)
        {
            // reading file sizes from the storage
            // todo: use db to get file size (faster)
            foreach (var eachFile in viewModel.Files)
            {
                var fullStoragePath = string.Format("{0}\\{1}", eachFile.ContainerName, eachFile.BlobName);
                var mustStartWith = string.Format("patient-files-{0}\\", dbUserId);
                if (!fullStoragePath.StartsWith(mustStartWith))
                    continue;

                eachFile.FileLength = storage.GetFileLength(eachFile.ContainerName, eachFile.BlobName);
            }
        }
        private static PatientFilesGroupViewModel GetViewModel(IBlobStorageManager storage, PatientFileGroup dbFileGroup, int dbUserId, Func<DateTime, DateTime> toLocal)
        {
            if (dbFileGroup == null)
                return new PatientFilesGroupViewModel();

            var result = new PatientFilesGroupViewModel
                {
                    Id = dbFileGroup.Id,
                    PatientId = dbFileGroup.PatientId,
                    CreatedOn = dbFileGroup.CreatedOn,
                    Title = dbFileGroup.GroupTitle,
                    Notes = dbFileGroup.GroupNotes,
                    FileGroupDate = toLocal(dbFileGroup.FileGroupDate),
                    ReceiveDate = toLocal(dbFileGroup.ReceiveDate),
                };

            result.Files.AddRange(dbFileGroup.PatientFiles.Select(dbFile => new PatientFileViewModel
                {
                    Id = dbFile.Id,
                    FileTitle = dbFile.Title,
                    ContainerName = dbFile.FileMetadata.ContainerName,
                    SourceFileName = dbFile.FileMetadata.SourceFileName,
                    BlobName = dbFile.FileMetadata.BlobName,
                    ExpirationDate = dbFile.FileMetadata.ExpirationDate,
                    MetadataId = dbFile.FileMetadataId,
                }));

            FillFileLengths(storage, result, dbUserId);

            return result;
        }
        public ActionResult Edit(int? id, int? patientId, int? y, int? m, int? d)
        {
            PatientFilesGroupViewModel viewModel = null;

            if (id != null)
            {
                viewModel = GetViewModel(
                    this.storage,
                    (from pf in this.db.PatientFileGroups where pf.Id == id select pf).First(),
                    this.DbUser.Id,
                    this.GetToLocalDateTimeConverter());
            }
            else
            {
                Debug.Assert(patientId != null, "patientId != null");
                viewModel = new PatientFilesGroupViewModel
                    {
                        Id = null,
                        PatientId = patientId.Value,
                        FileGroupDate = null,
                        ReceiveDate = DateTimeHelper.CreateDate(y, m, d) ?? this.GetPracticeLocalNow(),
                    };
            }

            viewModel.NewGuid = Guid.NewGuid();

            this.ViewBag.FilesStatusGetter = (FilesStatusGetter)this.GetFilesStatus;

            return this.View("Edit", viewModel);
        }