예제 #1
0
        /// <summary>
        /// Displays a list of content types from Kentico Kontent and allows the user to select one
        /// </summary>
        /// <param name="types"></param>
        /// <returns></returns>
        public static ContentType VerifyContentType(DeliveryTypeListingResponse types)
        {
            Console.WriteLine();
            Program.WriteColoredText("Which content type would you like to create? [Enter a number]", ConsoleColor.Green);

            for (int i = 0; i < types.Types.Count; i++)
            {
                Console.WriteLine($"{i+1}. {types.Types[i].System.Name}");
            }

            int index = 0;
            var num   = Console.ReadLine();

            if (int.TryParse(num, out index) && index >= 1 && index <= types.Types.Count)
            {
                index--; // Subtract one since index is zero-based

                // Validate that the type has supported elements
                var elements = KontentHelper.FilterElements(types.Types[index].Elements);
                if (elements.Count == 0)
                {
                    Program.WriteColoredText("Chosen content type has no Text or Rich Text elements, please choose another.", ConsoleColor.Green);
                    Console.ReadKey();
                    return(VerifyContentType(types));
                }

                return(types.Types[index]);
            }
            else
            {
                Program.ShowError("Unrecognized or out-of-bounds number. Please press any key to try again.");
                Console.ReadKey();
                return(VerifyContentType(types));
            }
        }
예제 #2
0
        /// <summary>
        /// Displays a list of elements from the passed content type and allows the user to select one. Elements must be Text or Rich Text.
        /// </summary>
        /// <param name="contenttype"></param>
        /// <returns></returns>
        public static ContentElement VerifyContentElement(ContentType contenttype)
        {
            var elements = KontentHelper.FilterElements(contenttype.Elements);

            Console.WriteLine();
            Program.WriteColoredText("Which element should the imported text be inserted into? [Enter a number]", ConsoleColor.Green);
            for (int i = 0; i < elements.Count; i++)
            {
                Console.WriteLine($"{i+1}. {elements[i].Key} ({elements[i].Value.Type})");
            }
            int index = 0;
            var num   = Console.ReadLine();

            if (int.TryParse(num, out index) && index >= 1 && index <= elements.Count)
            {
                index--; //Subtract one since index is zero-based
                return(elements[index].Value);
            }
            else
            {
                Program.ShowError("Unrecognized or out-of-bounds number. Please press any key to try again.");
                Console.ReadKey();
                return(VerifyContentElement(contenttype));
            }
        }
예제 #3
0
 static void Main(string[] args)
 {
     if (Init())
     {
         if (args.Length == 0)
         {
             // Open with GUI
             Run();
         }
         else
         {
             // Run autoimport with passed args
             KontentHelper.AutoImport(args, driveHelper);
         }
     }
 }
예제 #4
0
        private static void InitKontent(KontentConfig config)
        {
            ContentManagementOptions cmoptions = new ContentManagementOptions
            {
                ProjectId = config.ProjectID,
                ApiKey    = config.ApiKey
            };

            KontentHelper.Init(
                DeliveryClientBuilder
                .WithOptions(builder => builder
                             .WithProjectId(config.ProjectID)
                             .UsePreviewApi(config.PreviewKey)
                             .Build())
                .Build(),
                new ContentManagementClient(cmoptions));
        }
예제 #5
0
        public static void Run()
        {
            ContentType    targetType    = null;
            ContentElement targetElement = null;

            Google.Apis.Drive.v3.Data.File sourceFile = null;
            bool update   = false;
            bool hasError = false;

            var files = driveHelper.ListFiles();

            if (files != null && files.Count > 0)
            {
                sourceFile = VerifySourceFile(files);
            }
            else
            {
                Program.ShowError("No files found.");
                Console.ReadKey();
                return;
            }

            if (!DriveHelper.IsAsset(sourceFile))
            {
                var types = KontentHelper.ListContentTypes();
                targetType = VerifyContentType(types);

                // If imported file is not a spreadsheet, get target element
                if (!DriveHelper.IsSpreadsheet(sourceFile))
                {
                    targetElement = VerifyContentElement(targetType);
                }

                update = AskUpdate();
            }

            // Verify data and begin import
            Console.WriteLine();
            if (!CanImport(sourceFile, targetElement, targetType))
            {
                ShowError("Something went wrong.. Please press any key to close.");
                Console.ReadKey();
                return;
            }
            else
            {
                try
                {
                    var stream = driveHelper.DownloadFile(sourceFile);
                    if (stream != null)
                    {
                        KontentHelper.BeginImport(stream, sourceFile, targetElement, targetType, update);
                    }
                }
                catch (Exception e)
                {
                    hasError = true;
                    ShowError(e.Message);
                }
                finally
                {
                    Console.WriteLine(hasError ? "Import completed with error(s), please check error messages" : "Import complete");
                    // Ask if user wants to run import again
                    Console.WriteLine("Do you want to perform another import? [Enter = Yes, Any other key = Quit]");
                    if (Console.ReadKey().Key == ConsoleKey.Enter)
                    {
                        Run();
                    }
                }
            }
        }