View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors.
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   * 
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.graph.query.process;
25  
26  import java.util.ArrayList;
27  import java.util.Collections;
28  import java.util.List;
29  import org.modeshape.graph.Location;
30  import org.modeshape.graph.query.QueryContext;
31  import org.modeshape.graph.query.QueryResults.Columns;
32  import org.modeshape.graph.query.model.Column;
33  import org.modeshape.graph.query.model.Constraint;
34  import org.modeshape.graph.query.model.Limit;
35  import org.modeshape.graph.query.model.SelectorName;
36  import org.modeshape.graph.query.plan.PlanNode;
37  import org.modeshape.graph.query.plan.PlanNode.Property;
38  import org.modeshape.graph.query.plan.PlanNode.Type;
39  import org.modeshape.graph.query.validate.Schemata;
40  
41  /**
42   * A reusable base class for {@link ProcessingComponent} implementations that does everything except obtain the correct
43   * {@link Location} objects for the query results.
44   */
45  public abstract class AbstractAccessComponent extends ProcessingComponent {
46  
47      protected final PlanNode accessNode;
48      protected final SelectorName sourceName;
49      protected final List<Column> projectedColumns;
50      protected final List<Constraint> andedConstraints;
51      protected final Limit limit;
52  
53      protected AbstractAccessComponent( QueryContext context,
54                                         Columns columns,
55                                         PlanNode accessNode ) {
56          super(context, columns);
57          this.accessNode = accessNode;
58  
59          // Find the table name; should be
60          PlanNode source = accessNode.findAtOrBelow(Type.SOURCE);
61          if (source != null) {
62              this.sourceName = source.getProperty(Property.SOURCE_NAME, SelectorName.class);
63              // if (!AllNodes.ALL_NODES_NAME.equals(this.sourceName)) {
64              // throw new IllegalArgumentException();
65              // }
66          } else {
67              throw new IllegalArgumentException();
68          }
69  
70          // Find the project ...
71          PlanNode project = accessNode.findAtOrBelow(Type.PROJECT);
72          if (project != null) {
73              List<Column> projectedColumns = project.getPropertyAsList(Property.PROJECT_COLUMNS, Column.class);
74              if (projectedColumns != null) {
75                  this.projectedColumns = projectedColumns;
76              } else {
77                  // Get the columns from the source columns ...
78                  List<Schemata.Column> schemataColumns = source.getPropertyAsList(Property.SOURCE_COLUMNS, Schemata.Column.class);
79                  this.projectedColumns = new ArrayList<Column>(schemataColumns.size());
80                  for (Schemata.Column schemataColumn : schemataColumns) {
81                      String columnName = schemataColumn.getName();
82                      // PropertyType type = schemataColumn.getPropertyType();
83                      String propertyName = columnName;
84                      Column column = new Column(sourceName, propertyName, columnName);
85                      this.projectedColumns.add(column);
86                  }
87              }
88          } else {
89              throw new IllegalArgumentException();
90          }
91  
92          // Add the criteria ...
93          List<Constraint> andedConstraints = null;
94          for (PlanNode select : accessNode.findAllAtOrBelow(Type.SELECT)) {
95              Constraint selectConstraint = select.getProperty(Property.SELECT_CRITERIA, Constraint.class);
96              if (andedConstraints == null) andedConstraints = new ArrayList<Constraint>();
97              andedConstraints.add(selectConstraint);
98          }
99          this.andedConstraints = andedConstraints != null ? andedConstraints : Collections.<Constraint>emptyList();
100 
101         // Find the limit ...
102         Limit limit = Limit.NONE;
103         PlanNode limitNode = accessNode.findAtOrBelow(Type.LIMIT);
104         if (limitNode != null) {
105             Integer count = limitNode.getProperty(Property.LIMIT_COUNT, Integer.class);
106             if (count != null) limit = limit.withRowLimit(count.intValue());
107             Integer offset = limitNode.getProperty(Property.LIMIT_OFFSET, Integer.class);
108             if (offset != null) limit = limit.withOffset(offset.intValue());
109         }
110         this.limit = limit;
111     }
112 
113 }