001/*
002 $Id: QueryFactory.java 3617 2010-03-24 09:41:06Z gregory $
003
004 Copyright (C) 2007 Gregory Vincic
005
006 Files are copyright by their respective authors. The contributions to
007 files where copyright is not explicitly stated can be traced with the
008 source code revision system.
009
010 This file is part of Proteios.
011 Available at http://www.proteios.org/
012
013 Proteios-2.x is free software; you can redistribute it and/or
014 modify it under the terms of the GNU General Public License
015 as published by the Free Software Foundation; either version 2
016 of the License, or (at your option) any later version.
017
018 Proteios is distributed in the hope that it will be useful,
019 but WITHOUT ANY WARRANTY; without even the implied warranty of
020 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
021 GNU General Public License for more details.
022
023 You should have received a copy of the GNU General Public License
024 along with this program; if not, write to the Free Software
025 Foundation, Inc., 59 Temple Place - Suite 330,
026 Boston, MA  02111-1307, USA.
027 */
028package org.proteios.core;
029
030import org.proteios.core.query.Expressions;
031import org.proteios.core.query.Hql;
032import org.proteios.core.News;
033import org.proteios.core.query.Restrictions;
034import java.util.HashMap;
035
036/**
037 * Used to query for items.
038 * 
039 * @author gregory
040 */
041public class QueryFactory
042{
043        private HashMap<Class<?>, QueryState> states;
044
045
046        public QueryFactory()
047        {
048                super();
049                states = new HashMap<Class<?>, QueryState>();
050        }
051
052
053        public <D extends BasicItem> ItemQuery<D> select(Class<D> itemClass)
054        {
055  ItemQuery<D> query;  
056  if(itemClass.equals(News.class))
057  {
058    QueryRuntimeFilter filter = QueryRuntimeFilterFactory.getRequiredFilter(Item.NEWS);
059    query = new ItemQuery<D>(itemClass,filter);
060  }
061  else
062  {
063    query = new ItemQuery<D>(itemClass);
064  }
065
066                QueryState state = states.get(itemClass);
067                if (state == null)
068                {
069                        state = new QueryState();
070                        save(itemClass, state);
071                }
072                setLimits(query, state);
073                return query;
074        }
075
076
077        /**
078         * @param dc DbControl used for this query
079         * @return query with disabled filters or null if dc is null
080         */
081        public ItemQuery<File> selectFilesInProject(DbControl dc)
082        {
083                if (dc != null)
084                {
085                        ItemQuery<File> query = select(File.class);
086                        query.disableFilters(dc);
087                        query.restrict(Restrictions.in(Hql.property("projectKey.id"),
088                                Expressions.parameter("name")));
089                        query.setParameter("name", dc.getSessionControl().getProjectKeys(),
090                                Type.INT);
091                        return query;
092                }
093                return null;
094        }
095
096
097        public <D extends BasicItem> void save(Class<D> itemClass, QueryState state)
098        {
099                states.put(itemClass, state);
100        }
101
102
103        public <D extends BasicItem> QueryState getState(Class<D> itemClass)
104        {
105                return states.get(itemClass);
106        }
107
108
109        public void setLimits(ItemQuery<?> query, QueryState state)
110        {
111                if (query != null)
112                {
113                        query.setFirstResult(state.getFrom());
114                        query.setMaxResults(state.getLimit());
115                }
116        }
117
118        public class QueryState
119        {
120                private int from = 0;
121                private int limit = 0;
122
123
124                public int getFrom()
125                {
126                        return from;
127                }
128
129
130                public void setFrom(int from)
131                {
132                        this.from = from;
133                }
134
135
136                public int getLimit()
137                {
138                        return limit;
139                }
140
141
142                public void setLimit(int limit)
143                {
144                        this.limit = limit;
145                }
146        }
147}