private List<String> rebuildAttributes(SimpleFeatureType sft) { Layer layer = applicationLayer.getService().getSingleLayer(applicationLayer.getLayerName()); List<String> attributesToRetain = new ArrayList<String>(); for (AttributeDescriptor ad : sft.getAttributes()) { String name = ad.getName(); String fullName = sft.getId() + ":" + name; // if attribute already added return. if (attributesToRetain.contains(fullName)) { return attributesToRetain; } attributesToRetain.add(fullName); // Used for display in JSP if (StringUtils.isNotBlank(ad.getAlias())) { attributeAliases.put(fullName, ad.getAlias()); } if (applicationLayer.getAttribute(sft, name) == null) { ConfiguredAttribute ca = new ConfiguredAttribute(); // default visible if not geometry type // and not a attribute of a related featuretype boolean defaultVisible = true; if (layer.getFeatureType().getId() != sft.getId() || AttributeDescriptor.GEOMETRY_TYPES.contains(ad.getType())) { defaultVisible = false; } ca.setVisible(defaultVisible); ca.setAttributeName(name); ca.setFeatureType(sft); applicationLayer.getAttributes().add(ca); Stripersist.getEntityManager().persist(ca); if (!"save".equals(getContext().getEventName())) { String message = "Nieuw attribuut \"{0}\" gevonden in "; if (layer.getFeatureType().getId() != sft.getId()) { message += "gekoppelde "; } message += "attribuutbron"; if (layer.getFeatureType().getId() == sft.getId()) { message += ": wordt zichtbaar na opslaan"; } getContext().getMessages().add(new SimpleMessage(message, name)); } } } if (sft.getRelations() != null) { for (FeatureTypeRelation rel : sft.getRelations()) { attributesToRetain.addAll(rebuildAttributes(rel.getForeignFeatureType())); } } return attributesToRetain; }
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; }
/** Get the propertynames and add the needed propertynames to the query. */ private List<String> setPropertyNames( ApplicationLayer appLayer, Query q, SimpleFeatureType sft, boolean edit) { List<String> propertyNames = new ArrayList<String>(); boolean haveInvisibleProperties = false; if (propertyNamesQueryCache.containsKey(sft.getId())) { haveInvisibleProperties = haveInvisiblePropertiesCache.get(sft.getId()); if (haveInvisibleProperties) { q.setPropertyNames(propertyNamesQueryCache.get(sft.getId())); } return propertyNamesReturnCache.get(sft.getId()); } else { for (ConfiguredAttribute ca : appLayer.getAttributes(sft)) { if ((!edit && !graph && ca.isVisible()) || (edit && ca.isEditable()) || (graph && attributesToInclude.contains(ca.getId()))) { propertyNames.add(ca.getAttributeName()); } else { haveInvisibleProperties = true; } } haveInvisiblePropertiesCache.put(sft.getId(), haveInvisibleProperties); propertyNamesReturnCache.put(sft.getId(), propertyNames); propertyNamesQueryCache.put(sft.getId(), propertyNames); if (haveInvisibleProperties) { // By default Query retrieves Query.ALL_NAMES // Query.NO_NAMES is an empty String array q.setPropertyNames(propertyNames); // If any related featuretypes are set, add the leftside names in the query // don't add them to propertynames, maybe they are not visible if (sft.getRelations() != null) { List<String> withRelations = new ArrayList<String>(); withRelations.addAll(propertyNames); for (FeatureTypeRelation ftr : sft.getRelations()) { if (ftr.getRelationKeys() != null) { for (FeatureTypeRelationKey key : ftr.getRelationKeys()) { if (!withRelations.contains(key.getLeftSide().getName())) { withRelations.add(key.getLeftSide().getName()); } } } } propertyNamesQueryCache.put(sft.getId(), withRelations); q.setPropertyNames(withRelations); } } propertyNamesReturnCache.put(sft.getId(), propertyNames); return propertyNames; } }
private Filter createFilter(SimpleFeature feature, FeatureTypeRelation rel) { FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2(); List<Filter> filters = new ArrayList<Filter>(); for (FeatureTypeRelationKey key : rel.getRelationKeys()) { AttributeDescriptor rightSide = key.getRightSide(); AttributeDescriptor leftSide = key.getLeftSide(); Object value = feature.getAttribute(leftSide.getName()); if (value == null) { continue; } if (AttributeDescriptor.GEOMETRY_TYPES.contains(rightSide.getType()) && AttributeDescriptor.GEOMETRY_TYPES.contains(leftSide.getType())) { filters.add(ff.not(ff.isNull(ff.property(rightSide.getName())))); filters.add(ff.intersects(ff.property(rightSide.getName()), ff.literal(value))); } else { filters.add(ff.equals(ff.property(rightSide.getName()), ff.literal(value))); } } if (filters.size() > 1) { return ff.and(filters); } else if (filters.size() == 1) { return filters.get(0); } else { return null; } }
/** 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; }