Esempio n. 1
0
        public IList <Album> GetAlbumsByArtistID(int artistID)
        {
            IList <Album> result = new List <Album>();

            ISession session = SessionFactory.GetSession();

            try
            {
                DetachedCriteria artistsCriteria = DetachedCriteria.For <AlbumEntity>().CreateCriteria("Artists").
                                                   Add(Restrictions.Eq("ID", artistID)).
                                                   SetProjection(Property.ForName("ID"));

                ICriteria criteria = session.CreateCriteria <AlbumEntity>().
                                     Add(Subqueries.PropertyIn("ID", artistsCriteria)).AddOrder(new Order("ID", true));

                IList <AlbumEntity> albums = criteria.List <AlbumEntity>();

                EntityConverter converter = new EntityConverter();

                foreach (AlbumEntity dataEntity in albums)
                {
                    Album businessEntity = converter.FromDataEntity(dataEntity, AlbumConvertOptions.Small);
                    result.Add(businessEntity);
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
            }
            finally
            {
                session.Close();
            }

            return(result);
        }
Esempio n. 2
0
        public IPagedList <TaggedObject> GetTaggedObjects(ILoadOptions loadOptions)
        {
            /*
             * This method uses sophisticated stored procedure which performs actual pagination and
             * filtering logic against special database view.
             *
             * First record of result set is stub one and returns total number of items matching filtering
             * rules.
             */

            var result = new PagedList <TaggedObject>();

            ISession session = SessionFactory.GetSession();

            try
            {
                var    specialOptions   = loadOptions as TagLoadOptions;
                string entityTypeFilter = String.Empty;
                string entityNameFilter = String.Empty;
                if (specialOptions != null)
                {
                    if (!specialOptions.ExcludeAlbums && !specialOptions.ExcludeArtists)
                    {
                        entityTypeFilter = String.Format("{0},{1}", (int)TaggedObjectType.Artist, (int)TaggedObjectType.Album);
                    }
                    else if (!specialOptions.ExcludeAlbums)
                    {
                        entityTypeFilter = String.Format("{0}", (int)TaggedObjectType.Album);
                    }
                    else if (!specialOptions.ExcludeArtists)
                    {
                        entityTypeFilter = String.Format("{0}", (int)TaggedObjectType.Artist);
                    }

                    entityNameFilter = specialOptions.EntityName == null ? String.Empty : specialOptions.EntityName;
                }

                var query = session.GetNamedQuery("GetTaggedObjects");
                query.SetParameter("tagIDs", loadOptions.FilterValue);
                query.SetParameter("entityTypes", entityTypeFilter);
                query.SetParameter("ioffset", loadOptions.FirstResult);
                query.SetParameter("ilimit", loadOptions.MaxResults);
                query.SetParameter("entityName", entityNameFilter);
                var dataEntities = query.SetResultTransformer(new TaggedObjectResultTransformer()).List <TaggedObjectEntity>();

                //get stub entity generated by sp, which stores total items count needed for pagination
                var stubCounterEntity = dataEntities.Where(e => e.ObjectType == -1).First();
                dataEntities.Remove(stubCounterEntity);

                EntityConverter converter = new EntityConverter();

                foreach (var de in dataEntities)
                {
                    var tag = converter.FromDataEntity(de);
                    result.Add(tag);
                }

                result.TotalItems = stubCounterEntity.ID;
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
            }
            finally
            {
                session.Close();
            }

            return(result);
        }