Пример #1
0
        public static void UpdateRepositoryItemFromModel(CatalogRecord record)
        {
            var client = RepositoryHelper.GetClient();

            var logger = LogManager.GetLogger("Curation");

            long version = 0;

            try
            {
                version = client.GetLatestVersionNumber(record.Id, record.Organization.AgencyID);
            }
            catch
            {
                // StudyUnit does not exist yet. That is okay, because we are making one from
                // scratch anyway.
                logger.Debug("StudyUnit does not yet exist in the repository");
            }

            var study = new StudyUnit()
            {
                Identifier = record.Id,
                AgencyId   = record.Organization.AgencyID,
                Version    = version + 1
            };

            var studyMapper = new CatalogRecordToStudyUnitMapper();

            studyMapper.Map(record, study);

            // Add each file as either a PhysicalInstance or an OtherMaterial.
            foreach (var file in record.Files.Where(x => x.Status != FileStatus.Removed))
            {
                if (file.IsStatisticalDataFile())
                {
                    try
                    {
                        var pi = client.GetLatestItem(file.Id, record.Organization.AgencyID)
                                 as PhysicalInstance;
                        if (pi != null)
                        {
                            var mapper = new ManagedFileToPhysicalInstanceMapper();
                            mapper.Map(file, pi);

                            pi.Version++;

                            // Make sure the file is added to the StudyUnit.
                            if (!study.PhysicalInstances.Any(x => x.Identifier == pi.Identifier))
                            {
                                study.PhysicalInstances.Add(pi);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Warn("Could not attach PhysicalInstance to StudyUnit", ex);
                    }
                }
                else
                {
                    var material = study.OtherMaterials.Where(x => x.DublinCoreMetadata.Title.Current == file.PublicName)
                                   .FirstOrDefault();

                    // If this material is not already on the study, add it.
                    if (material == null)
                    {
                        material = new OtherMaterial();
                        study.OtherMaterials.Add(material);
                    }

                    // Perform the mapping.
                    var mapper = new ManagedFileToOtherMaterialMapper();
                    mapper.Map(file, material);
                    material.Version++;
                }
            }

            // Register the updated item with the repository.
            var commitOptions = new CommitOptions();

            commitOptions.NamedOptions.Add("RegisterOrReplace");

            var dirty = new DirtyItemGatherer();

            study.Accept(dirty);

            foreach (var dirtyItem in dirty.DirtyItems)
            {
                client.RegisterItem(dirtyItem, commitOptions);
            }
        }