コード例 #1
0
  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;
  }
コード例 #2
0
 /**
  * Get the features as JSONArray with the given params
  *
  * @param al The application layer(if there is a application layer)
  * @param ft The featuretype that must be used to get the features
  * @param fs The featureSource
  * @param q The query
  * @param sort The attribute name that is used to sort
  * @param dir Sort direction (DESC or ASC)
  * @return JSONArray with features.
  * @throws IOException if any
  * @throws JSONException if transforming to json fails
  * @throws Exception if any
  */
 public JSONArray getJSONFeatures(
     ApplicationLayer al, SimpleFeatureType ft, FeatureSource fs, Query q, String sort, String dir)
     throws IOException, JSONException, Exception {
   Map<String, String> attributeAliases = new HashMap<String, String>();
   if (!edit) {
     for (AttributeDescriptor ad : ft.getAttributes()) {
       if (ad.getAlias() != null) {
         attributeAliases.put(ad.getName(), ad.getAlias());
       }
     }
   }
   List<String> propertyNames;
   if (al != null) {
     propertyNames = this.setPropertyNames(al, q, ft, edit);
   } else {
     propertyNames = new ArrayList<String>();
     for (AttributeDescriptor ad : ft.getAttributes()) {
       propertyNames.add(ad.getName());
     }
   }
   if (sort != null) {
     setSortBy(q, propertyNames, sort, dir);
   }
   /* Use the first property as sort field, otherwise geotools while give a error when quering
    * a JDBC featureType without a primary key.
    */
   else if (fs instanceof org.geotools.jdbc.JDBCFeatureSource && !propertyNames.isEmpty()) {
     setSortBy(q, propertyNames.get(0), dir);
   }
   Integer start = q.getStartIndex();
   if (start == null) {
     start = 0;
   }
   boolean offsetSupported = fs.getQueryCapabilities().isOffsetSupported();
   // if offSet is not supported, get more features (start + the wanted features)
   if (!offsetSupported && q.getMaxFeatures() < MAX_FEATURES) {
     q.setMaxFeatures(q.getMaxFeatures() + start);
   }
   FeatureIterator<SimpleFeature> it = null;
   JSONArray features = new JSONArray();
   try {
     it = fs.getFeatures(q).features();
     int featureIndex = 0;
     while (it.hasNext()) {
       SimpleFeature feature = it.next();
       /* if offset not supported and there are more features returned then
        * only get the features after index >= start*/
       if (offsetSupported || featureIndex >= start) {
         JSONObject j =
             this.toJSONFeature(
                 new JSONObject(), feature, ft, al, propertyNames, attributeAliases, 0);
         features.put(j);
       }
       featureIndex++;
     }
   } finally {
     if (it != null) {
       it.close();
     }
     fs.getDataStore().dispose();
   }
   return features;
 }