// Copyright (C) Stichting Deltares 2025. All rights reserved.
//
// This file is part of the application DAM - UI.
//
// 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.Globalization;
using Deltares.Authorization;
namespace Deltares.Dam.Data.License;
///
/// License class to checkout and checkin licenses
///
public class DamLicense
{
private static DAuthClient dAuthClient;
private static string checkedOutFeature;
///
/// Gets the type of the dam license.
///
///
/// The type of the dam license.
///
public static DamLicenseType DamLicenseType { get; private set; } = DamLicenseType.None;
///
/// Checks out the license.
///
/// The feature.
/// The version.
public static void CheckoutLicense(string feature, string version)
{
if (feature == null)
{
throw new ArgumentNullException(nameof(feature));
}
int majorVersion = GetMajorVersionFromVersionString(version);
var versionString = majorVersion.ToString(CultureInfo.InvariantCulture);
if (DAuthClient.CheckOut(feature, versionString))
{
DamLicenseType = DamLicenseType.LFM;
checkedOutFeature = feature;
}
else
{
DamLicenseType = DamLicenseType.None;
checkedOutFeature = null;
}
}
///
/// Checks in the license.
///
public static void CheckinLicense()
{
if (!String.IsNullOrEmpty(checkedOutFeature))
{
DAuthClient.CheckIn(checkedOutFeature);
}
}
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[]
{
'.'
};
string[] versionNumbers = version.Split(seperator);
int majorVersion;
int.TryParse(versionNumbers[0], out majorVersion);
return majorVersion;
}
}