Exemplo n.º 1
0
            public override State process(CsvParser p)
            {
                for (;;)
                {
                    int ch = p.ReadChar();
                    switch (ch)
                    {
                    case '\n':
                    case -1:
                        if (p._line.Count > 0)
                        {
                            p.AddField();
                        }
                        return(null);

                    case '\t':
                        // This must be a tab-delimited file - switch mode to tab delimited
                        TabStart.AddField(p);
                        p._startState = TabStart;
                        return(TabStart);

                    case ',':
                        // Empty field
                        p.AddField();
                        continue;

                    case '"':
                        return(QuotedField);

                    default:
                        // Anything else is a normal field - switch state to field processor
                        p.AddChar(ch);
                        return(FieldData);
                    }
                }
            }
Exemplo n.º 2
0
 /// <summary>
 /// Given a file, read its headers, and find a suitable importer for it
 /// </summary>
 public static Importer ImporterFor(CsvParser csv)
 {
     return(Importers.FirstOrDefault(i => i.Matches(csv)));
 }
Exemplo n.º 3
0
 public void ImportFile(UploadedFile file, string dateFormat)
 {
     Method = "Import";
     Stream s = null;
     try {
         s = file.Stream();
         if (Path.GetExtension(file.Name).ToLower() == ".qif") {
             QifImporter qif = new QifImporter();
             new ImportBatchJob(this, qif, delegate() {
                 try {
                     Batch.Records = file.Content.Length;
                     Batch.Status = "Importing file " + file.Name + " as QIF";
                     Database.BeginTransaction();
                     qif.DateFormat = dateFormat;
                     qif.Import(new StreamReader(s), this);
                     Database.Commit();
                 } catch (Exception ex) {
                     throw new CheckException(ex, "Error at line {0}\r\n{1}", qif.Line, ex.Message);
                 } finally {
                     s.Dispose();
                 }
                 Message = "File " + file.Name + " imported successfully as QIF";
             });
         } else {
             CsvParser csv = new CsvParser(new StreamReader(s));
             Importer importer = Importer.ImporterFor(csv);
             Utils.Check(importer != null, "No importer for file {0}", file.Name);
             new ImportBatchJob(this, csv, delegate() {
                 try {
                     Batch.Records = file.Content.Length;
                     Batch.Status = "Importing file " + file.Name + " as " + importer.Name + " to ";
                     Database.BeginTransaction();
                     importer.DateFormat = dateFormat;
                     importer.Import(csv, this);
                     Database.Commit();
                 } catch (Exception ex) {
                     throw new CheckException(ex, "Error at line {0}\r\n{1}", csv.Line, ex.Message);
                 } finally {
                     s.Dispose();
                 }
                 Message = "File " + file.Name + " imported successfully as " + importer.Name + " to " + importer.TableName;
             });
         }
     } catch (Exception ex) {
         Log(ex.ToString());
         Message = ex.Message;
         if (s != null)
             s.Dispose();
     }
 }
Exemplo n.º 4
0
 public override State process(CsvParser p)
 {
     for (; ; ) {
         int ch = p.ReadChar();
         switch (ch) {
             case '\n':
             case -1:
                 if (p._line.Count > 0 || p._field.Length > 0) AddField(p);
                 return null;
             case '\t':
                 AddField(p);
                 continue;
             default:
                 p.AddChar(ch);
                 continue;
         }
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// Switching to this state from Csv - remove any quotes from current field, and add it
 /// </summary>
 public void AddField(CsvParser p)
 {
     string f = p._field.ToString();
     if (f.StartsWith("\"") && f.EndsWith("\"")) {
         p._field.Length = 0;
         p._field.Append(f.Substring(1, f.Length - 2));
     }
     p.AddField();
 }
Exemplo n.º 6
0
 public virtual State process(CsvParser p)
 {
     for(;;) {
         int ch = p.ReadChar();
         switch(ch) {
             case '\n':
             case -1:
                 // End of line/file - add current field
                 p.AddField();
                 return null;
             case '\t':
                 // This must be a tab-delimited file - switch mode to tab delimited
                 TabStart.AddField(p);
                 p._startState = TabStart;
                 return TabStart;
             case ',':
                 // End of field
                 p.AddField();
                 return Start;	// Initial field state
             default:
                 // Add character to field
                 p.AddChar(ch);
                 continue;
         }
     }
 }
Exemplo n.º 7
0
 public override State process(CsvParser p)
 {
     for(;;) {
         int ch = p.ReadChar();
         switch(ch) {
             case '\n':
             case -1:
                 if (p._line.Count > 0) p.AddField();
                 return null;
             case '\t':
                 // This must be a tab-delimited file - switch mode to tab delimited
                 TabStart.AddField(p);
                 p._startState = TabStart;
                 return TabStart;
             case ',':
                 // Empty field
                 p.AddField();
                 continue;
             case '"':
                 return QuotedField;
             default:
                 // Anything else is a normal field - switch state to field processor
                 p.AddChar(ch);
                 return FieldData;
         }
     }
 }
Exemplo n.º 8
0
 public override State process(CsvParser p)
 {
     for(;;) {
         int ch = p.ReadChar();
         switch(ch) {
             case -1:
                 p.AddField();
                 return null;
             case '"':
                 ch = p.ReadChar();
                 switch(ch) {
                     case '\n':
                     case -1:
                         // Quote at end of line/file just terminates the field
                         p.AddField();
                         return null;
                     case ',':
                         // Quote, comma is just end of field
                         p.AddField();
                         return Start;
                     case '\t':
                         // Must be a tab-delimited file - switch
                         p.AddField();
                         return TabStart;
                     case '"':
                         // Quote Quote = Quote
                         p.AddChar('"');
                         continue;
                     default:
                         // Quote anychar is a syntax error, but we allow it and pass through unchanged
                         p.AddChar('"');
                         p.AddChar(ch);
                         continue;
                 }
             default:
                 // This includes newlines and tabs!
                 p.AddChar(ch);
                 continue;
         }
     }
 }
Exemplo n.º 9
0
 public override void ImportData(CsvParser csv)
 {
     _tranId = 0;
     _line = 0;
     // Do the import
     base.ImportData(csv);
     // Now update the last cheque numbers, etc.
     AppModule.AppSettings.RegisterNumber(_module, (int)DocType.Invoice, _lastInvoiceNumber);
     AppModule.AppSettings.RegisterNumber(_module, (int)DocType.Bill, _lastBillNumber);
     AppModule.AppSettings.RegisterNumber(_module, (int)DocType.GeneralJournal, _lastJournalNumber);
     foreach (int k in _lastChequeNumber.Keys.Union(_lastDepositNumber.Keys)) {
         FullAccount acct = _module.Database.Get<FullAccount>(k);
         int n;
         bool save = false;
         if (_lastChequeNumber.TryGetValue(k, out n))
             save |= acct.RegisterNumber(DocType.Cheque, n);
         if (_lastDepositNumber.TryGetValue(k, out n))
             save |= acct.RegisterNumber(DocType.Deposit, n);
         if (save)
             _module.Database.Update(acct);
     }
     // Try to guess what VAT payments apply to each document
     foreach(JObject vatPayment in new Select().VatPayments()) {
         int id = vatPayment.AsInt("id");
         DateTime q = AppModule.AppSettings.QuarterStart(vatPayment.AsDate("value"));
         _module.Database.Execute(@"UPDATE Document
     JOIN Journal ON IdDocument = DocumentId
     JOIN Line ON idLine = idJournal
     SET VatPaid = " + id + @"
     WHERE (DocumentTypeId IN (1, 3, 4, 6) OR Line.VatCodeId IS NOT NULL)
     AND VatPaid = 0
     AND idDocument < " + id + @"
     AND DocumentDate < " + Database.Quote(q));
         _module.Database.Execute("UPDATE Document SET VatPaid = " + id + " WHERE idDocument = " + id);
     }
     // Set the remainder to null (importer doesn't do nulls)
     _module.Database.Execute(@"UPDATE Document SET VatPaid = NULL WHERE VatPaid = 0");
 }
Exemplo n.º 10
0
 /// <summary>
 /// Look for "!" lines (which start a new data set), and import each data set
 /// </summary>
 public override void ImportData(CsvParser csv)
 {
     _importer = null;
     csv.PermitAnyFieldCount = true;
     string delimiter = "";
     TableName = "";
     foreach (JObject dataIn in csv.Read()) {
         string[] line = csv.Data;
         if (line[0].StartsWith("!")) {
             // New data set - remove the ! from the first field
             line[0] = line[0].Substring(1);
             // This will be the headers for the new import
             csv.Headers = line;
             _importer = _importers.FirstOrDefault(i => i.Matches(csv));
             if (_importer != null) {
                 // We want this data set - set up the importer, and tell the user
                 _importer._module = _module;
                 _importer._keys = new HashSet<string>();
                 _importer.DateFormat = DateFormat;
                 if (_module.Batch != null) {
                     _module.Batch.Status += delimiter + _importer.TableName;
                 }
                 TableName += delimiter + _importer.TableName;
                 delimiter = ",";
             }
             continue;
         }
         if (_importer != null)
             _importer.ImportDataLine(dataIn);
     }
 }
Exemplo n.º 11
0
 /// <summary>
 /// Whether the csv file has all the fields required for this importer
 /// </summary>
 public bool Matches(CsvParser csv)
 {
     HashSet<string> fieldNames = new HashSet<string>(csv.Headers);
     foreach (ImportField f in Fields) {
         if (f.TheirName != null && !fieldNames.Contains(f.TheirName)) {
             System.Diagnostics.Trace.WriteLine("Does not match " + this.Name + " " + f.TheirName + " missing");
             return false;
         }
     }
     return true;
 }
Exemplo n.º 12
0
 /// <summary>
 /// Import the actual data
 /// </summary>
 public virtual void ImportData(CsvParser csv)
 {
     if (_module.Batch != null)
         _module.Batch.Status += TableName;
     foreach (JObject dataIn in csv.Read()) {
         ImportDataLine(dataIn);
     }
 }
Exemplo n.º 13
0
 /// <summary>
 /// Set up import, import the data, commit to the database if no errors
 /// </summary>
 public void Import(CsvParser csv, AppModule module)
 {
     lock (this) {
         _module = module;
         _keys = new HashSet<string>();
         ImportData(csv);
         _module.Database.Commit();
     }
 }
Exemplo n.º 14
0
 /// <summary>
 /// Given a file, read its headers, and find a suitable importer for it
 /// </summary>
 public static Importer ImporterFor(CsvParser csv)
 {
     return Importers.FirstOrDefault(i => i.Matches(csv));
 }