Skip to content

Commit

Permalink
Integration - Column mapping - None
Browse files Browse the repository at this point in the history
  • Loading branch information
DWDBE committed Aug 1, 2024
1 parent 586d42f commit 68109ed
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>10.6.0</Version>
<Version>10.6.1</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<Title>User Provider</Title>
<Description>User Provider</Description>
Expand Down
91 changes: 58 additions & 33 deletions src/UserDestinationWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ public void Write(Dictionary<string, object> row, Mapping mapping, bool discardD
foreach (ColumnMapping columnMapping in columnMappings.Where(cm => cm.Active))
{
object rowValue = null;
bool hasValueInRow = row.TryGetValue(columnMapping.SourceColumn?.Name, out rowValue);
bool hasValueInRow = columnMapping.SourceColumn is not null && row.TryGetValue(columnMapping.SourceColumn?.Name, out rowValue);
if (columnMapping.HasScriptWithValue || hasValueInRow)
{
object evaluatedValue = columnMapping.ConvertInputValueToOutputValue(rowValue);
Expand All @@ -478,7 +478,8 @@ public void Write(Dictionary<string, object> row, Mapping mapping, bool discardD
}
else
{
throw new Exception(BaseDestinationWriter.GetRowValueNotFoundMessage(row, columnMapping.SourceColumn.Table.Name, columnMapping.SourceColumn.Name));
throw new Exception(GetRowValueNotFoundMessage(row, columnMapping.SourceColumn?.Table?.Name ?? columnMapping.DestinationColumn?.Table?.Name,
columnMapping.SourceColumn?.Name ?? columnMapping.DestinationColumn?.Name));
}
}

Expand Down Expand Up @@ -522,7 +523,7 @@ public void Write(Dictionary<string, object> row, Mapping mapping, bool discardD
ColumnMapping cm = columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserEmail");
if (cm != null)
{
dataRow["AccessUserUserName"] = row[cm.SourceColumn.Name];
dataRow["AccessUserUserName"] = GetValue(cm, row);
}
}
}
Expand All @@ -531,9 +532,13 @@ public void Write(Dictionary<string, object> row, Mapping mapping, bool discardD
if (_useEmailForUsername)
{
ColumnMapping cm = columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserEmail");
if (cm != null && !string.IsNullOrEmpty(Converter.ToString(row[cm.SourceColumn.Name])))
if (cm != null)
{
dataRow["AccessUserUserName"] = row[cm.SourceColumn.Name];
var accessUserUserName = GetValue(cm, row);
if (!string.IsNullOrEmpty(accessUserUserName))
{
dataRow["AccessUserUserName"] = accessUserUserName;
}
}
}
}
Expand All @@ -547,10 +552,10 @@ public void Write(Dictionary<string, object> row, Mapping mapping, bool discardD
//the user is being imported for the first time, there is a mapping, and the input is either NULL or ""
var pcm = columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserPassword");
bool isPasswordPresentInSource = pcm != null &&
!string.IsNullOrEmpty(row[pcm.SourceColumn.Name] as string);
!string.IsNullOrEmpty(GetValue(pcm, row));
if (isPasswordPresentInSource)
{
password = row[pcm.SourceColumn.Name] as string;
password = GetValue(pcm, row);
if (_encryptUserPasswords)
{
string encryptedPassword = encryptedPassword = Crypto.EncryptPassword(password, _userPasswordHashAlgorithm);
Expand Down Expand Up @@ -579,18 +584,20 @@ public void Write(Dictionary<string, object> row, Mapping mapping, bool discardD
string name = null;
var cm = columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserName");
if (cm != null)
name = row[cm.SourceColumn.Name] as string;
name = GetValue(cm, row);
string userName = null;
cm = columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserUserName");
if (cm != null)
userName = row[cm.SourceColumn.Name] as string;
if (columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserEmail") != null && Converter.ToBoolean(dataRow["AccessUserActive"]) == true)
userName = GetValue(cm, row);
var emailMapping = columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserEmail");
if (emailMapping != null && Converter.ToBoolean(dataRow["AccessUserActive"]) == true)
{
string email = row[columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserEmail").SourceColumn.Name] as string;
string email = GetValue(emailMapping, row);
UserPassword userPasswordData = new UserPassword(userName, name, password, email);
if (columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserCountry") != null)
var countryMapping = columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserCountry");
if (countryMapping != null)
{
userPasswordData.Country = row[columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserCountry").SourceColumn.Name] as string;
userPasswordData.Country = GetValue(countryMapping, row);
}
UsersPasswordsToSend.Add(userPasswordData);
}
Expand Down Expand Up @@ -621,25 +628,21 @@ public void Write(Dictionary<string, object> row, Mapping mapping, bool discardD
var accessUserUserNameMapping = columnMappings.FirstOrDefault(m => m.Active && m.DestinationColumn.Name == "AccessUserUserName");
if (accessUserNameMapping == null && accessUserUserNameMapping != null)
{
groupName = ((string)row[accessUserUserNameMapping.SourceColumn.Name]).Trim();
groupName = GetValue(accessUserUserNameMapping, row).Trim();
dataRow["AccessUserUserName"] = groupName;
dataRow["AccessUserName"] = groupName;//AccessUserName field should be the same as AccessUserUserName
}
else if (accessUserUserNameMapping == null && accessUserNameMapping != null)
{
groupName = ((string)row[accessUserNameMapping.SourceColumn.Name]).Trim();
groupName = GetValue(accessUserNameMapping, row).Trim();
dataRow["AccessUserName"] = groupName;
dataRow["AccessUserUserName"] = groupName;//AccessUserUserName field should be the same as AccessUserName
}
else
{
groupName = accessUserNameMapping.IsKey ? row[accessUserNameMapping.SourceColumn.Name].ToString().Trim() :
row[accessUserUserNameMapping.SourceColumn.Name].ToString().Trim();
}

if (columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserActive") == null)
{
dataRow["AccessUserActive"] = true;
}
}
if (columnMappings.Find(m => m.DestinationColumn.Name == "AccessUserType") == null)
{
//Handle GroupType: If it is not existing group - import with AccessType = 2
Expand Down Expand Up @@ -689,17 +692,11 @@ public void Write(Dictionary<string, object> row, Mapping mapping, bool discardD
}
break;
case "SystemFieldValue":
if (columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, "SystemFieldValueSystemName", true) == 0) != null)
var fieldValueSystemName = GetValue(columnMappings.Find(cm => string.Compare(cm.DestinationColumn.Name, "SystemFieldValueSystemName", true) == 0), row);
if (!string.IsNullOrEmpty(fieldValueSystemName) && !SystemFields.Any(sf => string.Compare(sf.SystemName, fieldValueSystemName, true) == 0))
{
if (row[columnMappings.Find(m => string.Compare(m.DestinationColumn.Name, "SystemFieldValueSystemName", true) == 0).SourceColumn.Name] != DBNull.Value)
{
string fieldValueSystemName = Converter.ToString(row[columnMappings.Find(m => string.Compare(m.DestinationColumn.Name, "SystemFieldValueSystemName", true) == 0).SourceColumn.Name]);
if (!string.IsNullOrEmpty(fieldValueSystemName) && !SystemFields.Any(sf => string.Compare(sf.SystemName, fieldValueSystemName, true) == 0))
{
_logger.Log(string.Format("Can't find the '{0}' in the user system fields. Skipped row: '{1}'.", fieldValueSystemName, BaseProvider.GetFailedSourceRowMessage(row)));
return;
}
}
_logger.Log(string.Format("Can't find the '{0}' in the user system fields. Skipped row: '{1}'.", fieldValueSystemName, BaseProvider.GetFailedSourceRowMessage(row)));
return;
}
dataRow["SystemFieldValueTableName"] = "AccessUser";
break;
Expand Down Expand Up @@ -1659,9 +1656,10 @@ private DataRow GetExistingUserBySearchColumn(Dictionary<string, object> row, Ma
{
DataRow ret = null;
var columnMappings = mapping.GetColumnMappings();
if (columnMappings.Find(m => string.Compare(m.DestinationColumn.Name, searchColumn, true) == 0) != null)
var cm = columnMappings.Find(m => string.Compare(m.DestinationColumn.Name, searchColumn, true) == 0);
if (cm != null)
{
string searchValue = Converter.ToString(row[columnMappings.Find(m => string.Compare(m.DestinationColumn.Name, searchColumn, true) == 0).SourceColumn.Name]);
string searchValue = GetValue(cm, row);
ret = GetExistingUserBySearchColumn(searchColumn, searchValue);
}
return ret;
Expand Down Expand Up @@ -1956,6 +1954,33 @@ private void UpdateAccessUserGroupPkColumns(List<Column> columns, Dictionary<str
}
}

private string GetValue(ColumnMapping? columnMapping, Dictionary<string, object> row)

Check warning on line 1957 in src/UserDestinationWriter.cs

View workflow job for this annotation

GitHub Actions / call-workflow / Build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
string? result = null;
if (columnMapping != null && (columnMapping.HasScriptWithValue || row.ContainsKey(columnMapping.SourceColumn.Name)))
{
switch (columnMapping.ScriptType)
{
case ScriptType.None:
result = Converter.ToString(row[columnMapping.SourceColumn.Name]);
break;
case ScriptType.Append:
result = Converter.ToString(row[columnMapping.SourceColumn.Name]) + columnMapping.ScriptValue;
break;
case ScriptType.Prepend:
result = columnMapping.ScriptValue + Converter.ToString(row[columnMapping.SourceColumn.Name]);
break;
case ScriptType.Constant:
result = columnMapping.GetScriptValue();
break;
case ScriptType.NewGuid:
result = columnMapping.GetScriptValue();
break;
}
}
return result;
}

//internal void CleanRelationsTables(SqlTransaction transaction)
//{
// _sqlCommand.Transaction = transaction;
Expand Down

0 comments on commit 68109ed

Please sign in to comment.