Ejemplo n.º 1
0
        public override IList <IDataObject> Get(string objectType, DataFilter filter, int pageSize, int startIndex)
        {
            DataFilter  newFilter   = Utility.CloneDataContractObject <DataFilter>(filter);
            AccessLevel accessLevel = Authorize(objectType, ref newFilter);

            if (accessLevel < AccessLevel.Read)
            {
                throw new UnauthorizedAccessException(String.Format(UNAUTHORIZED_ERROR, _settings["scope"]));
            }

            ISession session = NHibernateSessionManager.Instance.GetSession(_settings["AppDataPath"], _settings["Scope"]);

            try
            {
                if (_dbDictionary.IdentityConfiguration != null)
                {
                    IdentityProperties identityProperties = _dbDictionary.IdentityConfiguration[objectType];
                    if (identityProperties.UseIdentityFilter)
                    {
                        newFilter = FilterByIdentity(objectType, newFilter, identityProperties);
                    }
                }

                DataObject objectDefinition = _dbDictionary.dataObjects.Find(x => x.objectName.ToUpper() == objectType.ToUpper());

                if (objectDefinition == null)
                {
                    throw new Exception("Object type [" + objectType + "] not found.");
                }

                string ns = String.IsNullOrEmpty(objectDefinition.objectNamespace)
          ? String.Empty : (objectDefinition.objectNamespace + ".");

                Type type = Type.GetType(ns + objectType + ", " + _settings["ExecutingAssemblyName"]);

                // make an exception for tests
                if (type == null)
                {
                    type = Type.GetType(ns + objectType + ", NUnit.Tests");
                }

                ICriteria criteria = NHibernateUtility.CreateCriteria(session, type, objectDefinition, newFilter);

                if (pageSize == 0 && startIndex == 0)
                {
                    List <IDataObject> dataObjects = new List <IDataObject>();
                    long totalCount       = GetCount(objectType, filter);
                    int  internalPageSize = (_settings["InternalPageSize"] != null) ? int.Parse(_settings["InternalPageSize"]) : 1000;
                    int  numOfRows        = 0;

                    while (numOfRows < totalCount)
                    {
                        if (filter != null && filter.OrderExpressions != null && filter.OrderExpressions.Count > 0)
                        {
                            criteria.SetFirstResult(numOfRows).SetMaxResults(internalPageSize);
                        }
                        else
                        {
                            NHibernate.Criterion.Order order = new NHibernate.Criterion.Order(objectDefinition.keyProperties.First().keyPropertyName, true);
                            criteria.AddOrder(order).SetFirstResult(numOfRows).SetMaxResults(internalPageSize);
                        }

                        dataObjects.AddRange(criteria.List <IDataObject>());
                        numOfRows += internalPageSize;
                    }

                    return(dataObjects);
                }
                else
                {
                    if (filter != null && filter.OrderExpressions != null && filter.OrderExpressions.Count > 0)
                    {
                        criteria.SetFirstResult(startIndex).SetMaxResults(pageSize);
                    }
                    else
                    {
                        NHibernate.Criterion.Order order = new NHibernate.Criterion.Order(objectDefinition.keyProperties.First().keyPropertyName, true);
                        criteria.AddOrder(order).SetFirstResult(startIndex).SetMaxResults(pageSize);
                    }

                    IList <IDataObject> dataObjects = criteria.List <IDataObject>();
                    return(dataObjects);
                }
            }
            catch (Exception ex)
            {
                _logger.Error("Error in Get: " + ex);
                throw new Exception(string.Format("Error while getting a list of data objects of type [{0}]. {1}", objectType, ex));
            }
            finally
            {
                CloseSession(session);
            }
        }
Ejemplo n.º 2
0
        public override IList <IDataObject> Get(string objectType, DataFilter filter, int pageSize, int startIndex)
        {
            ISession session = NHibernateSessionManager.Instance.GetSession(_settings["AppDataPath"], _settings["Scope"]);

            try
            {
                if (_databaseDictionary.IdentityConfiguration != null)
                {
                    IdentityProperties identityProperties = _databaseDictionary.IdentityConfiguration[objectType];
                    if (identityProperties.UseIdentityFilter)
                    {
                        filter = FilterByIdentity(objectType, filter, identityProperties);
                    }
                }

                DataObject objectDefinition = _databaseDictionary.dataObjects.Find(x => x.objectName.ToUpper() == objectType.ToUpper());

                if (objectDefinition == null)
                {
                    throw new Exception("Object type [" + objectType + "] not found.");
                }

                string ns = String.IsNullOrEmpty(objectDefinition.objectNamespace)
          ? String.Empty : (objectDefinition.objectNamespace + ".");

                Type type = Type.GetType(ns + objectType + ", " + _settings["ExecutingAssemblyName"]);

                // make an exception for tests
                if (type == null)
                {
                    type = Type.GetType(ns + objectType + ", NUnit.Tests");
                }

                ICriteria criteria = NHibernateUtility.CreateCriteria(session, type, objectDefinition, filter);

                if (pageSize == 0 && startIndex == 0)
                {
                    List <IDataObject> dataObjects = new List <IDataObject>();
                    long totalCount       = GetCount(objectType, filter);
                    int  internalPageSize = (_settings["InternalPageSize"] != null) ? int.Parse(_settings["InternalPageSize"]) : 1000;
                    int  numOfRows        = 0;

                    while (numOfRows < totalCount)
                    {
                        criteria.SetFirstResult(numOfRows).SetMaxResults(internalPageSize);
                        dataObjects.AddRange(criteria.List <IDataObject>());
                        numOfRows += internalPageSize;
                    }

                    return(dataObjects);
                }
                else
                {
                    criteria.SetFirstResult(startIndex).SetMaxResults(pageSize);
                    IList <IDataObject> dataObjects = criteria.List <IDataObject>();
                    return(dataObjects);
                }
            }
            catch (Exception ex)
            {
                _logger.Error("Error in Get: " + ex);
                throw new Exception(string.Format("Error while getting a list of data objects of type [{0}]. {1}", objectType, ex));
            }
            finally
            {
                CloseSession(session);
            }
        }