// Copyright (C) Stichting Deltares 2018. All rights reserved.
//
// This file is part of the application DAM - Clients Library.
//
// DAM - UI is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
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";
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;
}
}
}