Ejemplo n.º 1
0
        /*public ReleaseEventDetailsContract GetReleaseEventDetailsByName(string name) {
         *
         *      return HandleQuery(session => new ReleaseEventDetailsContract(session.Query<ReleaseEvent>().F(r => r.Name == name)));
         *
         * }*/

        public void Archive(ISession session, ReleaseEvent releaseEvent, ReleaseEventDiff diff, EntryEditEvent reason)
        {
            var agentLoginData = SessionHelper.CreateAgentLoginData(session, PermissionContext);
            var archived       = releaseEvent.CreateArchivedVersion(diff, agentLoginData, reason);

            session.Save(archived);
        }
Ejemplo n.º 2
0
        public ArchivedEventContract(ReleaseEvent ev, ReleaseEventDiff diff)
        {
            ParamIs.NotNull(() => ev);
            ParamIs.NotNull(() => diff);

            Date         = ev.Date;
            Description  = ev.Description;
            Id           = ev.Id;
            Name         = ev.Name;
            Series       = ObjectRefContract.Create(ev.Series);
            SeriesNumber = ev.SeriesNumber;
            WebLinks     = diff.IsIncluded(ReleaseEventEditableFields.WebLinks) ? ev.WebLinks.Select(l => new ArchivedWebLinkContract(l)).ToArray() : null;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns a release event if it can be uniquely identified (by either name or ID).
        /// Otherwise attempts to create a new event based on given name.
        /// Makes an intelligent guess to create either standalone event or series event.
        /// This also makes sure no event with a duplicate name is created.
        /// </summary>
        /// <param name="ctx">Database context.</param>
        /// <param name="userContext">User context.</param>
        /// <param name="contract">Release event data.</param>
        /// <param name="forEntry">Which entry the release event will be created for. Optional, can be null.</param>
        /// <returns>The release event. Can be null if no name or ID is specified. Returned event can be either a new event or existing event.</returns>
        public ReleaseEvent FindOrCreate(IDatabaseContext ctx, IUserPermissionContext userContext, IReleaseEvent contract, IEntryBase forEntry)
        {
            if (contract == null || (contract.Id == 0 && string.IsNullOrWhiteSpace(contract.Name)))
            {
                return(null);
            }

            var existing = ctx.NullSafeLoad <ReleaseEvent>(contract);

            if (existing != null)
            {
                return(existing);
            }

            var searchResult = new ReleaseEventSearch(ctx).Find(contract.Name, userContext.LanguagePreference);

            if (searchResult.IsKnownEvent)
            {
                return(ctx.Load <ReleaseEvent>(searchResult.EventId));
            }

            var series = ctx.NullSafeLoad <ReleaseEventSeries>(searchResult.Series);

            ReleaseEvent newEvent;

            LocalizedStringContract[] names;

            if (series == null)
            {
                var nameValue = searchResult.EventName.EmptyToNull() ?? contract.Name;
                var name      = new LocalizedStringContract(nameValue, ContentLanguageSelection.English);
                names    = new[] { name };
                newEvent = new ReleaseEvent(string.Empty, null, ContentLanguageSelection.English);
            }
            else
            {
                names    = new LocalizedStringContract[0];
                newEvent = new ReleaseEvent(string.Empty, null, series, searchResult.SeriesNumber, searchResult.SeriesSuffix, ContentLanguageSelection.Unspecified, false);
            }

            ctx.Save(newEvent);
            new UpdateEventNamesQuery().UpdateNames(ctx, newEvent, series, false, searchResult.SeriesNumber, searchResult.SeriesSuffix, names);

            var eventDiff = new ReleaseEventDiff(ReleaseEventEditableFields.OriginalName | ReleaseEventEditableFields.Names);

            var archivedVersion = ArchivedReleaseEventVersion.Create(newEvent, eventDiff, ctx.OfType <User>().CreateAgentLoginData(userContext), EntryEditEvent.Created, "Created for " + forEntry);

            ctx.Save(archivedVersion);

            return(newEvent);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Returns a release event if it can be uniquely identified (by either name or ID).
        /// Otherwise attempts to create a new event based on given name.
        /// Makes an intelligent guess to create either standalone event or series event.
        /// This also makes sure no event with a duplicate name is created.
        /// </summary>
        /// <param name="ctx">Database context.</param>
        /// <param name="userContext">User context.</param>
        /// <param name="contract">Release event data.</param>
        /// <param name="forEntry">Which entry the release event will be created for. Optional, can be null.</param>
        /// <returns>The release event. Can be null if no name or ID is specified. Returned event can be either a new event or existing event.</returns>
        public ReleaseEvent FindOrCreate(IDatabaseContext ctx, IUserPermissionContext userContext, IReleaseEvent contract, IEntryBase forEntry)
        {
            if (contract == null || (contract.Id == 0 && string.IsNullOrWhiteSpace(contract.Name)))
            {
                return(null);
            }

            var existing = ctx.NullSafeLoad <ReleaseEvent>(contract);

            if (existing != null)
            {
                return(existing);
            }

            var searchResult = new ReleaseEventSearch(ctx).Find(contract.Name);

            if (searchResult.IsKnownEvent)
            {
                return(ctx.Load <ReleaseEvent>(searchResult.EventId));
            }

            var series = ctx.NullSafeLoad <ReleaseEventSeries>(searchResult.Series);

            ReleaseEvent newEvent;

            if (series == null)
            {
                newEvent = new ReleaseEvent(string.Empty, null, searchResult.EventName.EmptyToNull() ?? contract.Name);
            }
            else
            {
                newEvent = new ReleaseEvent(string.Empty, null, series, searchResult.SeriesNumber, searchResult.SeriesSuffix, null, false);
            }

            ctx.Save(newEvent);

            var eventDiff = new ReleaseEventDiff(ReleaseEventEditableFields.Name);

            eventDiff.Name.Set();

            var archivedVersion = ArchivedReleaseEventVersion.Create(newEvent, eventDiff, ctx.OfType <User>().CreateAgentLoginData(userContext), EntryEditEvent.Created, "Created for " + forEntry);

            ctx.Save(archivedVersion);

            return(newEvent);
        }
Ejemplo n.º 5
0
        public ArchivedEventContract(ReleaseEvent ev, ReleaseEventDiff diff)
        {
            ParamIs.NotNull(() => ev);
            ParamIs.NotNull(() => diff);

            Artists         = diff.IncludeArtists ? ev.Artists.Select(l => new ArchivedArtistForEventContract(l)).ToArray() : null;
            Category        = ev.Category;
            Date            = ev.Date;
            Description     = ev.Description;
            Id              = ev.Id;
            MainPictureMime = ev.PictureMime;
            Names           = diff.IncludeNames ? ev.Names.Names.Select(n => new LocalizedStringContract(n)).ToArray() : null;
            PVs             = diff.IncludePVs ? ev.PVs.Select(p => new ArchivedPVContract(p)).ToArray() : null;
            Series          = ObjectRefContract.Create(ev.Series);
            SeriesNumber    = ev.SeriesNumber;
            SongList        = ObjectRefContract.Create(ev.SongList);
            TranslatedName  = new ArchivedTranslatedStringContract(ev.TranslatedName);
            VenueName       = ev.VenueName;
            WebLinks        = diff.IncludeWebLinks ? ev.WebLinks.Select(l => new ArchivedWebLinkContract(l)).ToArray() : null;
        }
Ejemplo n.º 6
0
        private ArchivedReleaseEventVersion Archive(IDatabaseContext <ReleaseEvent> ctx, ReleaseEvent releaseEvent, ReleaseEventDiff diff, EntryEditEvent reason, string notes)
        {
            var agentLoginData = ctx.OfType <User>().CreateAgentLoginData(permissionContext);
            var archived       = ArchivedReleaseEventVersion.Create(releaseEvent, diff, agentLoginData, reason, notes);

            ctx.Save(archived);
            return(archived);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Updates or creates release event.
        ///
        /// Album release event names will be updated as well if the name changed.
        /// </summary>
        /// <param name="contract">Updated contract. Cannot be null.</param>
        /// <returns>Updated release event data. Cannot be null.</returns>
        /// <exception cref="DuplicateEventNameException">If the event name is already in use.</exception>
        public ReleaseEventContract Update(ReleaseEventForEditContract contract, EntryPictureFileContract pictureData)
        {
            ParamIs.NotNull(() => contract);

            PermissionContext.VerifyManageDatabase();

            return(repository.HandleTransaction(session => {
                ReleaseEvent ev;

                if (contract.Id == 0)
                {
                    var diff = new ReleaseEventDiff();

                    if (!contract.Series.IsNullOrDefault())
                    {
                        var series = session.OfType <ReleaseEventSeries>().Load(contract.Series.Id);
                        ev = new ReleaseEvent(contract.Description, contract.Date, series, contract.SeriesNumber, contract.SeriesSuffix,
                                              contract.DefaultNameLanguage, contract.CustomName);
                        series.AllEvents.Add(ev);
                    }
                    else
                    {
                        ev = new ReleaseEvent(contract.Description, contract.Date, contract.DefaultNameLanguage);
                    }

                    ev.Category = contract.Category;
                    ev.EndDate = contract.EndDate;
                    ev.SongList = session.NullSafeLoad <SongList>(contract.SongList);
                    ev.Status = contract.Status;
                    ev.VenueName = contract.VenueName;

                    if (contract.SongList != null)
                    {
                        diff.SongList.Set();
                    }

                    if (!string.IsNullOrEmpty(contract.VenueName))
                    {
                        diff.Venue.Set();
                    }

                    var weblinksDiff = WebLink.Sync(ev.WebLinks, contract.WebLinks, ev);

                    if (weblinksDiff.Changed)
                    {
                        diff.WebLinks.Set();
                    }

                    var pvDiff = ev.PVs.Sync(contract.PVs, ev.CreatePV);
                    session.OfType <PVForAlbum>().Sync(pvDiff);

                    if (pvDiff.Changed)
                    {
                        diff.PVs.Set();
                    }

                    var artistDiff = ev.SyncArtists(contract.Artists, artistId => session.Load <Artist>(artistId));

                    if (artistDiff.Changed)
                    {
                        diff.Artists.Set();
                    }

                    session.Save(ev);

                    var namesChanged = new UpdateEventNamesQuery().UpdateNames(session, ev, contract.Series, contract.CustomName, contract.SeriesNumber, contract.SeriesSuffix, contract.Names);
                    if (namesChanged)
                    {
                        session.Update(ev);
                    }

                    if (pictureData != null)
                    {
                        diff.MainPicture.Set();
                        SaveImage(ev, pictureData);
                        session.Update(ev);
                    }

                    var archived = Archive(session, ev, diff, EntryEditEvent.Created, string.Empty);
                    AddEntryEditedEntry(session.OfType <ActivityEntry>(), archived);

                    session.AuditLogger.AuditLog(string.Format("created {0}", entryLinkFactory.CreateEntryLink(ev)));

                    new FollowedArtistNotifier().SendNotifications(session, ev, ev.Artists.Where(a => a?.Artist != null).Select(a => a.Artist), PermissionContext.LoggedUser, entryLinkFactory, mailer, enumTranslations);
                }
                else
                {
                    ev = session.Load(contract.Id);
                    permissionContext.VerifyEntryEdit(ev);

                    var diff = new ReleaseEventDiff(DoSnapshot(ev, session));

                    if (ev.Category != contract.Category)
                    {
                        diff.Category.Set();
                    }

                    if (!ev.Date.Equals(contract.Date) || !ev.EndDate.Equals(contract.EndDate))
                    {
                        diff.Date.Set();
                    }

                    if (ev.Description != contract.Description)
                    {
                        diff.Description.Set();
                    }

                    var inheritedLanguage = ev.Series == null || contract.CustomName ? contract.DefaultNameLanguage : ev.Series.TranslatedName.DefaultLanguage;
                    if (ev.TranslatedName.DefaultLanguage != inheritedLanguage)
                    {
                        diff.OriginalName.Set();
                    }

                    var namesChanged = new UpdateEventNamesQuery().UpdateNames(session, ev, contract.Series, contract.CustomName, contract.SeriesNumber, contract.SeriesSuffix, contract.Names);

                    if (namesChanged)
                    {
                        diff.Names.Set();
                    }

                    if (!ev.Series.NullSafeIdEquals(contract.Series))
                    {
                        diff.Series.Set();
                    }

                    if (ev.SeriesNumber != contract.SeriesNumber)
                    {
                        diff.SeriesNumber.Set();
                    }

                    if (ev.SeriesSuffix != contract.SeriesSuffix)
                    {
                        diff.SeriesSuffix.Set();
                    }

                    if (!ev.SongList.NullSafeIdEquals(contract.SongList))
                    {
                        diff.SongList.Set();
                    }

                    if (ev.Status != contract.Status)
                    {
                        diff.Status.Set();
                    }

                    if (!string.Equals(ev.VenueName, contract.VenueName))
                    {
                        diff.Venue.Set();
                    }

                    ev.SetSeries(session.NullSafeLoad <ReleaseEventSeries>(contract.Series));
                    ev.Category = contract.Category;
                    ev.CustomName = contract.CustomName;
                    ev.Date = contract.Date;
                    ev.Description = contract.Description;
                    ev.EndDate = contract.EndDate > contract.Date ? contract.EndDate : null;
                    ev.SeriesNumber = contract.SeriesNumber;
                    ev.SeriesSuffix = contract.SeriesSuffix;
                    ev.SongList = session.NullSafeLoad <SongList>(contract.SongList);
                    ev.Status = contract.Status;
                    ev.TranslatedName.DefaultLanguage = inheritedLanguage;
                    ev.VenueName = contract.VenueName;

                    var weblinksDiff = WebLink.Sync(ev.WebLinks, contract.WebLinks, ev);

                    if (weblinksDiff.Changed)
                    {
                        diff.WebLinks.Set();
                        session.OfType <ReleaseEventWebLink>().Sync(weblinksDiff);
                    }

                    var pvDiff = ev.PVs.Sync(contract.PVs, ev.CreatePV);
                    session.OfType <PVForAlbum>().Sync(pvDiff);

                    if (pvDiff.Changed)
                    {
                        diff.PVs.Set();
                    }

                    var artistDiff = ev.SyncArtists(contract.Artists, artistId => session.Load <Artist>(artistId));

                    if (artistDiff.Changed)
                    {
                        diff.Artists.Set();
                    }

                    if (pictureData != null)
                    {
                        diff.MainPicture.Set();
                        SaveImage(ev, pictureData);
                    }

                    session.Update(ev);

                    var archived = Archive(session, ev, diff, EntryEditEvent.Updated, string.Empty);
                    AddEntryEditedEntry(session.OfType <ActivityEntry>(), archived);

                    var logStr = string.Format("updated properties for {0} ({1})", entryLinkFactory.CreateEntryLink(ev), diff.ChangedFieldsString);
                    session.AuditLogger.AuditLog(logStr);

                    var newSongCutoff = TimeSpan.FromHours(1);
                    if (artistDiff.Added.Any() && ev.CreateDate >= DateTime.Now - newSongCutoff)
                    {
                        var addedArtists = artistDiff.Added.Where(a => a.Artist != null).Select(a => a.Artist).Distinct().ToArray();

                        if (addedArtists.Any())
                        {
                            new FollowedArtistNotifier().SendNotifications(session, ev, addedArtists, PermissionContext.LoggedUser, entryLinkFactory, mailer, enumTranslations);
                        }
                    }
                }

                return new ReleaseEventContract(ev, LanguagePreference);
            }));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Updates or creates release event.
        ///
        /// Album release event names will be updated as well if the name changed.
        /// </summary>
        /// <param name="contract">Updated contract. Cannot be null.</param>
        /// <returns>Updated release event data. Cannot be null.</returns>
        public ReleaseEventContract Update(ReleaseEventDetailsContract contract)
        {
            ParamIs.NotNull(() => contract);

            PermissionContext.VerifyManageDatabase();

            return(repository.HandleTransaction(session => {
                ReleaseEvent ev;

                if (contract.Id == 0)
                {
                    var diff = new ReleaseEventDiff();

                    if (!contract.Series.IsNullOrDefault())
                    {
                        var series = session.OfType <ReleaseEventSeries>().Load(contract.Series.Id);
                        ev = new ReleaseEvent(contract.Description, contract.Date, series, contract.SeriesNumber, contract.SeriesSuffix,
                                              contract.Name, contract.CustomName);
                        series.Events.Add(ev);
                    }
                    else
                    {
                        ev = new ReleaseEvent(contract.Description, contract.Date, contract.Name);
                    }

                    var weblinksDiff = WebLink.Sync(ev.WebLinks, contract.WebLinks, ev);

                    if (weblinksDiff.Changed)
                    {
                        diff.WebLinks.Set();
                    }

                    CheckDuplicateName(session, ev);

                    session.Save(ev);

                    var archived = Archive(session, ev, diff, EntryEditEvent.Created, string.Empty);
                    AddEntryEditedEntry(session.OfType <ActivityEntry>(), archived);

                    session.AuditLogger.AuditLog(string.Format("created {0}", entryLinkFactory.CreateEntryLink(ev)));
                }
                else
                {
                    ev = session.Load(contract.Id);
                    var diff = new ReleaseEventDiff(DoSnapshot(ev, session));

                    if (!ev.Date.Equals(contract.Date))
                    {
                        diff.Date.Set();
                    }

                    if (ev.Description != contract.Description)
                    {
                        diff.Description.Set();
                    }

                    if (ev.Name != contract.Name && (contract.Series == null || contract.CustomName))
                    {
                        diff.Name.Set();
                    }

                    if (!ev.Series.NullSafeIdEquals(contract.Series))
                    {
                        diff.Series.Set();
                    }

                    if (ev.SeriesNumber != contract.SeriesNumber)
                    {
                        diff.SeriesNumber.Set();
                    }

                    if (ev.SeriesSuffix != contract.SeriesSuffix)
                    {
                        diff.SeriesSuffix.Set();
                    }

                    var oldName = ev.Name;

                    ev.Series = session.OfType <ReleaseEventSeries>().NullSafeLoad(contract.Series);
                    ev.CustomName = contract.CustomName;
                    ev.Date = contract.Date;
                    ev.Description = contract.Description;
                    ev.Name = contract.Name;
                    ev.SeriesNumber = contract.SeriesNumber;
                    ev.SeriesSuffix = contract.SeriesSuffix;
                    ev.UpdateNameFromSeries();

                    var weblinksDiff = WebLink.Sync(ev.WebLinks, contract.WebLinks, ev);

                    if (weblinksDiff.Changed)
                    {
                        diff.WebLinks.Set();
                        session.OfType <ReleaseEventWebLink>().Sync(weblinksDiff);
                    }

                    CheckDuplicateName(session, ev);

                    session.Update(ev);

                    var archived = Archive(session, ev, diff, EntryEditEvent.Updated, string.Empty);
                    AddEntryEditedEntry(session.OfType <ActivityEntry>(), archived);

                    var logStr = string.Format("updated properties for {0} ({1})", entryLinkFactory.CreateEntryLink(ev), diff.ChangedFieldsString);
                    session.AuditLogger.AuditLog(logStr);
                }

                return new ReleaseEventContract(ev);
            }));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Updates or creates release event.
        ///
        /// Album release event names will be updated as well if the name changed.
        /// </summary>
        /// <param name="contract">Updated contract. Cannot be null.</param>
        /// <returns>Updated release event data. Cannot be null.</returns>
        public ReleaseEventContract Update(ReleaseEventDetailsContract contract)
        {
            ParamIs.NotNull(() => contract);

            PermissionContext.VerifyManageDatabase();

            return(repository.HandleTransaction(session => {
                ReleaseEvent ev;

                if (contract.Id == 0)
                {
                    if (contract.Series != null)
                    {
                        var series = session.OfType <ReleaseEventSeries>().Load(contract.Series.Id);
                        ev = new ReleaseEvent(contract.Description, contract.Date, series, contract.SeriesNumber, contract.SeriesSuffix);
                        series.Events.Add(ev);
                    }
                    else
                    {
                        ev = new ReleaseEvent(contract.Description, contract.Date, contract.Name);
                    }

                    session.Save(ev);

                    Archive(session, ev, new ReleaseEventDiff(), EntryEditEvent.Created);

                    session.AuditLogger.AuditLog("created " + ev);
                }
                else
                {
                    ev = session.Load(contract.Id);
                    var diff = new ReleaseEventDiff();

                    if (ev.Date != contract.Date)
                    {
                        diff.Date = true;
                    }

                    if (ev.Description != contract.Description)
                    {
                        diff.Description = true;
                    }

                    if (ev.Name != contract.Name)
                    {
                        diff.Name = true;
                    }

                    if (ev.SeriesNumber != contract.SeriesNumber)
                    {
                        diff.SeriesNumber = true;
                    }

                    if (ev.SeriesSuffix != contract.SeriesSuffix)
                    {
                        diff.SeriesSuffix = true;
                    }

                    var oldName = ev.Name;

                    ev.Date = contract.Date;
                    ev.Description = contract.Description;
                    ev.Name = contract.Name;
                    ev.SeriesNumber = contract.SeriesNumber;
                    ev.SeriesSuffix = contract.SeriesSuffix;
                    ev.UpdateNameFromSeries();

                    UpdateAllReleaseEventNames(session, oldName, ev.Name);

                    session.Update(ev);

                    Archive(session, ev, diff, EntryEditEvent.Updated);

                    var logStr = string.Format("updated properties for {0} ({1})", entryLinkFactory.CreateEntryLink(ev), diff.ChangedFieldsString);
                    session.AuditLogger.AuditLog(logStr);
                }

                return new ReleaseEventContract(ev);
            }));
        }
Ejemplo n.º 10
0
        private void Archive(IRepositoryContext <ReleaseEvent> ctx, ReleaseEvent releaseEvent, ReleaseEventDiff diff, EntryEditEvent reason)
        {
            var agentLoginData = ctx.OfType <User>().CreateAgentLoginData(permissionContext);
            var archived       = releaseEvent.CreateArchivedVersion(diff, agentLoginData, reason);

            ctx.Save(archived);
        }
Ejemplo n.º 11
0
        public ReleaseEventContract UpdateEvent(ReleaseEventDetailsContract contract)
        {
            ParamIs.NotNull(() => contract);

            PermissionContext.VerifyPermission(PermissionToken.ManageDatabase);

            return(HandleTransaction(session => {
                ReleaseEvent ev;

                if (contract.Id == 0)
                {
                    if (contract.Series != null)
                    {
                        var series = session.Load <ReleaseEventSeries>(contract.Series.Id);
                        ev = new ReleaseEvent(contract.Description, contract.Date, series, contract.SeriesNumber);
                        series.Events.Add(ev);
                    }
                    else
                    {
                        ev = new ReleaseEvent(contract.Description, contract.Date, contract.Name);
                    }

                    session.Save(ev);

                    Archive(session, ev, new ReleaseEventDiff(), EntryEditEvent.Created);

                    AuditLog("created " + ev, session);
                }
                else
                {
                    ev = session.Load <ReleaseEvent>(contract.Id);
                    var diff = new ReleaseEventDiff();

                    if (ev.Date != contract.Date)
                    {
                        diff.Date = true;
                    }

                    if (ev.Description != contract.Description)
                    {
                        diff.Description = true;
                    }

                    if (ev.Name != contract.Name)
                    {
                        diff.Name = true;
                    }

                    if (ev.SeriesNumber != contract.SeriesNumber)
                    {
                        diff.SeriesNumber = true;
                    }

                    ev.Date = contract.Date;
                    ev.Description = contract.Description;
                    ev.Name = contract.Name;
                    ev.SeriesNumber = contract.SeriesNumber;

                    session.Update(ev);

                    Archive(session, ev, diff, EntryEditEvent.Updated);

                    var logStr = string.Format("updated properties for {0} ({1})", CreateEntryLink(ev), diff.ChangedFieldsString);
                    AuditLog(logStr, session);
                }

                return new ReleaseEventContract(ev);
            }));
        }