Пример #1
0
        public void Delete(int?id)
        {
            if (id == null)
            {
                throw new PhraseException("You must select an Phrase to delete.");
            }

            EntityAlarmRepository entityAlarmRepository = new EntityAlarmRepository();
            AuthorAlarmRepository authorAlarmRepository = new AuthorAlarmRepository();

            using (SentimentAnalysisContext context = new SentimentAnalysisContext())
            {
                Entities.Phrase toRemove = context.Phrases.FirstOrDefault(p => p.Id == id);

                if (toRemove == null)
                {
                    throw new PhraseException("Phrase not found.");
                }

                context.Phrases.Remove(toRemove);
                context.SaveChanges();

                foreach (var alarm in Helper.Instance.GetEntityAlarms(context.EntityAlarms))
                {
                    alarm.ReAnalyePhrases(Helper.Instance.GetPhrases(context.Phrases));
                    entityAlarmRepository.Modify(alarm.Id, alarm);
                }

                foreach (var alarm in Helper.Instance.GetAuthorAlarms(context.AuthorAlarms))
                {
                    alarm.ReAnalyePhrases(Helper.Instance.GetPhrases(context.Phrases));
                    authorAlarmRepository.Modify(alarm.Id, alarm);
                }
            }
        }
Пример #2
0
        public void Add(Sentiment sentiment)
        {
            using (SentimentAnalysisContext context = new SentimentAnalysisContext())
            {
                if (context.Sentiments.Any(s => s.Id == sentiment.Id))
                {
                    throw new SentimentException("An Sentiment with the same ID already exists.");
                }

                Exists(sentiment);

                Entities.Sentiment toAdd = Helper.Instance.ToSentimentEF(sentiment);
                context.Sentiments.Add(toAdd);
                context.SaveChanges();
                sentiment.Id = toAdd.Id;

                PhraseRepository      phraseRepository = new PhraseRepository();
                EntityAlarmRepository alarmRepository  = new EntityAlarmRepository();

                foreach (var phrase in Helper.Instance.GetPhrases(context.Phrases))
                {
                    try
                    {
                        phraseRepository.Modify(phrase.Id, phrase);
                    }
                    catch (AnalysisException) { }
                }

                foreach (var alarm in Helper.Instance.GetEntityAlarms(context.EntityAlarms).Where(a => a.Type == sentiment.Type && !a.IsEnabled()))
                {
                    alarmRepository.Modify(alarm.Id, alarm);
                }
            }
        }
Пример #3
0
        public void Modify(int?id, Sentiment sentiment)
        {
            if (!id.HasValue)
            {
                throw new SentimentException("You must select an Sentiment to modify.");
            }

            PhraseRepository      phraseRepository = new PhraseRepository();
            EntityAlarmRepository alarmRepository  = new EntityAlarmRepository();

            using (SentimentAnalysisContext context = new SentimentAnalysisContext())
            {
                var sentimentFound = context.Sentiments.FirstOrDefault(s => s.Id == id);

                if (id != sentiment.Id || !sentimentFound.Word.Equals(sentiment.Word))
                {
                    Exists(sentiment);
                }

                SentimentType oldType = sentimentFound.Type;

                sentimentFound.Word = sentiment.Word;
                sentimentFound.Type = sentiment.Type;

                context.Sentiments.AddOrUpdate(sentimentFound);
                context.SaveChanges();

                foreach (var phrase in Helper.Instance.GetPhrases(context.Phrases).Where(p => p.Type == null || p.Type == oldType))
                {
                    phrase.Type   = null;
                    phrase.Entity = null;

                    try
                    {
                        phrase.AnalyzePhrase(Helper.Instance.GetEntities(context.Entities), Helper.Instance.GetSentiments(context.Sentiments));
                        phraseRepository.Modify(phrase.Id, phrase);
                    }
                    catch (AnalysisException)
                    {
                        phraseRepository.Modify(phrase.Id, phrase);
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                }

                foreach (var alarm in Helper.Instance.GetEntityAlarms(context.EntityAlarms))
                {
                    alarm.ReAnalyePhrases(Helper.Instance.GetPhrases(context.Phrases));
                    alarmRepository.Modify(alarm.Id, alarm);
                }
            }
        }
        public void Modify(int?id, Entity entity)
        {
            if (!id.HasValue)
            {
                throw new EntityException("You must select an Entity to modify.");
            }

            PhraseRepository      phraseRepository = new PhraseRepository();
            EntityAlarmRepository alarmRepository  = new EntityAlarmRepository();

            using (SentimentAnalysisContext context = new SentimentAnalysisContext())
            {
                var entityFound = context.Entities.FirstOrDefault(e => e.Id == id);

                if (id != entity.Id || !entityFound.Name.Equals(entity.Name))
                {
                    Exists(entity);
                }

                entityFound.Name = entity.Name;

                context.SaveChanges();

                foreach (var phrase in context.Phrases.Where(p => p.Entity == null || p.Entity.Id == entityFound.Id))
                {
                    phrase.Entity = null;

                    try
                    {
                        Phrase toModify = Helper.Instance.ToPhraseBL(phrase);
                        toModify.AnalyzePhrase(Helper.Instance.GetEntities(context.Entities), Helper.Instance.GetSentiments(context.Sentiments));
                        phraseRepository.Modify(phrase.Id, toModify);
                    }
                    catch (AnalysisException) { }
                }

                foreach (var alarm in context.EntityAlarms.ToList())
                {
                    EntityAlarm toModify = Helper.Instance.ToEntityAlarmBL(alarm);
                    toModify.ReAnalyePhrases(Helper.Instance.GetPhrases(context.Phrases));
                    alarmRepository.Modify(alarm.Alarm.Id, toModify);
                }
            }
        }
Пример #5
0
        public void Delete(int?id)
        {
            if (!id.HasValue)
            {
                throw new SentimentException("You must select an Sentiment to delete.");
            }

            EntityAlarmRepository alarmRepository = new EntityAlarmRepository();

            using (SentimentAnalysisContext context = new SentimentAnalysisContext())
            {
                Entities.Sentiment toRemove = context.Sentiments.FirstOrDefault(s => s.Id == id);

                if (toRemove == null)
                {
                    throw new SentimentException("Sentiment not found.");
                }

                context.Sentiments.Remove(toRemove);
                context.SaveChanges();

                foreach (var phrase in context.Phrases.Where(p => p.Type != null && p.Type == toRemove.Type))
                {
                    phrase.Type = null;
                }

                foreach (var alarm in context.EntityAlarms.ToList())
                {
                    EntityAlarm toModify = Helper.Instance.ToEntityAlarmBL(alarm);

                    try
                    {
                        alarmRepository.Modify(alarm.Id, toModify);
                    }
                    catch { }
                }
            }
        }
Пример #6
0
        public void Modify(int?id, Phrase phrase)
        {
            if (!id.HasValue)
            {
                throw new PhraseException("You must select an Phrase to modify.");
            }

            EntityAlarmRepository entityAlarmRepository = new EntityAlarmRepository();
            AuthorAlarmRepository authorAlarmRepository = new AuthorAlarmRepository();

            using (SentimentAnalysisContext context = new SentimentAnalysisContext())
            {
                Phrase phraseFound = Get(id.Value);

                if (!phraseFound.Word.Equals(phrase.Word))
                {
                    Exists(phrase);
                }

                phraseFound.Word       = phrase.Word;
                phraseFound.PostedDate = phrase.PostedDate;
                phraseFound.Author     = phrase.Author;
                phraseFound.Type       = null;
                phraseFound.Entity     = null;

                try
                {
                    phraseFound.AnalyzePhrase(Helper.Instance.GetEntities(context.Entities), Helper.Instance.GetSentiments(context.Sentiments));
                }
                catch (AnalysisException) { }

                var toUpdate = context.Phrases.FirstOrDefault(p => p.Id == id);
                toUpdate.Word       = phraseFound.Word;
                toUpdate.PostedDate = phraseFound.PostedDate;
                toUpdate.Author     = context.Authors.AsNoTracking().ToList().First(a => a.Id == phraseFound.Author.Id);
                toUpdate.Type       = phraseFound.Type;
                toUpdate.Entity     = phraseFound.Entity != null?context.Entities.AsNoTracking().ToList().First(e => e.Id == phraseFound.Entity.Id) : null;

                toUpdate.Grade = phraseFound.Grade;

                context.Phrases.AddOrUpdate(toUpdate);

                ObjectStateEntry authorEntry = null;
                ObjectStateEntry entityEntry = null;

                if (toUpdate.Author != null)
                {
                    ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager.TryGetObjectStateEntry(toUpdate.Author, out authorEntry);
                }

                if (toUpdate.Entity != null)
                {
                    ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager.TryGetObjectStateEntry(toUpdate.Entity, out entityEntry);
                }

                if (authorEntry != null)
                {
                    ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager.ChangeObjectState(toUpdate.Author, toUpdate.Author.Id == phraseFound.Author.Id ? EntityState.Modified : EntityState.Unchanged);
                }

                if (entityEntry != null)
                {
                    ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager.ChangeObjectState(toUpdate.Entity, (phraseFound.Entity == null || toUpdate.Entity.Id == phraseFound.Entity.Id) ? EntityState.Modified : EntityState.Unchanged);
                }

                context.SaveChanges();

                foreach (var alarm in Helper.Instance.GetEntityAlarms(context.EntityAlarms))
                {
                    entityAlarmRepository.Modify(alarm.Id, alarm);
                }

                foreach (var alarm in Helper.Instance.GetAuthorAlarms(context.AuthorAlarms))
                {
                    authorAlarmRepository.Modify(alarm.Id, alarm);
                }
            }
        }
Пример #7
0
        public void Add(Phrase phrase)
        {
            EntityAlarmRepository alarmEntityRepository = new EntityAlarmRepository();
            AuthorAlarmRepository authorAlarmRepository = new AuthorAlarmRepository();
            PhraseRepository      phraseRepository      = new PhraseRepository();

            using (SentimentAnalysisContext context = new SentimentAnalysisContext())
            {
                if (context.Phrases.Any(p => p.Id == phrase.Id))
                {
                    throw new PhraseException("An Phrase with the same ID already exists.");
                }

                Exists(phrase);

                try
                {
                    phrase.AnalyzePhrase(Helper.Instance.GetEntities(context.Entities), Helper.Instance.GetSentiments(context.Sentiments));
                }
                catch (AnalysisException aex)
                {
                    throw aex;
                }
                catch (Exception e)
                {
                    throw e;
                }
                finally
                {
                    Entities.Phrase toAdd = Helper.Instance.ToPhraseEF(phrase);
                    context.Phrases.Add(toAdd);

                    ObjectStateEntry authorEntry = null;
                    ObjectStateEntry entityEntry = null;

                    if (toAdd.Author != null)
                    {
                        ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager.TryGetObjectStateEntry(toAdd.Author, out authorEntry);
                    }

                    if (toAdd.Entity != null)
                    {
                        ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager.TryGetObjectStateEntry(toAdd.Entity, out entityEntry);
                    }

                    if (authorEntry != null)
                    {
                        ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager.ChangeObjectState(toAdd.Author, EntityState.Unchanged);
                    }

                    if (entityEntry != null)
                    {
                        ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager.ChangeObjectState(toAdd.Entity, toAdd.Entity.Id == phrase.Entity.Id ? EntityState.Modified : EntityState.Unchanged);
                    }

                    context.SaveChanges();
                    phrase.Id = toAdd.Id;

                    try
                    {
                        foreach (var alarm in Helper.Instance.GetEntityAlarms(context.EntityAlarms).Where(a => !a.IsEnabled() && a.Type == phrase.Type && a.Entity.Id == phrase.Entity.Id))
                        {
                            alarm.AnalyzePhrases(Helper.Instance.GetPhrases(context.Phrases));
                            alarmEntityRepository.Modify(alarm.Id, alarm);
                        }
                    }
                    catch (AnalysisException) { }

                    try
                    {
                        foreach (var alarm in Helper.Instance.GetAuthorAlarms(context.AuthorAlarms).Where(a => !a.IsEnabled() && a.Type == phrase.Type))
                        {
                            alarm.AnalyzePhrases(Helper.Instance.GetPhrases(context.Phrases));
                            authorAlarmRepository.Modify(alarm.Id, alarm);
                        }
                    }
                    catch (AnalysisException) { }
                }
            }
        }