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 Resolution save() throws JSONException {
    // Only remove details which are editable and re-added layer if not empty,
    // retain other details possibly used in other parts than this page
    // See JSP for which keys are edited
    applicationLayer
        .getDetails()
        .keySet()
        .removeAll(
            Arrays.asList(
                "titleAlias",
                "legendImageUrl",
                "transparency",
                "influenceradius",
                "summary.title",
                "summary.image",
                "summary.description",
                "summary.link",
                "editfunction.title",
                "style",
                "summary.noHtmlEncode",
                "summary.nl2br"));
    for (Map.Entry<String, String> e : details.entrySet()) {
      if (e.getValue() != null) { // Don't insert null value ClobElement
        applicationLayer.getDetails().put(e.getKey(), new ClobElement(e.getValue()));
      }
    }

    applicationLayer.getReaders().clear();
    for (String groupName : groupsRead) {
      applicationLayer.getReaders().add(groupName);
    }

    applicationLayer.getWriters().clear();
    for (String groupName : groupsWrite) {
      applicationLayer.getWriters().add(groupName);
    }

    if (applicationLayer.getAttributes() != null && applicationLayer.getAttributes().size() > 0) {
      List<ConfiguredAttribute> appAttributes = applicationLayer.getAttributes();
      int i = 0;

      for (Iterator it = appAttributes.iterator(); it.hasNext(); ) {
        ConfiguredAttribute appAttribute = (ConfiguredAttribute) it.next();
        // save visible
        if (selectedAttributes.contains(appAttribute.getFullName())) {
          appAttribute.setVisible(true);
        } else {
          appAttribute.setVisible(false);
        }

        // save editable
        if (attributesJSON.length() > i) {
          JSONObject attribute = attributesJSON.getJSONObject(i);

          if (attribute.has("editable")) {
            appAttribute.setEditable(new Boolean(attribute.get("editable").toString()));
          }
          if (attribute.has("editalias")) {
            appAttribute.setEditAlias(attribute.get("editalias").toString());
          }
          if (attribute.has("editvalues")) {
            appAttribute.setEditValues(attribute.get("editvalues").toString());
          }
          if (attribute.has("editHeight")) {
            appAttribute.setEditHeight(attribute.get("editHeight").toString());
          }

          // save selectable
          if (attribute.has("selectable")) {
            appAttribute.setSelectable(new Boolean(attribute.get("selectable").toString()));
          }
          if (attribute.has("filterable")) {
            appAttribute.setFilterable(new Boolean(attribute.get("filterable").toString()));
          }

          if (attribute.has("defaultValue")) {
            appAttribute.setDefaultValue(attribute.get("defaultValue").toString());
          }
        }
        i++;
      }
    }

    Stripersist.getEntityManager().persist(applicationLayer);
    application.authorizationsModified();

    displayName = applicationLayer.getDisplayName();

    Stripersist.getEntityManager().getTransaction().commit();

    getContext().getMessages().add(new SimpleMessage("De kaartlaag is opgeslagen"));
    return edit();
  }