Example #1
0
        /// <summary>
        /// Creates new instance of SampleModel by deserializing it from the json file provided.
        /// </summary>
        /// <param name="samplePath">Full path to the metadata JSON file of the sample</param>
        /// <returns>Deserialized SampleModel</returns>
        internal static SampleModel Create(string samplePath)
        {
            var metadataStream = SampleManager.Current.GetMetadataManifest(samplePath);

            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(SampleModel));

            SampleModel sampleModel = null;

            // The samplePath is the path specified in the groups.json file for each metadata.json file
            // var assetPath = Path.Combine(samplePath, "metadata.json");
            try
            {
                // TODO: Wondering if we can rework this to not have to open two different MemoryStreams.
                using (Stream stream = metadataStream)
                {
                    if (stream != null)
                    {
                        using (MemoryStream ms = new MemoryStream())
                        {
                            stream.CopyTo(ms);
                            var jsonInBytes = ms.ToArray();

                            using (MemoryStream ms2 = new MemoryStream(jsonInBytes))
                            {
                                sampleModel = serializer.ReadObject(ms2) as SampleModel;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLine(ex.Message);
            }
            return(sampleModel);
        }
 /// <summary>
 /// Check if the sample has a type registered.
 /// </summary>
 /// <param name="sampleModel">SampleModel that is checked.</param>
 /// <returns>Returns true if the type if found. False otherwise.</returns>
 private bool DoesSampleTypeExists(SampleModel sampleModel)
 {
     var fullTypeAsString = string.Format("{0}.{1}", sampleModel.SampleNamespace,
        sampleModel.GetSampleName());
     var sampleType = _samplesAssembly.GetType(fullTypeAsString);
     if (sampleType == null)
         return false;
     return true;
 }
Example #3
0
        /// <summary>
        /// Creates new instance of <see cref="SampleStructureMap"/> by deserializing it from the json file provided.
        /// Returned instance will be fully loaded including other information that is not provided
        /// in the json file like samples.
        /// </summary>
        /// <param name="groupsJSON">Full path to the groups JSON file</param>
        /// <returns>Deserialized <see cref="SampleStructureMap"/></returns>
        internal static SampleStructureMap Create(Stream groupsJSON)
        {
            var serializer = new DataContractJsonSerializer(typeof(SampleStructureMap));

            SampleStructureMap structureMap = null;

            try
            {
                // KD - Need two MemoryStreams? Need to investigate. Has to do with needing to open the json from the Android
                // Activity which gives you a stream. Then you need to get back to bytes.
                using (groupsJSON)
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        groupsJSON.CopyTo(ms);
                        var jsonInBytes = ms.ToArray();

                        using (MemoryStream ms2 = new MemoryStream(jsonInBytes))
                        {
                            structureMap         = serializer.ReadObject(ms2) as SampleStructureMap;
                            structureMap.Samples = new List <SampleModel>();
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                Logger.WriteLine(ex.Message);
            }

            #region CreateSamples

            List <string> pathList = new List <string>();
            foreach (var category in structureMap.Categories)
            {
                foreach (var subCategory in category.SubCategories)
                {
                    if (subCategory.SampleInfos != null)
                    {
                        foreach (var sample in subCategory.SampleInfos)
                        {
                            pathList.Add(sample.Path.Replace("/", "."));
                        }
                    }
                }
            }

            var sampleModel = new SampleModel();
            foreach (var samplePath in pathList)
            {
                sampleModel = SampleModel.Create(samplePath);
                if (sampleModel != null)
                {
                    structureMap.Samples.Add(sampleModel);
                }
            }

            foreach (var category in structureMap.Categories)
            {
                foreach (var subCategory in category.SubCategories)
                {
                    if (subCategory.Samples == null)
                    {
                        subCategory.Samples = new List <SampleModel>();
                    }

                    foreach (var sampleName in subCategory.SampleInfos)
                    {
                        var sample = structureMap.Samples.FirstOrDefault(x => x.SampleName == sampleName.SampleName);

                        if (sample == null)
                        {
                            continue;
                        }

                        subCategory.Samples.Add(sample);
                    }
                }
            }

            #endregion

            return(structureMap);
        }
        /// <summary>
        /// Gets sample by it's name.
        /// </summary>
        /// <param name="sampleName">The name of the sample.</param>
        /// <returns>Return <see cref="SampleModel"/> for the sample if found. Null if sample not found.</returns>
        //public SampleModel GetSampleByName(string sampleName)
        //{
        //    List<SampleModel> sampleList = new List<SampleModel>();

        //    foreach (var category in Categories)
        //    {
        //        foreach (var subCategory in category.SubCategories)
        //        {
        //            // Changed to use the SampleInfo class, but that means you have to manually create a list of the SampleModel items.
        //            foreach (var item in subCategory.SampleInfo)
        //            {
        //               // sampleList.Add(item.Sample);
        //            }
        //            var result = sampleList.FirstOrDefault(x => x.SampleName == sampleName);
        //            if (result != null)
        //                return result;
        //        }
        //    }
        //    return null;
        //}

        #region Factory methods
        /// <summary>
        /// Creates new instance of <see cref="SampleStructureMap"/> by deserializing it from the json file provided.
        /// Returned instance will be fully loaded including other information that is not provided
        /// in the json file like samples.
        /// </summary>
        /// <param name="groupsJSON">Full path to the groups JSON file</param>
        /// <returns>Deserialized <see cref="SampleStructureMap"/></returns>
        internal static SampleStructureMap Create(Stream groupsJSON)
        {
            var serializer = new DataContractJsonSerializer(typeof(SampleStructureMap));

            SampleStructureMap structureMap = null;

            try
            {
                // KD - Need two MemoryStreams? Need to investigate.
                using (groupsJSON)
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        groupsJSON.CopyTo(ms);
                        var jsonInBytes = ms.ToArray();

                        using (MemoryStream ms2 = new MemoryStream(jsonInBytes))
                        {
                            structureMap         = serializer.ReadObject(ms2) as SampleStructureMap;
                            structureMap.Samples = new List <SampleModel>();
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            #region CreateSamples
            //TODO: This part basically works, but needs some review, particularly once we add
            // Tutorials and Workflows and such.

            // Create all samples and add them to the groups since they are not part of
            // main configuration file

            List <string> pathList = new List <string>();
            foreach (var category in structureMap.Categories)
            {
                foreach (var subCategory in category.SubCategories)
                {
                    if (subCategory.SampleInfos != null)
                    {
                        foreach (var sample in subCategory.SampleInfos)
                        {
                            pathList.Add(sample.Path.Replace("/", "."));
                        }
                    }
                }
            }

            SampleModel sampleModel = new SampleModel();
            foreach (var samplePath in pathList)
            {
                sampleModel = SampleModel.Create(samplePath);
                if (sampleModel != null)
                {
                    structureMap.Samples.Add(sampleModel);
                }
            }

            var addedSamples = new List <SampleModel>();
            foreach (var category in structureMap.Categories)
            {
                foreach (var subCategory in category.SubCategories)
                {
                    if (subCategory.Samples == null)
                    {
                        subCategory.Samples = new List <SampleModel>();
                    }

                    //if (subCategory.SampleNames == null)
                    //    subCategory.SampleNames = new List<string>();

                    foreach (var sampleName in subCategory.SampleInfos)
                    {
                        var sample = structureMap.Samples.FirstOrDefault(x => x.SampleName == sampleName.SampleName);

                        if (sample == null)
                        {
                            continue;
                        }

                        subCategory.Samples.Add(sample);
                        addedSamples.Add(sample);
                    }
                }
            }

            #endregion
            // Create all samples
            //foreach (var sampleGroupFolder in sampleGroupFolders) // ie. Samples\Layers
            //{
            //    // This creates samples from all folders and adds them to the samples list
            //    // This means that sample is created even if it's not defined in the groups list
            //    var sampleFolders = sampleGroupFolder.GetDirectories();
            //    foreach (var sampleFolder in sampleFolders)  // ie. Samples\Layers\ArcGISTiledLayerFromUrl
            //    {
            //        var sampleModel = SampleModel.Create(
            //            Path.Combine(sampleFolder.FullName, "metadata.json"));

            //        if (sampleModel != null)
            //            structureMap.Samples.Add(sampleModel);
            //    }
            //}

            //// Create all tutorials
            //if (tutorialsDirectory.Exists)
            //    foreach (var sampleFolder in tutorialsDirectory.GetDirectories()) // ie. Tutorials\AddMapToApp
            //    {
            //        var sampleModel = SampleModel.Create(
            //            Path.Combine(sampleFolder.FullName, "metadata.json"));

            //        if (sampleModel != null)
            //            structureMap.Samples.Add(sampleModel);
            //    }

            //// Create all workflows
            //if (workflowDirectory.Exists)
            //    foreach (var sampleFolder in workflowDirectory.GetDirectories()) // ie. Workflows\SearchFeatures
            //    {
            //        var sampleModel = SampleModel.Create(
            //            Path.Combine(sampleFolder.FullName, "metadata.json"));

            //        if (sampleModel != null)
            //            structureMap.Samples.Add(sampleModel);
            //    }

            //// Set samples to the sub-categories
            //var addedSamples = new List<SampleModel>();
            //foreach (var cateory in structureMap.Categories)
            //{
            //    foreach (var subCategory in cateory.SubCategories)
            //    {
            //        if (subCategory.Samples == null)
            //            subCategory.Samples = new List<SampleModel>();

            //        if (subCategory.SampleNames == null)
            //            subCategory.SampleNames = new List<string>();

            //        foreach (var sampleName in subCategory.SampleNames)
            //        {
            //            var sample = structureMap.Samples.FirstOrDefault(x => x.SampleName == sampleName);

            //            if (sample == null) continue;

            //            subCategory.Samples.Add(sample);
            //            addedSamples.Add(sample);
            //        }
            //    }
            //}

            //// Add samples that are not defined to the end of the groups
            //var notAddedSamples = structureMap.Samples.Where(x => !addedSamples.Contains(x)).ToList();
            //foreach (var sampleModel in notAddedSamples)
            //{
            //    var category = structureMap.Categories.FirstOrDefault(x => x.CategoryName == sampleModel.Category);
            //    if (category == null)
            //        continue;

            //    var subCategory = category.SubCategories.FirstOrDefault(x => x.SubCategoryName == sampleModel.SubCategory);
            //    if (subCategory != null)
            //    {
            //        subCategory.SampleNames.Add(sampleModel.SampleName);
            //        subCategory.Samples.Add(sampleModel);
            //    }
            //}

            //if (structureMap.Featured == null)
            //    structureMap.Featured = new List<FeaturedModel>();

            //// Set all sample models to the featured models
            //foreach (var featured in structureMap.Featured)
            //{
            //    var sample = structureMap.Samples.FirstOrDefault(x => x.SampleName == featured.SampleName);
            //    if (sample != null)
            //        featured.Sample = sample;
            //}
            //#endregion

            return(structureMap);
        }
        /// <summary>
        /// Creates new instance of <see cref="SampleStructureMap"/> by deserializing it from the json file provided.
        /// Returned instance will be fully loaded including other information that is not provided
        /// in the json file like samples.
        /// </summary>
        /// <param name="groupsJSON">Full path to the groups JSON file</param>
        /// <returns>Deserialized <see cref="SampleStructureMap"/></returns>
        internal static SampleStructureMap Create(Stream groupsJSON)
        {
            var serializer = new DataContractJsonSerializer(typeof(SampleStructureMap));

            SampleStructureMap structureMap = null;

            try
            {
                // KD - Need two MemoryStreams? Need to investigate. 
                using (groupsJSON)
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        groupsJSON.CopyTo(ms);
                        var jsonInBytes = ms.ToArray();

                        using (MemoryStream ms2 = new MemoryStream(jsonInBytes))
                        {
                            structureMap = serializer.ReadObject(ms2) as SampleStructureMap;
                            structureMap.Samples = new List<SampleModel>();
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            #region CreateSamples
   
            List<string> pathList = new List<string>();
            foreach (var category in structureMap.Categories)
            {
                foreach (var subCategory in category.SubCategories)
                {
                    if (subCategory.SampleInfos != null)
                    {
                        foreach (var sample in subCategory.SampleInfos)
                        {
                            pathList.Add(sample.Path.Replace("/","."));
                        }
                    }
                }
            }

            SampleModel sampleModel = new SampleModel();
            foreach (var samplePath in pathList)
            {
                sampleModel = SampleModel.Create(samplePath);
                if (sampleModel != null)
                    structureMap.Samples.Add(sampleModel);
            }

            foreach (var category in structureMap.Categories)
            {
                foreach (var subCategory in category.SubCategories)
                {
                    if (subCategory.Samples == null)
                        subCategory.Samples = new List<SampleModel>();

                    foreach (var sampleName in subCategory.SampleInfos)
                    {
                        var sample = structureMap.Samples.FirstOrDefault(x => x.SampleName == sampleName.SampleName);

                        if (sample == null) continue;

                        subCategory.Samples.Add(sample);
                    }
                }
            }

            #endregion
           
            return structureMap;
        }
Example #6
0
        /// <summary>
        /// Gets the name of C# the xaml file.
        /// </summary>
        //public static string GetSamplesXamlFileName(this SampleModel model)
        //{
        //    return string.Format("{0}.xaml", model.GetSampleName());
        //}

        /// <summary>
        /// Gets the name of the C# code behind file.
        /// </summary>
        //public static string GetSamplesCodeBehindFileName(this SampleModel model)
        //{
        //    return string.Format("{0}.xaml.cs", model.GetSampleName());
        //}

        public static string GetSamplesFileName(this SampleModel model)
        {
            return(string.Format("{0}.cs", model.GetSampleName()));
        }
Example #7
0
 public static string GetSampleName(this SampleModel model)
 {
     return(model.SampleName);
 }