public static QuestionModel BuildStudiesTree(QuestionModel model, List <TreeViewNode> nodes)
        {
            // move
            MultilingualString.CurrentCulture = "en-GB";

            SearchFacet facet = new SearchFacet();

            facet.ItemTypes.Add(DdiItemType.Group);
            facet.ResultOrdering      = SearchResultOrdering.ItemType;
            facet.SearchLatestVersion = true;
            var            client1  = ClientHelper.GetClient();
            SearchResponse response = client1.Search(facet);

            facet.ItemTypes.Add(DdiItemType.StudyUnit);
            // facet.ItemTypes.Add(DdiItemType.VariableGroup);
            facet.ResultOrdering      = SearchResultOrdering.ItemType;
            facet.SearchLatestVersion = true;
            var            client2   = ClientHelper.GetClient();
            SearchResponse allsweeps = client2.Search(facet);

            model.Studies = LoadStudies(model, response);
            List <StudyItem> studies = new List <StudyItem>();
            int i = 1;

            foreach (var result in response.Results)
            {
                nodes.Add(new TreeViewNode {
                    id = result.AgencyId, parent = "#", text = result.DisplayLabel
                });
                studies = BuildSweepsTree(studies, allsweeps, result.DisplayLabel, result.AgencyId, nodes, result.AgencyId);
                i++;
            }
            model.Results = studies;
            return(model);
        }
        private static void TestUserIdSearch()
        {
            RepositoryConnectionInfo info = new RepositoryConnectionInfo();

            info.AuthenticationMethod = RepositoryAuthenticationMethod.Windows;
            info.Url             = "localhost:19893";
            info.TransportMethod = RepositoryTransportMethod.NetTcp;
            WcfRepositoryClient client = new WcfRepositoryClient(info);

            Category c = new Category()
            {
                AgencyId = "int.example"
            };

            c.ItemName.Current    = "Test Category";
            c.Label.Current       = "Test Category Label";
            c.Description.Current = "TestCategoryDesc";
            string userId = Guid.NewGuid().ToString();

            c.UserIds.Add(new UserId("sometype", userId));
            client.RegisterItem(c, new CommitOptions());

            var facet = new SearchFacet();

            facet.SearchTargets.Add(DdiStringType.UserId);
            //facet.SearchTerms.Add("TestCategoryDesc");
            facet.SearchTerms.Add(userId);

            var response = client.Search(facet);

            Console.WriteLine("Found " + response.Results.Count + " items");
        }
        public LoadTVLinking(string _filepath)
        {
            Init(_filepath);
            if (vs == default(VariableScheme))
            {
                var client = Utility.GetClient();
                var facet  = new SearchFacet();
                facet.ItemTypes.Add(DdiItemType.VariableScheme);
                facet.SearchTargets.Add(DdiStringType.Name);
                facet.SearchTerms.Add("Topic Variable Groups");
                facet.SearchLatestVersion = true;
                SearchResponse response = client.Search(facet);

                var graphPopulator = new GraphPopulator(client)
                {
                    ChildProcessing = ChildReferenceProcessing.PopulateLatest,
                };
                //graphPopulator.TypesToPopulate.Add(DdiItemType.Variable);
                graphPopulator.TypesToPopulate.Add(DdiItemType.VariableGroup);

                if (response.Results.Count == 1)
                {
                    vs = client.GetItem(
                        response.Results[0].CompositeId,
                        ChildReferenceProcessing.PopulateLatest) as VariableScheme;
                    vs.Accept(new GraphPopulator(client));
                }
            }
        }
Exemplo n.º 4
0
        public void ItemSearch()
        {
            // Search the entire repository for all variables with the word
            // "age".
            var facet = new SearchFacet();
            facet.ItemTypes.Add(DdiItemType.Variable);
            facet.SearchTerms.Add("age");

            // Perform the search.
            var response = client.Search(facet);

            // Show a summary of the search results. How many items were found
            // and how long the search took.
            Console.WriteLine(string.Format(
                "Displaying {0} of {1} results. Search took {2}.",
                response.ReturnedResults,
                response.TotalResults,
                response.RepositoryTime));

            // Write a line for each result.
            foreach (var result in response.Results)
            {
                Console.WriteLine(result.Label["en-US"]);
            }
        }
        /// <summary>
        /// If you require the entire model to be populated, you can use the set populator to increase efficiency.
        /// </summary>
        public void SearchRetrieveUseSetPopulator()
        {
            var client = GetClient();

            // Create a SearchFacet, which lets us set the parameters of the search.
            SearchFacet facet = new SearchFacet();

            // Find all VariableSchemes in the Repository.
            facet.ItemTypes.Add(DdiItemType.VariableScheme);

            // Submit the search to the Repository.
            SearchResponse response = client.Search(facet);

            if (response.ReturnedResults > 0)
            {
                // The root of the item graph for this example will be a variable scheme
                var variableSchemeId = response.Results[0].CompositeId;

                var variableScheme = client.GetItem(variableSchemeId, ChildReferenceProcessing.Instantiate);

                // The SetPopulator uses only two Repository calls are needed to populate the entire item graph.
                // It will find all the children, children of children, etc within this graph of items, and populate them
                SetPopulator setPopulator = new SetPopulator(client);
                variableScheme.Accept(setPopulator);
            }
        }
Exemplo n.º 6
0
        public void ItemSearch()
        {
            // Search the entire repository for all variables with the word
            // "age".
            var facet = new SearchFacet();

            facet.ItemTypes.Add(DdiItemType.Variable);
            facet.SearchTerms.Add("age");

            // Perform the search.
            var response = client.Search(facet);

            // Show a summary of the search results. How many items were found
            // and how long the search took.
            Console.WriteLine(string.Format(
                                  "Displaying {0} of {1} results. Search took {2}.",
                                  response.ReturnedResults,
                                  response.TotalResults,
                                  response.RepositoryTime));

            // Write a line for each result.
            foreach (var result in response.Results)
            {
                Console.WriteLine(result.Label["en-US"]);
            }
        }
        public ISearchResult Results(SearchParameters sp)
        {
            var client = GetClient();
            var config = GetConfiguration();

            sp.HighlightFields = new List <string>();

            foreach (var field in config.SearchFields.Where(f => f.IsSearchable))
            {
                sp.HighlightFields.Add(field.Name);
            }

            sp.HighlightPreTag  = "<match>";
            sp.HighlightPostTag = "</match>";

            ISearchIndexClient indexClient = client.Indexes.GetClient(config.IndexName);
            var startTime = DateTime.UtcNow;
            var response  = indexClient.Documents.Search(_searchTerm, sp);

            var processStartTime = DateTime.UtcNow;
            var results          = new Models.SearchResult();

            foreach (var result in response.Results)
            {
                var doc = FromDocument(result.Document, result.Score);

                doc.Properties.Add("__match", result.Highlights);
                results.Content.Add(doc);
            }

            if (response.Facets != null)
            {
                foreach (var facet in response.Facets)
                {
                    var searchFacet = new SearchFacet()
                    {
                        Name  = facet.Key,
                        Items = facet.Value.Select(x => new KeyValuePair <string, long>(x.Value.ToString(), x.Count.HasValue ? x.Count.Value : 0))
                    };

                    results.Facets.Add(searchFacet);
                }
            }

            if (response.Count != null)
            {
                results.Count = (int)response.Count;
            }

            if (config.LogSearchPerformance)
            {
                string lb = Environment.NewLine;
                Log.Info($"AzureSearch Log (cached client){lb} - Response Duration: {(int)(processStartTime - startTime).TotalMilliseconds}ms{lb} - Process Duration: {(int)(DateTime.UtcNow - processStartTime).TotalMilliseconds}ms{lb} - Results Count: {results.Count}{lb} - Origin: {HttpContext.Current?.Request?.Url}{lb} - Index name: {config.IndexName}{lb} - Base uri: {indexClient.BaseUri}{lb} - Search term: {_searchTerm}{lb} - Uri query string: {HttpUtility.UrlDecode(sp.ToString())}{lb}");
            }
            return(results);
        }
        /// <summary>
        /// Search for some items, retrieve one, and populate its children.
        /// </summary>
        public void SearchRetrievePopulate()
        {
            var client = GetClient();

            // Create a SearchFacet, which lets us set the parameters of the search.
            SearchFacet facet = new SearchFacet();

            // Find all VariableSchemes in the Repository.
            facet.ItemTypes.Add(DdiItemType.VariableScheme);

            // If we wanted, we could search for certain text.
            //facet.SearchTerms.Add("Variables");

            // Submit the search to the Repository.
            SearchResponse response = client.Search(facet);

            if (response.ReturnedResults > 0)
            {
                // Use GetItem to retrieve the first VariableScheme in the search results.
                // By passing in Populate as the last parameter, we are requesting the
                // Repository to send back the VariableScheme along with all child Variables information.
                // By default, the Repository would only Instantiate the child Variables, meaning
                // they would contain identification information, but all other properties would be blank.
                var item = client.GetItem(response.Results[0].CompositeId, ChildReferenceProcessing.Populate);

                // You can use the IsPopulated property to determine whether a particular has its
                // information present, or if only the identification is present.
                Console.WriteLine("Is the item populated? " + item.IsPopulated);

                var unpopulatedVariableScheme = client.GetItem(
                    response.Results[0].CompositeId, ChildReferenceProcessing.Instantiate)
                                                as VariableScheme;

                Console.WriteLine("Is the first Variable populated? " + unpopulatedVariableScheme.Variables[0].IsPopulated);

                // If the child Variables are not populated, we can manually request them to be
                // populated using the PopulateItem method.
                foreach (var variable in unpopulatedVariableScheme.Variables)
                {
                    // Check to make sure this variable isn't already populated. We don't want to
                    // populate an item more than once.
                    if (!variable.IsPopulated)
                    {
                        client.PopulateItem(variable, false, ChildReferenceProcessing.Populate);
                    }
                }

                // As another alternative, you can fully populate all an item's children
                // (and their children) by using the GraphPopulator. This object will
                // visit every child item and call PopulateItem for that item.
                GraphPopulator populator = new GraphPopulator(client);
                unpopulatedVariableScheme.Accept(populator);
            }
        }
Exemplo n.º 9
0
        private ISearchResult Results(SearchParameters sp)
        {
            var profiler = MiniProfiler.Current;

            using (profiler.Step($"Calling Results(SearchParameters sp)"))
            {
                //client = GetClient();

                //var config = GetConfiguration();


                //indexClient = client.Indexes.GetClient(config.IndexName);


                var startTime = DateTime.UtcNow;


                var response = _indexClient.Documents.Search(_searchTerm, sp);


                var processStartTime = DateTime.UtcNow;
                var results          = new Models.SearchResult();

                foreach (var result in response.Results)
                {
                    results.Content.Add(FromDocument(result.Document, result.Score));
                }

                if (response.Facets != null)
                {
                    foreach (var facet in response.Facets)
                    {
                        var searchFacet = new SearchFacet()
                        {
                            Name  = facet.Key,
                            Items = facet.Value.Select(x =>
                                                       new KeyValuePair <string, long>(x.Value.ToString(),
                                                                                       x.Count.HasValue ? x.Count.Value : 0))
                        };

                        results.Facets.Add(searchFacet);
                    }
                }

                if (response.Count != null)
                {
                    results.Count = (int)response.Count;
                }


                return(results);
            }
        }
Exemplo n.º 10
0
        //
        // GET: /Home/

        public ActionResult Index()
        {
            // Since all the information in the sample Repository is
            // in en-US, we can set the CurrentCulture here and
            // use MultilingualString's Current property to access
            // the text.
            //
            // If your data has multiple languages, you may want to
            // access those specific languages instead.
            MultilingualString.CurrentCulture = "en-US";

            // Create a new SearchFacet that will find all
            // StudyUnits, CodeSchemes, and CategorySchemes.
            SearchFacet facet = new SearchFacet();

            facet.ItemTypes.Add(DdiItemType.StudyUnit);
            //facet.ItemTypes.Add(DdiItemType.CodeScheme);
            //facet.ItemTypes.Add(DdiItemType.CategoryScheme);

            // Set the sort order of the results. Options are
            // Alphabetical, ItemType, MetadataRank, and VersionDate.
            facet.ResultOrdering = SearchResultOrdering.ItemType;

            // Add SearchTerms to the facet to only return results that contain the specified text.
            //facet.SearchTerms.Add("isco");

            // Add Cultures to only search for text in certain languages.
            //facet.Cultures.Add("da-DK");

            // Use MaxResults and ResultOffset to implement paging, if large numbers of items may be returned.
            //facet.MaxResults = 100;
            //facet.ResultOffset = 0;

            // Now that we have a facet, search for the items in the Repository.
            // The client object takes care of making the Web Services calls.
            var            client   = ClientHelper.GetClient();
            SearchResponse response = client.Search(facet);

            // Create the model object, and add all the search results to
            // the model's list of results so they can be displayed.
            HomeModel model = new HomeModel();

            foreach (var result in response.Results)
            {
                model.Results.Add(result);
            }

            // Return the view, passing in the model.
            return(View(model));
        }
Exemplo n.º 11
0
        //
        // GET: /Home/
        public ActionResult Index()
        {
            // Since all the information in the sample Repository is
            // in en-US, we can set the CurrentCulture here and
            // use MultilingualString's Current property to access
            // the text.
            //
            // If your data has multiple languages, you may want to
            // access those specific languages instead.
            MultilingualString.CurrentCulture = "en-US";

            // Create a new SearchFacet that will find all
            // StudyUnits, CodeSchemes, and CategorySchemes.
            SearchFacet facet = new SearchFacet();
            facet.ItemTypes.Add(DdiItemType.StudyUnit);
            //facet.ItemTypes.Add(DdiItemType.CodeScheme);
            //facet.ItemTypes.Add(DdiItemType.CategoryScheme);

            // Set the sort order of the results. Options are
            // Alphabetical, ItemType, MetadataRank, and VersionDate.
            facet.ResultOrdering = SearchResultOrdering.ItemType;

            // Add SearchTerms to the facet to only return results that contain the specified text.
            //facet.SearchTerms.Add("isco");

            // Add Cultures to only search for text in certain languages.
            //facet.Cultures.Add("da-DK");

            // Use MaxResults and ResultOffset to implement paging, if large numbers of items may be returned.
            //facet.MaxResults = 100;
            //facet.ResultOffset = 0;

            // Now that we have a facet, search for the items in the Repository.
            // The client object takes care of making the Web Services calls.
            var client = ClientHelper.GetClient();
            SearchResponse response = client.Search(facet);

            // Create the model object, and add all the search results to
            // the model's list of results so they can be displayed.
            HomeModel model = new HomeModel();
            foreach (var result in response.Results)
            {
                model.Results.Add(result);
            }

            // Return the view, passing in the model.
            return View(model);
        }
        public static EquivalenceModel GetStudies(EquivalenceModel model, string agency)
        {
            // move
            MultilingualString.CurrentCulture = "en-GB";

            SearchFacet facet = new SearchFacet();

            facet.ItemTypes.Add(DdiItemType.Group);
            facet.ResultOrdering      = SearchResultOrdering.ItemType;
            facet.SearchLatestVersion = true;
            var            client1  = ClientHelper.GetClient();
            SearchResponse response = client1.Search(facet);

            facet.ItemTypes.Add(DdiItemType.StudyUnit);
            facet.ResultOrdering      = SearchResultOrdering.ItemType;
            facet.SearchLatestVersion = true;
            var            client2   = ClientHelper.GetClient();
            SearchResponse allsweeps = client2.Search(facet);

            model.Studies = LoadStudies(model, response);
            List <StudyItem> studies = new List <StudyItem>();
            string           study   = null;

            if (agency != null)
            {
                foreach (var result in response.Results)
                {
                    if (result.AgencyId == agency)
                    {
                        study = result.DisplayLabel;
                    }
                }
                studies = GetSweeps(studies, allsweeps, study, agency);
            }
            else
            {
                foreach (var result in response.Results)
                {
                    studies = GetSweeps(studies, allsweeps, result.DisplayLabel, result.AgencyId);
                }
            }
            model.Results = studies;
            return(model);
        }
Exemplo n.º 13
0
        public static List <SearchResult> GetAllPhysicalInstance(string agency = null)
        {
            SearchFacet facet = new SearchFacet();

            facet.ItemTypes.Add(new Guid("A51E85BB-6259-4488-8DF2-F08CB43485F8"));


            facet.ResultOrdering      = SearchResultOrdering.Alphabetical;
            facet.SearchLatestVersion = true;


            var            client   = ClientHelper.GetClient();
            SearchResponse response = client.Search(facet);

            var Results = response.Results.OrderBy(a => a.VersionDate).ToList();

            Results = Results.Where(x => x.AgencyId == agency).ToList();
            return(Results);
        }
        public List <SearchResult> GetRepository(Guid itemType)
        {
            DateTime start, finish;

            MultilingualString.CurrentCulture = "en-US";

            // Create a new SearchFacet that will find all
            // StudyUnits, CodeSchemes, and CategorySchemes.

            SearchFacet facet = new SearchFacet();

            facet.ItemTypes.Add(itemType);


            facet.ResultOrdering      = SearchResultOrdering.ItemType;
            facet.SearchLatestVersion = true;

            // Add SearchTerms to the facet to only return results that contain the specified text.

            // Add Cultures to only search for text in certain languages.
            //facet.Cultures.Add("da-DK");

            // Use MaxResults and ResultOffset to implement paging, if large numbers of items may be returned.
            //facet.MaxResults = pageSize;
            //facet.ResultOffset = (pageSize * page);

            // Now that we have a facet, search for the items in the Repository.
            // The client object takes care of making the Web Services calls.
            start = DateTime.Now;
            var            client   = ClientHelper.GetClient();
            SearchResponse response = client.Search(facet);
            // Create the model object, and add all the search results to
            // the model's list of results so they can be displayed.)
            IEnumerable <SearchResult> results  = response.Results;
            List <SearchResult>        results1 = new List <SearchResult>();

            results = results.ToList();

            return(results.ToList());
        }
        /// <summary>
        /// If you require the entire model to be populated, you can use the set populator to increase efficiency.
        /// </summary>
        public void SearchRetrieveAllItems()
        {
            var client = GetClient();

            // Create a SearchFacet, which lets us set the parameters of the search.
            SearchFacet facet = new SearchFacet();

            // Find all VariableSchemes in the Repository.
            facet.ItemTypes.Add(DdiItemType.VariableScheme);

            // Submit the search to the Repository.
            SearchResponse response = client.Search(facet);

            if (response.ReturnedResults > 0)
            {
                // The root of the item graph for this example will be a variable scheme
                var variableSchemeId = response.Results[0].CompositeId;

                // GetSet will find all the children, children of children, etc within this graph of items, and populate them
                var itemGraphIds   = client.GetSet(variableSchemeId);
                var itemGraphItems = client.GetItems(itemGraphIds);
            }
        }
        public static List <TreeViewNode> BuildStudiesNodes(List <TreeViewNode> nodes)
        {
            MultilingualString.CurrentCulture = "en-GB";

            SearchFacet facet = new SearchFacet();

            facet.ItemTypes.Add(DdiItemType.Group);
            facet.ResultOrdering      = SearchResultOrdering.ItemType;
            facet.SearchLatestVersion = true;
            var            client1  = ClientHelper.GetClient();
            SearchResponse response = client1.Search(facet);

            facet.ItemTypes.Add(DdiItemType.StudyUnit);
            facet.ResultOrdering      = SearchResultOrdering.ItemType;
            facet.SearchLatestVersion = true;
            var            client2   = ClientHelper.GetClient();
            SearchResponse allsweeps = client2.Search(facet);
            // model.Studies = LoadStudies(response);
            List <StudyItem> studies = new List <StudyItem>();
            int i = 1;

            foreach (var result in response.Results)
            {
                State cstate = new State();
                cstate.selected = false;

                nodes.Add(new TreeViewNode {
                    id = result.AgencyId, parent = "#", text = result.DisplayLabel, state = cstate
                });
                studies = BuildSweepsNodes(studies, allsweeps, result.DisplayLabel, result.AgencyId, nodes, result.AgencyId);
                i++;
            }

            // model.Results = studies;
            return(nodes);
        }
        public static IHtmlContent LinkForSearchFacet(this IHtmlHelper html, SearchRequest request, SearchFacet facet, SearchFacetValue facetValue)
        {
            var routeValues = request.Clone();
            var linkText    = $"{facetValue.DisplayName} ({facetValue.Count})";

            if (string.Equals(facet.FieldName, nameof(Concert.Price), StringComparison.OrdinalIgnoreCase))
            {
                if (!string.IsNullOrWhiteSpace(routeValues.PriceRange))
                {
                    routeValues.PriceRange = null;
                    linkText = "[X] " + linkText;
                }
                else
                {
                    routeValues.PriceRange = facetValue.Value;
                }
            }
            else if (string.Equals(facet.FieldName, nameof(Concert.Genre), StringComparison.OrdinalIgnoreCase))
            {
                if (!string.IsNullOrWhiteSpace(routeValues.Genre))
                {
                    routeValues.Genre = null;
                    linkText          = "[X] " + linkText;
                }
                else
                {
                    routeValues.Genre = facetValue.Value;
                }
            }
            else if (string.Equals(facet.FieldName, nameof(Concert.Location), StringComparison.OrdinalIgnoreCase))
            {
                if (!string.IsNullOrWhiteSpace(routeValues.Location))
                {
                    routeValues.Location = null;
                    linkText             = "[X] " + linkText;
                }
                else
                {
                    routeValues.Location = facetValue.Value;
                }
            }
            return(html.ActionLink(linkText, "Search", "Concert", null, null, null, routeValues, null));
        }
Exemplo n.º 18
0
        private void CreateQuestionGroups()
        {
            //Get concepts that are used, add the implicit ones and except for "0" create question groups from them in the relevant question scheme and put into the working set
            this.usedConcepts = questionsConcepts.Values.Distinct().ToList();

            //add implicit level-1 groups that are parents of level-2 groups
            var implicits = new List <string>();

            foreach (var uc in this.usedConcepts)
            {
                if (uc.Length == 1 + 2 + 2)
                {
                    if (!usedConcepts.Contains(uc.Substring(0, 3)))
                    {
                        implicits.Add(uc.Substring(0, 3));
                    }
                }
            }
            this.usedConcepts.AddRange(implicits.Distinct());

            //get the concept scheme from the repository
            //I assume there is only one
            //if there is none, get concepts from the working set
            ConceptScheme qcgConceptScheme = new ConceptScheme();
            var           client           = Utility.GetClient();
            var           facet            = new SearchFacet();

            facet.ItemTypes.Add(DdiItemType.ConceptScheme);
            SearchResponse response = client.Search(facet);
            bool           fromRepo = false;

            if (response.ReturnedResults > 0)
            {
                fromRepo         = true;
                qcgConceptScheme = client.GetItem(response.Results[0].CompositeId, ChildReferenceProcessing.Populate) as ConceptScheme;
            }

            var controlConstructScheme = WorkingSet.OfType <ControlConstructScheme>().Where(x => string.Compare(x.ItemName.Best, this.qcsName, ignoreCase: true) == 0).First();

            foreach (var uc in this.usedConcepts)
            {
                if (uc != "0")
                {
                    ControlConstructGroup qcg = new ControlConstructGroup();
                    qcg.TypeOfGroup = "ConceptGroup";
                    Concept qcgConcept = new Concept();
                    if (fromRepo)
                    {
                        qcgConcept = qcgConceptScheme.Concepts.Where(x => string.Compare(x.ItemName.Best, uc, ignoreCase: true) == 0).First();
                    }
                    else    //from working set
                    {
                        qcgConcept = WorkingSet.OfType <Concept>().Where(x => string.Compare(x.ItemName.Best, uc, ignoreCase: true) == 0).First();
                    }
                    qcg.Concept = qcgConcept;
                    qcg.ItemName.Add("en-GB", "Question Construct Group - " + qcgConcept.Label.Best);
                    //Trace.WriteLine("   " + qcg.ItemName.Best);
                    controlConstructScheme.ControlConstructGroups.Add(qcg);
                }
            }
            WorkingSet.AddRange(controlConstructScheme.ControlConstructGroups);
            Trace.WriteLine("  question construct groups: " + controlConstructScheme.ControlConstructGroups.Count().ToString() + " for " + this.qcsName);
        }
Exemplo n.º 19
0
        private void CreateVariableGroups()
        {
            //Get concepts that are used, add the implicit ones and except for "0" create variable groups from them in the relevant variable scheme and put into the working set
             this.usedConcepts = variablesConcepts.Values.Distinct().ToList();

             //add implicit level-1 groups that are parents of level-2 groups
             var implicits = new List<string>();
             foreach (var uc in this.usedConcepts)
             {
                 if (uc.Length == 1 + 2 + 2)
                 {
                     if (!usedConcepts.Contains(uc.Substring(0, 3)))
                     {
                         implicits.Add(uc.Substring(0,3));
                     }
                 }
             }
             this.usedConcepts.AddRange(implicits.Distinct());

            //get the concept scheme from the repository
            //I assume there is only one
            //if there is none, get concepts from the working set
            ConceptScheme vgConceptScheme = new ConceptScheme();
            var client = Utility.GetClient();
            var facet = new SearchFacet();
            facet.ItemTypes.Add(DdiItemType.ConceptScheme);
            SearchResponse response = client.Search(facet);
            bool fromRepo = false;
            if (response.ReturnedResults > 0)
            {
                fromRepo = true;
                vgConceptScheme = client.GetItem(response.Results[0].CompositeId, ChildReferenceProcessing.Populate) as ConceptScheme;
            }

             var variableScheme = WorkingSet.OfType<VariableScheme>().Where(x => string.Compare(x.ItemName.Best, this.vsName, ignoreCase: true) == 0).First();
             foreach (var uc in this.usedConcepts)
             {
                 if (uc != "0")
                 {
                     VariableGroup vg = new VariableGroup();
                     vg.TypeOfGroup = "ConceptGroup";
                     Concept vgConcept = new Concept();
                     if (fromRepo)
                     {
                         vgConcept = vgConceptScheme.Concepts.Where(x => string.Compare(x.ItemName.Best, uc, ignoreCase: true) == 0).First();
                     }
                     else    //from working set
                     {
                         vgConcept = WorkingSet.OfType<Concept>().Where(x => string.Compare(x.ItemName.Best, uc, ignoreCase: true) == 0).First();
                     }

                     vg.Concept = vgConcept;
                     vg.ItemName.Add("en-GB", "Variable Group - " + vgConcept.Label.Best);
                     //Trace.WriteLine("   " + vg.ItemName.Best);
                     variableScheme.VariableGroups.Add(vg);
                 }
             }
             WorkingSet.AddRange(variableScheme.VariableGroups);
             Trace.WriteLine("  concept groups: " + variableScheme.VariableGroups.Count().ToString() + " for " + this.vsName);
        }
Exemplo n.º 20
0
        // save
        public ActionResult List(ItemTypesModel model, string agency = null, string id = null, long version = 0)
        {
            if (agency != null)
            {
                model.ItemType = "Question Group";
                var clientitem = ClientHelper.GetClient();
                var item1      = clientitem.GetRepositoryItem(new Guid(id), agency, version);
            }

            model.ItemTypes = GetItemTypes();

            // Since all the information in the sample Repository is
            // in en-US, we can set the CurrentCulture here and
            // use MultilingualString's Current property to access
            // the text.
            //
            // If your data has multiple languages, you may want to
            // access those specific languages instead.
            MultilingualString.CurrentCulture = "en-GB";

            // Create a new SearchFacet that will find all
            // StudyUnits, CodeSchemes, and CategorySchemes.

            SearchFacet facet = new SearchFacet();

            switch (model.ItemType)
            {
            case "Action":
                facet.ItemTypes.Add(DdiItemType.Archive);
                break;

            case "Archive":
                facet.ItemTypes.Add(DdiItemType.Archive);
                break;

            case "Category":
                facet.ItemTypes.Add(DdiItemType.Category);
                break;

            case "Category Group":
                facet.ItemTypes.Add(DdiItemType.CategoryGroup);
                break;

            case "Category Set":
                facet.ItemTypes.Add(DdiItemType.CategoryScheme);
                break;

            case "Code List":
                facet.ItemTypes.Add(DdiItemType.CodeList);
                break;

            case "Code List Group":
                facet.ItemTypes.Add(DdiItemType.CodeListGroup);
                break;

            case "Code List Scheme":
                facet.ItemTypes.Add(DdiItemType.CodeListScheme);
                break;

            case "Code Set":
                facet.ItemTypes.Add(new Guid("8b108ef8-b642-4484-9c49-f88e4bf7cf1d"));
                break;

            case "Concept":
                facet.ItemTypes.Add(DdiItemType.Concept);
                break;

            case "Concept Group":
                facet.ItemTypes.Add(DdiItemType.ConceptGroup);
                break;

            case "Concept Scheme":
                facet.ItemTypes.Add(DdiItemType.ConceptScheme);
                break;

            case "Data Collection":
                facet.ItemTypes.Add(DdiItemType.DataCollection);
                break;

            case "Data File":
                facet.ItemTypes.Add(new Guid("a51e85bb-6259-4488-8df2-f08cb43485f8"));
                break;

            case "Group":
                facet.ItemTypes.Add(DdiItemType.Group);
                break;

            case "Instrument":
                facet.ItemTypes.Add(DdiItemType.Instrument);
                break;

            case "Question Item":
                facet.ItemTypes.Add(DdiItemType.QuestionItem);
                break;

            case "Question Group":
                facet.ItemTypes.Add(DdiItemType.QuestionGroup);
                break;

            case "Question Set":
                facet.ItemTypes.Add(DdiItemType.QuestionScheme);
                break;

            case "Study":
                facet.ItemTypes.Add(DdiItemType.StudyUnit);
                break;

            case "Variable":
                facet.ItemTypes.Add(DdiItemType.Variable);
                break;

            case "Variable Group":
                facet.ItemTypes.Add(DdiItemType.VariableGroup);
                break;

            case "Variable Scheme":
                facet.ItemTypes.Add(DdiItemType.VariableScheme);
                break;

            case "Variable Statistic":
                facet.ItemTypes.Add(DdiItemType.VariableStatistic);
                break;

            default:
                facet.ItemTypes.Add(DdiItemType.Group);
                break;
            }


            // Set the sort order of the results. Options are
            // Alphabetical, ItemType, MetadataRank, and VersionDate.
            facet.ResultOrdering      = SearchResultOrdering.Alphabetical;
            facet.SearchLatestVersion = true;


            // Now that we have a facet, search for the items in the Repository.
            // The client object takes care of making the Web Services calls.
            var            client   = ClientHelper.GetClient();
            SearchResponse response = client.Search(facet);

            // Create the model object, and add all the search results to
            model.Results = response.Results.OrderBy(a => a.VersionDate).ToList();

            // Return the view, passing in the model.
            return(View(model));
        }
        /// <summary>
        /// Search for some items, retrieve one, and populate its children.
        /// </summary>
        public void SearchRetrievePopulate()
        {
            var client = GetClient();

            // Create a SearchFacet, which lets us set the parameters of the search.
            SearchFacet facet = new SearchFacet();

            // Find all VariableSchemes in the Repository.
            facet.ItemTypes.Add(DdiItemType.VariableScheme);

            // If we wanted, we could search for certain text.
            //facet.SearchTerms.Add("Variables");

            // Submit the search to the Repository.
            SearchResponse response = client.Search(facet);

            if (response.ReturnedResults > 0)
            {
                // Use GetItem to retrieve the first VariableScheme in the search results.
                // By passing in Populate as the last parameter, we are requesting the
                // Repository to send back the VariableScheme along with all child Variables information.
                // By default, the Repository would only Instantiate the child Variables, meaning
                // they would contain identification information, but all other properties would be blank.
                var item = client.GetItem(response.Results[0].CompositeId, ChildReferenceProcessing.Populate);

                // You can use the IsPopulated property to determine whether a particular has its
                // information present, or if only the identification is present.
                Console.WriteLine("Is the item populated? " + item.IsPopulated);

                var unpopulatedVariableScheme = client.GetItem(
                    response.Results[0].CompositeId, ChildReferenceProcessing.Instantiate)
                    as VariableScheme;

                Console.WriteLine("Is the first Variable populated? " + unpopulatedVariableScheme.Variables[0].IsPopulated);

                // If the child Variables are not populated, we can manually request them to be
                // populated using the PopulateItem method.
                foreach (var variable in unpopulatedVariableScheme.Variables)
                {
                    // Check to make sure this variable isn't already populated. We don't want to
                    // populate an item more than once.
                    if (!variable.IsPopulated)
                    {
                        client.PopulateItem(variable, false, ChildReferenceProcessing.Populate);
                    }
                }

                // As another alternative, you can fully populate all an item's children
                // (and their children) by using the GraphPopulator. This object will
                // visit every child item and call PopulateItem for that item.
                GraphPopulator populator = new GraphPopulator(client);
                unpopulatedVariableScheme.Accept(populator);
            }
        }
        // Used for uploading of Questions using Question Items. Only to be used for development
        // to use set the itemType to "All" in Equivalences
        public static List <SearchResult> GetRepository(string itemType, string searchTerm, List <Word> equivalences)
        {
            DateTime start, finish;

            MultilingualString.CurrentCulture = "en-GB";

            // Create a new SearchFacet that will find all
            // StudyUnits, CodeSchemes, and CategorySchemes.

            SearchFacet facet = new SearchFacet();

            switch (itemType)
            {
            case "Action":
                facet.ItemTypes.Add(DdiItemType.Archive);
                break;

            case "Archive":
                facet.ItemTypes.Add(DdiItemType.Archive);
                break;

            case "Category":
                facet.ItemTypes.Add(DdiItemType.Category);
                break;

            case "Category Group":
                facet.ItemTypes.Add(DdiItemType.CategoryGroup);
                break;

            case "Category Set":
                facet.ItemTypes.Add(DdiItemType.CategoryScheme);
                break;

            case "Code List":
                facet.ItemTypes.Add(DdiItemType.CodeList);
                break;

            case "Code List Group":
                facet.ItemTypes.Add(DdiItemType.CodeListGroup);
                break;

            case "Code List Scheme":
                facet.ItemTypes.Add(DdiItemType.CodeListScheme);
                break;

            case "Concept":
                facet.ItemTypes.Add(DdiItemType.Concept);
                break;

            case "Concept Group":
                facet.ItemTypes.Add(DdiItemType.ConceptGroup);
                break;

            case "Concept Scheme":
                facet.ItemTypes.Add(DdiItemType.ConceptScheme);
                break;

            case "Data Collection":
                facet.ItemTypes.Add(DdiItemType.DataCollection);
                break;

            case "Group":
                facet.ItemTypes.Add(DdiItemType.Group);
                break;

            case "Instrument":
                facet.ItemTypes.Add(DdiItemType.Instrument);
                break;

            case "Question Item":
                facet.ItemTypes.Add(DdiItemType.QuestionItem);
                break;

            case "Question Group":
                facet.ItemTypes.Add(DdiItemType.QuestionGroup);
                break;

            case "Study":
                facet.ItemTypes.Add(DdiItemType.StudyUnit);
                break;

            case "Variable":
                facet.ItemTypes.Add(DdiItemType.Variable);
                break;

            case "Variable Group":
                facet.ItemTypes.Add(DdiItemType.VariableGroup);
                break;

            case "Variable Scheme":
                facet.ItemTypes.Add(DdiItemType.VariableScheme);
                break;

            case "Variable Statistic":
                facet.ItemTypes.Add(DdiItemType.VariableStatistic);
                break;

            default:
                facet.ItemTypes.Add(DdiItemType.Group);
                break;
            }

            facet.ResultOrdering      = SearchResultOrdering.ItemType;
            facet.SearchLatestVersion = true;

            // Add SearchTerms to the facet to only return results that contain the specified text.
            if (searchTerm != null)
            {
                facet.SearchTerms.Add(searchTerm);
            }

            // Add Cultures to only search for text in certain languages.
            facet.Cultures.Add("en-GB");

            // Use MaxResults and ResultOffset to implement paging, if large numbers of items may be returned.
            //facet.MaxResults = pageSize;
            //facet.ResultOffset = (pageSize * page);

            // Now that we have a facet, search for the items in the Repository.
            // The client object takes care of making the Web Services calls.
            start = DateTime.Now;
            var            client   = ClientHelper.GetClient();
            SearchResponse response = client.Search(facet);
            List <string>  words    = new List <string>();

            foreach (var item in equivalences)
            {
                words.Add(item.Value);
            }
            // Create the model object, and add all the search results to
            // the model's list of results so they can be displayed.)
            IEnumerable <SearchResult> results  = response.Results;
            List <SearchResult>        results1 = new List <SearchResult>();

            results = results.ToList();

            return(results.ToList());
        }
        public void CompareWithRepository()
        {
            var client = Utility.GetClient();

            var facet = new SearchFacet()
            {
                SearchLatestVersion = true
            };

            facet.ItemTypes.Add(DdiItemType.StudyUnit);
            var response = client.Search(facet);

            foreach (var result in response.Results)
            {
                var su = client.GetItem(
                    result.CompositeId,
                    ChildReferenceProcessing.PopulateLatest) as StudyUnit;
                foreach (var dc in su.DataCollections)
                {
                    try
                    {
                        var scope = scopes[dc.ItemName.Best];
                        scope.su = su;
                        scope.rp = su.ResourcePackages.Where(x => x.ItemName.Best == dc.ItemName.Best).FirstOrDefault();
                    } catch (KeyNotFoundException)
                    {
                    }
                }
            }

            foreach (var scope in scopes)
            {
                if (scope.Value.rp != default(ResourcePackage))
                {
                    continue;
                }

                var wsRps = workingSet.OfType <ResourcePackage>().Where(x => string.Compare(
                                                                            x.DublinCoreMetadata.Title.Best, scope.Value.name
                                                                            ) == 0
                                                                        );
                if (wsRps.Any())
                {
                    scope.Value.rp = wsRps.First();
                    var bubbleOut = false;
                    foreach (var g in workingSet.OfType <Algenta.Colectica.Model.Ddi.Group>())
                    {
                        foreach (var su in g.StudyUnits)
                        {
                            if (su.DataCollections.Count(x => x.ItemName.Best == scope.Key) > 0)
                            {
                                scope.Value.su = su;
                                var gatherer = new ItemGathererVisitor();
                                g.Accept(gatherer);
                                toBeAdded.AddRange(gatherer.FoundItems);
                                bubbleOut = true;
                            }
                            if (bubbleOut)
                            {
                                break;
                            }
                        }
                        if (bubbleOut)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    var rp = new ResourcePackage();
                    rp.DublinCoreMetadata.Title["en-GB"] = scope.Key;
                    scope.Value.su.AddChild(rp);
                    toBeAdded.Add(scope.Value.su);
                    rp.AddChild(scope.Value.su.DataCollections.First(x => x.ItemName.Best == scope.Key));
                    scope.Value.rp = rp;
                }
            }

            var ccgs = workingSet.OfType <ControlConstructGroup>();

            if (ccgs.Count() > 1)
            {
                facet.ItemTypes.Clear();
                facet.SearchTargets.Clear();
                facet.ItemTypes.Add(DdiItemType.QuestionConstruct);
                facet.SearchTargets.Add(DdiStringType.UserId);
                foreach (var ccg in ccgs)
                {
                    var qcs = ccg.GetChildren().OfType <QuestionActivity>().ToList();
                    foreach (var qc in ccg.GetChildren().OfType <QuestionActivity>())
                    {
                        facet.SearchTerms.Clear();
                        facet.SearchTerms.Add(qc.UserIds.First().Identifier);

                        response = client.Search(facet);
                        if (response.Results.Count > 1)
                        {
                            Console.WriteLine("{0} question constrcuts found during CCG syncing for the question '{1}'", response.Results.Count, qc.UserIds.First().Identifier);
                        }
                        else if (response.Results.Count < 1)
                        {
                            Console.WriteLine("No question constructs were found for the CCG syncing matching '{0}'", qc.UserIds.First().Identifier);
                        }
                        else
                        {
                            var remote_qc = client.GetItem(response.Results.First().CompositeId) as IVersionable;
                            if (remote_qc != default(IVersionable))
                            {
                                ccg.ReplaceChild(qc.CompositeId, remote_qc);
                            }
                        }
                    }
                }
            }

            var vgs = workingSet.OfType <VariableGroup>();

            if (vgs.Count() > 0)
            {
                facet.ItemTypes.Clear();
                facet.ItemTypes.Add(DdiItemType.Variable);
                foreach (var vg in vgs)
                {
                    foreach (var variable in vg.GetChildren().OfType <Variable>())
                    {
                        facet.SearchTerms.Clear();
                        facet.SearchTargets.Clear();
                        bool closer_id_found = false;
                        foreach (var user_id in variable.UserIds)
                        {
                            if (user_id.Type == "closer:id")
                            {
                                closer_id_found = true;
                                facet.SearchTerms.Add(user_id.Identifier);
                                facet.SearchTargets.Add(DdiStringType.UserId);
                                break;
                            }
                        }
                        if (!closer_id_found)
                        {
                            facet.SearchTerms.Add(variable.ItemName.Best);
                            facet.SearchTargets.Add(DdiStringType.Name);
                        }
                        response = client.Search(facet);
                        if (response.Results.Count > 1)
                        {
                            Console.WriteLine("{0} variables found during variable group syncing for the variable '{1}:{2}'", response.Results.Count, name, variable.ItemName.Best);
                        }
                        else if (response.Results.Count < 1)
                        {
                            Console.WriteLine("No variables were found for the variable grouping syncing matching '{0}:{1}'", name, variable.ItemName.Best);
                        }
                        else
                        {
                            var remote_variable = client.GetItem(response.Results.First().CompositeId) as IVersionable;
                            if (remote_variable != default(IVersionable))
                            {
                                vg.ReplaceChild(variable.CompositeId, remote_variable);
                            }
                        }
                    }
                }
            }

            var progress = new ParallelProgressMonitor(scopes.Count);

            Parallel.ForEach <KeyValuePair <string, Scope> >(scopes, scope =>
            {
                string text = String.Format("{0}: Comparing {1}", name, scope.Value.name);
                progress.StartThread(
                    Thread.CurrentThread.ManagedThreadId,
                    text
                    );
                scope.Value.Compare();
                progress.FinishThread(
                    Thread.CurrentThread.ManagedThreadId,
                    text.PadRight(40, '-') +
                    "> done." +
                    String.Format("{0} compared.", scope.Value.counter[Counters.Compared]).PadLeft(16) +
                    String.Format("{0} updated.", scope.Value.counter[Counters.Updated]).PadLeft(16) +
                    String.Format("{0} added.", scope.Value.counter[Counters.Added]).PadLeft(16) +
                    String.Format("{0} removed.", scope.Value.counter[Counters.Removed]).PadLeft(16)
                    );
            });
        }
        public void RunGlobalActions(bool prepare = false)
        {
            bool prepared = false;
            if (prepare)
            {
                prepared = Prepare();
            }
            if (prepare == prepared)
            {
                foreach (var action in actions)
                {
                    try 
                    {
                        action.Validate();
                        workingSet.AddRange(action.Build(workingSet));
                    }
                    catch (Exception e)
                    {
                        console.WriteLine("{0}", e.Message);
                        continue;
                    }
                }
                var client = Utility.GetClient();
                var facet = new SearchFacet();
                facet.ItemTypes.Add(DdiItemType.DdiInstance);
                facet.SearchTargets.Add(DdiStringType.Name);

                //Compare
                var repoItems = new List<IVersionable>();
                var wsIs = workingSet.OfType<DdiInstance>();
                var countint = wsIs.Count();

                foreach (var wsI in wsIs)
                {
                    facet.SearchTerms.Clear();
                    facet.SearchTerms.Add(wsI.ItemName.Best);
                    var response = client.Search(facet);
                    foreach (var res in response.Results)
                    {
                        var rp = client.GetItem(
                        res.CompositeId,
                        ChildReferenceProcessing.PopulateLatest) as DdiInstance;
                        var graphPopulator = new GraphPopulator(client)
                        {
                            ChildProcessing = ChildReferenceProcessing.PopulateLatest
                        };
                        rp.Accept(graphPopulator);
                        var gatherer = new ItemGathererVisitor();
                        rp.Accept(gatherer);
                        repoItems.AddRange(gatherer.FoundItems);
                    }
                }
                toBeAdded = workingSet;
                var toBeRemoved = new List<IVersionable>();
                foreach (var repoItem in repoItems)
                {
                    if (repoItem.UserIds.Count == 0) continue;
                    var wsItem = workingSet.Find(x => (x.UserIds.Count > 0 ? x.UserIds[0].ToString() : "") == repoItem.UserIds[0].ToString());
                    if (wsItem != default(IVersionable))
                    {
                        if (toBeAdded.IndexOf(wsItem) != -1)
                        {
                            toBeAdded.Remove(wsItem);
                        }
                        else
                        {
                            var node = toBeAdded.Where(
                                item => item.UserIds.Count > 0
                            ).FirstOrDefault(
                                item => item.UserIds[0] == wsItem.UserIds[0]
                            );
                            if (node != default(IVersionable))
                            {
                                toBeAdded.Remove(node);
                            }
                        }
                    }
                    else
                    {
                        toBeRemoved.Add(repoItem);
                    }
                }

                console.WriteLine("Global: Commiting {0} items...", workingSet.Count);
                client.RegisterItems(workingSet, new CommitOptions());
            }
            else
            {
                console.WriteLine("Failed to prepare build.");
            }
            console.Publish();
        }