using System.Collections.Generic; using BeWo.Data.Access; using BeWo.Data.Entities; using BS.Shared.DataContracts; namespace BeWo.Service.DCEntityMapper { public class TextModuleDC_TextModule : AbstractIDCEntityMapper { public IList CreateDcList(IEnumerable textmodules) { var list = new List(); Dictionary> parentChildDict = new Dictionary>(); foreach (var tm in textmodules) { var dc = CreateTextModuleDC(tm, parentChildDict); list.Add(dc); } return list; } private TextModuleDC CreateTextModuleDC(TextModule tm, Dictionary> 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()); } 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 && tm.Parent.IsActive != BS.Shared.ActivationTypeId.Deleted) { dc.Parent = CreateTextModuleDC(tm.Parent, parentChildDict); } } return dc; } 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; pDataContract.IsExpanded = pEntity.IsExpanded; pDataContract.ActivationType = pEntity.IsActive; pDataContract.Parent = pEntity.Parent != null ? MapperFactory.TextModuleDC_TextModule.MapToNewDC(pEntity.Parent) : null; pDataContract.ServiceCategory = pEntity.ServiceCategory != null ? MapperFactory.ServiceCategoryDC_ServiceCategory.MapToNewDC(pEntity.ServiceCategory) : null; pDataContract.Employee = pEntity.Employee != null ? MapperFactory.CompactEmployeeDC_Employee.MapToNewDC(pEntity.Employee) : null; 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; pEntity.IsExpanded = pDataContract.IsExpanded; pEntity.IsActive = pDataContract.ActivationType; pEntity.Parent = pDataContract.Parent != null ? DAOFactory.GenericDAO.LoadByID(pDataContract.Parent.TextModuleOid.Value) : null; if(pDataContract.ServiceCategory != null) { pEntity.ServiceCategory = pDataContract.ServiceCategory.ServiceCategoryOid.HasValue ? DAOFactory.GenericDAO.LoadByID(pDataContract.ServiceCategory.ServiceCategoryOid.Value) : MapperFactory.ServiceCategoryDC_ServiceCategory.MapToNewEntity(pDataContract.ServiceCategory); } else { pEntity.ServiceCategory = null; } pEntity.Employee = pDataContract.Employee != null ? DAOFactory.GenericDAO.LoadByID(pDataContract.Employee.EmployeeOid) : null; return pEntity; } protected override bool AreDCAndEntityEqual(TextModuleDC pDC, TextModule pEntity) { return pDC.TextModuleOid == pEntity.Oid; } } }