using System;
using System.Collections.Generic;
namespace Vinno.vCloud.Common.License.Licenses
{
public static class LicenseFileHelper
{
///
/// used'$'when saving File
///
private static char separator = '$';
///
/// Get text from Lincese
///
///
///
public static string LinceseToFile(License lincese)
{
string text = string.Empty;
string expirationUtcDatetext = "ExpirationUtcDate=" + lincese.ExpirationUtcDate;
text += expirationUtcDatetext + separator;
string maxAccountCounttext = "MaxAccountCount=" + lincese.MaxAccountCount;
text += maxAccountCounttext + separator;
string maxUsCounttext = "MaxUsCount=" + lincese.MaxUsCount;
text += maxUsCounttext + separator;
string maxDiskSpacetext = "MaxDiskSpace=" + lincese.MaxDiskSpace;
text += maxDiskSpacetext + separator;
string idtext = "Identification=" + lincese.Identification;
text += idtext + separator;
string funItemValue = "FunctionalItemValue=" + lincese.FunctionalItemValue;
text += funItemValue + separator;
string asskey = "AssociationKey=" + lincese.AssociationKey;
text += asskey + separator;
string useVersionValue = "LicenseType=" + lincese.LicenseType.ToString();
text += useVersionValue + separator;
string lId = "LicenseId=" + lincese.LicenseId;
text += lId + separator;
string customerName = "CustomerName=" + lincese.CustomerName;
text += customerName + separator;
string MachineId = "MachineId=" + lincese.MachineId;
text += MachineId + separator;
return text;
}
///
/// Get License from text file
///
///
///
internal static License FromFile(string text)
{
License license = new License();
var stringlist = text.Split(separator);
var dict = new Dictionary();
foreach (var s in stringlist)
{
var arry = s.Split('=');
if (arry.Length > 1)
{
dict.Add(arry[0], arry[1]);
}
if (arry.Length == 1)
{
dict.Add(arry[0], "");
}
}
var properties = license.GetType().GetProperties();
foreach (var propertyInfo in properties)
{
dict.TryGetValue(propertyInfo.Name, out var value);
if (string.IsNullOrEmpty(value)) continue;
object obj;
switch (propertyInfo.Name)
{
case "MaxUsCount":
obj = Convert.ToInt32(value);
break;
case "ExpirationDay":
obj = Convert.ToInt32(value);
break;
case "ExpirationUtcDate":
obj = Convert.ToDateTime(value);
break;
case "MaxAccountCount":
obj = Convert.ToInt32(value);
break;
case "MaxDiskSpace":
obj = Convert.ToInt32(value);
break;
case "LicenseType":
obj = Enum.Parse(typeof(LicenseType), value);
break;
case "Identification":
obj = value.ToString();
break;
case "FunctionalItemValue":
obj = value.ToString();
break;
case "AssociationKey":
obj = value.ToString();
break;
case "UseVersionItemValue":
obj = value.ToString();
break;
case "LicenseId":
obj = value.ToString();
break;
case "CustomerName":
obj = value.ToString();
break;
case "MachineId":
obj = value.ToString();
break;
default:
obj = value;
break;
}
propertyInfo.SetValue(license, obj);
}
return license;
}
///
/// Get text from StartupLicnese
///
/// StartupLicnese object
///
internal static string StartupLicneseToFile(StartupLicnese lincese)
{
return "FirstStartupUtcDate=" + lincese.FirstStartupUtcDate;
}
///
/// Get StartupLicnese from text File
///
///
///
internal static StartupLicnese StartupLicneseFromFile(string text)
{
var license = new StartupLicnese();
var stringlist = text.Split(separator);
var dict = new Dictionary();
foreach (var s in stringlist)
{
var arry = s.Split('=');
if (arry.Length > 1)
{
dict.Add(arry[0], arry[1]);
}
if (arry.Length == 1)
{
dict.Add(arry[0], "");
}
}
var properties = license.GetType().GetProperties();
foreach (var propertyInfo in properties)
{
dict.TryGetValue(propertyInfo.Name, out var value);
if (string.IsNullOrEmpty(value)) continue;
object obj;
switch (propertyInfo.Name)
{
case "FirstStartupUtcDate":
obj = Convert.ToDateTime(value);
break;
default:
obj = value;
break;
}
propertyInfo.SetValue(license, obj);
}
return license;
}
}
}