public string LoadEmails(string filename)
        {
            VerificacionComprobantes.Service.ConfigurationServer configServer = VerificacionComprobantes.Service.ConfigurationServer.Instance;
            string notFound = "";

            using (var db = new LiteDatabase(@configServer.dbConfig)) {
                var collection = db.GetCollection <Model.Person> ("personas");

                IWorkbook workbook =
                    new XSSFWorkbook(File.Open(filename, System.IO.FileMode.Open));
                var sheet = workbook.GetSheetAt(0);
                Dictionary <string, int> columnIndex = null;

                foreach (IRow row in sheet)
                {
                    if (columnIndex == null)
                    {
                        int column = 0;
                        columnIndex = new Dictionary <string, int> ();
                        string name = null;

                        while ((name = getCellValue(row.GetCell(column))) != null)
                        {
                            columnIndex.Add(name, column++);
                        }
                    }
                    else
                    {
                        string name = TreatName(getCellValue(row.GetCell(columnIndex [ColumnMappings.NAME])));

                        if (string.IsNullOrEmpty(name))
                        {
                            continue;
                        }
                        string mail = getCellValue(row.GetCell(columnIndex [ColumnMappings.MAIL]));
                        Console.Out.WriteLine(mail + " : " + name);

                        Model.Person found = collection.FindOne(Query.EQ("Name", name));

                        if (null != found)
                        {
                            found.Email = mail;

                            collection.Update(found);
                        }
                        else
                        {
                            notFound += name + "\n";
                        }
                    }
                }
            }

            return(notFound);
        }
        private Model.Person BuildPerson(IRow row, Dictionary <string, int> mapping, string entity)
        {
            string nombreCel = TreatName(getCellValue(row.GetCell(mapping [ColumnMappings.NAME])));

            Model.Person person = new Model.Person()
            {
                Operations = new List <Model.Operation> (), Name = nombreCel
            };

            Model.Operation operation = BuildOperation(row, mapping, entity);

            if (null != operation)
            {
                person.Operations.Add(operation);
            }

            return(person);
        }
        public void Init(string filename, System.ComponentModel.BackgroundWorker worker)
        {
            Service.ConfigurationServer configServer = Service.ConfigurationServer.Instance;

            using (var db = new LiteDatabase(@configServer.dbConfig)) {
                var collection = db.GetCollection <Model.Person> ("personas");
                collection.EnsureIndex(x => x.Name);
                collection.EnsureIndex("Operations.OperationNumber");

                IWorkbook workbook =
                    new XSSFWorkbook(File.Open(filename, System.IO.FileMode.Open));

                int sheets = workbook.NumberOfSheets;

                string[] names = ColumnMappings.SHEETS.Split(',');

                int rowCount = 0;

                if (null != worker)
                {
                    for (int i = 0; i < names.Length; i++)
                    {
                        ISheet sheet = workbook.GetSheet(names[i]);
                        rowCount += sheet.LastRowNum;
                    }
                }

                int currentRow = 0;

                for (int i = 0; i < names.Length; i++)
                {
                    ISheet sheet = workbook.GetSheet(names [i]);
                    if (null == sheet)
                    {
                        throw new Exceptions.SheetNotPresentException(names [i] + " does not exist");
                    }
                    Dictionary <string, int> columnIndex = null;

                    int column = 0;

                    foreach (IRow row in sheet)
                    {
                        if (null != worker)
                        {
                            worker.ReportProgress((++currentRow * 100) / rowCount);
                        }


                        if (columnIndex == null)
                        {
                            columnIndex = new Dictionary <string, int> ();
                            string name = null;

                            while ((name = getCellValue(row.GetCell(column))) != null)
                            {
                                columnIndex.Add(name, column++);
                            }
                        }
                        else
                        {
                            string name = TreatName(getCellValue(row.GetCell(columnIndex [ColumnMappings.NAME])));

                            if (string.IsNullOrEmpty(name))
                            {
                                continue;
                            }

                            Model.Person found = collection.FindOne(Query.EQ("Name", name));

                            if (null == found)
                            {
                                Model.Person person = BuildPerson(row, columnIndex, names [i]);
                                collection.Insert(person);
                            }
                            else
                            {
                                string operationNumber = getCellValue(row.GetCell(columnIndex [ColumnMappings.OPERATION]));

                                Predicate <Model.Operation> isOperation = delegate(Model.Operation op) {
                                    return(op.OperationNumber.Equals(operationNumber));
                                };

                                Model.Operation operation = found.Operations.Find(isOperation);

                                if (null == operation)
                                {
                                    operation = BuildOperation(row, columnIndex, names [i]);

                                    if (null != operation)
                                    {
                                        found.Operations.Add(operation);

                                        collection.Update(found);
                                    }
                                }
                                else
                                {
                                    string voucher = getCellValue(row.GetCell(columnIndex [ColumnMappings.VOUCHER]));

                                    if (string.IsNullOrEmpty(operation.Voucher) &&
                                        !string.IsNullOrEmpty(voucher))
                                    {
                                        operation.Voucher = voucher;

                                        collection.Update(found);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }