/* Copyright Dassault Systemes, 1999, 2011 */


package examples.development.components.plate;

import com.engineous.sdk.exception.IException;
import com.engineous.sdk.model.exceptions.DtModelException;
import com.engineous.sdk.resmgr.ResMgr;
import com.engineous.sdk.runtime.PostProcessor;
import com.engineous.sdk.runtime.RunResultData;
import com.engineous.sdk.model.*;
import com.engineous.sdk.vars.*;
import com.engineous.sdk.visual.VisualContainer;


import java.awt.Component;
import java.util.*;

//=========================================================================
/**
 * Title:        PlatePostProcessor <br>
 * Description:  Post processor class for plate component<br>
 * Copyright:    Copyright (c) 2004<br>
 * Company:      Engineous Software Inc.<br>
 * @author      Engineous Software Inc.<br>
 */
//=========================================================================

public class PlatePostProcessor
		extends AbstractSystemExtension
		implements PostProcessor {

	private transient static final Class CLASS = PlatePostProcessor.class;

	//=========================================================================
	/**
	 * Method  getSummary
	 * This method returns the summary for plate execution
	 * @param rrData
	 * @return String
	 * @throws IException
	 */
	//=========================================================================
	public String getSummary(RunResultData rrData)
			throws IException {

		DtComponent component = rrData.getComponent();
		StringBuffer buffer = new StringBuffer();

		buffer.append(ResMgr.getMessage(CLASS, 84662, "\nExecution summary for component ")).append(component.getName());

		String shape = ((DtScalarVariable)component.getProperty("Shape")).getValueObj().getAsString();
		buffer.append("\n\nShape: " + shape);
		String material = ((DtScalarVariable)component.getProperty("Material")).getValueObj().getAsString();
		buffer.append("\nMaterial: " + material);

		double thickness = rrData.getValue(0, component.getParameter("Thickness")).getAsReal();
		buffer.append("\nThickness: " + Double.toString(thickness));


		AggregateVariable dimensionsVar = (AggregateVariable)component.getParameter("Dimensions");
		buffer.append("\nDimensions:");
		Iterator dimIter = dimensionsVar.getMemberList().iterator();
		while (dimIter.hasNext()) {
			ScalarVariable nextDim = (ScalarVariable)dimIter.next();
			String dimName = nextDim.getName();
			String dimVal = nextDim.getValueObj().getAsString();
			buffer.append("\n\t" + dimName + ": " + dimVal);
		}

		buffer.append("\n\n************* Results **************");
		String area = rrData.getValue(0, component.getParameter("Area")).getAsString();
		String volume = rrData.getValue(0, component.getParameter("Volume")).getAsString();
		String weight = rrData.getValue(0, component.getParameter("Weight")).getAsString();
		buffer.append("\nArea: " + area);
		buffer.append("\nVolume: " + volume);
		buffer.append("\nWeight: ");

		return buffer.toString();
	}

	//=========================================================================
	/**
	 * Returns a list of the variables (com.engineous.sdk.vars.VariableReference
	 *  objects) whose values are needed following execution to create a summary
	 *  for this component that is created and returned by the getSummary()
	 *  method.
	 *
	 * @param component
	 * @return List of com.engineous.sdk.vars.VariableReference objects
	 * @throws DtModelException, VariableException
	 * @throws VariableException
	 */
	//=========================================================================
	public List getVariablesForDataView(DtComponent component)
			throws DtModelException, VariableException {

		// Need all parameters for this component
		ArrayList varRefList = new ArrayList();
		Iterator parmIter = component.getParameterList().iterator();
		while (parmIter.hasNext()) {
			Variable nextVar = (Variable)parmIter.next();
			varRefList.add(new VariableReference(nextVar));
		}
		return varRefList;
	}

	/**
	 * Returns the widgets used to display component-specific results.
	 * This default implementation returns {@code null}.
	 * @param component
	 * @param jobId
	 * @param container
	 * @return {@link List} of {@link Component}
	 * @throws IException
	 */
	@Override
	public List<Component> getResultsViews(DtComponent component, String jobId, VisualContainer container)
			throws IException {
		return null;
	}

}

