/// <summary> /// Background task work method. /// Sends the entity to the database. /// </summary> private void DoSendEntity(object sender, DoWorkEventArgs e) { SrsEntryOperationEnum operationType = (SrsEntryOperationEnum)e.Argument; // Acquire the send object. Thus, only one send operation can occur // at any given time. lock (_sendLock) { // Once the lock is acquired, check that the work is not done. // You wouldn't want to submit the item again if it worked // the first time. if (!_isDone) { // The work has still not been done. // Set the IsSending value and start the job. IsSending = true; if (operationType == SrsEntryOperationEnum.Update) { // Update try { _isDone = _srsEntryDao.Update(_entry.Reference); if (!_isDone) { ErrorMessage = R.SrsItem_EditFailure; } } catch (Exception ex) { // An exception occured. // Log the exception and set the error message. LogHelper.GetLogger(this.GetType().Name) .Error("Could not update the entity.", ex); ErrorMessage = R.SrsItem_EditFailure; } } else if (operationType == SrsEntryOperationEnum.Add) { // Add try { // Sets some properties Entry.Reference.CreationDate = DateTime.UtcNow; // Add the entity to the database. _srsEntryDao.Add(_entry.Reference); _isDone = true; // Saves the value of the tags Properties.Settings.Default.LastSrsTagsValue = Entry.Tags; } catch (Exception ex) { // An exception occured. // Log the exception and set the error message. LogHelper.GetLogger(this.GetType().Name) .Error("Could not add the entity.", ex); ErrorMessage = R.SrsItem_EditFailure; } } else { // Delete try { _isDone = _srsEntryDao.Delete(_entry.Reference); if (!_isDone) { ErrorMessage = R.SrsItem_EditFailure; } } catch (Exception ex) { // An exception occured. // Log the exception and set the error message. LogHelper.GetLogger(this.GetType().Name) .Error("Could not delete the entity.", ex); ErrorMessage = R.SrsItem_EditFailure; } } // After the job. if (_isDone && FinishedEditing != null) { // If the job has been done, raise the event. ExtendedSrsEntry result = (operationType == SrsEntryOperationEnum.Delete ? null : Entry); FinishedEditing(this, new SrsEntryEditedEventArgs(result, true)); } IsSending = false; } } }
private void DoImportToDatabase() { foreach (SrsEntry entry in ParentMode.NewEntries) { // Update log and progress. string log = string.Format("Importing {0} \"{1}\"... ", string.IsNullOrEmpty(entry.AssociatedKanji) ? "vocab" : "kanji", entry.AssociatedKanji + entry.AssociatedVocab); ProgressCount++; // Check parameters: what do we have to do with duplicates? // Basically we need to look for a duplicate in any case, except when Existing is set to 'Ignore' and NewItem to 'Import'. bool needDuplicate = !(ParentMode.DuplicateOptions.DuplicateExistingItemAction == Models.ImportDuplicateExistingItemAction.Ignore && ParentMode.DuplicateOptions.DuplicateNewItemAction == Models.ImportDuplicateNewItemAction.Import); SrsEntry duplicate = needDuplicate ? _srsDao.GetSimilarItem(entry) : null; if (duplicate != null) { if (ParentMode.DuplicateOptions.DuplicateNewItemAction == ImportDuplicateNewItemAction.Ignore) { // Found a duplicate and new item action is set to ignore on duplicate. log += "Duplicate found: SKIP."; // Do not do anything. Relax. } else { // Found a duplicate and new item action is set to import on duplicate. // What do we do with the duplicate? if (ParentMode.DuplicateOptions.DuplicateExistingItemAction == ImportDuplicateExistingItemAction.Delete) { // Delete existing item. _srsDao.Delete(duplicate); log += "Duplicate found: DELETE... "; } else if (ParentMode.DuplicateOptions.DuplicateExistingItemAction == ImportDuplicateExistingItemAction.Disable) { // Disable existing item if not already suspended. log += "Duplicate found: DISABLE... "; if (duplicate.SuspensionDate == null) { duplicate.SuspensionDate = DateTime.Now; } _srsDao.Update(duplicate); } // Import! _srsDao.Add(entry); log += "IMPORTED."; SuccessfulCount++; } } else if (duplicate == null) { // No duplicate. Just add to database. _srsDao.Add(entry); log += "IMPORTED."; SuccessfulCount++; } log += Environment.NewLine; DbImportLog = log + DbImportLog; } }