001/*
002        $Id: DataResultIterator.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.ResultIterator;
029
030/**
031        Return the results of a {@link DataQuery} as an iterator. This type
032        of iterator is only used for batchable items like reporter and raw data.
033        It is expected that the query uses the stateless Hibernate session available
034        from {@link DbControl#getStatelessSession()}. If 
035        The returned objects are automatically disconnected from the Hibernate session
036        to avoid memory problems and bypassing permission checks.
037        
038        @author Samuel, Nicklas
039        @version 2.0
040        @see DataQuery#iterate(DbControl)
041        @base.modified $Date: 2009-04-09 08:48:11 +0200 (Thu, 09 Apr 2009) $
042*/
043public class DataResultIterator<I extends BasicData>
044        implements ResultIterator<I>
045{
046        /**
047                The internal iterator of BasicData objects.
048        */
049        private final ScrollIterator<I> data;
050
051        /**
052                The DbControl that was used in the query, if it used the regular 
053                Hibernate session, null otherwise.
054        */
055        private final DbControl dc;
056
057        private final SessionControl sc;
058        
059        /**
060                The total number of items.
061        */
062        private final int totalCount;
063
064        /**
065                The type of items returned by the query.
066        */
067        private final Item itemType;
068
069        DataResultIterator(ScrollIterator<I> data, DbControl dc, Class<I> dataClass, int totalCount)
070        {
071                assert data != null : "data == null";
072                this.data = data;
073                this.dc = dc;
074                this.sc = dc.getSessionControl();
075                this.itemType = Item.fromDataClass(dataClass);
076                this.totalCount = totalCount;
077        }
078
079        /**
080                From the QueryResult interface
081                ---------------------------------
082        */
083        public int getTotalCount()
084        {
085                return totalCount;
086        }
087        // ---------------------------------
088        /**
089                From the ResultIterator interface
090                ---------------------------------
091        */
092        public void close()
093        {
094                data.close();
095        }
096        public boolean isClosed()
097        {
098                return data.isClosed();
099        }
100        // ---------------------------------
101        /*
102                From the Iterator interface
103                ---------------------------
104        */
105        public boolean hasNext()
106        {
107                return data.hasNext();
108        }
109        public I next()
110        {
111                sc.updateLastAccess();
112                I dataObject = data.next();
113                if (dc != null)
114                {
115                        HibernateUtil.evictData(dc.getHibernateSession(), dataObject);
116                }
117                return dataObject;
118        }
119        /**
120                Not supported.
121                @throws UnsupportedOperationException Always
122        */
123        public void remove()
124        {
125                throw new UnsupportedOperationException();
126        }
127        // ---------------------------------
128        
129        /**
130                Get the type of items contained in this list.
131                @return An {@link Item} object or null if not known
132        */
133        public Item getItemType()
134        {
135                return itemType;
136        }
137
138}