// Add category and childrens in a graph to the database
        public async Task <Boolean> AddCategory(Graph g, Category broader)
        {
            IUriNode dcatDataset = g.CreateUriNode("skos:Concept");

            String[] datasetUri = findSubjectUri(g, dcatDataset).Split(",");
            // From the dataset uri make a dictionary with the attributes
            Dictionary <string, string> attributes = getAttributesFromSubject(g, datasetUri[0]);

            String[] prefLabels = attributes.GetValueOrDefault("prefLabel", "").Split(",");

            Category category = new Category {
                Name    = prefLabels[0],
                Broader = broader
            };

            await _categoryRepository.AddAsync(category);

            await _unitOfWork.CompleteAsync();

            String[] narrowerUrls = attributes.GetValueOrDefault("narrower", "").Split(",");
            foreach (String url in narrowerUrls)
            {
                if (!String.IsNullOrEmpty(url))
                {
                    await AddCategory(NetworkHandling.LoadFromUriXml(url), category);
                }
            }
            return(true);
        }
        // Find urls from felleskatalogen with search on "kommune"
        private List <string> findUrlsFromFellesKatalogen(int numberOfDatasets)
        {
            List <string> urls = new List <string>();

            // In order to find the first page with new datasets.
            int startPage = (int)(Math.Floor((double)numberOfAddedDatasets / 10));
            int stopPage  = startPage + (int)(Math.Ceiling((double)numberOfDatasets / 10)) + 1;
            // Find the offset if some has been imported before
            int offset = numberOfAddedDatasets % 10;

            // Find new datasets starting on right page
            JArray hits = new JArray();

            for (int i = startPage; i < stopPage; i++)
            {
                JArray newHits = NetworkHandling.LoadFromUrlJson("https://fellesdatakatalog.digdir.no/api/datasets?q=kommune&page=" + i.ToString());
                foreach (JObject dataset in newHits)
                {
                    hits.Add(dataset);
                }
            }

            // Add the urls to the result and increment the static vaiable number of added datasets
            for (int i = offset; i < Math.Min(numberOfDatasets + offset, hits.Count); i++)
            {
                urls.Add("https://fellesdatakatalog.digdir.no/api/datasets/" + (string)hits[i]["_id"]);
                numberOfAddedDatasets++;
            }

            return(urls);
        }
        // Add concept scheme in a graph to the database
        public async Task <Boolean> AddConceptScheme(Graph g)
        {
            IUriNode dcatDataset = g.CreateUriNode("skos:ConceptScheme");

            String[] datasetUri = findSubjectUri(g, dcatDataset).Split(",");
            // From the dataset uri make a dictionary with the attributes
            Dictionary <string, string> attributes = getAttributesFromSubject(g, datasetUri[0]);

            String[] topConceptUrls = attributes.GetValueOrDefault("hasTopConcept", "").Split(",");

            foreach (String topConceptUrl in topConceptUrls)
            {
                await AddCategory(NetworkHandling.LoadFromUriXml(topConceptUrl), null);
            }
            return(true);
        }
        // TODO: De fra data.norge.no har flere format på en distribution?? Skal vi støtte det? :o
        // RART: Finner ikke kategori kobling i rdfene
        // Import dataset from link containing rdf schema.
        public async Task <DatasetResponse> import(String url, int categoryId)
        {
            Graph g;

            try
            {
                // Guess the url is actually an url.
                if (url.Contains("/"))
                {
                    // If it is in fellesdatakatalog API we need to request with headers to get on rdf format
                    if (url.Contains("fellesdatakatalog.digdir.no"))
                    {
                        g = NetworkHandling.LoadFromUriWithHeadersTurtle(url);
                    }
                    // If the url is directly to the page on data.norge.no fetch with the id
                    else if (url.Contains("data.norge.no"))
                    {
                        g = NetworkHandling.LoadFromUriWithHeadersTurtle("https://fellesdatakatalog.digdir.no/api/datasets/" + url.Substring(url.LastIndexOf("/") + 1));
                    }
                    // Otherwise hope the url is directly to a rdf file location on XML format
                    else
                    {
                        g = NetworkHandling.LoadFromUriXml(url);
                    }
                }
                // Guess it is not an url, and instead ID to some dataset in data.norge.no
                else
                {
                    g = NetworkHandling.LoadFromUriWithHeadersTurtle("https://fellesdatakatalog.digdir.no/api/datasets/" + url);
                }
            }
            catch (Exception ex)
            {
                return(new DatasetResponse($"Invalid import url. {ex.Message}"));
            }

            // Try to parse the dataset and save it in the database
            DatasetResponse dataset = await _graphService.AddDataset(g, categoryId);

            return(dataset);
        }
        // Import categories
        public async Task <Boolean> importCategories(String url)
        {
            Graph g = NetworkHandling.LoadFromUriXml(url);

            return(await _graphService.AddCategory(g, null));
        }