/**
   * Gets the serializable string for design values. Design values include data set parameter
   * definitions and result set definitions.
   *
   * @param setDesign the data set desgin
   * @return the serializable string for design values
   */
  private String serializeOdaValues(DataSetDesign setDesign) {
    DataSetParameters params =
        SchemaConversionUtil.convertToAdapterParameters(setDesign.getParameters());
    ResultSets resultSets = setDesign.getResultSets();

    DesignValues values = ModelFactory.eINSTANCE.createDesignValues();
    values.setVersion(IConstants.DESINGER_VALUES_VERSION);
    boolean hasData = false;

    if (params != null) {
      values.setDataSetParameters((DataSetParameters) EcoreUtil.copy(params));
      hasData = true;
    }

    if (resultSets != null) {
      values.setResultSets((ResultSets) EcoreUtil.copy(resultSets));
      hasData = true;
    }

    if (!hasData) return IConstants.EMPTY_STRING;

    try {
      return SerializerImpl.instance().write(values);
    } catch (IOException e) {
      return null;
    }
  }
  /**
   * @param setHandle
   * @return
   */
  private DataSetParameters getCachedParameters(OdaDataSetHandle setHandle) {
    String strDesignValues = setHandle.getDesignerValues();

    DesignValues designerValues = null;

    try {
      if (strDesignValues != null) designerValues = SerializerImpl.instance().read(strDesignValues);
    } catch (IOException e) {
    }

    // the driver defined parameters are in the designer values

    DataSetParameters cachedParams = null;
    if (designerValues != null) cachedParams = designerValues.getDataSetParameters();

    return cachedParams;
  }
  /**
   * Updates the designer value. Designer values only contain Driver-defined parameters.
   *
   * @param setDesign the data set design
   * @param setHandle the data set handle
   * @param designerValues the designer values
   * @param userDefinedList the user defined parameters
   * @throws SemanticException
   */
  private void updateDesignerValue(
      DataSetDesign setDesign,
      OdaDataSetHandle setHandle,
      DataSetParameters requestParameters,
      List userDefinedList,
      ResultSets requestResultSets)
      throws SemanticException {

    DesignValues designerValues = null;

    // update design values.

    org.eclipse.datatools.connectivity.oda.design.DataSetParameters setDefinedParams =
        setDesign.getParameters();

    if (setDefinedParams == null) {
      if (requestParameters != null) {
        designerValues = ModelFactory.eINSTANCE.createDesignValues();
        designerValues.setDataSetParameters(null);
      }
    } else {
      org.eclipse.datatools.connectivity.oda.design.DataSetParameters driverParams =
          DataSetParameterAdapter.getDriverDefinedParameters(
              setDefinedParams.getParameterDefinitions(), userDefinedList);

      if (driverParams.getParameterDefinitions().size() > 0) {
        designerValues = ModelFactory.eINSTANCE.createDesignValues();

        DataSetParameters adapterParams =
            SchemaConversionUtil.convertToAdapterParameters(driverParams);
        designerValues.setDataSetParameters(adapterParams);

        clearReportParameterRelatedValues(
            adapterParams.getParameters(), setHandle.getModuleHandle());
      }
    }

    if (designerValues == null && requestResultSets == null) {
      setHandle.setDesignerValues(null);
      return;
    }

    if (requestResultSets != null) {
      if (designerValues == null) {
        designerValues = ModelFactory.eINSTANCE.createDesignValues();
        designerValues.setDataSetParameters(null);
      }

      designerValues.setResultSets(requestResultSets);
    } else designerValues.setResultSets(null);

    // Set DesignerValues

    try {
      String dValue = SerializerImpl.instance().write(designerValues);
      setHandle.setDesignerValues(dValue);
    } catch (IOException e) {
    }
  }
  public void updateDataSetHandle(
      DataSetDesign setDesign, OdaDataSetHandle setHandle, boolean isSourceChanged)
      throws SemanticException {
    if (setDesign == null || setHandle == null) return;

    // serialize and get the designer values
    DesignValues designerValues = null;
    try {
      designerValues = SerializerImpl.instance().read(setHandle.getDesignerValues());
    } catch (IOException e) {
      e.printStackTrace();
    }

    DataSetParameters requestParameters = null;
    ResultSets requestResultSets = null;
    if (designerValues != null) {
      requestParameters = designerValues.getDataSetParameters();
      requestResultSets = designerValues.getResultSets();
    }

    updateDataSetHandle(
        setDesign, setHandle, isSourceChanged, requestParameters, requestResultSets);
  }
  /**
   * Converts ROM result set columns with column hints to ODA data set resultSets.
   *
   * @throws Exception
   */
  public void testToODAResultSetsWithColumnHint() throws Exception {
    // create oda set handle.

    openDesign("OdaDataSetConvertResultSetsTest.xml"); // $NON-NLS-1$
    OdaDataSetHandle setHandle =
        (OdaDataSetHandle) designHandle.findDataSet("myDataSet1"); // $NON-NLS-1$

    DataSetDesign setDesign = new ModelOdaAdapter().createDataSetDesign(setHandle);

    DesignValues values = ModelFactory.eINSTANCE.createDesignValues();
    values.setResultSets(setDesign.getResultSets());

    saveDesignValuesToFile(values);
    save();

    saveAndOpenDesign();
    setHandle = (OdaDataSetHandle) designHandle.findDataSet("myDataSet1"); // $NON-NLS-1$

    setDesign = new ModelOdaAdapter().createDataSetDesign(setHandle);

    String hint =
        setDesign
            .getResultSets()
            .getResultSetDefinitions()
            .get(0)
            .getResultSetColumns()
            .getResultColumnDefinitions()
            .get(0)
            .getUsageHints()
            .getHelpText();

    assertEquals(hint, "Help me!");

    // assertTrue( compareTextFile( "ResultSetsWithHint_golden.xml" ) ); //$NON-NLS-1$

  }
  /**
   * @param setHandle
   * @param setDesign
   */
  void updateDataSetDesign(OdaDataSetHandle setHandle, DataSetDesign setDesign) {
    // properties on ReportElement, like name, displayNames, etc.

    setDesign.setName(setHandle.getName());

    String displayName = setHandle.getDisplayName();
    String displayNameKey = setHandle.getDisplayNameKey();

    if (displayName != null || displayNameKey != null) {
      setDesign.setDisplayName(displayName);
      setDesign.setDisplayNameKey(displayNameKey);
    }

    // properties such as comments, extends, etc are kept in
    // DataSourceHandle, not DataSourceDesign.

    // scripts of DataSource are kept in
    // DataSourceHandle, not DataSourceDesign.

    setDesign.setOdaExtensionDataSetId(setHandle.getExtensionID());

    setDesign.setPublicProperties(
        newOdaPublicProperties(setHandle.getExtensionPropertyDefinitionList(), setHandle));

    setDesign.setPrivateProperties(
        newOdaPrivateProperties(setHandle.privateDriverPropertiesIterator()));

    setDesign.setPrimaryResultSetName(setHandle.getResultSetName());

    setDesign.setQueryText(setHandle.getQueryText());

    // create a new data source design for this set design.

    OdaDataSourceHandle sourceHandle = (OdaDataSourceHandle) setHandle.getDataSource();

    if (sourceHandle != null)
      setDesign.setDataSourceDesign(new DataSourceAdapter().createDataSourceDesign(sourceHandle));

    // when converts the ODA to BIRT, some information for parameter
    // definitions may be lost. So, must get the cached values first. Then
    // try to restore to latest value.

    String strDesignValues = setHandle.getDesignerValues();

    DesignValues designerValues = null;

    try {
      if (strDesignValues != null) designerValues = SerializerImpl.instance().read(strDesignValues);

    } catch (IOException e) {
    }

    // the driver defined parameters are in the designer values

    DataSetParameters cachedParams = null;
    if (designerValues != null) {
      cachedParams = designerValues.getDataSetParameters();
    }

    DataSetParameterAdapter dataParamAdapter = new DataSetParameterAdapter(setHandle, setDesign);

    org.eclipse.datatools.connectivity.oda.design.DataSetParameters designDefinedParams = null;
    if (cachedParams != null) {
      // cached are driver-defined parameters
      designDefinedParams =
          SchemaConversionUtil.convertToDesignParameters(EcoreUtil.copy(cachedParams));

      setDesign.setParameters(designDefinedParams);

      dataParamAdapter.updateDriverDefinedParameter(
          designDefinedParams, SchemaConversionUtil.getCachedDynamicList(cachedParams));
    }

    // if there is no driver defined parameters, update parameters with set
    // handle defined with cached in the last request

    if (designDefinedParams == null) {
      designDefinedParams = dataParamAdapter.newOdaDataSetParams(designDefinedParams);
      setDesign.setParameters(designDefinedParams);
    }

    // handle those parameters defined by user

    dataParamAdapter.updateUserDefinedParameter(designDefinedParams);

    org.eclipse.datatools.connectivity.oda.design.DataSetParameters userDefinedParams =
        dataParamAdapter.newOdaDataSetParams(dataParamAdapter.getUserDefinedParams());

    if (designDefinedParams == null) {
      designDefinedParams = userDefinedParams;
      setDesign.setParameters(designDefinedParams);
    } else if (userDefinedParams != null)
      designDefinedParams
          .getParameterDefinitions()
          .addAll(userDefinedParams.getParameterDefinitions());

    new ResultSetsAdapter(setHandle, setDesign).updateOdaResultSetDefinition();

    updateODAMessageFile(setDesign.getDataSourceDesign(), setHandle.getModuleHandle());
  }