/// <summary>
        /// Returns all payment methods. See NetLicensingAPI JavaDoc for details:
        /// http://netlicensing.labs64.com/javadoc/v2/com/labs64/netlicensing/core/service/PaymentMethodService.html
        /// </summary>
        public static List<PaymentMethod> list(Context context)
        {
            netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.GET, Constants.PaymentMethod.ENDPOINT_PATH, null);

            List<PaymentMethod> paymentMethods = new List<PaymentMethod>();
            if (output.items.item != null) {
                foreach (item i in output.items.item) {
                    paymentMethods.Add (new PaymentMethod (i));
                }
            }
            return paymentMethods;
        }
 /// <summary>
 /// Creates new license object with given properties. See NetLicensingAPI JavaDoc for details:
 /// http://netlicensing.labs64.com/javadoc/v2/com/labs64/netlicensing/core/service/LicenseService.html
 /// </summary>
 public static License create(Context context, String licenseeNumber, String licenseTemplateNumber, String transactionNumber, License newLicense)
 {
     newLicense.licenseeNumber = licenseeNumber;
     newLicense.licenseTemplateNumber = licenseTemplateNumber;
     string transactionOldValue;
     if (newLicense.licenseProperties.TryGetValue(Constants.Transaction.TRANSACTION_NUMBER, out transactionOldValue))
     {
         newLicense.licenseProperties.Remove(Constants.Transaction.TRANSACTION_NUMBER);
     }
     newLicense.licenseProperties.Add(Constants.Transaction.TRANSACTION_NUMBER, transactionNumber);
     netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.POST, Constants.License.ENDPOINT_PATH, newLicense.ToDictionary());
     return new License(output.items.item[0]);
 }
        /// <summary>
        /// Returns all licensing models.
        /// </summary>
        public static List<String> listLicensingModels(Context context)
        {
            netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.GET, Constants.Utility.ENDPOINT_PATH + "/" + Constants.Utility.LICENSING_MODELS, null);

            List<String> licensingModels = new List<String>();
            if (output.items.item != null) {
                foreach (item i in output.items.item) {
                    if (Constants.Utility.LICENSING_MODELS_PROPERTIES.Equals (i.type)) {
                        foreach (property p in i.property) {
                            if (p.name == Constants.NAME) {
                                licensingModels.Add (p.Value);
                            }
                        }
                    }
                }
            }
            return licensingModels;
        }
        /// <summary>
        /// Returns all products of a vendor. See NetLicensingAPI JavaDoc for details:
        /// http://netlicensing.labs64.com/javadoc/v2/com/labs64/netlicensing/core/service/ProductService.html
        /// </summary>
        public static List<Product> list(Context context, String filter)
        {
            Dictionary<String, String> parameters = new Dictionary<String, String>();
            if (!String.IsNullOrEmpty(filter))
            {
                parameters.Add(Constants.FILTER, filter);
            }

            netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.GET, Constants.Product.ENDPOINT_PATH, null);

            List<Product> products = new List<Product>();
            if (output.items.item != null) {
                foreach (item i in output.items.item) {
                    products.Add (new Product (i));
                }
            }
            return products;
        }
        /// <summary>
        /// Returns all license templates of a vendor. See NetLicensingAPI JavaDoc for details:
        /// http://netlicensing.labs64.com/javadoc/v2/com/labs64/netlicensing/core/service/LicenseTemplateService.html
        /// </summary>
        public static List<LicenseTemplate> list(Context context, String filter)
        {
            Dictionary<String, String> parameters = new Dictionary<String, String>();
            if (!String.IsNullOrEmpty(filter))
            {
                parameters.Add(Constants.FILTER, filter);
            }

            netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.GET, Constants.LicenseTemplate.ENDPOINT_PATH, parameters);

            List<LicenseTemplate> licenseTemplates = new List<LicenseTemplate>();
            if (output.items.item != null) {
                foreach (item i in output.items.item) {
                    licenseTemplates.Add (new LicenseTemplate (i));
                }
            }
            return licenseTemplates;
        }
        /// <summary>
        /// Genarates token by its number. See NetLicensingAPI JavaDoc for details:
        /// http://netlicensing.labs64.com/javadoc/v2/com/labs64/netlicensing/core/service/TokenService.html
        /// </summary>
        public static Token create(Context context, Token newToken)
        {
            Dictionary<String, String> parameters = new Dictionary<String, String>();
            if (newToken.tokenType == null)
            {
                newToken.tokenType = Constants.Token.TYPE_DEFAULT;
            }
            parameters.Add(Constants.Token.TOKEN_TYPE, newToken.tokenType);
            if (newToken.tokenType.Equals(Constants.Token.TYPE_SHOP) && newToken.tokenProperties.ContainsKey(Constants.Licensee.LICENSEE_NUMBER))
            {
                String licenseeNumber = newToken.tokenProperties[Constants.Licensee.LICENSEE_NUMBER];
                if (!String.IsNullOrEmpty(licenseeNumber))
                {
                    parameters.Add(Constants.Licensee.LICENSEE_NUMBER, licenseeNumber);
                }
            }

            netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.POST, Constants.Token.ENDPOINT_PATH, parameters);
            return new Token(output.items.item[0]);
        }
        public static List<Token> list(Context context, String tokenType, String filter)
        {
            Dictionary<String, String> parameters = new Dictionary<String, String>();
            if (!String.IsNullOrEmpty(tokenType))
            {
                parameters.Add(Constants.Token.TOKEN_TYPE, tokenType);
            }
            if (!String.IsNullOrEmpty(filter))
            {
                parameters.Add(Constants.FILTER, filter);
            }

            netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.GET, Constants.Token.ENDPOINT_PATH, parameters);

            List<Token> tokens = new List<Token>();
            if (output.items.item != null)
            {
                foreach (item i in output.items.item)
                {
                    tokens.Add(new Token(i));
                }
            }
            return tokens;
        }
        /// <summary>
        /// Validates active licenses of the licensee. See NetLicensingAPI JavaDoc for details:
        /// http://netlicensing.labs64.com/javadoc/v2/com/labs64/netlicensing/core/service/LicenseeService.html
        /// </summary>
        public static ValidationResult validate(Context context, String number, String productNumber)
        {
            Dictionary<String, String> parameters = new Dictionary<String, String>();
            if (!String.IsNullOrEmpty(productNumber))
            {
                parameters.Add("productNumber", productNumber);
            }

            netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.GET, Constants.Licensee.ENDPOINT_PATH + "/" + number + "/validate", parameters);
            return new ValidationResult(output);
        }
 /// <summary>
 /// Deactivates token by its number. See NetLicensingAPI JavaDoc for details:
 /// http://netlicensing.labs64.com/javadoc/v2/com/labs64/netlicensing/core/service/TokenService.html
 /// </summary>
 public static void deactivate(Context context, String number)
 {
     netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.DELETE, Constants.Token.ENDPOINT_PATH + "/" + number, null);
 }
 /// <summary>
 /// Gets token by its number. See NetLicensingAPI JavaDoc for details:
 /// http://netlicensing.labs64.com/javadoc/v2/com/labs64/netlicensing/core/service/TokenService.html
 /// </summary>
 public static Token get(Context context, String number)
 {
     netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.GET, Constants.Token.ENDPOINT_PATH + "/" + number, null);
     return new Token(output.items.item[0]);
 }
 /// <summary>
 /// Updates product properties. See NetLicensingAPI JavaDoc for details:
 /// http://netlicensing.labs64.com/javadoc/v2/com/labs64/netlicensing/core/service/ProductService.html
 /// </summary>
 public static Product update(Context context, String number, Product updateProduct)
 {
     updateProduct.number = number;
     netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.POST, Constants.Product.ENDPOINT_PATH + "/" + number, updateProduct.ToDictionary());
     return new Product(output.items.item[0]);
 }
 /// <summary>
 /// Updates payment method with the given number.. See NetLicensingAPI JavaDoc for details:
 /// http://netlicensing.labs64.com/javadoc/v2/com/labs64/netlicensing/core/service/PaymentMethodService.html
 /// </summary>
 public static PaymentMethod update(Context context, String number, PaymentMethod newPaymentMethod)
 {
     netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.POST, Constants.PaymentMethod.ENDPOINT_PATH + "/" + number, newPaymentMethod.ToDictionary());
     return new PaymentMethod(output.items.item[0]);
 }
 /// <summary>
 /// Deletes product. See NetLicensingAPI JavaDoc for details:
 /// http://netlicensing.labs64.com/javadoc/v2/com/labs64/netlicensing/core/service/ProductService.html
 /// </summary>
 public static void delete(Context context, String number, bool forceCascade)
 {
     String strCascade = Convert.ToString(forceCascade).ToLower();
     netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.DELETE, Constants.Product.ENDPOINT_PATH + "/" + number, Utilities.forceCascadeToDict(forceCascade));
 }
        static void Main(string[] args)
        {
            // ServicePointManager.ServerCertificateValidationCallback = delegate { return true;  // Trust any (self-signed) certificate };

            Context context = new Context();
            context.baseUrl = "https://netlicensing.labs64.com";
            context.username = "******";
            context.password = "******";
            context.securityMode = SecutiryMode.BASIC_AUTHENTICATION;

            String demoProductNumber = "P001demo";
            String demoProductModuleNumber = "M001demo";
            String demoLicensingModel = "TimeLimitedEvaluation";
            String demoLicenseTemplate1_Number = "E001demo";
            String demoLicenseTemplate1_Name = "Demo Evaluation Period";
            String demoLicenseTemplate1_Type = "FEATURE";
            Decimal demoLicenseTemplate1_Price = 12.50M;
            String demoLicenseTemplate1_Currency = "EUR";
            Boolean demoLicenseTemplate1_Automatic = false;
            Boolean demoLicenseTemplate1_Hidden = false;
            String demoLicenseeNumber = "I001demo";
            String demoLicenseNumber = "L001demoTV";

            try
            {

                #region ****************** Lists

                List<String> licenseTypes = UtilityService.listLicenseTypes(context);
                ConsoleWriter.WriteList("License Types:", licenseTypes);

                List<String> licensingModels = UtilityService.listLicensingModels(context);
                ConsoleWriter.WriteList("Licensing Models:", licensingModels);

                #endregion

                #region ****************** Product

                Product newProduct = new Product();
                newProduct.number = demoProductNumber;
                newProduct.name = "Demo product";
                Product product = ProductService.create(context, newProduct);
                ConsoleWriter.WriteEntity("Added product:", product);

                product = ProductService.get(context, demoProductNumber);
                ConsoleWriter.WriteEntity("Got product:", product);

                List<Product> products = ProductService.list(context, null);
                ConsoleWriter.WriteList("Got the following products:", products);

                Product updateProduct = new Product();
                updateProduct.productProperties.Add("Updated property name", "Updated value");
                product = ProductService.update(context, demoProductNumber, updateProduct);
                ConsoleWriter.WriteEntity("Updated product:", product);

                ProductService.delete(context, demoProductNumber, true);
                ConsoleWriter.WriteMsg("Deleted Product!");

                products = ProductService.list(context, null);
                ConsoleWriter.WriteList("Got the following Products:", products);

                product = ProductService.create(context, newProduct);
                ConsoleWriter.WriteEntity("Added product again:", product);

                products = ProductService.list(context, null);
                ConsoleWriter.WriteList("Got the following Products:", products);

                #endregion

                #region ****************** ProductModule

                ProductModule newProductModule = new ProductModule();
                newProductModule.number = demoProductModuleNumber;
                newProductModule.name = "Demo product module";
                newProductModule.licensingModel = demoLicensingModel;
                ProductModule productModule = ProductModuleService.create(context, demoProductNumber, newProductModule);
                ConsoleWriter.WriteEntity("Added product module:", productModule);

                productModule = ProductModuleService.get(context, demoProductModuleNumber);
                ConsoleWriter.WriteEntity("Got product module:", productModule);

                List<ProductModule> productModules = ProductModuleService.list(context, null);
                ConsoleWriter.WriteList("Got the following ProductModules:", productModules);

                ProductModule updateProductModule = new ProductModule();
                updateProductModule.productModuleProperties.Add("Updated property name", "Updated property value");
                productModule = ProductModuleService.update(context, demoProductModuleNumber, updateProductModule);
                ConsoleWriter.WriteEntity("Updated product module:", productModule);

                ProductModuleService.delete(context, demoProductModuleNumber, true);
                ConsoleWriter.WriteMsg("Deleted ProductModule!");

                productModules = ProductModuleService.list(context, null);
                ConsoleWriter.WriteList("Got the following ProductModules:", productModules);

                productModule = ProductModuleService.create(context, demoProductNumber, newProductModule);
                ConsoleWriter.WriteEntity("Added product module again:", productModule);

                productModules = ProductModuleService.list(context, null);
                ConsoleWriter.WriteList("Got the following ProductModules:", productModules);

                #endregion

                #region ****************** LicenseTemplate
                LicenseTemplate newLicenseTemplate = new LicenseTemplate();
                newLicenseTemplate.number = demoLicenseTemplate1_Number;
                newLicenseTemplate.name = demoLicenseTemplate1_Name;
                newLicenseTemplate.licenseType = demoLicenseTemplate1_Type;
                newLicenseTemplate.price = demoLicenseTemplate1_Price;
                newLicenseTemplate.currency = demoLicenseTemplate1_Currency;
                newLicenseTemplate.automatic = demoLicenseTemplate1_Automatic;
                newLicenseTemplate.hidden = demoLicenseTemplate1_Hidden;
                ConsoleWriter.WriteEntity("Adding license template:", newLicenseTemplate);
                LicenseTemplate licenseTemplate = LicenseTemplateService.create(context, demoProductModuleNumber, newLicenseTemplate);
                ConsoleWriter.WriteEntity("Added license template:", licenseTemplate);

                licenseTemplate = LicenseTemplateService.get(context, demoLicenseTemplate1_Number);
                ConsoleWriter.WriteEntity("Got licenseTemplate:", licenseTemplate);

                List<LicenseTemplate> licenseTemplates = LicenseTemplateService.list(context, null);
                ConsoleWriter.WriteList("Got the following license templates:", licenseTemplates);

                LicenseTemplate updateLicenseTemplate = new LicenseTemplate();
                updateLicenseTemplate.active = true;
                updateLicenseTemplate.automatic = demoLicenseTemplate1_Automatic; // workaround: at the moment not specified booleans treated as "false"
                updateLicenseTemplate.hidden = demoLicenseTemplate1_Hidden; // workaround: at the moment not specified booleans treated as "false"
                licenseTemplate = LicenseTemplateService.update(context, demoLicenseTemplate1_Number, updateLicenseTemplate);
                ConsoleWriter.WriteEntity("Updated license template:", licenseTemplate);

                LicenseTemplateService.delete(context, demoLicenseTemplate1_Number, true);
                ConsoleWriter.WriteMsg("Deleted LicenseTemplate!");

                licenseTemplates = LicenseTemplateService.list(context, null);
                ConsoleWriter.WriteList("Got the following license templates:", licenseTemplates);

                licenseTemplate = LicenseTemplateService.create(context, demoProductModuleNumber, newLicenseTemplate);
                ConsoleWriter.WriteEntity("Added license template again:", licenseTemplate);

                licenseTemplates = LicenseTemplateService.list(context, null);
                ConsoleWriter.WriteList("Got the following license templates:", licenseTemplates);

                #endregion

                #region ****************** Licensee

                Licensee newLicensee = new Licensee();
                newLicensee.number = demoLicenseeNumber;
                Licensee licensee = LicenseeService.create(context, demoProductNumber, newLicensee);
                ConsoleWriter.WriteEntity("Added licensee:", licensee);

                List<Licensee> licensees = LicenseeService.list(context, null);
                ConsoleWriter.WriteList("Got the following licensees:", licensees);

                LicenseeService.delete(context, demoLicenseeNumber, true);
                ConsoleWriter.WriteMsg("Deleted licensee!");

                licensees = LicenseeService.list(context, null);
                ConsoleWriter.WriteList("Got the following licensees after delete:", licensees);

                licensee = LicenseeService.create(context, demoProductNumber, newLicensee);
                ConsoleWriter.WriteEntity("Added licensee again:", licensee);

                licensee = LicenseeService.get(context, demoLicenseeNumber);
                ConsoleWriter.WriteEntity("Got licensee:", licensee);

                Licensee updateLicensee = new Licensee();
                updateLicensee.licenseeProperties.Add("Updated property name", "Updated value");
                licensee = LicenseeService.update(context, demoLicenseeNumber, updateLicensee);
                ConsoleWriter.WriteEntity("Updated licensee:", licensee);

                licensees = LicenseeService.list(context, null);
                ConsoleWriter.WriteList("Got the following licensees:", licensees);

                #endregion

                #region ****************** License
                License newLicense = new License();
                newLicense.number = demoLicenseNumber;
                License license = LicenseService.create(context, demoLicenseeNumber, demoLicenseTemplate1_Number, null, newLicense);
                ConsoleWriter.WriteEntity("Added license:", license);

                List<License> licenses = LicenseService.list(context, null);
                ConsoleWriter.WriteList("Got the following license templates:", licenses);

                LicenseService.delete(context, demoLicenseNumber);
                Console.WriteLine("Deleted license");

                licenses = LicenseService.list(context, null);
                ConsoleWriter.WriteList("Got the following license templates:", licenses);

                license = LicenseService.create(context, demoLicenseeNumber, demoLicenseTemplate1_Number, null, newLicense);
                ConsoleWriter.WriteEntity("Added license again:", license);

                license = LicenseService.get(context, demoLicenseNumber);
                ConsoleWriter.WriteEntity("Got license:", license);

                License updateLicense = new License();
                updateLicense.licenseProperties.Add("Updated property name", "Updated value");
                license = LicenseService.update(context, demoLicenseNumber, null, updateLicense);
                ConsoleWriter.WriteEntity("Updated license:", license);

                #endregion

                #region ****************** PaymentMethod

                List<PaymentMethod> paymentMethods = PaymentMethodService.list(context);
                ConsoleWriter.WriteList("Got the following payment methods:", paymentMethods);

                #endregion

                #region ****************** Token

                Token newToken = new Token();
                newToken.tokenType = Constants.Token.TYPE_APIKEY;
                Token apiKey = TokenService.create(context, newToken);
                ConsoleWriter.WriteEntity("Created API Key:", apiKey);
                context.apiKey = apiKey.number;

                newToken.tokenType = Constants.Token.TYPE_SHOP;
                newToken.tokenProperties.Add(Constants.Licensee.LICENSEE_NUMBER, demoLicenseeNumber);
                context.securityMode = SecutiryMode.APIKEY_IDENTIFICATION;
                Token shopToken = TokenService.create(context, newToken);
                context.securityMode = SecutiryMode.BASIC_AUTHENTICATION;
                ConsoleWriter.WriteEntity("Got the following shop token:", shopToken);

                List<Token> tokens = TokenService.list(context, Constants.Token.TYPE_SHOP, null);
                ConsoleWriter.WriteList("Got the following shop tokens:", tokens);

                TokenService.deactivate(context, shopToken.number);
                ConsoleWriter.WriteMsg("Deactivated shop token!");

                tokens = TokenService.list(context, Constants.Token.TYPE_SHOP, null);
                ConsoleWriter.WriteList("Got the following shop tokens after deactivate:", tokens);

                #endregion

                #region ****************** Validate

                ValidationResult validationResult = LicenseeService.validate(context, demoLicenseeNumber, demoProductNumber);
                ConsoleWriter.WriteEntity("Validation result for created licensee:", validationResult);

                context.securityMode = SecutiryMode.APIKEY_IDENTIFICATION;
                validationResult = LicenseeService.validate(context, demoLicenseeNumber, demoProductNumber);
                context.securityMode = SecutiryMode.BASIC_AUTHENTICATION;
                ConsoleWriter.WriteEntity("Validation repeated with API Key:", validationResult);

                #endregion

            }
            catch (NetLicensingException e)
            {
                Console.WriteLine("Got NetLicensing exception:");
                Console.WriteLine(e);
            }
            catch (Exception e)
            {
                Console.WriteLine("Got exception:");
                Console.WriteLine(e);
            }
            finally
            {
                try
                {
                    // Cleanup:
                    context.securityMode = SecutiryMode.BASIC_AUTHENTICATION;
                    // deactivate api key
                    TokenService.deactivate(context, context.apiKey);
                    // delete test product with all its related items
                    ProductService.delete(context, demoProductNumber, true);
                }
                catch (NetLicensingException e)
                {
                    Console.WriteLine("Got NetLicensing exception during cleanup:");
                    Console.WriteLine(e);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Got exception during cleanup:");
                    Console.WriteLine(e);
                }
            }

            Console.WriteLine("Press <Enter> to exit...");
            Console.ReadLine();
        }
 /// <summary>
 /// Creates new product object with given properties. See NetLicensingAPI JavaDoc for details:
 /// http://netlicensing.labs64.com/javadoc/v2/com/labs64/netlicensing/core/service/ProductService.html
 /// </summary>
 public static Product create(Context context, Product newProduct)
 {
     netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.POST, Constants.Product.ENDPOINT_PATH, newProduct.ToDictionary());
     return new Product(output.items.item[0]);
 }
 /// <summary>
 /// Gets product module by its number. See NetLicensingAPI JavaDoc for details:
 /// http://netlicensing.labs64.com/javadoc/v2/com/labs64/netlicensing/core/service/ProductModuleService.html
 /// </summary>
 public static ProductModule get(Context context, String number)
 {
     netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.GET, Constants.ProductModule.ENDPOINT_PATH + "/" + number, null);
     return new ProductModule(output.items.item[0]);
 }
 /// <summary>
 /// Creates new licensee object with given properties. See NetLicensingAPI JavaDoc for details:
 /// http://netlicensing.labs64.com/javadoc/v2/com/labs64/netlicensing/core/service/LicenseeService.html
 /// </summary>
 public static Licensee create(Context context, String productNumber, Licensee newLicensee)
 {
     newLicensee.productNumber = productNumber;
     netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.POST, Constants.Licensee.ENDPOINT_PATH, newLicensee.ToDictionary());
     return new Licensee(output.items.item[0]);
 }
 /// <summary>
 /// Updates licensee properties. See NetLicensingAPI JavaDoc for details:
 /// http://netlicensing.labs64.com/javadoc/v2/com/labs64/netlicensing/core/service/LicenseeService.html
 /// </summary>
 public static Licensee update(Context context, String number, Licensee updateLicensee)
 {
     updateLicensee.number = number;
     netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.POST, Constants.Licensee.ENDPOINT_PATH + "/" + number, updateLicensee.ToDictionary());
     return new Licensee(output.items.item[0]);
 }
 /// <summary>
 /// Deletes licensee. See NetLicensingAPI JavaDoc for details:
 /// http://netlicensing.labs64.com/javadoc/v2/com/labs64/netlicensing/core/service/LicenseeService.html
 /// </summary>
 public static void delete(Context context, String number, Boolean forceCascade)
 {
     netlicensing output = NetLicensingAPI.request(context, NetLicensingAPI.Method.DELETE, Constants.Licensee.ENDPOINT_PATH + "/" + number, Utilities.forceCascadeToDict(forceCascade));
 }