protected List <Action> SetImageUrl(IFileBased sourceQuestion, IFileBased targetQuestion) { List <Action> fileListToDelete = new List <Action>(); if (sourceQuestion.file != null) { if (!sourceQuestion.file.isDeleted) { if (String.IsNullOrWhiteSpace(targetQuestion.id)) { targetQuestion.id = Guid.NewGuid().ToString(); } fileListToDelete.Add(() => File.Delete(FILE_IMAGE_PATH + "/" + targetQuestion.id + sourceQuestion.file.ext)); fileListToDelete.Add(() => File.Move(FILE_TEMP_PATH + "/" + sourceQuestion.file.fileName, FILE_IMAGE_PATH + "/" + targetQuestion.id + sourceQuestion.file.ext)); targetQuestion.imageUrl = targetQuestion.id + sourceQuestion.file.ext; } else { if (targetQuestion.imageUrl != null) { fileListToDelete.Add(() => File.Delete(FILE_IMAGE_PATH + "/" + targetQuestion.imageUrl)); targetQuestion.imageUrl = null; } } } else { targetQuestion.imageUrl = sourceQuestion.imageUrl; } return(fileListToDelete); }
/// <summary> /// Construcor, initializes the Connector object. /// </summary> internal Connector(IFileBased entity, bool Shared) { this.pooledEntity = entity; this._Shared = Shared; this.Pooled = true; Connector.InstanceCounter++; this.InstanceNumber = Connector.InstanceCounter; }
/// <summary> /// Searches the shared and pooled connector lists for a /// matching connector object or creates a new one. /// </summary> /// <param name="entity">Entity to be pooled.</param> /// <param name="Shared">Allows multiple connections on a single connector. </param> /// <returns>A pooled connector object.</returns> internal Connector RequestConnector(IFileBased entity, bool Shared) { // if a shared connector is requested then the Shared // Connector List is searched first: if (Shared) { foreach (Connector connector in this.SharedConnectors) { if (entity.Path == connector.PooledEntity.Path) { // Bingo! // Return the shared connector to caller. // The connector is already in use. connector._ShareCount++; return(connector); } } } // if a shared connector could not be found or a // nonshared connector is requested, then the pooled // (unused) connectors are beeing searched. foreach (Connector connector in this.PooledConnectors) { if (connector.PooledEntity.Path == entity.Path) { // Bingo! // Remove the Connector from the pooled connectors list. this.PooledConnectors.Remove(connector); // Make the connector shared if requested if (connector.Shared = Shared) { this.SharedConnectors.Add(connector); connector._ShareCount = 1; } // done... connector.InUse = true; return(connector); } } // No suitable connector found, so create new one Connector NewConnector = new Connector(entity, Shared); // Shared connections must be added to the shared // connectors list if (Shared) { this.SharedConnectors.Add(NewConnector); NewConnector._ShareCount = 1; } // and then returned to the caller NewConnector.InUse = true; NewConnector.Open(); return(NewConnector); }
/// <summary> /// Searches the shared and pooled connector lists for a /// matching connector object or creates a new one. /// </summary> /// <param name="entity">Entity to be pooled.</param> /// <param name="Shared">Allows multiple connections on a single connector. </param> /// <returns>A pooled connector object.</returns> internal Connector RequestConnector(IFileBased entity, bool Shared) { // if a shared connector is requested then the Shared // Connector List is searched first: if (Shared) { foreach (Connector connector in this.SharedConnectors) { if (entity.Path == connector.PooledEntity.Path) { // Bingo! // Return the shared connector to caller. // The connector is already in use. connector._ShareCount++; return connector; } } } // if a shared connector could not be found or a // nonshared connector is requested, then the pooled // (unused) connectors are beeing searched. foreach (Connector connector in this.PooledConnectors) { if (connector.PooledEntity.Path == entity.Path) { // Bingo! // Remove the Connector from the pooled connectors list. this.PooledConnectors.Remove(connector); // Make the connector shared if requested if (connector.Shared = Shared) { this.SharedConnectors.Add(connector); connector._ShareCount = 1; } // done... connector.InUse = true; return connector; } } // No suitable connector found, so create new one Connector NewConnector = new Connector(entity, Shared); // Shared connections must be added to the shared // connectors list if (Shared) { this.SharedConnectors.Add(NewConnector); NewConnector._ShareCount = 1; } // and then returned to the caller NewConnector.InUse = true; NewConnector.Open(); return NewConnector; }
/// <summary> /// Make sure data is loaded before it is accessed. /// IFileBased /// </summary> private void SynchronizeStore() { if (!(Store is IFileBased)) { return; } IFileBased fileBased = (IFileBased)Store; if ((!fileBased.IsOpen) && (fileBased.Path != null)) { fileBased.Open(); } }
public void SetUpMocks() { mocks = new MockRepository(); fileBased = mocks.StrictMock <IFileBased>(); }
/// <summary> /// Creates a new table in a Microsoft SQL Server database and copies rows from an existing datasource. /// </summary> /// <remarks> /// <para>The datatable created will contain six extra columns besides the attribute data: "OID" (Object ID row), /// "WKB_Geometry" (Geometry stored as WKB), and Envelope_MinX, Envelope_MinY, Envelope_MaxX, Envelope_MaxY /// for geometry bounding box.</para> /// <para> /// <example> /// Upload a ShapeFile to a database: /// <code> /// public void CreateDatabase(string shapeFile) /// { /// if (!System.IO.File.Exists(shapeFile)) /// { /// MessageBox.Show("File not found"); /// return; /// } /// ShapeFile shp = new ShapeFile(shapeFile, false); /// //Create tablename from filename /// string tablename = shapeFile.Substring(shapeFile.LastIndexOf('\\') + 1, /// shapeFile.LastIndexOf('.') - shapeFile.LastIndexOf('\\') - 1); /// //Create connectionstring /// string connstr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|GeoDatabase.mdf;Integrated Security=True;User Instance=True"; /// int count = SharpMap.Data.Providers.MsSql.CreateDataTable(shp, tablename, connstr); /// MessageBox.Show("Uploaded " + count.ToString() + " features to datatable '" + tablename + "'"); /// } /// </code> /// </example> /// </para> /// </remarks> /// <param name="datasource">Datasource to upload</param> /// <param name="tablename">Name of table to create (existing table will be overwritten!)</param> /// <param name="connstr">Connection string to database</param> /// <returns>Number or rows inserted, -1 if failed and 0 if table created but no rows inserted.</returns> public static int CreateDataTable(SharpMap.Data.Providers.IFeatureProvider datasource, string tablename, string connstr) { if (datasource is IFileBased) { IFileBased fileBasedDataSource = (IFileBased)datasource; fileBasedDataSource.Open(fileBasedDataSource.Path); } // TODO: this should work as IFeature FeatureDataRow geom = (FeatureDataRow)datasource.GetFeature(0); DataColumnCollection columns = geom.Table.Columns; int counter = -1; using (SqlConnection conn = new SqlConnection(connstr)) { SqlCommand command = new SqlCommand(); command.Connection = conn; conn.Open(); //Try to drop table if it exists try { command.CommandText = "DROP TABLE \"" + tablename + "\";"; command.ExecuteNonQuery(); } catch { } //Create new table for storing the datasource string sql = "CREATE TABLE " + tablename + " (oid INTEGER IDENTITY PRIMARY KEY, WKB_Geometry Image, " + "Envelope_MinX real, Envelope_MinY real, Envelope_MaxX real, Envelope_MaxY real"; foreach (DataColumn col in columns) { if (col.DataType != typeof(String)) { sql += ", " + col.ColumnName + " " + Type2SqlType(col.DataType).ToString(); } else { sql += ", " + col.ColumnName + " VARCHAR(256)"; } } command.CommandText = sql + ");"; command.ExecuteNonQuery(); counter++; ICollection <int> indexes = datasource.GetObjectIDsInView(datasource.GetExtents()); //Select all indexes in shapefile, loop through each feature and insert them one-by-one foreach (int idx in indexes) { //Get feature from shapefile // TODO: this should work as IFeature SharpMap.Data.FeatureDataRow feature = (FeatureDataRow)datasource.GetFeature(idx); if (counter == 0) { //Create insert script string strSQL = " ("; foreach (DataColumn col in feature.Table.Columns) { strSQL += "@" + col.ColumnName + ","; } strSQL += "@WKB_Geometry,@Envelope_MinX,@Envelope_MinY, " + "@Envelope_MaxX,@Envelope_MaxY)"; strSQL = "INSERT INTO " + tablename + strSQL.Replace("@", "") + " VALUES" + strSQL; command.CommandText = strSQL; command.Parameters.Clear(); //Add datacolumn parameters foreach (DataColumn col in feature.Table.Columns) { command.Parameters.Add("@" + col.ColumnName, Type2SqlType(col.DataType)); } //Add geometry parameters command.Parameters.Add("@WKB_Geometry", SqlDbType.VarBinary); command.Parameters.Add("@Envelope_MinX", SqlDbType.Real); command.Parameters.Add("@Envelope_MinY", SqlDbType.Real); command.Parameters.Add("@Envelope_MaxX", SqlDbType.Real); command.Parameters.Add("@Envelope_MaxY", SqlDbType.Real); } //Set values foreach (DataColumn col in feature.Table.Columns) { command.Parameters["@" + col.ColumnName].Value = feature[col]; } if (feature.Geometry != null) { command.Parameters["@WKB_Geometry"].Value = feature.Geometry.AsBinary(); //Add the geometry as Well-Known Binary GeoAPI.Geometries.IEnvelope box = feature.Geometry.EnvelopeInternal; command.Parameters["@Envelope_MinX"].Value = box.MinX; command.Parameters["@Envelope_MinY"].Value = box.MinY; command.Parameters["@Envelope_MaxX"].Value = box.MaxX; command.Parameters["@Envelope_MaxY"].Value = box.MaxY; } else { command.Parameters["@WKB_Geometry"].Value = DBNull.Value; command.Parameters["@Envelope_MinX"].Value = DBNull.Value; command.Parameters["@Envelope_MinY"].Value = DBNull.Value; command.Parameters["@Envelope_MaxX"].Value = DBNull.Value; command.Parameters["@Envelope_MaxY"].Value = DBNull.Value; } //Insert row command.ExecuteNonQuery(); counter++; } //Create indexes command.Parameters.Clear(); command.CommandText = "CREATE INDEX [IDX_Envelope_MinX] ON " + tablename + " (Envelope_MinX)"; command.ExecuteNonQuery(); command.CommandText = "CREATE INDEX [IDX_Envelope_MinY] ON " + tablename + " (Envelope_MinY)"; command.ExecuteNonQuery(); command.CommandText = "CREATE INDEX [IDX_Envelope_MaxX] ON " + tablename + " (Envelope_MaxX)"; command.ExecuteNonQuery(); command.CommandText = "CREATE INDEX [IDX_Envelope_MaxY] ON " + tablename + " (Envelope_MaxY)"; command.ExecuteNonQuery(); conn.Close(); } if (datasource is IFileBased) { ((IFileBased)datasource).Close(); } return(counter); }
/// <summary> /// Construcor, initializes the Connector object. /// </summary> internal Connector(IFileBased entity, bool Shared) { this.pooledEntity = entity; this._Shared = Shared; this.Pooled = true; Connector.InstanceCounter++; this.InstanceNumber = Connector.InstanceCounter; }
public void SetUpMocks() { mocks = new MockRepository(); fileBased = mocks.StrictMock<IFileBased>(); }