using BeWo.Data.Access;
using BeWo.Data.Entities;

using BS.Shared;
using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class WohnheimEmployeeRelDC_Employee2WohnheimMapper : AbstractIDCEntityMapper<Employee2Wohnheim, WohnheimEmployeeRelationDC>
    {
        public override WohnheimEmployeeRelationDC MergeWithDC(Employee2Wohnheim pEntity, WohnheimEmployeeRelationDC pDataContract)
        {
            if (pEntity.ValueList.Count > 0)
            {
                pDataContract.RelationRole = MapperFactory.ValueListEntryDC_ValueListEntry.MapToNewDC(pEntity.ValueList[0].Entry);
            }

            pDataContract.Employee = MapperFactory.CompactEmployeeDC_Employee.MapToNewDC(pEntity.Employee);
            pDataContract.Wohnheim = MapperFactory.CompactWohnheimDC_Wohnheim.MapToNewDC(pEntity.Wohnheim);
            pDataContract.Employee2WohnheimOid = pEntity.Oid;
            pDataContract.Employee2WohnheimVersion = pEntity.Version;
            pDataContract.Notice = pEntity.Notice;

            return pDataContract;
        }

        public override Employee2Wohnheim MergeWithEntity(WohnheimEmployeeRelationDC pDataContract, Employee2Wohnheim pEntity)
        {
            this.ConcurrencyCheck(pDataContract.Employee2WohnheimVersion, pEntity);

            MapperFactory.ValueListEntryDC_ValueListEntry.MapUniqueTypeValueListEntryBack2Entity(pDataContract.RelationRole, pEntity.ValueList, ValueListEntryType.StaffRoleType, TableID.Employee2Wohnheim);

            if (pDataContract.Employee != null)
            {
                pEntity.Employee = DAOFactory.GenericDAO.LoadByID<Employee>(pDataContract.Employee.EmployeeOid);
            }

            if (pDataContract.Wohnheim != null)
            {
                pEntity.Wohnheim = DAOFactory.GenericDAO.LoadByID<Wohnheim>(pDataContract.Wohnheim.WohnheimOid);
            }

            pEntity.Oid = pDataContract.Employee2WohnheimOid;
            pEntity.Notice = pDataContract.Notice;

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(WohnheimEmployeeRelationDC pDC, Employee2Wohnheim pEntity)
        {
            if (pDC.Employee2WohnheimOid == null)
            {
                return false;
            }

            return pDC.Employee2WohnheimOid == pEntity.Oid;
        }
    }
}