public void Synchronize(Notebook instructionNotebook, int lotNumber) { var removeOldInstructions = _oldContext.tblBatchInstrs.Where(i => i.Lot == lotNumber).ToList(); var addNewInstructions = new List <tblBatchInstr>(); DateTime entryDate; var entryDates = GetEntryDates(out entryDate); foreach (var newNote in instructionNotebook.Notes ?? new List <Note>()) { var oldNote = removeOldInstructions.FirstOrDefault(i => i.Action == newNote.Text && i.Step == newNote.Sequence); if (oldNote != null) { removeOldInstructions.Remove(oldNote); } else { while (entryDates.Contains(entryDate)) { entryDate = entryDate.AddMilliseconds(10).RoundMillisecondsForSQL(); } entryDates.Add(entryDate); addNewInstructions.Add(new tblBatchInstr { EntryDate = entryDate, Lot = lotNumber, Step = newNote.Sequence, Action = newNote.Text, Serialized = SerializableBatchInstruction.Serialize(newNote) }); } } foreach (var instruction in removeOldInstructions) { _oldContext.tblBatchInstrs.DeleteObject(instruction); } foreach (var instruction in addNewInstructions) { _oldContext.tblBatchInstrs.AddObject(instruction); } }
protected override IEnumerable <InstructionResult> BirthRecords() { _loadCount.Reset(); var instructions = new HashSet <string>(); foreach (var oldInstructionsByLot in SelectBatchInstructionsToLoad(OldContext).GroupBy(i => i.Lot)) { var lastSequence = 0; foreach (var oldInstruction in oldInstructionsByLot.OrderBy(i => i.Step)) { _loadCount.AddRead(EntityTypes.Note); if (string.IsNullOrWhiteSpace(oldInstruction.Action)) { Log(new CallbackParameters(CallbackReason.EmptyAction) { Instruction = oldInstruction }); continue; } var lotKey = LotNumberParser.ParseLotNumber(oldInstruction.Lot); if (lotKey == null) { Log(new CallbackParameters(CallbackReason.InvalidLotKey) { Instruction = oldInstruction }); continue; } var notebookKey = _newContextHelper.GetBatchInstructionNotebookKeyByLotKey(lotKey); if (notebookKey == null) { Log(new CallbackParameters(CallbackReason.NotebookNotLoaded) { Instruction = oldInstruction }); continue; } var instruction = new string(oldInstruction.Action.Take(Constants.StringLengths.InstructionText).ToArray()); Instruction newInstruction = null; if (!instructions.Contains(instruction)) { _loadCount.AddRead(EntityTypes.Instruction); newInstruction = new Instruction { InstructionText = instruction, InstructionType = InstructionType.ProductionBatchInstruction }; instructions.Add(instruction); _loadCount.AddLoaded(EntityTypes.Instruction); } var sequence = oldInstruction.Step.Value; if (sequence <= lastSequence) { sequence = lastSequence + 1; } lastSequence = sequence; var newNote = new Note { EmployeeId = _newContextHelper.DefaultEmployee.EmployeeId, TimeStamp = oldInstruction.EntryDate.ConvertLocalToUTC(), NotebookDate = notebookKey.NotebookKey_Date, NotebookSequence = notebookKey.NotebookKey_Sequence, Sequence = sequence, Text = oldInstruction.Action }; SerializableBatchInstruction.DeserializeIntoNote(newNote, oldInstruction.Serialized); _loadCount.AddLoaded(EntityTypes.Note); yield return(new InstructionResult { Note = newNote, Instruction = newInstruction }); } } _loadCount.LogResults(s => Log(new CallbackParameters(s))); }