public static Filter reformatFilter(Filter filter, SimpleFeatureType ft) throws Exception { FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(); if (Filter.INCLUDE.equals(filter) || Filter.EXCLUDE.equals(filter)) { return filter; } for (FeatureTypeRelation rel : ft.getRelations()) { if (FeatureTypeRelation.JOIN.equals(rel.getType())) { filter = reformatFilter(filter, rel.getForeignFeatureType()); filter = (Filter) filter.accept(new ValidFilterExtractor(rel), filter); } } filter = (Filter) filter.accept(new SimplifyingFilterVisitor(), null); return filter; }
/** Populate the json object with related featues */ private JSONObject populateWithRelatedFeatures( JSONObject j, SimpleFeature feature, SimpleFeatureType ft, ApplicationLayer al, int index) throws Exception { if (ft.hasRelations()) { JSONArray related_featuretypes = new JSONArray(); for (FeatureTypeRelation rel : ft.getRelations()) { boolean isJoin = rel.getType().equals(FeatureTypeRelation.JOIN); if (isJoin) { FeatureSource foreignFs = rel.getForeignFeatureType().openGeoToolsFeatureSource(TIMEOUT); FeatureIterator<SimpleFeature> foreignIt = null; try { Query foreignQ = new Query(foreignFs.getName().toString()); // create filter Filter filter = createFilter(feature, rel); if (filter == null) { continue; } // if join only get 1 feature foreignQ.setMaxFeatures(1); foreignQ.setFilter(filter); // set propertynames List<String> propertyNames; if (al != null) { propertyNames = setPropertyNames(al, foreignQ, rel.getForeignFeatureType(), edit); } else { propertyNames = new ArrayList<String>(); for (AttributeDescriptor ad : rel.getForeignFeatureType().getAttributes()) { propertyNames.add(ad.getName()); } } if (propertyNames.isEmpty()) { // if there are no properties to retrieve just get out continue; } // get aliases Map<String, String> attributeAliases = new HashMap<String, String>(); if (!edit) { for (AttributeDescriptor ad : rel.getForeignFeatureType().getAttributes()) { if (ad.getAlias() != null) { attributeAliases.put(ad.getName(), ad.getAlias()); } } } // Get Feature and populate JSON object with the values. foreignIt = foreignFs.getFeatures(foreignQ).features(); while (foreignIt.hasNext()) { SimpleFeature foreignFeature = foreignIt.next(); // join it in the same json j = toJSONFeature( j, foreignFeature, rel.getForeignFeatureType(), al, propertyNames, attributeAliases, index); } } finally { if (foreignIt != null) { foreignIt.close(); } foreignFs.getDataStore().dispose(); } } else { Filter filter = createFilter(feature, rel); if (filter == null) { continue; } JSONObject related_ft = new JSONObject(); related_ft.put("filter", CQL.toCQL(filter)); related_ft.put("id", rel.getForeignFeatureType().getId()); related_ft.put("foreignFeatureTypeName", rel.getForeignFeatureType().getTypeName()); related_featuretypes.put(related_ft); } } if (related_featuretypes.length() > 0) { j.put("related_featuretypes", related_featuretypes); } } return j; }