Exemplo n.º 1
0
        public static bool ProcessRecord <RecordType, RecordLogType>(this DCDContext dc, Record record, ImportLog importLog)
            where RecordType : IDCD, new()
            where RecordLogType : IDCDRecordImportLog, new()
        {
            //First see if the record already exists
            IDCD entry = dc.GetRecord <RecordType>(record.Identification.RecordId);

            //Process delete
            if (record.Command == CommandType.Delete)
            {
                if (entry == null)
                {
                    dc.CreateRecordLogEntry <RecordLogType>(record.Identification.RecordId, importLog, RecordImportResult.Skipped, RecordImportOperation.Delete, "Cannot delete. Record doesn't exist.");
                    return(true);
                }
                else
                {
                    try
                    {
                        entry.DeleteRecordOnSubmit(dc);
                        dc.SubmitChanges();
                    }
                    catch (Exception e)
                    {
                        dc.CreateRecordLogEntry <RecordLogType>(record.Identification.RecordId, importLog, RecordImportResult.Failure, RecordImportOperation.Delete, "Error deleting record.", e);
                        return(false);
                    }

                    dc.CreateRecordLogEntry <RecordLogType>(record.Identification.RecordId, importLog, RecordImportResult.Success, RecordImportOperation.Delete, string.Empty);
                    return(true);
                }
            }
            //Upsert
            else
            {
                RecordImportOperation op = RecordImportOperation.Insert;
                IDCD processingRecord;
                try
                {
                    //If the record already exists
                    if (entry != null)
                    {
                        op = RecordImportOperation.Update;
                        //Only records that are newer than the records in the feed will be skipped
                        if (entry.LastModified > record.Identification.LastModified)
                        {
                            dc.CreateRecordLogEntry <RecordLogType>(record.Identification.RecordId, importLog, RecordImportResult.Skipped, op, "Existing record has a newer Last Modified Date.");
                            return(true);
                        }
                        else if (record.Content == null || string.IsNullOrEmpty(record.Content.InnerXml))
                        {
                            dc.CreateRecordLogEntry <RecordLogType>(record.Identification.RecordId, importLog, RecordImportResult.Skipped, op, "Record does not have any content.");
                            return(true);
                        }
                        else
                        {
                            entry.RecordNumber = record.Identification.RecordNumber;
                            entry.Title        = XMLFileUtilities.RemoveBrackets(record.Identification.Title);
                            entry.LastModified = record.Identification.LastModified;
                            entry.Created      = record.Identification.Created;
                            entry.Published    = record.Identification.Created;
                            if (!record.Identification.Published.ToString().Equals(DateTime.MinValue.ToString()))
                            {
                                entry.Published = record.Identification.Published;
                            }
                            entry.Content = record.Content.InnerXml;
                        }
                    }
                    else
                    {
                        op = RecordImportOperation.Insert;
                        processingRecord              = new RecordType();
                        processingRecord.RecordId     = record.Identification.RecordId;
                        processingRecord.RecordNumber = record.Identification.RecordNumber;
                        processingRecord.Title        = XMLFileUtilities.RemoveBrackets(record.Identification.Title);
                        processingRecord.LastModified = record.Identification.LastModified;
                        processingRecord.Created      = record.Identification.Created;
                        processingRecord.Published    = record.Identification.Created;
                        if (!record.Identification.Published.ToString().Equals(DateTime.MinValue.ToString()))
                        {
                            processingRecord.Published = record.Identification.Published;
                        }
                        processingRecord.Content = record.Content.InnerXml;
                        processingRecord.InsertRecordOnSubmit(dc);
                    }
                    dc.SubmitChanges();
                }
                catch (Exception e)
                {
                    dc.CreateRecordLogEntry <RecordLogType>(record.Identification.RecordId, importLog, RecordImportResult.Failure, op, "Error inserting/updating record.", e);
                    return(false);
                }

                dc.CreateRecordLogEntry <RecordLogType>(record.Identification.RecordId, importLog, RecordImportResult.Success, op, string.Empty);
                return(true);
            }
        }
Exemplo n.º 2
0
        private void Watcher_FileChanged(object sender, FileSystemEventArgs e)
        {
            string fullPath = e.FullPath;
            string name     = e.Name;

            DCDImportLogger.Debug("FileChanged event raised for [" + name + "] found at [" + fullPath + "]");

            if (!Directory.Exists(DestinationDirectory))
            {
                Directory.CreateDirectory(DestinationDirectory);
            }

            if (!Directory.Exists(ProcessingDirectory))
            {
                Directory.CreateDirectory(ProcessingDirectory);
            }

            //If the file does not exist, exit out
            //This event can be fired when a file is deleted, so we need this check
            if (!File.Exists(fullPath))
            {
                return;
            }
            DCDImportLogger.Info(string.Format("Processing File: " + name));

            // check number one to make sure file is done uploading
            if (XMLFileUtilities.IsOpen(fullPath))
            {
                bool bFileOpen = true;
                while (bFileOpen)
                {
                    if (XMLFileUtilities.IsOpen(fullPath))
                    {
                        DCDImportLogger.Info(string.Format("File is open, Waiting 2 seconds and looping..."));
                        Thread.Sleep(2000);
                    }
                    else
                    {
                        bFileOpen = false;
                        DCDImportLogger.Info(string.Format("File is available... continue processing."));
                    }
                }
            }

            //clean bad characters from the file
            string       processingName     = "Processing_" + name.Replace(".xml", "") + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xml";
            string       processingFullPath = ProcessingDirectory + processingName;
            StreamReader reader             = new StreamReader(fullPath, Encoding.GetEncoding("Windows-1252"));
            StreamWriter writer             = new StreamWriter(processingFullPath, false, Encoding.UTF8);
            bool         first = true;

            while (true)
            {
                string line = reader.ReadLine();
                if (line == null)
                {
                    break;
                }
                if (first && line.Contains("<?xml version"))
                {
                    writer.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                    first = false;
                }
                else
                {
                    writer.WriteLine(XMLFileUtilities.SanitizeXmlString(line));
                }
            }
            reader.Close();
            writer.Close();

            //Copy the original file to the archive folder
            string processingDestPath = DestinationDirectory + processingName;

            File.Copy(fullPath, processingDestPath);
            //Delete the original file
            File.Delete(fullPath);

            processFile(processingFullPath);
        }