예제 #1
0
        public Airing Save(Airing airing, bool hasImmediateDelivery, bool updateHistorical)
        {
            var currentCollection    = _database.GetCollection <Airing>(DataStoreConfiguration.CurrentAssetsCollection);
            var expiredCollection    = _database.GetCollection <Airing>(DataStoreConfiguration.ExpiredAssetsCollection);
            var historicalCollection = _database.GetCollection <Airing>(DataStoreConfiguration.HistoricalAssetsCollection);

            if (updateHistorical)
            {
                var airingInString = JsonConvert.SerializeObject(airing);
                var historyAiring  = JsonConvert.DeserializeObject <Airing>(airingInString);
                historyAiring.Id = ObjectId.Empty;
                historicalCollection.Save(historyAiring);
            }

            airing.Id = ObjectId.Empty;

            var query = Query.EQ("AssetId", airing.AssetId);

            bool hasActiveFlights = airing.Flights.Any(e => e.End > DateTime.UtcNow);

            var currentAsset = currentCollection.FindOne(query);
            var expiredAsset = expiredCollection.FindOne(query);

            if (currentAsset != null || hasActiveFlights || hasImmediateDelivery)
            {
                currentCollection.Update(query,
                                         Update.Replace(airing),
                                         UpdateFlags.Upsert);

                if (expiredAsset != null)
                {
                    expiredCollection.Remove(query);
                    _dfStatusMover.MoveToCurrentCollection(airing.AssetId);
                }

                return(_getAiringQuery.GetBy(airing.AssetId));
            }

            expiredCollection.Update(query,
                                     Update.Replace(airing),
                                     UpdateFlags.Upsert);

            return(_getAiringQuery.GetBy(airing.AssetId, AiringCollection.ExpiredCollection));
        }
예제 #2
0
 public BLModel.Airing GetBy(string assetId, AiringCollection getFrom = AiringCollection.CurrentOrExpiredCollection)
 {
     return
         (airingQueryHelper.GetBy(assetId, getFrom)
          .ToBusinessModel <DLModel.Airing, BLModel.Airing>());
 }
예제 #3
0
        public AiringValidator(IGetAiringQuery airingQuery, IDestinationQuery desQuery)
        {
            _airingQuery = airingQuery;
            _desQuery    = desQuery;


            RuleSet(AiringValidationRuleSet.PostAiring.ToString(), () =>
            {
                // Verify required fields are provided
                RuleFor(c => c)
                .Must(c => !String.IsNullOrEmpty(c.Network))
                .WithMessage("Brand required")
                .Must(c => c.Flights.Any())
                .WithMessage("Flight information required")
                .Must(c => !String.IsNullOrEmpty(c.ReleaseBy))
                .WithMessage("ReleaseBy field required");

                // Detect deleted airing
                RuleFor(c => c.AssetId)
                .Must(x =>
                {
                    return((String.IsNullOrEmpty(x)) ? true : !_airingQuery.IsAiringDeleted(x));
                })
                .WithMessage("Airing previously deleted. Cannot reuse airing id: {0}", c => c.AssetId);

                // Validate flight information
                RuleForEach(c => c.Flights)
                .SetValidator(new FlightValidator(_desQuery));
            });



            RuleSet(AiringValidationRuleSet.DeleteAiring.ToString(), () =>
            {
                // Verify required fields are provided
                RuleFor(c => c)
                .Must(c => !String.IsNullOrEmpty(c.AssetId))
                .WithMessage("Airing Id required")
                .Must(c => !String.IsNullOrEmpty(c.ReleaseBy))
                .WithMessage("ReleaseBy field required")
                .DependentRules(dr =>
                {
                    // Verify that the given airingId exist
                    Func <String, bool> airingIdExistRule = new Func <String, bool>((airingId) =>
                    {
                        try
                        {
                            return(_airingQuery.GetBy(airingId) != null);
                        }
                        catch (Exception)
                        {
                            return(false);
                        }
                    });

                    dr.RuleFor(c => c.AssetId)
                    .Must(airingIdExistRule)
                    .WithMessage("Provided AiringId does not exist.");
                });
            });

            RuleSet(AiringValidationRuleSet.PostPlaylist.ToString(), () =>
            {
                // Verify required fields are provided
                RuleFor(c => c)
                .Must(c => !String.IsNullOrEmpty(c.AssetId))
                .WithMessage("AiringId is required OR AiringId does not exist.")
                .Must(c => !String.IsNullOrEmpty(c.ReleaseBy))
                .WithMessage("ReleaseBy field required")
                .Must(c => (c.Versions != null && c.Versions.Any()))
                .WithMessage("Version not exists in existing airing.")
                .Must(c => (c.PlayList != null && c.PlayList.Any()))
                .WithMessage("Playlist is required")
                .DependentRules(pl =>
                {
                    // Verify that the given airingId exist
                    Func <BLModel.Airing, bool> playlistRule = new Func <BLModel.Airing, bool>((airing) =>
                    {
                        try
                        {
                            var foundCids = new List <string>();

                            foreach (var segment in airing.PlayList.Where(e => e.ItemType == "Segment"))
                            {
                                var versionFound = false;
                                foreach (var version in airing.Versions)
                                {
                                    if (segment.Id.StartsWith(version.ContentId))
                                    {
                                        versionFound = true;

                                        if (!foundCids.Contains(version.ContentId))
                                        {
                                            foundCids.Add(version.ContentId);
                                        }

                                        break;
                                    }
                                }

                                //Return validation error if Segment CID's not matches with Version CID's
                                if (!versionFound)
                                {
                                    return(false);
                                }
                            }


                            //Returns true if all version matches with Segements, if not then it will return false.
                            return(foundCids.Count == airing.Versions.Count);
                        }
                        catch (Exception)
                        {
                            return(false);
                        }
                    });

                    pl.RuleFor(c => c)
                    .Must(playlistRule)
                    .WithMessage("Provided Segment CID(s) does not match with Version CID(s) {0}.",
                                 c => string.Join(",", c.Versions.Select(e => e.ContentId)));
                });
            });
        }