001/*
002        $Id: ItemQuery.java 3207 2009-04-09 06:48:11Z gregory $
003        
004        Copyright (C) 2006 Gregory Vincic, Olle Mansson
005        Copyright (C) 2007 Gregory Vincic
006
007        This file is part of Proteios.
008        Available at http://www.proteios.org/
009
010        Proteios is free software; you can redistribute it and/or modify it
011        under the terms of the GNU General Public License as published by
012        the Free Software Foundation; either version 2 of the License, or
013        (at your option) any later version.
014
015        Proteios is distributed in the hope that it will be useful, but
016        WITHOUT ANY WARRANTY; without even the implied warranty of
017        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
018        General Public License for more details.
019
020        You should have received a copy of the GNU General Public License
021        along with this program; if not, write to the Free Software
022        Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
023        02111-1307, USA.
024*/
025package org.proteios.core;
026
027import org.proteios.core.data.BasicData;
028import org.proteios.core.query.Query;
029import java.util.List;
030
031/**
032        An implementation of the {@link Query} interface that returns item
033        objects. This type of query is used for all items except those that
034        are batchable, for example reporters and raw data. The result of a
035        query can be returned as a list or an iterator.
036
037        @author Nicklas
038        @version 2.0
039        @base.modified $Date: 2009-04-09 08:48:11 +0200 (Thu, 09 Apr 2009) $
040*/
041public class ItemQuery<I extends BasicItem>
042        extends AbstractEntityQuery
043{
044
045        /**
046                The class of the item objects that are returned.
047        */
048        private final Class<I> itemClass;
049
050        /**
051                The data layer class of the item objects that are returned.
052        */
053        private final Class<? extends BasicData> dataClass;
054
055        /**
056                Create a new query for the specified item, using the default optional
057                runtime filter.
058                @param itemClass The class of the item objects that are returned
059        */
060        ItemQuery(Class<I> itemClass)
061        {
062                super(Item.fromItemClass(itemClass), null, false);
063                this.itemClass = itemClass;
064                this.dataClass = getItemType().getDataClass();
065        }
066        
067        /**
068                Create a new query for the specified item, using a non-default optional
069                runtime filter.
070                @param itemClass The class of the item objects that are returned
071                @param optionalFilter A runtime filter replacing the default optional filter
072                        or null to not use any optional filter
073        */
074        ItemQuery(Class<I> itemClass, QueryRuntimeFilter optionalFilter)
075        {
076                super(Item.fromItemClass(itemClass), null, false, optionalFilter);
077                this.itemClass = itemClass;
078                this.dataClass = getItemType().getDataClass();
079        }
080
081        /**
082                Execute the query and return the results as a list.
083                @param dc The <code>DbControl</code> used to access the database
084                        and check permissions
085                @throws BaseException If there is an error
086        */
087        public ItemResultList<I> list(DbControl dc)
088                throws BaseException
089        {
090                enableFilters(dc);
091                
092                List<? extends BasicData> result = HibernateUtil.loadList(dataClass, getMainHqlQuery(dc));
093                int totalCount = result.size();
094                
095                // Load the total count if needed and requested
096                if ((getFirstResult() >= 0 || getMaxResults() > 0) && isReturningTotalCount())
097                {
098                        totalCount = HibernateUtil.loadData(Integer.class, getCountHqlQuery(dc));
099                }
100
101                disableFilters(dc);
102                return new ItemResultList<I>(result, dc, itemClass, totalCount);
103        }
104
105        /**
106                Execute the query and return the results as an iterator.
107                @param dc The <code>DbControl</code> used to access the database
108                        and check permissions
109                @throws BaseException If there is an error
110        */
111        public ItemResultIterator<I> iterate(DbControl dc)
112                throws BaseException
113        {
114                enableFilters(dc);
115                int totalCount = -1;
116                
117                // Load the total count if it is requested
118                if (isReturningTotalCount())
119                {
120                        totalCount = HibernateUtil.loadData(Integer.class, getCountHqlQuery(dc));
121                }
122
123                ScrollIterator<? extends BasicData> result = HibernateUtil.loadIterator(dataClass, getMainHqlQuery(dc));
124                disableFilters(dc);
125                return new ItemResultIterator<I>(result, dc, itemClass, totalCount);
126        }
127
128}