193 lines
5.1 KiB
C#
193 lines
5.1 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
using BeWo.Data.Entities;
|
|
using BeWo.Service.Core;
|
|
using BS.Shared;
|
|
using BS.Shared.Extensions;
|
|
|
|
namespace BeWo.Service.DCEntityMapper
|
|
{
|
|
public abstract class AbstractIDCEntityMapper<TEntity, TDC> : IDCEntityMapper<TEntity, TDC>
|
|
where TDC : class, new() where TEntity : BeWoEntityBase, new()
|
|
{
|
|
public bool ConcurrencyCheck(long? pDataContractVersion, BeWoEntityBase pEntity)
|
|
{
|
|
return ServiceLogic.ConcurrencyCheck(pDataContractVersion, pEntity);
|
|
}
|
|
|
|
public virtual List<TDC> MapToNewDCs(IEnumerable<TEntity> pEntitys)
|
|
{
|
|
List<TDC> lResult = new List<TDC>();
|
|
if (pEntitys != null)
|
|
{
|
|
foreach (TEntity iEntity in pEntitys)
|
|
{
|
|
lResult.Add(this.MergeWithDC(iEntity, CreateNewDC()));
|
|
}
|
|
}
|
|
return lResult;
|
|
}
|
|
|
|
public virtual TDC CreateOrMapToNewDC(TEntity pEntity, bool nullDCOnNullEntity = false)
|
|
{
|
|
if (pEntity is object)
|
|
return MapToNewDC(pEntity);
|
|
|
|
if (nullDCOnNullEntity)
|
|
return null;
|
|
|
|
return CreateNewDC();
|
|
}
|
|
public virtual TDC MapToNewDC(TEntity pEntity, bool dontCreateNewDConNullEntity)
|
|
{
|
|
if (dontCreateNewDConNullEntity || pEntity is object)
|
|
return MapToNewDC(pEntity);
|
|
|
|
return CreateNewDC();
|
|
}
|
|
public virtual TDC MapToNewDC(TEntity pEntity)
|
|
{
|
|
return MergeWithDC(pEntity, CreateNewDC());
|
|
}
|
|
public virtual TDC CreateNewDC()
|
|
{
|
|
return new TDC();
|
|
}
|
|
|
|
public virtual List<TEntity> MapToNewEntities(IEnumerable<TDC> pDataContracts)
|
|
{
|
|
List<TEntity> lResult = new List<TEntity>();
|
|
if (pDataContracts != null)
|
|
{
|
|
foreach (TDC iDataContract in pDataContracts)
|
|
{
|
|
lResult.Add(this.MergeWithEntity(iDataContract, new TEntity()));
|
|
}
|
|
}
|
|
return lResult;
|
|
}
|
|
public virtual TEntity MapToNewEntity(TDC pDataContract, bool ignoreNullDC)
|
|
{
|
|
if (ignoreNullDC || pDataContract is object)
|
|
return MergeWithEntity(pDataContract, new TEntity());
|
|
|
|
return CreateNewEntity();
|
|
}
|
|
public virtual TEntity MapToNewEntity(TDC pDataContract)
|
|
{
|
|
return MergeWithEntity(pDataContract, new TEntity());
|
|
}
|
|
public virtual TEntity CreateNewEntity()
|
|
{
|
|
return new TEntity();
|
|
}
|
|
|
|
public abstract TDC MergeWithDC(TEntity pEntity, TDC pDataContract);
|
|
|
|
public virtual void MergeWithEntitys(IList<TDC> pDataContractList, IList<TEntity> pEntityList)
|
|
{
|
|
if (pEntityList.Count == 0 && pDataContractList != null && pDataContractList.Count > 0)
|
|
{
|
|
pEntityList.AddRange(pDataContractList.Select(dc => this.MapToNewEntity(dc)));
|
|
return;
|
|
}
|
|
|
|
if (pDataContractList == null || pDataContractList.Count == 0)
|
|
{
|
|
pEntityList.Clear();
|
|
return;
|
|
}
|
|
|
|
// remove deleted
|
|
pEntityList.RemoveRange(entity => pDataContractList.Count(dc => this.AreDCAndEntityEqual(dc, entity)) == 0);
|
|
|
|
// merge same
|
|
var lToMerge = pEntityList.Select(entity => new { Entity = entity, DataContract = pDataContractList.Single(dc => this.AreDCAndEntityEqual(dc, entity)) });
|
|
|
|
foreach (var iToMerge in lToMerge)
|
|
{
|
|
MergeWithEntity(iToMerge.DataContract, iToMerge.Entity);
|
|
}
|
|
|
|
// add new
|
|
List<TDC> newList = new List<TDC>();
|
|
|
|
foreach (TDC dc in pDataContractList)
|
|
{
|
|
bool isnew = true;
|
|
foreach (TEntity entity in pEntityList)
|
|
{
|
|
if (isnew && AreDCAndEntityEqual(dc, entity))
|
|
{
|
|
isnew = false;
|
|
}
|
|
}
|
|
if (isnew)
|
|
newList.Add(dc);
|
|
}
|
|
|
|
foreach (TDC newDC in newList)
|
|
{
|
|
pEntityList.Add(MapToNewEntity(newDC));
|
|
}
|
|
|
|
//pEntityList.AddRange(
|
|
// pDataContractList
|
|
// .Where(dc => pEntityList
|
|
// .Count(entity => AreDCAndEntityEqual(dc, entity)) == 0)
|
|
// .Select(dc => MapToNewEntity(dc)));
|
|
|
|
}
|
|
public virtual TEntity MergeOrCreateWithEntity(TDC pDataContract, TEntity pEntity)
|
|
{
|
|
if (pDataContract is object)
|
|
{
|
|
if (pEntity is null)
|
|
pEntity = CreateNewEntity();
|
|
|
|
return MergeWithEntity(pDataContract, pEntity);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
public abstract TEntity MergeWithEntity(TDC pDataContract, TEntity pEntity);
|
|
|
|
protected abstract bool AreDCAndEntityEqual(TDC pDC, TEntity pEntity);
|
|
|
|
// Ausgelagert in AiCore - Kann bei Zeit entfernt werden
|
|
//public virtual Dictionary<string, object> ToJsonDictionary(TDC pDC)
|
|
//{
|
|
// throw new NotImplementedException("ToJsonDictionary");
|
|
//}
|
|
|
|
//public Dictionary<string, object> ToJsonDictionary(TEntity pEntity)
|
|
//{
|
|
// var dc = MapToNewDC(pEntity);
|
|
// var dict = ToJsonDictionary(dc);
|
|
// return dict;
|
|
//}
|
|
|
|
//public IList<Dictionary<string, object>> ToJsonDictionary(IList<TEntity> pEntities)
|
|
//{
|
|
// var dcs = MapToNewDCs(pEntities);
|
|
// var dicts = ToJsonDictionary(dcs);
|
|
// return dicts;
|
|
//}
|
|
|
|
//public IList<Dictionary<string, object>> ToJsonDictionary(IList<TDC> pDCs)
|
|
//{
|
|
// var dicts = new List<Dictionary<string, object>>();
|
|
|
|
// if (pDCs == null)
|
|
// return dicts;
|
|
|
|
// foreach (var dc in pDCs)
|
|
// dicts.Add(ToJsonDictionary(dc));
|
|
|
|
// return dicts;
|
|
//}
|
|
}
|
|
} |