public int InsertSubjectHeader(StudyHeaderModel header) { Task <StudyHeaderModel> existing = BiblesData.database .Table <StudyHeaderModel>() .FirstOrDefaultAsync(st => st.StudyHeaderId == header.StudyHeaderId); if (existing.Result == null) { Task <int> result = BiblesData.database.InsertAsync(header); int resultCheck = result.Result; return(header.StudyHeaderId); } else { existing.Result.StudyName = header.StudyName; existing.Result.Author = header.Author; existing.Result.StudyCategoryId = header.StudyCategoryId; BiblesData.database.UpdateAsync(existing.Result); return(header.StudyHeaderId); } }
private StudyHeaderModel DeleteCategoryChildVerification(int studyCategoryId, List <StudyCategoryModel> categoriesList) { if (categoriesList.Count == 0) { return(null); } foreach (StudyCategoryModel child in categoriesList.Where(cl => cl.ParentStudyCategoryId == studyCategoryId)) { Task <StudyHeaderModel> headerTask = BiblesData.database .Table <StudyHeaderModel>() .FirstOrDefaultAsync(ca => ca.StudyCategoryId == studyCategoryId); if (headerTask.Result != null) { return(headerTask.Result); } StudyHeaderModel header = this.DeleteCategoryChildVerification(child.StudyCategoryId, categoriesList); if (header != null) { return(header); } } return(null); }
private static void StudyBookmark_Selected(object sender, RoutedEventArgs e) { try { MenuItem item = (MenuItem)sender; string studyKey = item.Tag.ParseToString(); string[] keySplit = studyKey.Split(new string[] { "||" }, StringSplitOptions.None); int studyHeaderId = keySplit[0].ToInt32(); #region CHECK FOR OPEN STUDIES foreach (Window window in Application.Current.Windows) { if (window.GetType() != typeof(ControlWindow)) { continue; } UserControlBase controlBase = window.GetPropertyValue("ControlContent").To <UserControlBase>(); if (controlBase.GetType() != typeof(EditStudy)) { continue; } StudyHeader studyHeader = controlBase.GetPropertyValue("SubjectHeader").To <StudyHeader>(); if (studyHeader.StudyHeaderId <= 0) { continue; } window.Focus(); return; } #endregion StudyHeaderModel model = BiblesData.Database.GetStudyHeader(studyHeaderId.ToInt32()); EditStudy edit = new EditStudy(model); ControlDialog.Show(model.StudyName, edit, "SaveStudy", autoSize: false); } catch (Exception err) { ErrorLog.ShowError(err); } }
public EditStudy(StudyHeaderModel study) { this.InitializeComponent(); if (study == null) { this.SubjectHeader = new StudyHeader(); this.SubjectContent = new StudyContentModel(); } else { this.SubjectHeader = study.CopyToObject(new StudyHeader()).To <StudyHeader>(); this.SubjectContent = BiblesData.Database.GetStudyContent(this.SubjectHeader.StudyHeaderId); this.SubjectHeader.StudyCategoryName = BiblesData.Database.GetCategoryName(this.SubjectHeader.StudyCategoryId); } this.uxSubjectHeader.Items.Add(this.SubjectHeader); this.Loaded += this.EditStudy_Loaded; }
public void DeleteCategory(int studyCategoryId) { Task <StudyHeaderModel> headerTask = BiblesData.database .Table <StudyHeaderModel>() .FirstOrDefaultAsync(ca => ca.StudyCategoryId == studyCategoryId); if (headerTask.Result != null) { throw new ApplicationException($"Cannot delete category whiles study {headerTask.Result.StudyName} is attached to it. Please move or remove the study before attempting to delete the category."); } Task <List <StudyCategoryModel> > currentCategories = BiblesData.database .Table <StudyCategoryModel>() .ToListAsync(); StudyHeaderModel header = this.DeleteCategoryChildVerification(studyCategoryId, currentCategories.Result); if (header != null) { throw new ApplicationException($"Cannot delete category whiles child category have study {header.StudyName} is attached to it. Please move or remove the study before attempting to delete the category."); } this.DeleteChildCategoryTreeDown(studyCategoryId, currentCategories.Result); }
private void ExportStudy_Click(object sender, RoutedEventArgs e) { try { SaveFileDialog saveDlg = new SaveFileDialog(); saveDlg.Title = "Export Study"; saveDlg.DefaultExt = ".study"; saveDlg.Filter = "Bible Study (.study)|*.study"; saveDlg.FileName = this.SubjectHeader.StudyName; if (saveDlg.ShowDialog(this.GetParentWindow()).IsFalse()) { return; } if (!this.SaveStudy()) { return; } StringBuilder result = new StringBuilder(); #region CATEGORIES StudyCategoryModel category = BiblesData.Database.GetCategory(this.SubjectHeader.StudyCategoryId); result.AppendLine(JsonConvert.SerializeObject(category)); while (category.ParentStudyCategoryId > 0) { category = BiblesData.Database.GetCategory(category.ParentStudyCategoryId); result.AppendLine(JsonConvert.SerializeObject(category)); } #endregion result.AppendLine(Constants.StudyBookarkMark); #region BOOKMARKS foreach (ModelsBookmark bookmark in this.uxStudyBookmarks.Bookmarks) { StudyBookmarkModel studyBookmark = bookmark.CopyToObject(new StudyBookmarkModel()).To <StudyBookmarkModel>(); studyBookmark.StudyName = this.SubjectHeader.StudyName; result.AppendLine(JsonConvert.SerializeObject(studyBookmark)); } #endregion result.AppendLine(Constants.StudyMark); #region STUDY StudyHeaderModel headerModel = this.SubjectHeader.CopyToObject(new StudyHeaderModel()).To <StudyHeaderModel>(); result.AppendLine(JsonConvert.SerializeObject(headerModel)); result.AppendLine(Constants.StudyContentMark); result.AppendLine(JsonConvert.SerializeObject(this.SubjectContent)); #endregion File.WriteAllText(saveDlg.FileName, result.ToString()); } catch (Exception err) { ErrorLog.ShowError(err); } }
public bool ImportStudy(string fileName) { try { StudyHeaderModel studyHeader = null; Dictionary <int, string> categoryNames = new Dictionary <int, string>(); Dictionary <int, int> categoryIdMapping = new Dictionary <int, int>(); List <StudyBookmarkModel> bookmarksList = new List <StudyBookmarkModel>(); List <string> fileLines = new List <string>(); fileLines.AddRange(File.ReadAllLines(fileName)); int constantIndex = 0; foreach (string line in fileLines) { #region ACTION SWITCH if (line == Constants.StudyBookarkMark) { constantIndex = 1; continue; } else if (line == Constants.StudyMark) { constantIndex = 2; continue; } else if (line == Constants.StudyContentMark) { constantIndex = 3; continue; } #endregion if (constantIndex == 0) { #region CATEGORIES StudyCategoryModel category = JsonConvert.DeserializeObject(line, typeof(StudyCategoryModel)).To <StudyCategoryModel>(); categoryNames.Add(category.StudyCategoryId, category.CategoryName); StudyCategoryModel existing = BiblesData.Database.GetCategory(category.CategoryName); if (existing == null) { int parentId = categoryIdMapping.ContainsKey(category.ParentStudyCategoryId) ? categoryIdMapping[category.ParentStudyCategoryId] : 0; int newId = BiblesData.Database.InsertCategory(category.CategoryName, parentId); categoryIdMapping.Add(category.StudyCategoryId, newId); } else { categoryIdMapping.Add(category.StudyCategoryId, existing.StudyCategoryId); } #endregion } else if (constantIndex == 1) { #region BOOKMARKS StudyBookmarkModel bookmark = JsonConvert.DeserializeObject(line, typeof(StudyBookmarkModel)).To <StudyBookmarkModel>(); bookmarksList.Add(bookmark); #endregion } else if (constantIndex == 2) { #region STUDY HEADER studyHeader = JsonConvert.DeserializeObject(line, typeof(StudyHeaderModel)).To <StudyHeaderModel>(); studyHeader.StudyCategoryId = categoryIdMapping[studyHeader.StudyCategoryId]; StudyHeaderModel existing = BiblesData.Database.GetStudyInCategory(studyHeader.StudyName, studyHeader.StudyCategoryId); if (existing != null) { studyHeader = existing.CopyTo(studyHeader); } studyHeader.StudyHeaderId = BiblesData.Database.InsertSubjectHeader(studyHeader); #endregion #region INSERT BOOKMARKS foreach (StudyBookmarkModel bookmark in bookmarksList) { bookmark.StudyVerseKey = $"{studyHeader.StudyHeaderId}||{bookmark.VerseKey}"; BiblesData.Database.InsertStudyBookmarkModel(bookmark); } #endregion } else if (constantIndex == 3) { #region STUDY CONTENT StudyContentModel content = JsonConvert.DeserializeObject(line, typeof(StudyContentModel)).To <StudyContentModel>(); content.StudyHeaderId = studyHeader.StudyHeaderId; BiblesData.Database.InsertStudyContent(content); #endregion } } } catch (Exception err) { ErrorLog.ShowError(err); return(false); } return(true); }