public MasterDataAllocationDTO Map(MasterDataAllocation allocation)
 {
     if (allocation == null) return null;
     var allocationDto = Mapper.Map<MasterDataAllocation, MasterDataAllocationDTO>(allocation);
     return allocationDto;
 }
 public MasterDataAllocationDTO Map(MasterDataAllocation allocation, MasterDataAllocationType allocationType)
 {
     MasterDataAllocationDTO allocationDto = new MasterDataAllocationDTO(); 
     if (allocation == null) return null;
     switch (allocationType)
     {
             case MasterDataAllocationType.CommodityProducerCentreAllocation:
                 allocationDto = Mapper.Map<MasterDataAllocation, CommodityProducerCentreAllocationDTO>(allocation);
             break;
             case MasterDataAllocationType.RouteCostCentreAllocation:
                 allocationDto = Mapper.Map<MasterDataAllocation, RouteCostCentreAllocationDTO>(allocation);
             break;
             case MasterDataAllocationType.RouteCentreAllocation:
                 allocationDto = Mapper.Map<MasterDataAllocation, RouteCentreAllocationDTO>(allocation);
             break;
             case MasterDataAllocationType.RouteRegionAllocation:
                 allocationDto = Mapper.Map<MasterDataAllocation, RouteRegionAllocationDTO>(allocation);
             break;
         default:
                 throw new Exception("Failed to map to DTO " + allocationType);
     }
     return allocationDto;
 }
        protected Guid AddMasterDataAllocation(Guid entityAId, Guid entityBId, MasterDataAllocationType allocationType)
        {
            MasterDataAllocation allocation = new MasterDataAllocation(Guid.NewGuid())
            {
                AllocationType = allocationType,
                EntityAId = entityAId,
                EntityBId = entityBId,
            };

            allocation._SetStatus(EntityStatus.Active);
            return _masterDataAllocationRepository.Save(allocation);
        }
        private List<Centre> LoadAssignedCenters(Guid commodityProducerId,Guid selectedCentreId)
        {
            var allocation = new MasterDataAllocation(Guid.NewGuid())
                {
                    AllocationType = MasterDataAllocationType.CommodityProducerCentreAllocation,
                    EntityAId = commodityProducerId,
                    EntityBId = selectedCentreId
                };


                _masterDataAllocationRepository.Save(allocation);
                var assignedMasterDataAllocation =
                    _masterDataAllocationRepository.GetByAllocationType(
                        MasterDataAllocationType.CommodityProducerCentreAllocation, commodityProducerId);

                var unassignedCenters = _centreRepository.GetAll().ToList();

                foreach (var c in assignedMasterDataAllocation)
                {
                    var center = _centreRepository.GetById(c.EntityBId);

                    _assignedCentresList.Add(center);
                    if (center != null && unassignedCenters.Any(l => l == center))
                    {
                        unassignedCenters.Remove(center);
                    }
                }
            
            ViewBag.UnassignedCentresList = unassignedCenters.Select(r => new { r.Id, r.Name }).ToDictionary(d => d.Id, d => d.Name);

            //if (assignedFarmCentres == null)
            //{
                List<Centre> assignedFarmCentres = new List<Centre>();
               assignedFarmCentres.AddRange(_assignedCentresList);

            return assignedFarmCentres;
        }
 public void AssignCenter(Guid centerId, Guid commodityProducerId)
 {
     var allocation = new MasterDataAllocation(Guid.NewGuid())
     {
         AllocationType = MasterDataAllocationType.CommodityProducerCentreAllocation,
         EntityAId = commodityProducerId,
         EntityBId = centerId
     };
     _masterDataAllocationRepository.Save(allocation);
 }
        private MasterEntity GetMasterDataAllocationEntity(MasterBaseDTO masterBase)
        {
            MasterDataAllocationDTO dto = masterBase as MasterDataAllocationDTO;
            MasterDataAllocation entity = entity = new MasterDataAllocation(dto.MasterId);

            if (dto is RouteCentreAllocationDTO)
            {
                entity.EntityAId = ((RouteCentreAllocationDTO) dto).RouteId;
                entity.EntityBId = ((RouteCentreAllocationDTO) dto).CentreId;
                entity.AllocationType = MasterDataAllocationType.RouteCentreAllocation;
            }

            if (dto is RouteCostCentreAllocationDTO)
            {
                entity.EntityAId = ((RouteCostCentreAllocationDTO)dto).RouteId;
                entity.EntityBId = ((RouteCostCentreAllocationDTO)dto).CostCentreId;
                entity.AllocationType = MasterDataAllocationType.RouteCostCentreAllocation;
            }

            if (dto is CommodityProducerCentreAllocationDTO)
            {
                entity.EntityAId = ((CommodityProducerCentreAllocationDTO)dto).CommodityProducerId;
                entity.EntityBId = ((CommodityProducerCentreAllocationDTO)dto).CentreId;
                entity.AllocationType = MasterDataAllocationType.CommodityProducerCentreAllocation;
            }

            if (dto is RouteRegionAllocationDTO)
            {
                entity.EntityAId = ((RouteRegionAllocationDTO)dto).RouteId;
                entity.EntityBId = ((RouteRegionAllocationDTO)dto).RegionId;
                entity.AllocationType = MasterDataAllocationType.RouteRegionAllocation;
            }

            entity._DateCreated = dto.DateCreated;
            entity._DateLastUpdated = dto.DateLastUpdated;
            entity._Status = (EntityStatus)dto.StatusId;

            return entity;
        }
        public HttpResponseMessage CommodityProducerListAdd(List<CommodityProducer> commodityProducersList)
        {
            var response = new ResponseBool { Success = false };
            using (TransactionScope scope = TransactionUtils.CreateTransactionScope())
            {
                try
                {
                    foreach (var commodityProducer in commodityProducersList)
                    {
                        _commodityProducerRepository.Save(commodityProducer);


                        var existingAllocationForThisProducer = _masterDataAllocationRepository.GetByAllocationType(
                            MasterDataAllocationType.CommodityProducerCentreAllocation)
                            .Where(n => n.EntityAId == commodityProducer.Id);

                        var unallocated =
                            existingAllocationForThisProducer.Where(
                                n =>
                                commodityProducer.CommodityProducerCentres.Select(c => c.Id).All(
                                    cId => n.EntityBId != cId));

                        foreach (var centre in commodityProducer.CommodityProducerCentres)
                        {
                            var allocation = new MasterDataAllocation(Guid.NewGuid())
                                                 {
                                                     _Status = EntityStatus.Active,
                                                     AllocationType =
                                                         MasterDataAllocationType.CommodityProducerCentreAllocation,
                                                     EntityAId = commodityProducer.Id,
                                                     EntityBId = centre.Id
                                                 };
                            _masterDataAllocationRepository.Save(allocation);
                        }

                        foreach (var allocation in unallocated)
                        {
                            _masterDataAllocationRepository.DeleteAllocation(allocation.Id);
                        }
                    }
                    response.Success = true;
                    response.ErrorInfo = "Commodity producer successfully added.";
                    scope.Complete();
                }
                catch (DomainValidationException dve)
                {
                    string errorMsg =
                        dve.ValidationResults.Results.Aggregate("Error: Invalid commodity producer fields.\n",
                                                                (current, msg) =>
                                                                current + ("\t- " + msg.ErrorMessage + "\n"));
                    response.ErrorInfo = errorMsg;
                    _log.Error(errorMsg, dve);
                }
                catch (Exception ex) //any other
                {
                    response.ErrorInfo = "Error: An error occurred when saving the commodity producer.\n" +
                                         ex.ToString();
                    _log.Error("Error: An error occurred when saving the commodity supplier.", ex);
                }
            }
            return Request.CreateResponse(HttpStatusCode.OK, response);
        }