/// <summary>
        /// Processing Test ID 001, 002, and 003
        /// Register Items with Repository
        /// </summary>
        /// <returns></returns>
        public static async Task RegisterItems(IVersionable item)
        {
            // First find all the items that should be registered.
            // The dirty item finder can collect new and changed items in a model
            var dirtyItemGatherer = new DirtyItemGatherer();

            item.Accept(dirtyItemGatherer);

            // Get an API client, windows or username
            var api = GetRepositoryApiWindows();

            // start a transaction
            var transaction = await api.CreateTransactionAsync();

            // Add all of the items to register to a transaction
            var addItemsRequest = new RepositoryTransactionAddItemsRequest();

            foreach (var itemToRegister in dirtyItemGatherer.DirtyItems)
            {
                addItemsRequest.Items.Add(VersionableToRepositoryItem(itemToRegister));
            }

            // commit the transaction, the Repository will handle the versioning
            var options = new RepositoryTransactionCommitOptions()
            {
                TransactionId   = transaction.TransactionId,
                TransactionType = RepositoryTransactionType.CommitAsLatestWithLatestChildrenAndPropagateVersions
            };
            var results = await api.CommitTransactionAsync(options);
        }
Esempio n. 2
0
        public void RewriteCCS()
        {
            MultilingualString.CurrentCulture = "en-GB";
            VersionableBase.DefaultAgencyId   = "example.org";

            var client = RepositoryIntro.GetClient();

            var instance = client.GetItem(new Guid("b9ee3aa5-5bc5-43ed-a24e-f560abb30801"), "example.org", 2)
                           as DdiInstance;

            //DDIWorkflowDeserializer deserializer = new DDIWorkflowDeserializer();
            //var instance = deserializer.LoadDdiFile(@"D:\Downloads\filesforMinneapolis\filesforMinneapolis\bcs08v08.xml");

            GraphPopulator populator = new GraphPopulator(client);

            populator.ChildProcessing = ChildReferenceProcessing.Populate;
            instance.Accept(populator);

            var resourcePackage = instance.ResourcePackages[0];

            var instrument = instance.ResourcePackages[0].DataCollections[0].Instruments[0];

            //var topLevelSequence = client.GetLatestItem(new Guid("ceaa9acf-2b2f-4c41-b298-b9f419412586"), "cls")
            //    as CustomSequenceActivity;

            var topLevelSequence = instrument.Sequence;

            var moduleSequences = topLevelSequence.Activities;

            foreach (CustomSequenceActivity module in moduleSequences.OfType <CustomSequenceActivity>())
            {
                DirtyItemGatherer gatherer = new DirtyItemGatherer(true);
                module.Accept(gatherer);

                var allChildren = gatherer.DirtyItems;

                ControlConstructScheme ccs = new ControlConstructScheme();
                ccs.ItemName.Copy(module.ItemName);

                foreach (var child in allChildren.OfType <ActivityBase>())
                {
                    ccs.ControlConstructs.Add(child);
                }

                //client.RegisterItem(ccs, new CommitOptions());

                resourcePackage.ControlConstructSchemes.Add(ccs);
            }

            DDIWorkflowSerializer serializer = new DDIWorkflowSerializer();

            serializer.UseConciseBoundedDescription = false;
            var doc = serializer.Serialize(instance);

            doc.Save(@"d:\ColecticaOutput\bcsv8.xml");
        }
        public void RewriteCCS()
        {
            MultilingualString.CurrentCulture = "en-GB";
            VersionableBase.DefaultAgencyId = "example.org";

            var client = RepositoryIntro.GetClient();

            var instance = client.GetItem(new Guid("b9ee3aa5-5bc5-43ed-a24e-f560abb30801"), "example.org", 2)
                as DdiInstance;

            //DDIWorkflowDeserializer deserializer = new DDIWorkflowDeserializer();
            //var instance = deserializer.LoadDdiFile(@"D:\Downloads\filesforMinneapolis\filesforMinneapolis\bcs08v08.xml");

            GraphPopulator populator = new GraphPopulator(client);
            populator.ChildProcessing = ChildReferenceProcessing.Populate;
            instance.Accept(populator);

            var resourcePackage = instance.ResourcePackages[0];

            var instrument = instance.ResourcePackages[0].DataCollections[0].Instruments[0];

            //var topLevelSequence = client.GetLatestItem(new Guid("ceaa9acf-2b2f-4c41-b298-b9f419412586"), "cls")
            //    as CustomSequenceActivity;

            var topLevelSequence = instrument.Sequence;

            var moduleSequences = topLevelSequence.Activities;

            foreach (CustomSequenceActivity module in moduleSequences.OfType<CustomSequenceActivity>())
            {
                DirtyItemGatherer gatherer = new DirtyItemGatherer(true);
                module.Accept(gatherer);

                var allChildren = gatherer.DirtyItems;

                ControlConstructScheme ccs = new ControlConstructScheme();
                ccs.ItemName.Copy(module.ItemName);

                foreach (var child in allChildren.OfType<ActivityBase>())
                {
                    ccs.ControlConstructs.Add(child);
                }

                //client.RegisterItem(ccs, new CommitOptions());

                resourcePackage.ControlConstructSchemes.Add(ccs);
            }

            DDIWorkflowSerializer serializer = new DDIWorkflowSerializer();
            serializer.UseConciseBoundedDescription = false;
            var doc = serializer.Serialize(instance);
            doc.Save(@"d:\ColecticaOutput\bcsv8.xml");
        }
Esempio n. 4
0
        public void IngestIntoRepository(Group group)
        {
            // Register every DDI item in the repository.
            var client  = new LocalRepositoryClient();
            var options = new CommitOptions();

            var gatherer = new DirtyItemGatherer(true);

            group.Accept(gatherer);

            options.NamedOptions.Add("RegisterOrReplace");
            client.RegisterItems(gatherer.DirtyItems, options);
        }
        public void GetItemChangeChildAndReregister()
        {
            VersionableBase.DefaultAgencyId = "int.example";

            var client = GetClient();


            // Get a ConceptScheme from the repository,
            // and populate the member concepts.
            // Note that you probably have to change the UUID here
            // to represent one that is in your repository.
            var scheme = client.GetItem(
                new Guid("0cc13be5-3c89-4d1a-927f-ac7634d0c05a"),
                "int.example", 2,
                ChildReferenceProcessing.Populate) as ConceptScheme;

            if (scheme == null || scheme.Concepts.Count < 3)
            {
                Console.WriteLine("No scheme, or not enough items in the scheme.");
                return;
            }

            // Grab the second concept and add a description to it.
            var concept = scheme.Concepts[2];

            concept.Description.Current = "This is the fourth concept";

            // When we change a property on the Concept, the concept's
            // IsDirty property is automatically set to true, indicating
            // that it has unsaved changes.
            Console.WriteLine("IsDirty: ", concept.IsDirty);

            // The DirtyItemGatherer will walk a set of objects and create
            // a list of all objects that are dirty.
            DirtyItemGatherer gatherer = new DirtyItemGatherer(
                gatherEvenIfNotDirty: false,
                markParentsDirty: true);

            scheme.Accept(gatherer);

            var options = new CommitOptions();

            // For every changed item, increase the version and
            // register the new version with the repository.
            foreach (var item in gatherer.DirtyItems)
            {
                item.Version++;
                client.RegisterItem(item, options);
            }
        }
Esempio n. 6
0
        public void IncrementDityItemAndParents(IVersionable item)
        {
            Dig(item);
            var dirtyGthr = new DirtyItemGatherer();

            item.Accept(dirtyGthr);
            foreach (var dirtyItem in dirtyGthr.DirtyItems)
            {
                Increment(dirtyItem);
                var allParents = GetAllParents(dirtyItem);
                foreach (var parent in allParents)
                {
                    Increment(parent);
                }
            }
        }
        /// <summary>
        /// Load the sample DDI 3.1 file and register all its items with the Repository.
        /// </summary>
        public void RegisterFile()
        {
            // Load the sample data file.
            DDIWorkflowDeserializer deserializer = new DDIWorkflowDeserializer();
            string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var    instance  = deserializer.LoadDdiFile(Path.Combine(directory, "sample.xml"));

            // Gather all items instance in the DdiInstance we just loaded.
            // We need to do this to get the items in a flat list.
            DirtyItemGatherer gatherer = new DirtyItemGatherer(gatherEvenIfNotDirty: true);

            instance.Accept(gatherer);

            // Grab a client to communicate with the Repository.
            var client = GetClient();

            // Register each item with the Repository.
            foreach (var item in gatherer.DirtyItems)
            {
                client.RegisterItem(item, new CommitOptions());
            }
        }
        /// <summary>
        /// Load the sample DDI 3.1 file and register all its items with the Repository.
        /// </summary>
        public void RegisterFile()
        {
            // Load the sample data file.
            DDIWorkflowDeserializer deserializer = new DDIWorkflowDeserializer();
            string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var instance = deserializer.LoadDdiFile(Path.Combine(directory, "sample.xml"));

            // Gather all items instance in the DdiInstance we just loaded.
            // We need to do this to get the items in a flat list.
            DirtyItemGatherer gatherer = new DirtyItemGatherer(gatherEvenIfNotDirty:true);
            instance.Accept(gatherer);

            // Grab a client to communicate with the Repository.
            var client = GetClient();

            // Register each item with the Repository.
            foreach (var item in gatherer.DirtyItems)
            {
                client.RegisterItem(item, new CommitOptions());
            }
        }
        public void GetItemChangeChildAndReregister()
        {
            VersionableBase.DefaultAgencyId = "int.example";

            var client = GetClient();

            // Get a ConceptScheme from the repository,
            // and populate the member concepts.
            // Note that you probably have to change the UUID here
            // to represent one that is in your repository.
            var scheme = client.GetItem(
                new Guid("0cc13be5-3c89-4d1a-927f-ac7634d0c05a"),
                "int.example", 2,
                ChildReferenceProcessing.Populate) as ConceptScheme;

            if (scheme == null || scheme.Concepts.Count < 3)
            {
                Console.WriteLine("No scheme, or not enough items in the scheme.");
                return;
            }

            // Grab the second concept and add a description to it.
            var concept = scheme.Concepts[2];
            concept.Description.Current = "This is the fourth concept";

            // When we change a property on the Concept, the concept's
            // IsDirty property is automatically set to true, indicating
            // that it has unsaved changes.
            Console.WriteLine("IsDirty: ", concept.IsDirty);

            // The DirtyItemGatherer will walk a set of objects and create
            // a list of all objects that are dirty.
            DirtyItemGatherer gatherer = new DirtyItemGatherer(
                gatherEvenIfNotDirty: false,
                markParentsDirty: true);
            scheme.Accept(gatherer);

            var options = new CommitOptions();

            // For every changed item, increase the version and
            // register the new version with the repository.
            foreach (var item in gatherer.DirtyItems)
            {
                item.Version++;
                client.RegisterItem(item, options);
            }
        }
Esempio n. 10
0
        public static string CreateOrUpdatePhysicalInstanceForFile <TImporter>(Guid fileId, string filePath, string agencyId, PhysicalInstance existingPhysicalInstance)
            where TImporter : IDataImporter, new()
        {
            var logger = LogManager.GetLogger("Curation");

            logger.Debug("File import: " + fileId);

            IDataImporter    importer         = new TImporter();
            var              client           = RepositoryHelper.GetClient();
            PhysicalInstance physicalInstance = existingPhysicalInstance;

            // For PhysicalInstances that do not exist yet, create from scratch.
            if (physicalInstance == null)
            {
                // Extract metadata.
                ResourcePackage rp = importer.Import(filePath, agencyId);
                logger.Debug("Imported metadata from data file.");

                if (rp.PhysicalInstances.Count == 0)
                {
                    logger.Debug("No dataset could be extracted from SPSS");
                    return(string.Empty);
                }

                physicalInstance            = rp.PhysicalInstances[0];
                physicalInstance.Identifier = fileId;
            }
            else
            {
                // If there is an existing PhysicalInstance, update it from the data file.
                var updateCommand = new UpdatePhysicalInstanceFromFile();
                updateCommand.DataImporters = new List <IDataImporter>();
                updateCommand.DataImporters.Add(new SpssImporter());
                updateCommand.DataImporters.Add(new StataImporter());
                updateCommand.DataImporters.Add(new SasImporter());
                updateCommand.DataImporters.Add(new RDataImporter());
                updateCommand.DataImporters.Add(new CsvImporter());


                var context = new Algenta.Colectica.ViewModel.Commands.VersionableCommandContext();

                // Update the PhysicalInstance from the data file.
                var repoNode     = new RepositoryNode(client, null);
                var repoItemNode = new RepositoryItemNode(existingPhysicalInstance.GetMetadata(), repoNode, client);
                context.Node = repoItemNode;
                context.Item = physicalInstance;

                try
                {
                    updateCommand.Execute(context);

                    physicalInstance.Version++;
                    foreach (var item in updateCommand.Result.ModifiedItems)
                    {
                        item.Version++;
                    }
                }
                catch (Exception ex)
                {
                    logger.Error("Problem updating PhysicalInstance.", ex);
                    return("Problem updating PhysicalInstance. " + ex.Message);
                }
            }

            // Calculate summary statistics, for both new and updated files.
            try
            {
                logger.Debug("Calculating summary statistics");
                var calculator = new PhysicalInstanceSummaryStatisticComputer();
                SummaryStatisticsOptions options = new SummaryStatisticsOptions()
                {
                    CalculateQuartiles = true
                };
                var stats = calculator.ComputeStatistics(importer, filePath, physicalInstance,
                                                         physicalInstance.FileStructure.CaseQuantity, options, (percent, message) => { });
                logger.Debug("Done calculating summary statistics");

                if (stats != null)
                {
                    physicalInstance.Statistics.Clear();
                    foreach (VariableStatistic stat in stats)
                    {
                        physicalInstance.Statistics.Add(stat);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error("Problem calculating summary statistics.", ex);
                return("Problem calculating summary statistics. " + ex.Message);
            }


            // Register all items that were created with the repository.
            DirtyItemGatherer visitor = new DirtyItemGatherer(false, true);

            physicalInstance.Accept(visitor);

            logger.Debug("Setting agency IDs");

            // The static default agency id is not thread safe, so set it explicitly here
            foreach (var item in visitor.DirtyItems)
            {
                item.AgencyId = agencyId;
            }

            logger.Debug("Done setting agency IDs");
            logger.Debug("Registering items with the repository");

            var repoOptions = new Algenta.Colectica.Model.Repository.CommitOptions();

            repoOptions.NamedOptions.Add("RegisterOrReplace");
            client.RegisterItems(visitor.DirtyItems, repoOptions);

            logger.Debug("Done registering items with the repository");
            logger.Debug("Done with CreatePhysicalInstanceForFile");

            return(string.Empty);
        }
Esempio n. 11
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);
            }
        }