/// <summary> /// Creates image exporter. /// </summary> /// <param name="tables">Table definitions.</param> /// <param name="mapLayer"><see cref="P:ESRI.ArcLogistics.MapLayer" /> that has to be used /// for images generation.</param> /// <param name="options"><see cref="P:ESRI.ArcLogistics.Export.ExportOptions" /> that has to be /// used for export.</param> /// <returns>Created map image exporter or null if not need</returns> private MapImageExporter _CreateImageExporter(ICollection <ITableDefinition> tables, MapLayer mapLayer, ExportOptions options) { Debug.Assert(null != tables); // check is image field select bool isImageFieldPresent = _IsImageFieldsPresent(tables); MapImageExporter mapImageExporter = null; if (isImageFieldPresent) { if (null == mapLayer) { throw new NotSupportedException(); // exception } mapImageExporter = new MapImageExporter(options.ShowLeadingStemTime, options.ShowTrailingStemTime); mapImageExporter.Init(mapLayer); } return(mapImageExporter); }
/////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Does export routine. /// </summary> /// <param name="filePath">Export file path.</param> /// <param name="tables">Table definitions.</param> /// <param name="schedules">Shedules to export.</param> /// <param name="routes">Routes to export (can be null).</param> /// <param name="imageExporter">Map's image exporter (can be null).</param> /// <param name="tracker">Cancel tracker (can be null).</param> /// <remarks>Routes must belong to the <c>schedule</c>. If <c>routes</c> collection /// is empty, Generator will use all the routes from the <c>schedules</c>.</remarks> public void DoExport(string filePath, ICollection <ITableDefinition> tables, ICollection <Schedule> schedules, ICollection <Route> routes, MapImageExporter imageExporter, ICancelTracker tracker) { Debug.Assert(!string.IsNullOrEmpty(filePath)); Debug.Assert(null != tables); Debug.Assert(null != schedules); Debug.Assert((null == routes) || ((null != routes) && (1 == schedules.Count))); try { _CreateDatabase(filePath, tables); _CheckCancelState(tracker); _WriteContent(filePath, tables, schedules, routes, imageExporter, tracker); } catch (Exception ex) { if (!(ex is UserBreakException)) { Logger.Error(ex); } _DeleteFile(filePath); throw; // exception } }
/// <summary> /// Writes routes. /// </summary> /// <param name="schedules">Schedules to export.</param> /// <param name="routes">Routes to export.</param> /// <param name="fields">Fields to export.</param> /// <param name="data">Data keeper.</param> /// <param name="imageExporter">Map image exporter.</param> /// <param name="table">Table to data writing.</param> /// <param name="tracker">Cancel tracker (can be null).</param> private void _WriteRoutes(ICollection <Schedule> schedules, ICollection <Route> routes, ICollection <string> fields, DataKeeper data, MapImageExporter imageExporter, DataTable table, ICancelTracker tracker) { foreach (Schedule schedule in schedules) { _CheckCancelState(tracker); Guid scheduleID = schedule.Id; foreach (Route route in schedule.Routes) { _CheckCancelState(tracker); if ((null != routes) && !routes.Contains(route)) { continue; // NOTE: skeep not selected } if ((null == route.Stops) || (0 == route.Stops.Count)) { continue; // NOTE: skeep empty routes } DataRow dr = table.NewRow(); foreach (string field in fields) { if ("OverviewMap" == field) { // special routine Debug.Assert(null != imageExporter); _CheckCancelState(tracker); Image image = imageExporter.GetRouteImage(route, ROUTE_MAP_IMAGE_SIZE_X, ROUTE_MAP_IMAGE_SIZE_Y, IMAGE_DPI); dr[field] = _ConvertImageToBlob(image); if (null != image) { image.Dispose(); image = null; } } else { Debug.Assert("PlannedDate" != field); // NOTE: do not supported dr[field] = _FormatFieldValue(data.GetRouteFieldValue(field, scheduleID, route)); } } table.Rows.Add(dr); } } }
/// <summary> /// Does report source. /// </summary> /// <param name="settings">Export settings.</param> /// <param name="tracker">Cancel tracker (can be null).</param> private void _DoExport(ExportSettings settings, ICancelTracker tracker) { Debug.Assert(null != settings); // prepare direction for file _PrepareDirToCreation(settings.FilePath); if (ExportType.Access == settings.Type) { // export to access // create image exporter MapImageExporter imageExporter = _CreateImageExporter(settings.Tables, settings.MapLayer, settings.Options); // do export to access format var writer = new AccessExporter(_structureKeeper); writer.DoExport(settings.FilePath, settings.Tables, settings.Schedules, settings.Routes, imageExporter, tracker); } else if ((ExportType.TextRoutes == settings.Type) || (ExportType.TextStops == settings.Type) || (ExportType.TextOrders == settings.Type)) { // export as text Debug.Assert(1 == settings.Tables.Count); var writer = new TextExporter(_structureKeeper); writer.DoExport(settings.FilePath, settings.Tables.First(), settings.Schedules, tracker); } else { Debug.Assert(false); // NOTE: not supported } }
/// <summary> /// Writes stops. /// </summary> /// <param name="schedules">Schedules to export.</param> /// <param name="routes">Routes to export.</param> /// <param name="fields">Fields to export.</param> /// <param name="data">Data keeper.</param> /// <param name="imageExporter">Map image exporter.</param> /// <param name="table">Table to data writing.</param> /// <param name="tracker">Cancel tracker (can be null).</param> private void _WriteStops(ICollection<Schedule> schedules, ICollection<Route> routes, ICollection<string> fields, DataKeeper data, MapImageExporter imageExporter, DataTable table, ICancelTracker tracker) { foreach (Schedule schedule in schedules) { _CheckCancelState(tracker); // stops Guid scheduleId = schedule.Id; foreach (Route route in schedule.Routes) { _CheckCancelState(tracker); if ((null != routes) && !routes.Contains(route)) continue; // NOTE: skeep not selected IDataObjectCollection<Stop> stops = route.Stops; for (int index = 0; index < stops.Count; ++index) { _CheckCancelState(tracker); // write stop Stop stop = stops[index]; DataRow dr = table.NewRow(); foreach (string field in fields) { if (field == "StopVicinityMap") { // NOTE: special routine Debug.Assert(null != imageExporter); _CheckCancelState(tracker); Image image = imageExporter.GetStopImage(route, stop, STOP_MAP_RADIUS, STOP_MAP_IMAGE_SIZE_X, STOP_MAP_IMAGE_SIZE_Y, IMAGE_DPI); dr[field] = _ConvertImageToBlob(image); if (null != image) { image.Dispose(); image = null; } } else if (field == "Directions") { // NOTE: special routine System.Text.UnicodeEncoding encoding = new System.Text.UnicodeEncoding(); dr[field] = encoding.GetBytes(_GetDirectionsText(stop.Directions)); } else { dr[field] = _FormatFieldValue(data.GetStopFieldValue(field, scheduleId, stop)); } } table.Rows.Add(dr); } } _CheckCancelState(tracker); // unassigned orders IDataObjectCollection<Order> orders = schedule.UnassignedOrders; if (null != orders) { foreach (Order order in orders) { _CheckCancelState(tracker); DataRow dr = table.NewRow(); foreach (string field in fields) { DataWrapper wrapper = data.GetStopFieldValue(field, scheduleId, order); dr[field] = _FormatFieldValue(wrapper); } table.Rows.Add(dr); } } } }
/// <summary> /// Writes routes. /// </summary> /// <param name="schedules">Schedules to export.</param> /// <param name="routes">Routes to export.</param> /// <param name="fields">Fields to export.</param> /// <param name="data">Data keeper.</param> /// <param name="imageExporter">Map image exporter.</param> /// <param name="table">Table to data writing.</param> /// <param name="tracker">Cancel tracker (can be null).</param> private void _WriteRoutes(ICollection<Schedule> schedules, ICollection<Route> routes, ICollection<string> fields, DataKeeper data, MapImageExporter imageExporter, DataTable table, ICancelTracker tracker) { foreach (Schedule schedule in schedules) { _CheckCancelState(tracker); Guid scheduleID = schedule.Id; foreach (Route route in schedule.Routes) { _CheckCancelState(tracker); if ((null != routes) && !routes.Contains(route)) continue; // NOTE: skeep not selected if ((null == route.Stops) || (0 == route.Stops.Count)) continue; // NOTE: skeep empty routes DataRow dr = table.NewRow(); foreach (string field in fields) { if ("OverviewMap" == field) { // special routine Debug.Assert(null != imageExporter); _CheckCancelState(tracker); Image image = imageExporter.GetRouteImage(route, ROUTE_MAP_IMAGE_SIZE_X, ROUTE_MAP_IMAGE_SIZE_Y, IMAGE_DPI); dr[field] = _ConvertImageToBlob(image); if (null != image) { image.Dispose(); image = null; } } else { Debug.Assert("PlannedDate" != field); // NOTE: do not supported dr[field] = _FormatFieldValue(data.GetRouteFieldValue(field, scheduleID, route)); } } table.Rows.Add(dr); } } }
/// <summary> /// Write database content. /// </summary> /// <param name="filePath">Export file path.</param> /// <param name="tables">Table definitions.</param> /// <param name="schedules">Schedules to export.</param> /// <param name="routes">Routes to export.</param> /// <param name="imageExporter">Map image exporter.</param> /// <param name="tracker">Cancel tracker (can be null).</param> private void _WriteContent(string filePath, ICollection<ITableDefinition> tables, ICollection<Schedule> schedules, ICollection<Route> routes, MapImageExporter imageExporter, ICancelTracker tracker) { OleDbConnection connection = new OleDbConnection(_GetConnectionString(filePath)); try { foreach (ITableDefinition tableDef in tables) { _CheckCancelState(tracker); TableDescription tableDescription = _structureKeeper.GetTableDescription(tableDef.Type); DataKeeper data = new DataKeeper(_listSeparator, tableDescription); ICollection<string> fields = tableDef.Fields; string tableName = tableDef.Name; // obtain dataset string selectCommand = string.Format(SQL_SELECT_COMMAND_FORMAT, tableName); OleDbCommand accessCommand = new OleDbCommand(selectCommand, connection); OleDbDataAdapter dataAdapter = new OleDbDataAdapter(accessCommand); DataSet dataSet = new DataSet(tableName); dataAdapter.Fill(dataSet, tableName); // select table DataTable table = dataSet.Tables[tableName]; // write data to table switch (tableDef.Type) { case TableType.Schedules: _WriteSchedules(schedules, fields, data, table, tracker); break; case TableType.Routes: { _WriteRoutes(schedules, routes, fields, data, imageExporter, table, tracker); break; } case TableType.Stops: { _WriteStops(schedules, routes, fields, data, imageExporter, table, tracker); break; } case TableType.Schema: _WriteSchema(fields, table, tracker); break; default: Debug.Assert(false); // NOTE: not supported break; } _CheckCancelState(tracker); // set insert command OleDbCommand insertCommand = _CreateInsertCommand(tableName, tableDescription, fields); insertCommand.Connection = connection; dataAdapter.InsertCommand = insertCommand; // store changes dataAdapter.Update(table); } } catch (Exception) { } finally { connection.Close(); } }
/////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////// /// <summary> /// Does export routine. /// </summary> /// <param name="filePath">Export file path.</param> /// <param name="tables">Table definitions.</param> /// <param name="schedules">Shedules to export.</param> /// <param name="routes">Routes to export (can be null).</param> /// <param name="imageExporter">Map's image exporter (can be null).</param> /// <param name="tracker">Cancel tracker (can be null).</param> /// <remarks>Routes must belong to the <c>schedule</c>. If <c>routes</c> collection /// is empty, Generator will use all the routes from the <c>schedules</c>.</remarks> public void DoExport(string filePath, ICollection<ITableDefinition> tables, ICollection<Schedule> schedules, ICollection<Route> routes, MapImageExporter imageExporter, ICancelTracker tracker) { Debug.Assert(!string.IsNullOrEmpty(filePath)); Debug.Assert(null != tables); Debug.Assert(null != schedules); Debug.Assert((null == routes) || ((null != routes) && (1 == schedules.Count))); try { _CreateDatabase(filePath, tables); _CheckCancelState(tracker); _WriteContent(filePath, tables, schedules, routes, imageExporter, tracker); } catch (Exception ex) { if ( !(ex is UserBreakException) ) Logger.Error(ex); _DeleteFile(filePath); throw; // exception } }
/// <summary> /// Write database content. /// </summary> /// <param name="filePath">Export file path.</param> /// <param name="tables">Table definitions.</param> /// <param name="schedules">Schedules to export.</param> /// <param name="routes">Routes to export.</param> /// <param name="imageExporter">Map image exporter.</param> /// <param name="tracker">Cancel tracker (can be null).</param> private void _WriteContent(string filePath, ICollection <ITableDefinition> tables, ICollection <Schedule> schedules, ICollection <Route> routes, MapImageExporter imageExporter, ICancelTracker tracker) { OleDbConnection connection = new OleDbConnection(_GetConnectionString(filePath)); try { foreach (ITableDefinition tableDef in tables) { _CheckCancelState(tracker); TableDescription tableDescription = _structureKeeper.GetTableDescription(tableDef.Type); DataKeeper data = new DataKeeper(_listSeparator, tableDescription); ICollection <string> fields = tableDef.Fields; string tableName = tableDef.Name; // obtain dataset string selectCommand = string.Format(SQL_SELECT_COMMAND_FORMAT, tableName); OleDbCommand accessCommand = new OleDbCommand(selectCommand, connection); OleDbDataAdapter dataAdapter = new OleDbDataAdapter(accessCommand); DataSet dataSet = new DataSet(tableName); dataAdapter.Fill(dataSet, tableName); // select table DataTable table = dataSet.Tables[tableName]; // write data to table switch (tableDef.Type) { case TableType.Schedules: _WriteSchedules(schedules, fields, data, table, tracker); break; case TableType.Routes: { _WriteRoutes(schedules, routes, fields, data, imageExporter, table, tracker); break; } case TableType.Stops: { _WriteStops(schedules, routes, fields, data, imageExporter, table, tracker); break; } case TableType.Schema: _WriteSchema(fields, table, tracker); break; default: Debug.Assert(false); // NOTE: not supported break; } _CheckCancelState(tracker); // set insert command OleDbCommand insertCommand = _CreateInsertCommand(tableName, tableDescription, fields); insertCommand.Connection = connection; dataAdapter.InsertCommand = insertCommand; // store changes dataAdapter.Update(table); } } catch (Exception) { } finally { connection.Close(); } }
/// <summary> /// Writes stops. /// </summary> /// <param name="schedules">Schedules to export.</param> /// <param name="routes">Routes to export.</param> /// <param name="fields">Fields to export.</param> /// <param name="data">Data keeper.</param> /// <param name="imageExporter">Map image exporter.</param> /// <param name="table">Table to data writing.</param> /// <param name="tracker">Cancel tracker (can be null).</param> private void _WriteStops(ICollection <Schedule> schedules, ICollection <Route> routes, ICollection <string> fields, DataKeeper data, MapImageExporter imageExporter, DataTable table, ICancelTracker tracker) { foreach (Schedule schedule in schedules) { _CheckCancelState(tracker); // stops Guid scheduleId = schedule.Id; foreach (Route route in schedule.Routes) { _CheckCancelState(tracker); if ((null != routes) && !routes.Contains(route)) { continue; // NOTE: skeep not selected } IDataObjectCollection <Stop> stops = route.Stops; for (int index = 0; index < stops.Count; ++index) { _CheckCancelState(tracker); // write stop Stop stop = stops[index]; DataRow dr = table.NewRow(); foreach (string field in fields) { if (field == "StopVicinityMap") { // NOTE: special routine Debug.Assert(null != imageExporter); _CheckCancelState(tracker); Image image = imageExporter.GetStopImage(route, stop, STOP_MAP_RADIUS, STOP_MAP_IMAGE_SIZE_X, STOP_MAP_IMAGE_SIZE_Y, IMAGE_DPI); dr[field] = _ConvertImageToBlob(image); if (null != image) { image.Dispose(); image = null; } } else if (field == "Directions") { // NOTE: special routine System.Text.UnicodeEncoding encoding = new System.Text.UnicodeEncoding(); dr[field] = encoding.GetBytes(_GetDirectionsText(stop.Directions)); } else { dr[field] = _FormatFieldValue(data.GetStopFieldValue(field, scheduleId, stop)); } } table.Rows.Add(dr); } } _CheckCancelState(tracker); // unassigned orders IDataObjectCollection <Order> orders = schedule.UnassignedOrders; if (null != orders) { foreach (Order order in orders) { _CheckCancelState(tracker); DataRow dr = table.NewRow(); foreach (string field in fields) { DataWrapper wrapper = data.GetStopFieldValue(field, scheduleId, order); dr[field] = _FormatFieldValue(wrapper); } table.Rows.Add(dr); } } } }