Пример #1
0
 private SpreadsheetProvider InitializeProvider(SpreadsheetConfiguration configuration)
 {
     try
     {
         if (_provider == null)
         {
             _provider = new SpreadsheetProvider(configuration);
         }
     }
     catch (Exception ex)
     {
         PrepareErrorResponse(ex);
     }
     return(_provider);
 }
Пример #2
0
        private Response SaveDataObjects(string objectType, IList <IDataObject> dataObjects)
        {
            try
            {
                Response response = new Response();

                SpreadsheetTable     cfTable        = _provider.GetConfigurationTable(objectType);
                SpreadsheetReference tableReference = cfTable.GetReference();
                WorksheetPart        worksheetPart  = _provider.GetWorksheetPart(cfTable);

                foreach (IDataObject dataObject in dataObjects)
                {
                    Status status = new Status();

                    try
                    {
                        string identifier = GetIdentifier(dataObject);
                        status.Identifier = identifier;

                        SpreadsheetColumn column = cfTable.Columns.First <SpreadsheetColumn>(c => c.Name.Equals(cfTable.Identifier));
                        Cell cell = worksheetPart.Worksheet.Descendants <Cell>().FirstOrDefault(c => SpreadsheetReference.GetColumnName(c.CellReference).Equals(column.ColumnIdx) && _provider.GetValue(c).Equals(identifier));

                        if (cell != null)
                        {
                            Row existingRow = cell.Ancestors <Row>().First();

                            foreach (SpreadsheetColumn col in cfTable.Columns)
                            {
                                Cell existingCell = existingRow.Descendants <Cell>().First(c => SpreadsheetReference.GetColumnName(c.CellReference).Equals(col.ColumnIdx));
                                existingCell.DataType = SpreadsheetProvider.GetCellValue(col.DataType);
                                if (!string.IsNullOrEmpty(Convert.ToString(dataObject.GetPropertyValue(col.Name))))
                                {
                                    existingCell.CellValue.Text = Convert.ToString(dataObject.GetPropertyValue(col.Name));
                                }
                                else if (existingCell.CellValue == null)
                                {
                                    existingCell.CellValue = new CellValue(Convert.ToString(dataObject.GetPropertyValue(col.Name)));
                                }
                            }
                        }
                        else
                        {
                            tableReference.EndRow++;

                            Row newRow = new Row
                            {
                                RowIndex = (UInt32Value)tableReference.EndRow,
                                Spans    = new ListValue <StringValue>
                                {
                                    InnerText = string.Format("1:{0}", cfTable.Columns.Count)
                                }
                            };

                            foreach (SpreadsheetColumn col in cfTable.Columns)
                            {
                                Cell newCell = new Cell
                                {
                                    CellReference = string.Format("{0}{1}", col.ColumnIdx, newRow.RowIndex),
                                    DataType      = SpreadsheetProvider.GetCellValue(col.DataType),
                                    CellValue     = new CellValue(Convert.ToString(dataObject.GetPropertyValue(col.Name)))
                                };

                                newRow.Append(newCell);
                            }

                            SheetData sheetData = (SheetData)worksheetPart.Worksheet.Descendants <SheetData>().First();
                            sheetData.Append(newRow);
                        }

                        worksheetPart.Worksheet.SheetDimension.Reference = tableReference.GetReference(false);
                        cfTable.Reference = tableReference.GetReference(true);

                        worksheetPart.Worksheet.Save();

                        status.Messages.Add("Record [" + identifier + "] has been saved successfully.");
                    }
                    catch (Exception ex)
                    {
                        status.Level = StatusLevel.Error;

                        string message = String.Format(
                            "Error while posting dataObject [{0}]. {1}",
                            dataObject.GetPropertyValue("Tag"),
                            ex.ToString()
                            );

                        status.Messages.Add(message);
                    }

                    response.Append(status);
                }

                return(response);
            }
            catch (Exception ex)
            {
                _logger.Error("Error in LoadDataObjects: " + ex);
                throw new Exception("Error while loading data objects of type [" + objectType + "].", ex);
            }
            finally
            {
                _provider.Dispose();
            }
        }
Пример #3
0
 public SpreadsheetDatalayer(AdapterSettings settings)
     : base(settings)
 {
     _provider = new SpreadsheetProvider(settings);
 }