public void handleAction(ActionParameters params) throws ActionException {

    final JSONObject root = new JSONObject();

    // Because of analysis layers
    String sid = params.getHttpParam(ID, "n/a");
    final int id = ConversionHelper.getInt(getBaseLayerId(sid), 0);

    final HttpServletResponse response = params.getResponse();
    response.setContentType("application/json");

    if (id == 0) {
      JSONHelper.putValue(root, ERROR, ERROR_NO_ID);
      ResponseHelper.writeResponse(params, root);
      // FIXME: throw ActionParamsException instead and modify client
      // response parsing
      return;
    }

    String json = WFSLayerConfiguration.getCache(id + "");
    if (json == null) {
      WFSLayerConfiguration lc = layerConfigurationService.findConfiguration(id);

      log.warn("id", id);
      log.warn(lc);

      // Extra manage for analysis
      if (sid.indexOf(ANALYSIS_PREFIX) > -1) {
        log.warn("sid", sid);
        // set id to original analysis layer id
        lc.setLayerId(sid);
        // Set analysis layer fields as id based
        lc.setSelectedFeatureParams(getAnalysisFeatureProperties(sid));
      }
      // Extra manage for analysis
      else if (sid.indexOf(MYPLACES_PREFIX) > -1) {
        log.warn("sid", sid);
        // set id to original my places layer id
        lc.setLayerId(sid);
      } else if (sid.indexOf(USERLAYER_PREFIX) > -1) {
        log.warn("sid", sid);
        // set id to original user layer id
        lc.setLayerId(sid);
      }
      if (lc == null) {
        JSONHelper.putValue(root, ERROR, ERROR_NOT_FOUND);
        ResponseHelper.writeResponse(params, root);
        // FIXME: throw ActionParamsException instead and modify client
        // response parsing
        return;
      }
      lc.save();
    }
    JSONHelper.putValue(root, RESULT, RESULT_SUCCESS);
    ResponseHelper.writeResponse(params, root);
  }
Пример #2
0
  /**
   * Parses WFS filter
   *
   * @param lc WFS layer configuration
   * @param filter WFS filter params
   * @param analysisId Analysis id when input is analysislayer, in other case null
   * @return String WFS filter xml
   * @throws fi.nls.oskari.service.ServiceException
   *     **********************************************************************
   */
  private String parseFilter(
      WFSLayerConfiguration lc, String filter, String analysisId, String categoryId)
      throws ServiceException {

    JSONObject filter_js = null;
    try {
      if (filter == null) {
        String idfilter = null;
        if (analysisId != null) {
          // Add analysis id filter when analysis in analysis
          idfilter = FILTER_ID_TEMPLATE1.replace("{propertyName}", ANALYSIS_PROPERTY_NAME);
          idfilter = idfilter.replace("{propertyValue}", analysisId);
        } else if (categoryId != null) {
          // Add category id filter when myplaces in analysis
          idfilter = FILTER_ID_TEMPLATE1.replace("{propertyName}", MYPLACES_PROPERTY_NAME);
          idfilter = idfilter.replace("{propertyValue}", categoryId);
        }
        if (idfilter != null) filter_js = JSONHelper.createJSONObject(idfilter);

      } else {
        filter_js = JSONHelper.createJSONObject(filter);
        // Add analysis id filter when analysis in analysis
        if (filter_js.has(JSON_KEY_FILTERS)) {
          String idfilter = null;
          if (analysisId != null) {
            // Add analysis id filter when analysis in analysis
            idfilter = FILTER_ID_TEMPLATE2.replace("{propertyName}", ANALYSIS_PROPERTY_NAME);
            idfilter = idfilter.replace("{propertyValue}", analysisId);
          } else if (categoryId != null) {
            // Add category id filter when myplaces in analysis
            idfilter = FILTER_ID_TEMPLATE2.replace("{propertyName}", MYPLACES_PROPERTY_NAME);
            idfilter = idfilter.replace("{propertyValue}", categoryId);
          }
          if (idfilter != null) {
            JSONObject analysis_id_filter = JSONHelper.createJSONObject(idfilter);
            filter_js.getJSONArray(JSON_KEY_FILTERS).put(analysis_id_filter);
          }

        } else {
          String idfilter = null;
          if (analysisId != null) {
            // Add analysis id filter when analysis in analysis
            idfilter = FILTER_ID_TEMPLATE3.replace("{propertyName}", ANALYSIS_PROPERTY_NAME);
            idfilter = idfilter.replace("{propertyValue}", analysisId);
          } else if (categoryId != null) {
            // Add category id filter when myplaces in analysis
            idfilter = FILTER_ID_TEMPLATE3.replace("{propertyName}", MYPLACES_PROPERTY_NAME);
            idfilter = idfilter.replace("{propertyValue}", categoryId);
          }
          if (idfilter != null) {
            JSONArray idfilter_js = JSONHelper.createJSONArray(idfilter);
            filter_js.put(JSON_KEY_FILTERS, idfilter_js);
          }
        }
      }
    } catch (JSONException e) {
      log.warn(e, "JSON parse failed");
    }

    // Build filter
    final String wfs_filter =
        WFSFilterBuilder.parseWfsFilter(filter_js, lc.getSRSName(), lc.getGMLGeometryProperty());

    return wfs_filter;
  }
Пример #3
0
  /**
   * Parses INTERSECT method parameters for WPS execute xml variables
   *
   * @param lc WFS layer configuration
   * @param json Method parameters and layer info from the front
   * @param baseUrl Url for Geoserver WPS reference input (input FeatureCollection)
   * @return IntersectMethodParams parameters for WPS execution
   *     **********************************************************************
   */
  private IntersectMethodParams parseIntersectParams(
      WFSLayerConfiguration lc, WFSLayerConfiguration lc2, JSONObject json, String baseUrl)
      throws ServiceException {
    IntersectMethodParams method = new IntersectMethodParams();
    //
    method.setMethod(INTERSECT);
    // General variable input and variable input of union input 1

    method.setLayer_id(ConversionHelper.getInt(lc.getLayerId(), 0));
    method.setServiceUrl(lc.getURL());
    baseUrl = baseUrl.replace("&", "&");
    method.setHref(baseUrl + String.valueOf(lc.getLayerId()));
    method.setTypeName(lc.getFeatureNamespace() + ":" + lc.getFeatureElement());

    method.setMaxFeatures(String.valueOf(lc.getMaxFeatures()));
    method.setSrsName(lc.getSRSName());
    method.setOutputFormat(DEFAULT_OUTPUT_FORMAT);
    method.setVersion(lc.getWFSVersion());
    method.setXmlns(
        "xmlns:" + lc.getFeatureNamespace() + "=\"" + lc.getFeatureNamespaceURI() + "\"");
    method.setGeom(lc.getGMLGeometryProperty());

    // Variable values of Union input 2
    method.setHref2(baseUrl + String.valueOf(lc2.getLayerId()));
    method.setTypeName2(lc2.getFeatureNamespace() + ":" + lc2.getFeatureElement());
    method.setXmlns2(
        "xmlns:" + lc2.getFeatureNamespace() + "=\"" + lc2.getFeatureNamespaceURI() + "\"");
    method.setGeom2(lc2.getGMLGeometryProperty());

    JSONObject bbox = null;

    try {

      bbox = json.getJSONObject("bbox");
      method.setX_lower(bbox.optString("left"));
      method.setY_lower(bbox.optString("bottom"));
      method.setX_upper(bbox.optString("right"));
      method.setY_upper(bbox.optString("top"));

      // TODO: Intersect retain columns
      // A layer
      // method.setFieldA1(fieldA1);
      // B layer
      // method.setFieldA1(fieldB1);

    } catch (JSONException e) {
      throw new ServiceException("Bbox parameters missing.");
    }

    return method;
  }
Пример #4
0
  /**
   * Parses AGGREGATE method parameters for WPS execute xml variables
   *
   * @param lc WFS layer configuration
   * @param json Method parameters and layer info from the front
   * @param baseUrl Url for Geoserver WPS reference input (input
   * @param aggre_field Field name for aggregate function
   * @return AggregateMethodParams parameters for WPS execution
   *     **********************************************************************
   */
  private AggregateMethodParams parseAggregateParams(
      WFSLayerConfiguration lc,
      JSONObject json,
      String baseUrl,
      String aggre_field,
      List<String> aggre_funcs)
      throws ServiceException {
    AggregateMethodParams method = new AggregateMethodParams();
    //
    method.setMethod(AGGREGATE);
    try {
      method.setLayer_id(ConversionHelper.getInt(lc.getLayerId(), 0));
      method.setServiceUrl(lc.getURL());
      baseUrl = baseUrl.replace("&", "&amp;");
      method.setHref(baseUrl + String.valueOf(lc.getLayerId()));
      method.setTypeName(lc.getFeatureNamespace() + ":" + lc.getFeatureElement());
      method.setMaxFeatures(String.valueOf(lc.getMaxFeatures()));
      method.setSrsName(lc.getSRSName());
      method.setOutputFormat(DEFAULT_OUTPUT_FORMAT);
      method.setVersion(lc.getWFSVersion());
      method.setXmlns(
          "xmlns:" + lc.getFeatureNamespace() + "=\"" + lc.getFeatureNamespaceURI() + "\"");

      method.setGeom(lc.getGMLGeometryProperty());

      JSONObject bbox = null;

      bbox = json.getJSONObject("bbox");
      method.setX_lower(bbox.optString("left"));
      method.setY_lower(bbox.optString("bottom"));
      method.setX_upper(bbox.optString("right"));
      method.setY_upper(bbox.optString("top"));

      // TODO: loop fields - current solution only for 1st field
      method.setAggreField1(aggre_field);
      method.setAggreFunctions(aggre_funcs);

    } catch (JSONException e) {
      throw new ServiceException("Method parameters missing.");
    }

    return method;
  }
Пример #5
0
  /**
   * Parses BUFFER method parameters for WPS execute xml variables
   *
   * @param lc WFS layer configuration
   * @param json Method parameters and layer info from the front
   * @param baseUrl Url for Geoserver WPS reference input (input FeatureCollection)
   * @return BufferMethodParams parameters for WPS execution
   *     **********************************************************************
   */
  private BufferMethodParams parseBufferParams(
      WFSLayerConfiguration lc, JSONObject json, String baseUrl) throws ServiceException {
    final BufferMethodParams method = new BufferMethodParams();
    method.setMethod(BUFFER);
    //
    try {
      method.setLayer_id(ConversionHelper.getInt(lc.getLayerId(), 0));
      method.setServiceUrl(lc.getURL());
      baseUrl = baseUrl.replace("&", "&amp;");
      method.setHref(baseUrl + String.valueOf(lc.getLayerId()));
      method.setTypeName(lc.getFeatureNamespace() + ":" + lc.getFeatureElement());
      method.setMaxFeatures(String.valueOf(lc.getMaxFeatures()));
      method.setSrsName(lc.getSRSName());
      method.setOutputFormat(DEFAULT_OUTPUT_FORMAT);
      method.setVersion(lc.getWFSVersion());
      method.setXmlns(
          "xmlns:" + lc.getFeatureNamespace() + "=\"" + lc.getFeatureNamespaceURI() + "\"");

      method.setGeom(lc.getGMLGeometryProperty());

      final JSONObject params = json.getJSONObject(JSON_KEY_METHODPARAMS);
      final JSONObject bbox = json.getJSONObject("bbox");
      method.setX_lower(bbox.optString("left"));
      method.setY_lower(bbox.optString("bottom"));
      method.setX_upper(bbox.optString("right"));
      method.setY_upper(bbox.optString("top"));

      method.setDistance(params.optString("distance"));

    } catch (JSONException e) {
      throw new ServiceException("Method parameters missing.");
    }

    return method;
  }
Пример #6
0
  /**
   * Parses method parameters to WPS execute xml syntax definition
   *
   * @param layerJSON method parameters and layer info from the front
   * @param baseUrl Url for Geoserver WPS reference input (input FeatureCollection)
   * @return AnalysisLayer parameters for WPS execution
   *     **********************************************************************
   */
  public AnalysisLayer parseAnalysisLayer(String layerJSON, String filter, String baseUrl)
      throws ServiceException {
    AnalysisLayer analysisLayer = new AnalysisLayer();

    JSONObject json = JSONHelper.createJSONObject(layerJSON);
    WFSLayerConfiguration lc = null;

    // analysis input data type - default is WFS layer
    analysisLayer.setInputType(ANALYSIS_INPUT_TYPE_WFS);
    // analysis rendering url
    analysisLayer.setWpsUrl(analysisRenderingUrl);
    // analysis element name
    analysisLayer.setWpsName(analysisRenderingElement);

    analysisLayer.setInputAnalysisId(null);
    int id = 0;
    try {
      // Analysis input property types
      this.prepareFieldtypeMap(analysisLayer, json);

      String sid = json.getString(JSON_KEY_LAYERID);

      // Input is wfs layer or analaysis layer or my places
      if (sid.indexOf(LAYER_PREFIX) == 0) {
        // Analysislayer is input
        if (!this.prepareAnalysis4Analysis(analysisLayer, json))
          throw new ServiceException("AnalysisInAnalysis parameters are invalid");
        id = analysisLayer.getId();
      } else if (sid.indexOf(MYPLACES_LAYER_PREFIX) == 0) {
        // myplaces is input
        if (!this.prepareAnalysis4Myplaces(analysisLayer, json))
          throw new ServiceException("AnalysisInMyPlaces parameters are invalid");
        id = analysisLayer.getId();
      } else {
        // Wfs layer id
        id = ConversionHelper.getInt(sid, -1);
      }
    } catch (JSONException e) {
      throw new ServiceException("AnalysisInAnalysis parameters are invalid");
    }
    // --- WFS layer is analysis input
    analysisLayer.setId(id);

    // Get wfs layer configuration
    lc = layerConfigurationService.findConfiguration(id);

    final OskariLayer wfsLayer = mapLayerService.find(id);
    log.debug("got wfs layer", wfsLayer);
    analysisLayer.setMinScale(wfsLayer.getMinScale());
    analysisLayer.setMaxScale(wfsLayer.getMaxScale());

    // Set WFS input type, other than analysis_ and myplaces_- default is REFERENCE
    this.setWpsInputLayerType(lc.getWps_params(), analysisLayer);

    // Extract parameters for analysis methods from layer

    String name = json.optString("name");
    if (name.isEmpty()) {
      throw new ServiceException("Analysis name missing.");
    } else {
      analysisLayer.setName(name);
    }

    JSONArray fields_in = json.optJSONArray("fields");
    List<String> fields = new ArrayList<String>();

    if (fields_in == null) {
      throw new ServiceException("Fields missing.");
    } else {
      // Add fields of WFS service, if empty and all fields mode on
      if (fields_in.length() == 0) fields_in = this.getWfsFields(analysisLayer);
      // Remove internal fields
      try {
        for (int i = 0; i < fields_in.length(); i++) {
          if (!HIDDEN_FIELDS.contains(fields_in.getString(i))) fields.add(fields_in.getString(i));
        }
      } catch (JSONException e) {
        throw new ServiceException("Method fields parameters missing.");
      }
      analysisLayer.setFields(fields);
    }

    String style = json.optString("style");
    if (style.isEmpty()) {
      throw new ServiceException("Style missing.");
    } else {
      analysisLayer.setStyle(style);
    }

    Integer opacity = json.optInt("opacity");
    if (opacity == 0) opacity = DEFAULT_OPACITY;
    analysisLayer.setOpacity(opacity);

    String analysisMethod = json.optString("method"); // "union_geom"; test
    analysisLayer.setMethod(analysisMethod);

    analysisLayer.setAggreFunctions(null);
    analysisLayer.setMergeAnalysisLayers(null);

    // ------------------LAYER_UNION -----------------------
    if (LAYER_UNION.equals(analysisMethod)) {
      JSONObject params;
      try {
        params = json.getJSONObject(JSON_KEY_METHODPARAMS);
      } catch (JSONException e) {
        throw new ServiceException("Method parameters missing.");
      }
      JSONArray sids = params.optJSONArray(JSON_KEY_LAYERS);
      // Loop merge layers - get analysis ids
      List<Long> ids = new ArrayList<Long>();
      List<String> mergelays = new ArrayList<String>();
      if (sids == null) {
        throw new ServiceException("merge layers missing");
      } else {
        try {
          for (int i = 0; i < sids.length(); i++) {
            Long aid = this.getAnalysisId(sids.getString(i));
            if (aid > 0) {
              ids.add(aid);
              mergelays.add(sids.getString(i));
            }
          }
        } catch (JSONException e) {
          throw new ServiceException("Merge layers missing.");
        }
        // Merge analysis Ids
        analysisLayer.setMergeAnalysisIds(ids);
        // Merge analysis Layers
        analysisLayer.setMergeAnalysisLayers(mergelays);
      }
    }
    // ------------------ BUFFER -----------------------
    else if (BUFFER.equals(analysisMethod)) {
      // when analysisMethod == vec:BufferFeatureCollection

      // Set params for WPS execute

      BufferMethodParams method = this.parseBufferParams(lc, json, baseUrl);

      method.setWps_reference_type(analysisLayer.getInputType());
      analysisLayer.setAnalysisMethodParams(method);

      // WFS filter
      analysisLayer
          .getAnalysisMethodParams()
          .setFilter(
              this.parseFilter(
                  lc,
                  filter,
                  analysisLayer.getInputAnalysisId(),
                  analysisLayer.getInputCategoryId()));
      // WFS Query properties
      analysisLayer
          .getAnalysisMethodParams()
          .setProperties(
              this.parseProperties(
                  analysisLayer.getFields(),
                  lc.getFeatureNamespace(),
                  lc.getGMLGeometryProperty()));
      // ------------------ INTERSECT -----------------------
    } else if (INTERSECT.equals(analysisMethod)) {
      JSONObject params;
      try {
        params = json.getJSONObject(JSON_KEY_METHODPARAMS);
      } catch (JSONException e) {
        throw new ServiceException("Method parameters missing.");
      }
      WFSLayerConfiguration lc2 = null;
      int id2 = 0;
      String sid = "";
      try {
        sid = params.getString(JSON_KEY_LAYERID);
        // Input is wfs layer or analaysis layer
        if (sid.indexOf(LAYER_PREFIX) == 0) {
          // Analysislayer is input
          // eg. analyse_216_340
          id2 = ConversionHelper.getInt(analysisBaseLayerId, 0);

        } else if (sid.indexOf(MYPLACES_LAYER_PREFIX) == 0) {
          // Myplaces is input
          id2 = ConversionHelper.getInt(myplacesBaseLayerId, 0);

        } else {
          // Wfs layer id
          id2 = ConversionHelper.getInt(sid, -1);
        }
      } catch (JSONException e) {
        throw new ServiceException("AnalysisInAnalysis parameters are invalid");
      }

      // Get wfs layer configuration for union input 2
      lc2 = layerConfigurationService.findConfiguration(id2);

      // Set params for WPS execute

      IntersectMethodParams method = this.parseIntersectParams(lc, lc2, json, baseUrl);

      method.setWps_reference_type(analysisLayer.getInputType());
      if (sid.indexOf(LAYER_PREFIX) == 0 || sid.indexOf(MYPLACES_LAYER_PREFIX) == 0) {
        method.setWps_reference_type2(ANALYSIS_INPUT_TYPE_GS_VECTOR);
      } else {
        method.setWps_reference_type2(ANALYSIS_INPUT_TYPE_WFS);
      }
      // Set WFS input type, other than analysis_ and myplaces_- default is REFERENCE
      this.setWpsInputLayerType(lc.getWps_params(), analysisLayer);

      // WFS filter

      method.setFilter(
          this.parseFilter(
              lc, filter, analysisLayer.getInputAnalysisId(), analysisLayer.getInputCategoryId()));

      if (sid.indexOf(MYPLACES_LAYER_PREFIX) == 0) {
        method.setFilter2(this.parseFilter(lc2, null, null, this.getAnalysisInputId(params)));
      } else {
        method.setFilter2(this.parseFilter(lc2, null, this.getAnalysisInputId(params), null));
      }
      // WFS Query properties
      method.setProperties(
          this.parseProperties(
              analysisLayer.getFields(), lc.getFeatureNamespace(), lc.getGMLGeometryProperty()));

      analysisLayer.setAnalysisMethodParams(method);
      // ------------------ AGGREGATE -----------------------
    } else if (AGGREGATE.equals(analysisMethod)) {

      // 1 to n aggregate wps tasks
      String aggre_field = null;
      try {

        aggre_field = json.getJSONObject(JSON_KEY_METHODPARAMS).optString(JSON_KEY_AGGRE_ATTRIBUTE);
        if (analysisLayer.getInputType().equals(ANALYSIS_INPUT_TYPE_GS_VECTOR)) {
          if (analysisLayer.getInputAnalysisId() != null) {
            aggre_field =
                analysisDataService.SwitchField2AnalysisField(
                    aggre_field, analysisLayer.getInputAnalysisId());
          }
        }
        JSONArray aggre_func_in =
            json.getJSONObject(JSON_KEY_METHODPARAMS).optJSONArray(JSON_KEY_FUNCTIONS);
        List<String> aggre_funcs = new ArrayList<String>();
        if (aggre_func_in == null) {
          throw new ServiceException("Aggregate functions missing.");
        } else {
          try {
            for (int i = 0; i < aggre_func_in.length(); i++) {
              aggre_funcs.add(aggre_func_in.getString(i));
            }
          } catch (JSONException e) {
            throw new ServiceException("Aggregate functions missing.");
          }
          analysisLayer.setAggreFunctions(aggre_funcs);
        }

      } catch (JSONException e) {
        throw new ServiceException("Method parameters missing.");
      }

      // Set params for WPS execute
      if (aggre_field == null) throw new ServiceException("Aggregate field parameter missing.");
      AggregateMethodParams method =
          this.parseAggregateParams(
              lc, json, baseUrl, aggre_field, analysisLayer.getAggreFunctions());

      method.setWps_reference_type(analysisLayer.getInputType());
      analysisLayer.setAnalysisMethodParams(method);
      // WFS filter

      analysisLayer
          .getAnalysisMethodParams()
          .setFilter(
              this.parseFilter(
                  lc,
                  filter,
                  analysisLayer.getInputAnalysisId(),
                  analysisLayer.getInputCategoryId()));
      // ------------------ UNION -----------------------
    } else if (UNION.equals(analysisMethod)) {
      JSONObject params;
      try {
        params = json.getJSONObject(JSON_KEY_METHODPARAMS);
      } catch (JSONException e) {
        throw new ServiceException("Method parameters missing.");
      }

      // Set params for WPS execute

      UnionMethodParams method = this.parseUnionParams(lc, json, baseUrl);
      method.setWps_reference_type(analysisLayer.getInputType());

      // WFS filter

      method.setFilter(
          this.parseFilter(
              lc, filter, analysisLayer.getInputAnalysisId(), analysisLayer.getInputCategoryId()));

      analysisLayer.setAnalysisMethodParams(method);

    } else {
      throw new ServiceException("Method parameters missing.");
    }

    return analysisLayer;
  }