using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using Deltares.Authorization; namespace Deltares.Dam.Data.License { /// /// License class to chekout and checkin licenses /// public class DamLicense { private const string dAuthFeature = "DGS_26_00_V"; private static DAuthClient dAuthClient; private static DamLicenseType damLicenseType = DamLicenseType.None; private static string checkedOutFeature; /// /// Gets the type of the dam license. /// /// /// The type of the dam license. /// public static DamLicenseType DamLicenseType { get { return damLicenseType; } } /// /// Checkouts the license. /// Look up all features starting with the specified string /// See if any of them has the correct license version, according to the version we get from the Application version info /// public static void CheckoutLicense(string version) { IEnumerable featureNames = GetAllFeaturesStartingWith(dAuthFeature); if (featureNames == null) { checkedOutFeature = null; damLicenseType = DamLicenseType.None; return; } int majorVersion = GetMajorVersionFromVersionString(version); string versionString = majorVersion.ToString(CultureInfo.InvariantCulture); damLicenseType = DamLicenseType.None; foreach (var featureName in featureNames) { if (DAuthClient.CheckOut(featureName, versionString)) { damLicenseType = DamLicenseType.LFM; checkedOutFeature = featureName; break; } } } /// /// Checks in the license. /// public static void CheckinLicense() { if (!String.IsNullOrEmpty(checkedOutFeature)) { DAuthClient.CheckIn(checkedOutFeature); } } /// /// Gets all features starting with the specified feature string /// /// the feature string to look up /// all the feature names thta start with the specified feature string public static IEnumerable GetAllFeaturesStartingWith(string feature) { string[] features = null; DAuthClient.GetAvailableFeatures(ref features); if (features == null) { return null; } IEnumerable featureNames = features.Where(s => s.StartsWith(feature)); return featureNames; } private static DAuthClient DAuthClient { get { if (dAuthClient != null) { return dAuthClient; } dAuthClient = new DAuthClient(false, true); return dAuthClient; } } private static int GetMajorVersionFromVersionString(string version) { var seperator = new[] { '.' }; var versionNumbers = version.Split(seperator); int majorVersion; int.TryParse(versionNumbers[0], out majorVersion); return majorVersion; } } }