2020-12-30 02:46:55 +01:00
using System.Collections.Generic ;
using BeWo.Data.Access ;
2017-09-26 13:30:20 +02:00
using BeWo.Data.Entities ;
using BS.Shared.DataContracts ;
namespace BeWo.Service.DCEntityMapper
{
public class TextModuleDC_TextModule : AbstractIDCEntityMapper < TextModule , TextModuleDC >
{
2020-12-30 02:46:55 +01:00
public IList < TextModuleDC > CreateDcList ( IEnumerable < TextModule > textmodules )
{
var list = new List < TextModuleDC > ( ) ;
Dictionary < long , IList < long > > parentChildDict = new Dictionary < long , IList < long > > ( ) ;
foreach ( var tm in textmodules )
{
var dc = CreateTextModuleDC ( tm , parentChildDict ) ;
list . Add ( dc ) ;
}
return list ;
}
private TextModuleDC CreateTextModuleDC ( TextModule tm , Dictionary < long , IList < long > > parentChildDict )
{
var dc = new TextModuleDC ( ) ;
dc . Position = tm . Position ;
dc . TextModuleVersion = tm . Version . Value ;
dc . TextModuleOid = tm . Oid . Value ;
dc . Text = tm . Text ;
dc . Name = tm . Name ;
dc . IsOnlyForEmployee = tm . IsOnlyForEmployee ;
dc . IsParent = tm . IsParent ;
dc . IsExpanded = tm . IsExpanded ;
dc . ActivationType = tm . IsActive ;
dc . ServiceCategory = tm . ServiceCategory ! = null ? MapperFactory . ServiceCategoryDC_ServiceCategory . MapToNewDC ( tm . ServiceCategory ) : null ;
dc . Employee = tm . Employee ! = null ? MapperFactory . CompactEmployeeDC_Employee . MapToNewDC ( tm . Employee ) : null ;
if ( tm . Parent ! = null )
{
//Vermeide Endlosschleifen. Es gab mal den Fall, da hat es ein Kunde geschafft, dass der Parent eines Textbausteins gleichzeitig ein Kind desselben Bausteins war.
if ( ! parentChildDict . ContainsKey ( tm . Parent . Oid . Value ) )
{
parentChildDict . Add ( tm . Parent . Oid . Value , new List < long > ( ) ) ;
}
parentChildDict [ tm . Parent . Oid . Value ] . Add ( tm . Oid . Value ) ;
bool addParent = true ;
if ( parentChildDict . ContainsKey ( tm . Oid . Value ) )
{
var childOids = parentChildDict [ tm . Oid . Value ] ;
if ( childOids . Contains ( tm . Parent . Oid . Value ) )
{
addParent = false ;
}
}
if ( addParent )
{
dc . Parent = CreateTextModuleDC ( tm . Parent , parentChildDict ) ;
}
}
return dc ;
}
2017-09-26 13:30:20 +02:00
public override TextModuleDC MergeWithDC ( TextModule pEntity , TextModuleDC pDataContract )
{
pDataContract . Position = pEntity . Position ;
pDataContract . TextModuleVersion = pEntity . Version . Value ;
pDataContract . TextModuleOid = pEntity . Oid . Value ;
pDataContract . Text = pEntity . Text ;
pDataContract . Name = pEntity . Name ;
pDataContract . IsOnlyForEmployee = pEntity . IsOnlyForEmployee ;
pDataContract . IsParent = pEntity . IsParent ;
2018-01-05 19:13:18 -04:00
pDataContract . IsExpanded = pEntity . IsExpanded ;
2020-04-02 13:19:14 +02:00
pDataContract . ActivationType = pEntity . IsActive ;
2017-09-26 13:30:20 +02:00
2018-01-05 19:13:18 -04:00
pDataContract . Parent = pEntity . Parent ! = null ? MapperFactory . TextModuleDC_TextModule . MapToNewDC ( pEntity . Parent ) : null ;
2017-09-26 13:30:20 +02:00
2018-01-05 19:13:18 -04:00
pDataContract . ServiceCategory = pEntity . ServiceCategory ! = null ? MapperFactory . ServiceCategoryDC_ServiceCategory . MapToNewDC ( pEntity . ServiceCategory ) : null ;
2017-09-26 13:30:20 +02:00
2018-01-05 19:13:18 -04:00
pDataContract . Employee = pEntity . Employee ! = null ? MapperFactory . CompactEmployeeDC_Employee . MapToNewDC ( pEntity . Employee ) : null ;
2017-09-26 13:30:20 +02:00
return pDataContract ;
}
public override TextModule MergeWithEntity ( TextModuleDC pDataContract , TextModule pEntity )
{
ConcurrencyCheck ( pDataContract . TextModuleVersion , pEntity ) ;
pEntity . Position = pDataContract . Position ;
pEntity . Oid = pDataContract . TextModuleOid ;
pEntity . Text = pDataContract . Text ;
pEntity . Name = pDataContract . Name ;
pEntity . IsOnlyForEmployee = pDataContract . IsOnlyForEmployee ;
pEntity . IsParent = pDataContract . IsParent ;
2018-01-05 19:13:18 -04:00
pEntity . IsExpanded = pDataContract . IsExpanded ;
2020-04-02 13:19:14 +02:00
pEntity . IsActive = pDataContract . ActivationType ;
2017-09-26 13:30:20 +02:00
2018-01-05 19:13:18 -04:00
pEntity . Parent = pDataContract . Parent ! = null ? DAOFactory . GenericDAO . LoadByID < TextModule > ( pDataContract . Parent . TextModuleOid . Value ) : null ;
2017-09-26 13:30:20 +02:00
2018-01-05 19:13:18 -04:00
if ( pDataContract . ServiceCategory ! = null )
2017-09-26 13:30:20 +02:00
{
2018-01-05 19:13:18 -04:00
pEntity . ServiceCategory = pDataContract . ServiceCategory . ServiceCategoryOid . HasValue
? DAOFactory . GenericDAO . LoadByID < ServiceCategory > ( pDataContract . ServiceCategory . ServiceCategoryOid . Value )
: MapperFactory . ServiceCategoryDC_ServiceCategory . MapToNewEntity ( pDataContract . ServiceCategory ) ;
2017-09-26 13:30:20 +02:00
}
2018-01-05 19:13:18 -04:00
else
2017-09-26 13:30:20 +02:00
{
2018-01-05 19:13:18 -04:00
pEntity . ServiceCategory = null ;
2017-09-26 13:30:20 +02:00
}
2018-01-05 19:13:18 -04:00
pEntity . Employee = pDataContract . Employee ! = null ? DAOFactory . GenericDAO . LoadByID < Employee > ( pDataContract . Employee . EmployeeOid ) : null ;
2017-09-26 13:30:20 +02:00
return pEntity ;
}
protected override bool AreDCAndEntityEqual ( TextModuleDC pDC , TextModule pEntity )
{
return pDC . TextModuleOid = = pEntity . Oid ;
}
}
}