/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

package org.eclipse.birt.report.model.api.util;

import java.util.List;

import org.eclipse.birt.report.model.api.CachedMetaDataHandle;
import org.eclipse.birt.report.model.api.DataSetHandle;
import org.eclipse.birt.report.model.api.StructureFactory;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.elements.structures.CachedMetaData;
import org.eclipse.birt.report.model.api.elements.structures.ResultSetColumn;

/**
 * Provides the backward compatibility for the user. Now support:
 * 
 * <ul>
 * <li>Updated CachedMetaData.resultSet.
 * </ul>
 * 
 * <p>
 * Backward compatibilty is different from other operations. It does not support
 * undo/redo and won't send out events.
 * 
 */

public class CompaibilityUtil
{

	/**
	 * Updates CachedMetaData.resultSet property with the given data set and the
	 * list of result set columns.
	 * 
	 * @param setHandle
	 *            the data set
	 * @param resultColumns
	 *            a list containing result set columns. Each item in the list is
	 *            {@link ResultSetColumn}
	 * @throws SemanticException
	 *             if any result set column in the list has invalid values.
	 */

	public static void updateResultSetinCachedMetaData(
			DataSetHandle setHandle, List resultColumns )
			throws SemanticException
	{
		if ( setHandle == null )
			return;

		if ( resultColumns == null || resultColumns.isEmpty( ) )
			return;

		CachedMetaDataHandle originalMetaData = setHandle
				.getCachedMetaDataHandle( );

		// uses the tmporary metadata value.

		CachedMetaData metaData = StructureFactory.createCachedMetaData( );
		metaData.setProperty( CachedMetaData.RESULT_SET_MEMBER, resultColumns );

		PropertyValueValidationUtil.validateProperty( setHandle,
				DataSetHandle.CACHED_METADATA_PROP, metaData );

		if ( originalMetaData == null )
		{
			setHandle.getElement( ).setProperty(
					DataSetHandle.CACHED_METADATA_PROP, metaData );
		}
		else
		{
			// clear metaData values.

			metaData.setProperty( CachedMetaData.RESULT_SET_MEMBER, null );
			metaData = null;

			metaData = (CachedMetaData) originalMetaData.getStructure( );
			metaData.setProperty( CachedMetaData.RESULT_SET_MEMBER,
					resultColumns );
		}
	}
}
