/* Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Engineous Software Inc.,
 * 2000 CentreGreen Way, Suite 100 Cary, NC 27513, U.S.A
 * All rights reserved.
 *
 * IN NO EVENT SHALL ENGINEOUS SOFTWARE INC. BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
 * OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF ENGINEOUS
 * SOFTWARE INC. HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ENGINEOUS SOFTWARE INCORPORATED SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND ENGINEOUS SOFTWARE INC. HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 */


package examples.development.components.plate;

import com.engineous.common.i18n.IString;
import com.engineous.sdk.runtime.AbstractComponent;
import com.engineous.sdk.runtime.RtException;
import com.engineous.sdk.runtime.RuntimeEnv;
import com.engineous.sdk.vars.AggregateVariable;
import com.engineous.sdk.vars.ScalarVariable;

/**
 * @author Engineous Software
 * Generic empty default runtime wrapper for Component
 */

public class PlateExecutor
		extends AbstractComponent {

	transient private static final Class CLASS = PlateExecutor.class;

	//=========================================================================
	/**
	 * execute
	 * <p><i>Implements interface <b>Component</b></i></p>
	 * @param runEnv
	 * @throws RtException
	 */
	public void execute(RuntimeEnv runEnv)
			throws RtException {

		runEnv.getJobLog().logDebug(new IString(CLASS, 61920, "Plate analysis started."));

		try {

			// get properties
			String theShape = runEnv.getScalarProperty("Shape").getValueObj().getAsString();
			String theMaterial = runEnv.getScalarProperty("Material").getValueObj().getAsString();

			runEnv.getJobLog().logInfo(new IString(CLASS, 85133, "Executing Plate Component with shape: {0}", theShape));

			// get parameter info from Context and send info to your application

			AggregateVariable dimensions = ((AggregateVariable)runEnv.getContext().get("dimensions"));
			ScalarVariable thicknessParm = runEnv.getContext().getScalar("Thickness");
			double thickness = thicknessParm.getValueObj().getAsReal();

			double area = 0.0;
			double volume = 0.0;
			double weight = 0.0;
			if (theShape.equals("circle")) {
				double radius = ((ScalarVariable)dimensions.getMember("radius")).getValueObj().getAsReal();
				area = Math.PI * Math.pow(radius, 2.0);
			}
			else if (theShape.equals("rectangle")) {
				double width = ((ScalarVariable)dimensions.getMember("width")).getValueObj().getAsReal();
				double height = ((ScalarVariable)dimensions.getMember("height")).getValueObj().getAsReal();
				area = width * height;
			}
			else if (theShape.equals("triangle")) {
				double base = ((ScalarVariable)dimensions.getMember("base")).getValueObj().getAsReal();
				double height = ((ScalarVariable)dimensions.getMember("height")).getValueObj().getAsReal();
				area = 0.5 * base * height;
			}

			volume = area * thickness;

			//Weight is the volume (m^3) times the density (kg/m^3) of the material
			double density = 0;
			if (theMaterial.equals("aluminum")) {
				density = 2700;    // Aluminum 3003
			}
			else if (theMaterial.equals("steel")) {
				density = 8030;    // Steel AISI 304
			}
			else if (theMaterial.equals("wood")) {
				density = 513;     // White Pine
			}
			weight = volume * density;

			runEnv.getContext().getScalar("Area").getValueObj().setValue(area);
			runEnv.getContext().getScalar("Volume").getValueObj().setValue(volume);
			runEnv.getContext().getScalar("Weight").getValueObj().setValue(weight);
		}
		catch (Exception e) {
			throw new RtException(e);
		}
	}

}
