///////////////////////////////////////////////////////////////////////////////////////////////////
  //	main
  ///////////////////////////////////////////////////////////////////////////////////////////////////
  public static void main(String[] args) throws IOException {

    final FileWriter okWriter =
        new FileWriter(
            new File("d:/temp_dev/opendata/servicios.csv")); // fichero al que escribir los datos
    final FileWriter errWriter =
        new FileWriter(
            new File(
                "d:/temp_dev/opendata/servicios_err.log")); // fichero al que escribir los datos
    try {
      okWriter.write(ServiceData.csvHEAD()); // write the header

      // [1] Construir la query
      R01MSearchFilterForContent qry = _buildQuery();

      log.debug("[SearchQuery]: " + qry.toCriteriaString());

      // [2] Inicializar la sesión
      R01MSearchSession.resultItemStreamForQuery(qry)
          .map(
              new Func1<R01MSearchResultItem, ServiceData>() {
                @Override
                public ServiceData call(final R01MSearchResultItem item) {
                  try {
                    return _resultItemToServiceData(item);
                  } catch (IOException ioEx) {
                    throw OnErrorThrowable.from(ioEx);
                  }
                }
              })
          .subscribe(
              new Observer<ServiceData>() {
                @Override
                public void onCompleted() {
                  System.out.println("---->End!");
                }

                @Override
                public void onNext(final ServiceData serviceData) {
                  _writeRow(okWriter, serviceData);
                }

                @Override
                public void onError(final Throwable th) {
                  _writeError(errWriter, th);
                }
              });
    } catch (Exception ex) {
      ex.printStackTrace(System.out);
    } finally {
      try {
        okWriter.close();
        errWriter.close();
      } catch (IOException ioEx) {
        ioEx.printStackTrace(System.out);
      }
    }
  }
  /**
   * Builds a searchQuery
   *
   * @return the SearchQuery
   */
  private static R01MSearchFilterForContent _buildQuery() {
    R01MSearchFilterForContent qry =
        R01MSearchFilterForContent.createFor(R01MContent.class)
            .typedInAnyOfTheseClusters(R01MTypoClusterID.forId("euskadi"))
            .typedInAnyOfTheseFamilies(R01MTypoFamilyID.forId("procedimientos_administrativos"))
            .typedInAnyOfTheseTypes(R01MTypoTypeID.forId("ayuda_subvencion"))
            .mustMeetThisMetaDataCondition(
                EqualsQueryClause.forField(IndexableFieldID.forId("procedureStatus")).of(16))
            .mustHaveStructureLabels(
                R01MStructureLabelOID.forIds(
                    "r01e00000ff26d46212a470b818464daec45c59fa", "default"))
            .orderedBy(
                R01MSearchQueryOrderByMetaData.forMetaData("createDate")
                    .ordered(R01MSearchQueryOrderBy.ASCENDING));

    return qry;
  }