View Javadoc

1   /*
2    * Copyright (c) 2011.  The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.yaoql.impl;
22  
23  import org.apache.expreval.expr.ExpressionTree;
24  import org.apache.expreval.expr.literal.BooleanLiteral;
25  import org.apache.hadoop.hbase.hbql.client.HBqlException;
26  import org.apache.hadoop.hbase.hbql.mapping.MappingContext;
27  import org.apache.hadoop.hbase.hbql.parser.ParserUtil;
28  import org.apache.hadoop.hbase.hbql.util.Lists;
29  import org.apache.yaoql.client.ObjectQuery;
30  import org.apache.yaoql.client.ObjectQueryListener;
31  import org.apache.yaoql.client.ObjectResultSet;
32  
33  import java.util.Collection;
34  import java.util.List;
35  
36  public class ObjectQueryImpl<T> extends ParameterBinding implements ObjectQuery<T> {
37  
38      private final String query;
39      private List<ObjectQueryListener<T>> queryListeners = null;
40  
41      public ObjectQueryImpl(final String query) {
42          this.query = query;
43      }
44  
45      public void addListener(final ObjectQueryListener<T> listener) {
46          if (this.getQueryListeners() == null)
47              this.queryListeners = Lists.newArrayList();
48  
49          this.getQueryListeners().add(listener);
50      }
51  
52      private List<ObjectQueryListener<T>> getQueryListeners() {
53          return this.queryListeners;
54      }
55  
56      public void clearListeners() {
57          if (this.getQueryListeners() != null)
58              this.getQueryListeners().clear();
59      }
60  
61      private void callOnQueryInit() {
62          if (this.getQueryListeners() != null) {
63              for (final ObjectQueryListener<T> listener : this.getQueryListeners())
64                  listener.onQueryStart();
65          }
66      }
67  
68      public T callOnEachObject(T val) {
69          if (this.getQueryListeners() != null) {
70              for (final ObjectQueryListener<T> listener : this.getQueryListeners())
71                  listener.onEachObject(val);
72          }
73          return val;
74      }
75  
76      public void callOnQueryComplete() {
77          if (this.getQueryListeners() != null) {
78              for (final ObjectQueryListener<T> listener : this.getQueryListeners())
79                  listener.onQueryComplete();
80          }
81      }
82  
83      public String getQuery() {
84          return this.query;
85      }
86  
87      public ExpressionTree getExpressionTree(final Collection<T> objects) throws HBqlException {
88  
89          final ExpressionTree expressionTree;
90  
91          if (objects == null || objects.size() == 0) {
92              expressionTree = ExpressionTree.newExpressionTree(null, new BooleanLiteral(true));
93              expressionTree.setMappingContext(null);
94              expressionTree.setAllowColumns(false);
95          }
96          else {
97              // Grab the first object to derive the mapping
98              final Object obj = objects.iterator().next();
99              final ReflectionMapping mapping = ReflectionMapping.getReflectionMapping(obj);
100             final MappingContext mappingContext = new MappingContext(mapping);
101             expressionTree = ParserUtil.parseWhereExpression(this.getQuery(), mappingContext);
102             this.applyParameters(expressionTree);
103         }
104 
105         return expressionTree;
106     }
107 
108     public ObjectResultSet<T> getResults(final Collection<T> objs) throws HBqlException {
109         this.callOnQueryInit();
110         return new ObjectResultSet<T>(this, objs);
111     }
112 
113     public List<T> getResultList(final Collection<T> objs) throws HBqlException {
114 
115         final List<T> retval = Lists.newArrayList();
116         final ObjectResultSet<T> results = this.getResults(objs);
117 
118         for (final T val : results)
119             retval.add(val);
120 
121         return retval;
122     }
123 }