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

using BS.Shared.DataContracts;

namespace BeWo.Service.DCEntityMapper
{
    public class PlacementDC_Placement : AbstractIDCEntityMapper<Placement, PlacementDC>
    {
        public override PlacementDC MergeWithDC(Placement pEntity, PlacementDC pDataContract)
        {
            pDataContract.PlacementVersion = pEntity.Version;
            pDataContract.PlacementOid = pEntity.Oid;
            pDataContract.PlacementDate = pEntity.PlacementDate;
            if (pEntity.Objective != null)
            {
                pDataContract.Objective = MapperFactory.ValueListEntryDC_ValueListEntry.MapToNewDC(pEntity.Objective);
            }

            return pDataContract;
        }

        public override Placement MergeWithEntity(PlacementDC pDataContract, Placement pEntity)
        {
            this.ConcurrencyCheck(pDataContract.PlacementVersion, pEntity);

            pEntity.Oid = pDataContract.PlacementOid;
            pEntity.PlacementDate = pDataContract.PlacementDate;
            if (pDataContract.Objective == null)
            {
                pEntity.Objective = null;
            }
            else if (pEntity.Objective == null || pEntity.Objective.Oid != pDataContract.Objective.ValueListEntryOid)
            {
                pEntity.Objective = DAOFactory.GenericDAO.LoadByID<ValueListEntry>(pDataContract.Objective.ValueListEntryOid.Value);
            }

            return pEntity;
        }

        protected override bool AreDCAndEntityEqual(PlacementDC pDC, Placement pEntity)
        {
            if (pDC.PlacementOid == null)
            {
                return false;
            }

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