Пример #1
0
        /// <summary>
        /// This method retrieves an IActiveRecord by it's ObjectID.
        /// </summary>
        /// <param name="oid">int</param>
        /// <exception cref="RepositoryQueryException"></exception>
        /// <returns>T (IActiveRecord)</returns>
        public T SingleByOID(int oid)
        {
            ITable  table      = GetTable;
            IObject esriObject = null;

            try
            {
                esriObject = table.GetRow(oid) as IObject;
            }
            catch (COMException comEx)
            {
                _log.Error("Ocorreu um erro COM ao tentar buscar o objeto via OBJECTID.", comEx);
            }
            catch (Exception ex)
            {
                _log.Error("Ocorreu um erro desconhecido ao tentar buscar o objeto via OBJECTID", ex);
            }

            if (esriObject == null)
            {
                throw new RepositoryQueryException(
                          "O objeto pesquisado não pode ser encontrado.",
                          GetWorkspaceDefinition, GetTableDefinition, IsBeingEdited,
                          String.Format("OID = {0}", oid.ToString()));
            }

            IActiveRecordFactory <T> factory = new ActiveRecordFactory <T>();

            IActiveRecord objectWrapper = factory.CreateActiveRecord(esriObject);

            return((T)objectWrapper);
        }
Пример #2
0
        public List <T> Filter(IQueryFilter filter)
        {
            var     results = new List <T>();
            var     table   = this.GetTable;
            ICursor cursor  = null;

            if (table == null)
            {
                throw new ActiveRecordAttributeException(
                          this.GetType(),
                          "Não foi possível encontrar o atributo de tabela.");
            }

            try
            {
                var count = table.RowCount(filter);

                if (count == 0)
                {
                    return(results);
                }

                // if we have more records then treshold,
                // just grab the object id field
                if (count > GetTableDefinition.RecordTreshold)
                {
                    filter.SubFields = table.OIDFieldName;
                }

                cursor = table.Search(filter, false);
                IActiveRecordFactory <T> factory = new ActiveRecordFactory <T>();
                IObject tempObject = null;

                while ((tempObject = cursor.NextRow() as IObject) != null)
                {
                    if (count <= GetTableDefinition.RecordTreshold)
                    {
                        IActiveRecord wrapper = factory.CreateActiveRecord(tempObject);
                        results.Add((T)wrapper);
                    }
                    else
                    {
                        IActiveRecord proxyWrapper = factory.CreateActiveRecord(tempObject.OID);
                        results.Add((T)proxyWrapper);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (cursor != null)
                {
                    Marshal.ReleaseComObject(cursor);
                }
            }
            return(results);
        }
Пример #3
0
        /// <summary>
        /// Retrieves a collection of IActiveRecord using a specific filter.
        /// </summary>
        /// <param name="filter">Filtering rules</param>
        /// <returns>List(T) (IActiveRecord)</returns>
        public List <T> FilterLazy(IQueryFilter filter)
        {
            List <T> results = new List <T>();
            ITable   table   = this.GetTable;
            ICursor  cursor  = null;

            if (table == null)
            {
                throw new RepositoryException(
                          "Não foi possível obter a tabela para pesquisa",
                          GetWorkspaceDefinition,
                          GetTableDefinition,
                          IsBeingEdited);
            }

            // lets just grab the object id field to speed things up a bit
            filter.SubFields = table.OIDFieldName;

            try
            {
                int count = table.RowCount(filter);

                if (count == 0)
                {
                    return(results);
                }

                cursor = table.Search(filter, false);
                IActiveRecordFactory <T> factory = new ActiveRecordFactory <T>();
                IObject tempObject = null;

                while ((tempObject = cursor.NextRow() as IObject) != null)
                {
                    IActiveRecord proxyWrapper = factory.CreateActiveRecord(tempObject.OID);
                    results.Add((T)proxyWrapper);
                }
            }
            catch (COMException comEx)
            {
                throw new RepositoryQueryException(
                          "Ocorreu um erro durante a pesquisa.",
                          comEx,
                          GetWorkspaceDefinition,
                          GetTableDefinition,
                          IsBeingEdited,
                          filter.WhereClause);
            }
            finally
            {
                if (cursor != null)
                {
                    Marshal.ReleaseComObject(cursor);
                }
            }
            return(results);
        }
Пример #4
0
        public List <T> FindAll()
        {
            var results = new List <T>();
            var table   = GetTable;

            ICursor cursor = null;

            if (table == null)
            {
                throw new ArgumentException("Não foi possível obter a tabela para pesquisa.");
            }

            try
            {
                IQueryFilter filter = new QueryFilterClass {
                    WhereClause = String.Empty
                };

                var count = table.RowCount(filter);

                if (count > GetTableDefinition.RecordTreshold)
                {
                    filter.SubFields = table.OIDFieldName;
                }

                cursor = table.Search(null, false);
                IActiveRecordFactory <T> factory = new ActiveRecordFactory <T>();
                IObject tempObject = null;

                while ((tempObject = cursor.NextRow() as IObject) != null)
                {
                    if (count <= GetTableDefinition.RecordTreshold)
                    {
                        IActiveRecord wrapper = factory.CreateActiveRecord(tempObject);
                        results.Add((T)wrapper);
                    }
                    else
                    {
                        IActiveRecord proxyWrapper = factory.CreateActiveRecord(tempObject.OID);
                        results.Add((T)proxyWrapper);
                    }
                }
            }
            finally
            {
                if (cursor != null)
                {
                    Marshal.ReleaseComObject(cursor);
                }
            }
            return(results);
        }
Пример #5
0
        public T Create()
        {
            ITable table = this.GetTable;

            if (table == null)
            {
                throw new ArgumentException("A tabela não pode ser obtida.");
            }

            IActiveRecordFactory <T> factory       = new ActiveRecordFactory <T>();
            IActiveRecord            objectWrapper = factory.CreateActiveRecord(table.CreateRow() as IObject);

            return((T)objectWrapper);
        }
Пример #6
0
        public T SingleByPrimaryKeys(List <object> primaryKeys)
        {
            IActiveRecordFactory <T> factory       = new ActiveRecordFactory <T>();
            IActiveRecord            objectWrapper = factory.CreateActiveRecord();
            //PrimaryFieldAttribute pkAttribute = objectWrapper.GetPrimaryKeyDefinition();
            List <PrimaryFieldAttribute> lstPkAttribute = objectWrapper.GetPrimarysKeyDefinitions();
            ITable table = this.GetTable;

            if (primaryKeys == null || primaryKeys.Count.Equals(0))
            {
                throw new Exception(
                          "É obrigatório informar um valor de primary key.");
            }
            else if (lstPkAttribute == null || lstPkAttribute.Count.Equals(0))
            {
                throw new Exception(
                          "Este repositório não possui Chave[s] Primária[s] definidas.");
            }
            else if (!lstPkAttribute.Count.Equals(primaryKeys.Count))
            {
                throw new Exception(
                          string.Format("A quantidade de Chave[s] Primária[s] {0} é diferente da quantidade de valor[es] informado[s] {1}", lstPkAttribute.Count.ToString(), primaryKeys.Count.ToString()));
            }
            else
            {
                var query = String.Empty;
                var ct    = 0;
                foreach (PrimaryFieldAttribute pkAttribute in lstPkAttribute)
                {
                    if (String.IsNullOrEmpty(query))
                    {
                        query = String.Format(pkAttribute.QuoteValue ? "{0} = '{1}'" : "{0} = {1}", pkAttribute.FieldName, primaryKeys[ct].ToString());
                    }
                    else
                    {
                        query += String.Format(pkAttribute.QuoteValue ? " and {0} = '{1}'" : " and {0} = {1}", pkAttribute.FieldName, primaryKeys[ct].ToString());
                    }
                    ct++;
                }
                IQueryFilter filter = new QueryFilterClass {
                    WhereClause = query
                };

                if (table.RowCount(filter) == 0)
                {
                    return(default(T));
                }
                try
                {
                    var cursor     = table.Search(filter, false);
                    var esriObject = cursor.NextRow() as IObject;

                    if (esriObject == null)
                    {
                        throw new RepositoryQueryException(
                                  "O objeto pesquisado não pode ser encontrado.",
                                  GetWorkspaceDefinition,
                                  GetTableDefinition,
                                  IsBeingEdited,
                                  filter.WhereClause);
                    }

                    objectWrapper.UnderlyingObject = esriObject;
                }
                catch (Exception ex)
                {
                }
            }
            return((T)objectWrapper);
        }
Пример #7
0
        public List <T> Find(List <string> attributeNames, List <object> values)
        {
            var results = new List <T>();
            var table   = GetTable;

            ICursor cursor = null;

            if (table == null)
            {
                throw new ArgumentException("Não foi possível obter a tabela para pesquisa.");
            }

            try
            {
                if (attributeNames == null || attributeNames.Count.Equals(0))
                {
                    throw new ArgumentException("Não foi informado nenhum nome de atributo.");
                }
                else if (values == null || values.Count.Equals(0))
                {
                    throw new ArgumentException("Não foi informado nenhum valor de atributo.");
                }
                else if (!attributeNames.Count.Equals(values.Count))
                {
                    throw new ArgumentException(
                              "A quantidade de atributos não confere com a quantidade de valores informado.");
                }
                else
                {
                    var query = string.Empty;

                    for (var cont = 0; cont < attributeNames.Count; cont++)
                    {
                        var indexField = table.FindField(attributeNames[cont]);
                        if (indexField > -1)
                        {
                            if (String.IsNullOrEmpty(query))
                            {
                                query = attributeNames[cont];
                            }
                            else
                            {
                                query += string.Format(" and {0}", attributeNames[cont]);
                            }

                            if (values[cont] == null)
                            {
                                query += " is null";
                            }
                            else
                            {
                                IField field = table.Fields.get_Field(indexField);

                                switch (field.Type)
                                {
                                case esriFieldType.esriFieldTypeDouble:
                                case esriFieldType.esriFieldTypeInteger:
                                case esriFieldType.esriFieldTypeSmallInteger:
                                case esriFieldType.esriFieldTypeSingle:
                                case esriFieldType.esriFieldTypeOID:
                                    query += string.Format(" = {0}", values[cont]);
                                    break;

                                default:
                                    query += string.Format(" = '{0}'", values[cont]);
                                    break;
                                }
                            }
                        }
                        else
                        {
                            throw new ArgumentException(
                                      "O atributo {0} não existe.", attributeNames[cont]);
                        }
                    }


                    IQueryFilter filter = new QueryFilterClass {
                        WhereClause = query
                    };


                    var count = table.RowCount(filter);

                    if (count > GetTableDefinition.RecordTreshold)
                    {
                        filter.SubFields = table.OIDFieldName;
                    }

                    cursor = table.Search(filter, false);
                    IActiveRecordFactory <T> factory = new ActiveRecordFactory <T>();
                    IObject tempObject = null;

                    while ((tempObject = cursor.NextRow() as IObject) != null)
                    {
                        if (count <= GetTableDefinition.RecordTreshold)
                        {
                            IActiveRecord wrapper = factory.CreateActiveRecord(tempObject);
                            results.Add((T)wrapper);
                        }
                        else
                        {
                            IActiveRecord proxyWrapper = factory.CreateActiveRecord(tempObject.OID);
                            results.Add((T)proxyWrapper);
                        }
                    }
                }
            }
            finally
            {
                if (cursor != null)
                {
                    Marshal.ReleaseComObject(cursor);
                }
            }
            return(results);
        }