예제 #1
0
        private Tuple <long, long> ProcessResults(
            ObservableCollection <OnlineResultItem> results,
            string json,
            OnlineQuery query)
        {
            long num  = -1;
            long next = -1;

            if (string.IsNullOrEmpty(json))
            {
                return(new Tuple <long, long>(num, next));
            }

            //process the query results
            dynamic queryResults = JObject.Parse(json);

            if (queryResults.error != null)
            {
                //there was an error in the query
                return(new Tuple <long, long>(num, next));
            }

            long numberOfTotalItems = queryResults.total.Value;
            int  count = 0;

            if (numberOfTotalItems > 0)
            {
                //these are the results
                List <dynamic> userItemResults = new List <dynamic>();
                // store the results in the list
                userItemResults.AddRange(queryResults.results);
                foreach (dynamic item in userItemResults)
                {
                    count++;
                    OnlineResultItem ri = new OnlineResultItem();
                    ri.Id    = item.id;
                    ri.Title = item.title ?? String.Format("Item {0}", count);
                    ri.Name  = item.name;
                    //ri.Snippet = item.snippet ?? "no snippet";
                    ri.Url = item.url ?? "";
                    string thumb = item.thumbnail ?? "";
                    string s     = item.snippet;
                    string t     = item.type;
                    string a     = item.access;
                    ri.Configure(query.URL, ri.Id, thumb, s, t, a);
                    results.Add(ri);
                }
                num  = queryResults.num.Value;
                next = queryResults.nextStart.Value;
            }
            return(new Tuple <long, long>(num, next));
        }
예제 #2
0
        private Task <Tuple <long, long> > ProcessResultsAsync(ObservableCollection <OnlineResultItem> results, string json, OnlineQuery query)
        {
            //do this in the background
            return(Task.Run(() => {
                long num = -1;
                long next = -1;
                if (json.IsEmpty())
                {
                    return new Tuple <long, long>(num, next);
                }

                //process the query results
                dynamic queryResults = JObject.Parse(json);
                if (queryResults.error != null)
                {
                    //there was an error in the query
                    return new Tuple <long, long>(num, next);
                }

                long numberOfTotalItems = queryResults.total.Value;
                int count = 0;

                if (numberOfTotalItems > 0)
                {
                    //these are the results
                    List <dynamic> userItemResults = new List <dynamic>();
                    // store the results in the list
                    userItemResults.AddRange(queryResults.results);
                    foreach (dynamic item in userItemResults)
                    {
                        count++;
                        OnlineResultItem ri = new OnlineResultItem();
                        ri.Id = item.id;
                        ri.Title = item.title ?? String.Format("Item {0}", count);
                        ri.Snippet = item.snippet ?? "no snippet";
                        ri.Url = item.url ?? "";
                        string thumb = item.thumbnail ?? "";
                        ri.SetThumbnailURL(query.Portal, ri.Id, thumb);
                        results.Add(ri);
                    }
                    num = queryResults.num.Value;
                    next = queryResults.nextStart.Value;
                }
                return new Tuple <long, long>(num, next);
            }));
        }
        protected internal async virtual void ExecuteItemAction(string url)
        {
            _result = _results.FirstOrDefault(ri => ri.Id == url);
            if (_result == null)
            {
                return;
            }

            //Either we open a project and then create the item or
            //we download the item and then create a project from it.
            //MapPackage is a special case. We download it, create
            //a project and then add the map package to it.
            switch (_result.OnlineItemType)
            {
            case OnlineItemType.WebMap:
            {
                await Project.CreateAsync(new CreateProjectSettings()
                    {
                        Name = System.IO.Path.GetFileNameWithoutExtension(_result.Name)
                    });

                var currentItem = ItemFactory.Instance.Create(_result.Id, ItemFactory.ItemType.PortalItem);
                if (MapFactory.Instance.CanCreateMapFrom(currentItem))
                {
                    await QueuedTask.Run(() =>
                        {
                            var newMap = MapFactory.Instance.CreateMapFromItem(currentItem);
                            FrameworkApplication.Panes.CreateMapPaneAsync(newMap);
                        });
                }
            }
            break;

            case OnlineItemType.Layer:
            {
                var ps = new CreateProjectSettings()
                {
                    Name         = System.IO.Path.GetFileNameWithoutExtension(_result.Name),
                    LocationPath = ConfigWithStartWizardModule.DefaultFolder(),
                    TemplatePath = ConfigWithStartWizardModule.GetDefaultTemplates()[0] //Scene
                };
                _eventToken = ArcGIS.Desktop.Mapping.Events.MapViewInitializedEvent.Subscribe((args) =>
                    {
                        Item currentItem = ItemFactory.Instance.Create(_result.Id, ItemFactory.ItemType.PortalItem);
                        if (LayerFactory.Instance.CanCreateLayerFrom(currentItem))
                        {
                            QueuedTask.Run(() => LayerFactory.Instance.CreateLayer(currentItem, args.MapView.Map));
                        }
                        ArcGIS.Desktop.Mapping.Events.MapViewInitializedEvent.Unsubscribe(_eventToken);
                        _eventToken = null;
                    });
                try
                {
                    await Project.CreateAsync(ps);
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.ToString());
                }
            }
            break;

            default:
                var query = new OnlineQuery(_activePortalUri);
                query.ConfigureData("", _result.Id, _result.Name);

                await new SubmitQuery().DownloadFileAsync(query);

                //Project package
                if (_result.OnlineItemType == OnlineItemType.ProjectPackage)
                {
                    await Project.OpenAsync(query.DownloadFileName);
                }
                //Project Template
                else if (_result.OnlineItemType == OnlineItemType.Template)
                {
                    var ps = new CreateProjectSettings()
                    {
                        Name         = System.IO.Path.GetFileNameWithoutExtension(_result.Name),
                        LocationPath = ConfigWithStartWizardModule.DefaultFolder(),
                        TemplatePath = query.DownloadFileName
                    };
                    try
                    {
                        await Project.CreateAsync(ps);
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex.ToString());
                    }
                }
                //Map Package
                else if (_result.OnlineItemType == OnlineItemType.MapPackage)
                {
                    await Project.CreateAsync(new CreateProjectSettings()
                    {
                        Name = System.IO.Path.GetFileNameWithoutExtension(_result.Name)
                    });

                    try
                    {
                        var mapPackage = ItemFactory.Instance.Create(query.DownloadFileName);
                        MapFactory.Instance.CreateMapFromItem(mapPackage);
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(ex.ToString());
                    }
                }
                break;
            }
        }