private JSONObject toJSONFeature( JSONObject j, SimpleFeature f, SimpleFeatureType ft, ApplicationLayer al, List<String> propertyNames, Map<String, String> attributeAliases, int index) throws JSONException, Exception { if (arrays) { for (String name : propertyNames) { Object value = f.getAttribute(name); j.put("c" + index++, formatValue(value)); } } else { for (String name : propertyNames) { String alias = null; if (attributeAliases != null) { alias = attributeAliases.get(name); } j.put(alias != null ? alias : name, formatValue(f.getAttribute(name))); } } // if edit and not yet set // removed check for edit variable here because we need to compare features in edit component // and feature info attributes // was if(edit && j.optString(FID,null)==null) { if (j.optString(FID, null) == null) { String id = f.getID(); j.put(FID, id); } if (ft.hasRelations()) { j = populateWithRelatedFeatures(j, f, ft, al, index); } return j; }
/** 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; }