Permalink
Browse files

GEOS-5779 Implementation of SortBy in App-Schema

  • Loading branch information...
1 parent aee0785 commit bd684ec5e0fdb1d7b41c10d8e4cd10e1cf9fd58d @NielsCharlier NielsCharlier committed Apr 23, 2013
@@ -326,23 +326,42 @@ public Query unrollQuery(Query query, FeatureTypeMapping mapping) {
newQuery.setHandle(query.getHandle());
newQuery.setMaxFeatures(query.getMaxFeatures());
+ List<SortBy> sort = new ArrayList<SortBy>();
+ if (query.getSortBy() != null) {
+ for (SortBy sortBy : query.getSortBy()) {
+ for (Expression expr : unrollProperty(sortBy.getPropertyName(), mapping)) {
+ if (expr != null) {
+ FilterAttributeExtractor extractor = new FilterAttributeExtractor();
+ expr.accept(extractor, null);
+
+ for (String att : extractor.getAttributeNameSet()) {
+ sort.add(new SortByImpl(filterFac.property(att), sortBy.getSortOrder()));
+ }
+ }
+ }
+ }
+ }
+
if (query instanceof JoiningQuery) {
FilterAttributeExtractor extractor = new FilterAttributeExtractor();
mapping.getFeatureIdExpression().accept(extractor, null);
- List<SortBy> sort = new ArrayList<SortBy>();
+
for (String att : extractor.getAttributeNameSet()) {
- sort.add(new SortByImpl(filterFac.property(att), SortOrder.ASCENDING ));
+ sort.add(new SortByImpl(filterFac.property(att), SortOrder.ASCENDING));
}
- newQuery.setSortBy( sort.toArray(new SortBy[sort.size()]) );
-
- JoiningQuery jQuery = new JoiningQuery(newQuery);
- jQuery.setQueryJoins(((JoiningQuery)query).getQueryJoins());
- jQuery.setSubset(((JoiningQuery) query).isSubset());
- unrolledQuery = jQuery;
- } else {
+
+ JoiningQuery jQuery = new JoiningQuery(newQuery);
+ jQuery.setQueryJoins(((JoiningQuery) query).getQueryJoins());
+ jQuery.setSubset(((JoiningQuery) query).isSubset());
+ unrolledQuery = jQuery;
+ } else {
unrolledQuery = newQuery;
}
+
+ unrolledQuery.setSortBy(sort.toArray(new SortBy[sort.size()]));
+
}
+
return unrolledQuery;
}
@@ -495,6 +514,40 @@ protected static boolean matchProperty(String requestedProperty, StepList target
}
return propNames;
}
+
+ private List<Expression> unrollProperty (PropertyName property, final FeatureTypeMapping mapping) {
+
+ final AttributeDescriptor targetDescriptor = mapping.getTargetFeature();
+ StepList propertySteps = XPath.steps(targetDescriptor, property.getPropertyName(), mapping.getNamespaces());
+
+ // add all surrogate attributes involved in mapping of the requested
+ // target schema attributes
+ //List<AttributeMapping> attMappings = mapping.getAttributeMappings();
+
+ return mapping.findMappingsFor(propertySteps);
+
+ /*for (final AttributeMapping entry : attMappings) {
+ final StepList targetSteps = entry.getTargetXPath();
+
+ if (propertySteps.size() >= targetSteps.size()) {
+ int i = 0;
+ boolean match = true;
+ for (; i < targetSteps.size(); i++) {
+ if (!propertySteps.get(i).equals(targetSteps.get(i))) {
+ match = false;
+ break;
+ }
+ }
+
+ if (match && i == propertySteps.size()) {
+ return entry.getSourceExpression();
+ }
+
+ }
+ }
+
+ return null;*/
+ }
/**
* Takes a filter that operates against a {@linkplain FeatureTypeMapping}'s target FeatureType,
@@ -384,7 +384,7 @@ protected void initialiseSourceFeatures(FeatureTypeMapping mapping, Query query,
for (AttributeMapping attMapping : selectedMapping) {
if (attMapping instanceof JoiningNestedAttributeMapping) {
- ((JoiningNestedAttributeMapping) attMapping).open(this, query);
+ ((JoiningNestedAttributeMapping) attMapping).open(this, query, mapping);
}
@@ -35,11 +35,14 @@
import org.geotools.factory.Hints;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
+import org.geotools.filter.FilterAttributeExtractor;
+import org.geotools.filter.SortByImpl;
import org.geotools.jdbc.JoiningJDBCFeatureSource;
import org.opengis.feature.Feature;
import org.opengis.feature.type.Name;
import org.opengis.filter.expression.Expression;
import org.opengis.filter.expression.PropertyName;
+import org.opengis.filter.sort.SortOrder;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.xml.sax.helpers.NamespaceSupport;
@@ -75,6 +78,8 @@ public Skip(List<Object> idValues) {
public List<Skip> skipped = new ArrayList<Skip>();
public Query baseTableQuery;
+
+ public FeatureTypeMapping mapping;
}
/**
@@ -147,10 +152,16 @@ public DataAccessMappingFeatureIterator initSourceFeatures(Instance instance,
join.setForeignKeyName(sourceExpression);
join.setJoiningKeyName(nestedSourceExpression);
join.setJoiningTypeName(instance.baseTableQuery.getTypeName());
- join.setSortBy(instance.baseTableQuery.getSortBy()); // incorporate order
+ join.setSortBy(instance.baseTableQuery.getSortBy()); // incorporate order
+ FilterAttributeExtractor extractor = new FilterAttributeExtractor();
+ instance.mapping.getFeatureIdExpression().accept(extractor, null);
+ for (String pn : extractor.getAttributeNameSet()) {
+ join.addId(pn);
+ }
+
joins.add(0, join);
query.setQueryJoins(joins);
-
+
if (selectedProperties != null) {
selectedProperties = new ArrayList<PropertyName>(selectedProperties);
selectedProperties.add(filterFac.property(this.nestedTargetXPath.toString()));
@@ -191,7 +202,7 @@ public DataAccessMappingFeatureIterator initSourceFeatures(Instance instance,
List<Expression> foreignIds = new ArrayList<Expression>();
for (int i = 0; i < query.getQueryJoins().size(); i++) {
- for (int j = 0; j < query.getQueryJoins().get(i).getSortBy().length; j++) {
+ for (int j = 0; j < join.getIds().size(); j++) {
foreignIds.add(filterFac.property(JoiningJDBCFeatureSource.FOREIGN_ID + "_" + i
+ "_" + j));
}
@@ -221,13 +232,14 @@ public DataAccessMappingFeatureIterator initSourceFeatures(Instance instance,
* @param baseTableQuery
* @throws IOException
*/
- public void open(Object caller, Query baseTableQuery) throws IOException {
+ public void open(Object caller, Query baseTableQuery, FeatureTypeMapping mapping) throws IOException {
if (instances.get(caller) != null) {
throw new IllegalArgumentException(
"Trying to open Joining Nested Attribute Mapping that is already open!");
} else {
Instance instance = new Instance();
instance.baseTableQuery = baseTableQuery;
+ instance.mapping = mapping;
instances.put(caller, instance);
}
@@ -16,6 +16,7 @@
*/
package org.geotools.data.joining;
+import java.util.ArrayList;
import java.util.List;
import org.geotools.data.Query;
@@ -38,6 +39,7 @@
protected Expression foreignKeyName;
protected Expression joiningKeyName;
protected SortBy[] sortBy;
+ protected List<String> ids = new ArrayList<String>();
public String getJoiningTypeName() {
return joiningTypeName;
@@ -70,6 +72,14 @@ public void setJoiningKeyName(Expression joiningKeyName) {
public void setSortBy(SortBy[] sortBy){
this.sortBy = sortBy;
}
+
+ public void addId(String pn) {
+ this.ids.add(pn);
+ }
+
+ public List<String> getIds() {
+ return ids;
+ }
}
protected List<QueryJoin> queryJoins;
@@ -444,13 +444,12 @@ protected String selectSQL(SimpleFeatureType featureType, JoiningQuery query, At
if (query.getQueryJoins() != null && query.getQueryJoins().size() > 0) {
for (int i = 0; i < query.getQueryJoins().size(); i++) {
- for (int j = 0; j < query.getQueryJoins().get(i).getSortBy().length; j++) {
+ for (int j = 0; j < query.getQueryJoins().get(i).getIds().size(); j++) {
if (aliases[i] != null) {
getDataStore().dialect.encodeColumnName(aliases[i], query.getQueryJoins()
- .get(i).getSortBy()[j].getPropertyName().getPropertyName(), sql);
+ .get(i).getIds().get(j), sql);
} else {
- encodeColumnName(query.getQueryJoins().get(i).getSortBy()[j]
- .getPropertyName().getPropertyName(), query.getQueryJoins().get(i)
+ encodeColumnName(query.getQueryJoins().get(i).getIds().get(j), query.getQueryJoins().get(i)
.getJoiningTypeName(), sql, query.getHints());
}
@@ -597,7 +596,7 @@ protected SimpleFeatureType getFeatureType(SimpleFeatureType origType, JoiningQu
AttributeTypeBuilder ab = new AttributeTypeBuilder();
for (int i=0; i<query.getQueryJoins().size(); i++) {
- for (int j=0; j<query.getQueryJoins().get(i).getSortBy().length; j++) {
+ for (int j=0; j<query.getQueryJoins().get(i).getIds().size(); j++) {
ab.setBinding(String.class);
builder.add(ab.buildDescriptor(new NameImpl(FOREIGN_ID) + "_" + i + "_" + j, ab.buildType() ) );
}

0 comments on commit bd684ec

Please sign in to comment.