View Javadoc

1   /*
2    * File:    AbstractQuery.java
3    * Created: 30.05.2007 10:43:19
4    *
5    * Copyright 2007 Michal Burda.
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20   */
21  
22  package net.sf.webmancer.db;
23  
24  import java.util.HashMap;
25  import java.util.LinkedList;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Vector;
29  
30  import net.sf.webmancer.util.ContractChecker;
31  import net.sf.webmancer.util.immutable.ImmutableList;
32  import net.sf.webmancer.util.immutable.ImmutableMap;
33  
34  /**
35   * @author Michal Burda
36   *
37   */
38  public class Query implements IQuery {
39      /**
40       * 
41       */
42      private Map<String, IAttribute> attributes;
43      
44      /**
45       * 
46       */
47      private List<IRestrictor> restrictors;
48  
49      /**
50       * 
51       */
52      private List<ICondition> conditions;
53  
54      /**
55       * 
56       */
57      private long limit;
58  
59      /**
60       * 
61       */
62      private long offset;
63  
64      /**
65       * 
66       */
67      private IAttribute orderingAttribute;
68  
69      /**
70       * 
71       */
72      private boolean orderingIsAscending;
73  
74      /**
75       * 
76       */
77      private IDatabaseOperationPerformer performer;
78  
79      /**
80       * Constructs the AbstractQuery.
81       *
82       */
83      public Query(IDatabaseOperationPerformer performer) {
84          super();
85          ContractChecker.mustNotBeNull(performer, "performer");
86          this.performer = performer;
87          this.attributes = new HashMap<String, IAttribute>();
88          this.restrictors = new Vector<IRestrictor>();
89          this.conditions = new LinkedList<ICondition>();
90          this.limit = 0;
91          this.orderingAttribute = null;
92          this.orderingIsAscending = true;
93      }
94  
95      /**
96       * @see net.sf.webmancer.db.IQuery#addAttribute(net.sf.webmancer.db.IAttribute)
97       */
98      public void addAttribute(IAttribute attribute) {
99          ContractChecker.mustNotBeNull(attribute, "attribute");
100         this.attributes.put(attribute.getId(), attribute);
101     }
102 
103     /**
104      * @see net.sf.webmancer.db.IQuery#queryCount()
105      */
106     public long queryCount() {
107         return performer.performQueryCount(this.getConditions());
108     }
109 
110     /**
111      * @see net.sf.webmancer.db.IQuery#queryResult()
112      */
113     public IData queryResult() {
114         return performer.performQueryResult(this.getAttributes(), this.getConditions(), this.getLimit(), this.getOffset(), this.getOrderingAttribute(), this.isOrderingAscending());
115     }
116 
117     /**
118      * @see net.sf.webmancer.db.IQuery#delete()
119      */
120     public long delete() {
121         return performer.performDelete(this.getConditions());
122     }
123 
124     /**
125      * @see net.sf.webmancer.db.IQuery#update(java.util.Map)
126      */
127     public long update(Map<IAttribute, ICell> row) {
128         return performer.performUpdate(row, this.getConditions());
129     }
130 
131     /**
132      * @see net.sf.webmancer.db.IQuery#insert(java.util.Map)
133      */
134     public void insert(Map<IAttribute, ICell> row) {
135         performer.performInsert(row);
136     }
137 
138     public void insert(IData data) {
139         performer.performInsert(data);
140     }
141 
142     /**
143      * @see net.sf.webmancer.db.IRestrictable#registerRestrictor(net.sf.webmancer.db.IRestrictor)
144      */
145     public void registerRestrictor(IRestrictor restrictor) {
146         ContractChecker.mustNotBeNull(restrictor, "restrictor");
147         this.restrictors.add(restrictor);
148     }
149 
150     /**
151      * @see net.sf.webmancer.db.IRestrictable#restrictByCondition(net.sf.webmancer.db.ICondition)
152      */
153     public void restrictByCondition(ICondition condition) {
154         ContractChecker.mustNotBeNull(condition, "condition");
155         this.conditions.add(condition);
156     }
157 
158     /**
159      * @see net.sf.webmancer.db.IRestrictable#restrictLimit(long)
160      */
161     public void restrictLimit(long limit) {
162         ContractChecker.mustNotBeNegative(limit, "limit");
163         this.limit = limit;
164     }
165 
166     /**
167      * @see net.sf.webmancer.db.IRestrictable#restrictOffset(long)
168      */
169     public void restrictOffset(long offset) {
170         ContractChecker.mustNotBeNegative(offset, "offset");
171         this.offset = offset;
172     }
173 
174     /**
175      * @see net.sf.webmancer.db.IRestrictable#restrictOrdering(net.sf.webmancer.db.IAttribute, boolean)
176      */
177     public void restrictOrdering(IAttribute attribute, boolean ascending) {
178         ContractChecker.mustNotBeNull(attribute, "attribute");
179         this.orderingAttribute = attribute;
180         this.orderingIsAscending = ascending;
181     }
182 
183     /**
184      * @param type
185      */
186     protected void callRestrictors(RestrictionType type) {
187         for (IRestrictor restrictor : this.restrictors) {
188             restrictor.restrict(this, type);
189         }
190     }
191 
192     /**
193      * Returns the attributes.
194      *
195      * @return the attributes
196      */
197     protected Map<String, IAttribute> getAttributes() {
198         return new ImmutableMap<String, IAttribute>(this.attributes);
199     }
200 
201     /**
202      * Sets the attributes.
203      *
204      * @param attributes the attributes to set
205      */
206     public void setAttributes(Map<String, IAttribute> attributes) {
207         ContractChecker.mustNotBeNull(attributes, "attributes");
208         this.attributes = attributes;
209     }
210 
211     /**
212      * Returns the conditions.
213      *
214      * @return the conditions
215      */
216     protected List<ICondition> getConditions() {
217         return new ImmutableList<ICondition>(this.conditions);
218     }
219 
220     /**
221      * Sets the conditions.
222      *
223      * @param conditions the conditions to set
224      */
225     public void setConditions(List<ICondition> conditions) {
226         this.conditions = conditions;
227     }
228 
229     /**
230      * Returns the limit.
231      *
232      * @return the limit
233      */
234     protected long getLimit() {
235         return this.limit;
236     }
237 
238     /**
239      * Sets the limit.
240      *
241      * @param limit the limit to set
242      */
243     public void setLimit(long limit) {
244         ContractChecker.mustNotBeNegative(limit, "limit");
245         this.limit = limit;
246     }
247 
248     /**
249      * Returns the offset.
250      *
251      * @return the offset
252      */
253     protected long getOffset() {
254         return this.offset;
255     }
256 
257     /**
258      * Sets the offset.
259      *
260      * @param offset the offset to set
261      */
262     public void setOffset(long offset) {
263         ContractChecker.mustNotBeNegative(offset, "offset");
264         this.offset = offset;
265     }
266 
267     /**
268      * Returns the ordering attribute.
269      *
270      * @return the orderingAttribute
271      */
272     protected IAttribute getOrderingAttribute() {
273         return this.orderingAttribute;
274     }
275 
276     /**
277      * Sets the ordering attribute.
278      *
279      * @param attribute the orderingAttribute to set
280      */
281     public void setOrderingAttribute(IAttribute attribute) {
282         ContractChecker.mustNotBeNull(attribute, "attribute");
283         this.orderingAttribute = attribute;
284     }
285 
286     /**
287      * Returns the orderingIsAscending.
288      *
289      * @return the orderingIsAscending
290      */
291     protected boolean isOrderingAscending() {
292         return this.orderingIsAscending;
293     }
294 
295     /**
296      * Sets the orderingIsAscending.
297      *
298      * @param ascending the orderingIsAscending to set
299      */
300     public void setOrderingAscending(boolean ascending) {
301         this.orderingIsAscending = ascending;
302     }
303 
304 }