Exemple #1
0
  /** Writes an XML representation of the given Grid to the given OutputStream. */
  public static void toXml(Grid grid, OutputStream out) {
    XMLWriter writer = XMLFactory.getXMLWriter(out);

    writer.openDocument();
    writer.openElement(
        ATTR_GRID,
        ATTR_TITLE,
        grid.getTitle(),
        ATTR_SUBTITLE,
        grid.getSubtitle(),
        ATTR_WIDTH,
        String.valueOf(grid.getWidth()),
        ATTR_HEIGHT,
        String.valueOf(grid.getHeight()));

    writer.openElement(ATTR_HEADERS);

    for (GridHeader header : grid.getHeaders()) {
      writer.writeElement(
          ATTR_HEADER,
          null,
          ATTR_NAME,
          header.getName(),
          ATTR_COLUMN,
          header.getColumn(),
          ATTR_TYPE,
          header.getType(),
          ATTR_HIDDEN,
          String.valueOf(header.isHidden()),
          ATTR_META,
          String.valueOf(header.isMeta()));
    }

    writer.closeElement();
    writer.openElement(ATTR_ROWS);

    for (List<Object> row : grid.getRows()) {
      writer.openElement(ATTR_ROW);

      for (Object field : row) {
        writer.writeElement(ATTR_FIELD, field != null ? String.valueOf(field) : EMPTY);
      }

      writer.closeElement();
    }

    writer.closeElement();
    writer.closeElement();

    writer.closeDocument();
  }
  public InputStream exportData(ExportParams params) {
    try {
      // -----------------------------------------------------------------
      // Pipes are input/output pairs. Data written on the output stream
      // shows up on the input stream at the other end of the pipe.
      // -----------------------------------------------------------------

      PipedOutputStream out = new PipedOutputStream();

      PipedInputStream in = new PipedInputStream(out);

      ZipOutputStream zipOut = new ZipOutputStream(out);

      zipOut.putNextEntry(new ZipEntry(ZIP_ENTRY_NAME));

      XMLWriter writer = XMLFactory.getXMLWriter(zipOut);

      // -----------------------------------------------------------------
      // Writes to one end of the pipe
      // -----------------------------------------------------------------

      String[] rootProperties = {
        ATTRIBUTE_NAMESPACE,
        NAMESPACE_10,
        ATTRIBUTE_MINOR_VERSION,
        MINOR_VERSION_13,
        ATTRIBUTE_EXPORTED,
        DateUtils.getMediumDateString()
      };

      ExportPipeThread thread = new ExportPipeThread(sessionFactory);

      thread.setZipOutputStream(zipOut);
      thread.setParams(params);
      thread.setWriter(writer);
      thread.setRootName(DXFROOT);
      thread.setRootProperties(rootProperties);

      thread.registerXMLConverter(new ConceptConverter(conceptService));
      thread.registerXMLConverter(new DataElementCategoryOptionConverter(categoryService));
      thread.registerXMLConverter(new DataElementCategoryConverter(categoryService));
      thread.registerXMLConverter(new DataElementCategoryComboConverter(categoryService));
      thread.registerXMLConverter(new DataElementCategoryOptionComboConverter(categoryService));

      thread.registerXMLConverter(new CategoryCategoryOptionAssociationConverter(categoryService));
      thread.registerXMLConverter(new CategoryComboCategoryAssociationConverter(categoryService));

      thread.registerXMLConverter(new DataElementConverter(dataElementService));
      thread.registerXMLConverter(new DataElementGroupConverter(dataElementService));
      thread.registerXMLConverter(new DataElementGroupMemberConverter(dataElementService));
      thread.registerXMLConverter(new DataElementGroupSetConverter(dataElementService));
      thread.registerXMLConverter(new DataElementGroupSetMemberConverter(dataElementService));

      thread.registerXMLConverter(new ConstantConverter(constantService));

      thread.registerXMLConverter(new IndicatorTypeConverter(indicatorService));
      thread.registerXMLConverter(new IndicatorConverter(indicatorService));
      thread.registerXMLConverter(new IndicatorGroupConverter(indicatorService));
      thread.registerXMLConverter(new IndicatorGroupMemberConverter(indicatorService));
      thread.registerXMLConverter(new IndicatorGroupSetConverter(indicatorService));
      thread.registerXMLConverter(new IndicatorGroupSetMemberConverter(indicatorService));

      thread.registerXMLConverter(new DataDictionaryConverter(dataDictionaryService));
      thread.registerXMLConverter(new DataDictionaryDataElementConverter(dataDictionaryService));
      thread.registerXMLConverter(new DataDictionaryIndicatorConverter(dataDictionaryService));

      thread.registerXMLConverter(new DataSetConverter(dataSetService));
      thread.registerXMLConverter(new DataSetMemberConverter(dataSetService, dataElementService));

      thread.registerXMLConverter(new OrganisationUnitConverter(organisationUnitService));
      thread.registerXMLConverter(
          new OrganisationUnitRelationshipConverter(organisationUnitService));
      thread.registerXMLConverter(new OrganisationUnitGroupConverter(organisationUnitGroupService));
      thread.registerXMLConverter(
          new OrganisationUnitGroupMemberConverter(
              organisationUnitGroupService, organisationUnitService));

      thread.registerXMLConverter(new GroupSetConverter(organisationUnitGroupService));
      thread.registerXMLConverter(new GroupSetMemberConverter(organisationUnitGroupService));
      thread.registerXMLConverter(new OrganisationUnitLevelConverter(organisationUnitService));

      thread.registerXMLConverter(
          new DataSetSourceAssociationConverter(dataSetService, organisationUnitService));

      thread.registerXMLConverter(new ValidationRuleConverter(validationRuleService));
      thread.registerXMLConverter(new PeriodConverter(periodService));

      thread.registerXMLConverter(new ReportConverter(reportService));
      thread.registerXMLConverter(new ReportTableConverter(reportTableService));
      thread.registerXMLConverter(new ChartConverter(chartService));
      thread.registerXMLConverter(
          new CompleteDataSetRegistrationConverter(
              completeDataSetRegistrationService,
              dataSetService,
              organisationUnitService,
              periodService));

      thread.registerXMLConverter(
          params.isAggregatedData()
              ? new AggregatedDataValueConverter(
                  aggregatedDataValueService, dataSetService, periodService)
              : new DataValueConverter(
                  aggregatedDataValueService, statementManager, periodService));

      thread.start();

      // -----------------------------------------------------------------
      // Reads at the other end of the pipe
      // -----------------------------------------------------------------

      InputStream bis = new BufferedInputStream(in);

      return bis;
    } catch (IOException ex) {
      throw new RuntimeException("Error occured during export to stream", ex);
    }
  }