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

using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class EmployeeWohnheimbuchungRelationDC_Employee2Wohnheimbuchung : AbstractIDCEntityMapper<Wohnheimbuchung2Employee, WohnheimbuchungEmployeeRelationDC>
    {
        public override WohnheimbuchungEmployeeRelationDC MergeWithDC(Wohnheimbuchung2Employee pEntity, WohnheimbuchungEmployeeRelationDC pDataContract)
        {
            pDataContract.Employee2WohnheimOid     = pEntity.Oid;
            pDataContract.Employee2WohnheimVersion = pEntity.Version;
            pDataContract.Employee                 = MapperFactory.CompactEmployeeDC_Employee.MapToNewDC(pEntity.Employee);
            pDataContract.StartDate                = pEntity.StartDate;
            pDataContract.EndDate                  = pEntity.EndDate;

            return pDataContract;
        }

        public override Wohnheimbuchung2Employee MergeWithEntity(WohnheimbuchungEmployeeRelationDC pDataContract, Wohnheimbuchung2Employee pEntity)
        {
            ConcurrencyCheck(pDataContract.Employee2WohnheimVersion, pEntity);

            pEntity.Employee = null;

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

            pEntity.StartDate = pDataContract.StartDate;
            pEntity.EndDate = pDataContract.EndDate;

            return pEntity;
        }

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

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