示例#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 Phrase Get(int id)
        {
            using (SentimentAnalysisContext context = new SentimentAnalysisContext())
            {
                Entities.Phrase phrase = context.Phrases.AsNoTracking().ToList().FirstOrDefault(p => p.Id == id);

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

                return(Helper.Instance.ToPhraseBL(phrase));
            }
        }
示例#3
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) { }
                }
            }
        }
示例#4
0
 public BusinessLogic.DTO.Phrase ToPhraseBL(Entities.Phrase phrase)
 {
     return(new BusinessLogic.DTO.Phrase(phrase.Id, phrase.Word, phrase.PostedDate, phrase.Type, ToEntityBL(phrase.Entity), phrase.Grade, ToAuthorBL(phrase.Author)));
 }