Skip to content

Commit

Permalink
GEOS-5779 Implementation of SortBy in App-Schema
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsCharlier committed Apr 23, 2013
1 parent aee0785 commit bd684ec
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 19 deletions.
Expand Up @@ -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;
}

Expand Down Expand Up @@ -495,6 +514,40 @@ private List<PropertyName> getSurrogatePropertyNames(List<PropertyName> requeste
}
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,
Expand Down
Expand Up @@ -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);

}

Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -75,6 +78,8 @@ public Skip(List<Object> idValues) {
public List<Skip> skipped = new ArrayList<Skip>();

public Query baseTableQuery;

public FeatureTypeMapping mapping;
}

/**
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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);
}
Expand Down
Expand Up @@ -16,6 +16,7 @@
*/
package org.geotools.data.joining;

import java.util.ArrayList;
import java.util.List;

import org.geotools.data.Query;
Expand All @@ -38,6 +39,7 @@ public static class QueryJoin {
protected Expression foreignKeyName;
protected Expression joiningKeyName;
protected SortBy[] sortBy;
protected List<String> ids = new ArrayList<String>();

public String getJoiningTypeName() {
return joiningTypeName;
Expand Down Expand Up @@ -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;
Expand Down
Expand Up @@ -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());

}
Expand Down Expand Up @@ -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() ) );
}
Expand Down

0 comments on commit bd684ec

Please sign in to comment.