@Override
  public Map<String, Object> execute(Map<String, Object> input, ProgressListener monitor)
      throws ProcessException {
    if (started) throw new IllegalStateException("Process can only be run once");
    started = true;

    if (monitor == null) monitor = new NullProgressListener();
    try {
      monitor.started();
      monitor.setTask(Text.text("Grabbing arguments"));
      monitor.progress(10.0f);

      SimpleFeatureCollection inputFeatures =
          (SimpleFeatureCollection)
              Params.getValue(input, PearsonCorrelationProcessFactory.inputFeatures, null);
      String inputFields =
          (String) Params.getValue(input, PearsonCorrelationProcessFactory.inputFields, null);
      if (inputFeatures == null || inputFields == null) {
        throw new NullPointerException("All parameters required");
      }

      monitor.setTask(Text.text("Processing ..."));
      monitor.progress(25.0f);

      if (monitor.isCanceled()) {
        return null; // user has canceled this operation
      }

      // start process
      PearsonOperation operation = new PearsonOperation();
      PearsonResult ret = operation.execute(inputFeatures, inputFields);
      // end process

      monitor.setTask(Text.text("Encoding result"));
      monitor.progress(90.0f);

      Map<String, Object> resultMap = new HashMap<String, Object>();
      resultMap.put(PearsonCorrelationProcessFactory.RESULT.key, ret);
      monitor.complete(); // same as 100.0f

      return resultMap;
    } catch (Exception eek) {
      monitor.exceptionOccurred(eek);
      return null;
    } finally {
      monitor.dispose();
    }
  }
Exemple #2
0
  /**
   * Executes the import operation using the parameters that have been specified. Features will be
   * added to the working tree, and a new working tree will be constructed. Either {@code all} or
   * {@code table}, but not both, must be set prior to the import process.
   *
   * @return RevTree the new working tree
   */
  @SuppressWarnings("deprecation")
  @Override
  public RevTree call() {

    // check preconditions and get the actual list of type names to import
    final String[] typeNames = checkPreconditions();

    ProgressListener progressListener = getProgressListener();
    progressListener.started();

    // use a local variable not to alter the command's state
    boolean overwrite = this.overwrite;
    if (alter) {
      overwrite = false;
    }

    final WorkingTree workTree = getWorkTree();

    final boolean destPathProvided = destPath != null;
    if (destPathProvided && overwrite) {
      // we delete the previous tree to honor the overwrite setting, but then turn it
      // to false. Otherwise, each table imported will overwrite the previous ones and
      // only the last one will be imported.
      try {
        workTree.delete(destPath);
      } catch (Exception e) {
        throw new GeoToolsOpException(e, StatusCode.UNABLE_TO_INSERT);
      }
      overwrite = false;
    }

    int tableCount = 0;

    for (String typeName : typeNames) {
      {
        tableCount++;
        String tableName = String.format("%-16s", typeName);
        if (typeName.length() > 16) {
          tableName = tableName.substring(0, 13) + "...";
        }
        progressListener.setDescription(
            "Importing " + tableName + " (" + tableCount + "/" + typeNames.length + ")... ");
      }

      FeatureSource featureSource = getFeatureSource(typeName);
      SimpleFeatureType featureType = (SimpleFeatureType) featureSource.getSchema();

      final String fidPrefix = featureType.getTypeName() + ".";

      String path;
      if (destPath == null) {
        path = featureType.getTypeName();
      } else {
        NodeRef.checkValidPath(destPath);
        path = destPath;
        featureType = createForceFeatureType(featureType, path);
      }
      featureSource =
          new ForceTypeAndFidFeatureSource<FeatureType, Feature>(
              featureSource, featureType, fidPrefix);

      ProgressListener taskProgress = subProgress(100.f / typeNames.length);
      if (overwrite) {
        try {
          workTree.delete(path);
          workTree.createTypeTree(path, featureType);
        } catch (Exception e) {
          throw new GeoToolsOpException(e, StatusCode.UNABLE_TO_INSERT);
        }
      }

      if (alter) {
        // first we modify the feature type and the existing features, if needed
        workTree.updateTypeTree(path, featureType);
        Iterator<Feature> transformedIterator = transformFeatures(featureType, path);
        try {
          final Integer collectionSize = collectionSize(featureSource);
          workTree.insert(path, transformedIterator, taskProgress, null, collectionSize);
        } catch (Exception e) {
          throw new GeoToolsOpException(StatusCode.UNABLE_TO_INSERT);
        }
      }
      try {
        insert(workTree, path, featureSource, taskProgress);
      } catch (Exception e) {
        throw new GeoToolsOpException(e, StatusCode.UNABLE_TO_INSERT);
      }
    }

    progressListener.progress(100.f);
    progressListener.complete();
    return workTree.getTree();
  }