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 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); } } }
public void Add(Entity entity) { using (SentimentAnalysisContext context = new SentimentAnalysisContext()) { if (context.Entities.Any(e => e.Id == entity.Id)) { throw new EntityException("An Entity with the same ID already exists."); } Exists(entity); Entities.Entity toAdd = Helper.Instance.ToEntityEF(entity); context.Entities.Add(toAdd); context.SaveChanges(); entity.Id = toAdd.Id; foreach (var phraseBD in context.Phrases.Where(p => p.Entity == null)) { PhraseRepository phraseRepository = new PhraseRepository(); var phrase = Helper.Instance.ToPhraseBL(phraseBD); try { phrase.AnalyzePhrase(Helper.Instance.GetEntities(context.Entities), Helper.Instance.GetSentiments(context.Sentiments)); } catch (AnalysisException) { } phraseRepository.Modify(phrase.Id, phrase); } } }
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); } } }