
import com.ptc.cipjava.*;
import com.ptc.cipjava.jxthrowable;

import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.pfcSession.Session;
import com.ptc.pfc.pfcGlobal.*;
import com.ptc.pfc.pfcGlobal.pfcGlobal;
import com.ptc.pfc.pfcCommand.*;
import com.ptc.pfc.pfcModel.*;
import com.ptc.pfc.pfcModelItem.*;
import com.ptc.pfc.pfcSolid.*;
import com.ptc.pfc.pfcSolid.Solid;
import com.ptc.pfc.pfcModelItem.*;
import com.ptc.pfc.pfcAssembly.*;
import com.ptc.pfc.pfcAssembly.Assembly;
import com.ptc.pfc.pfcComponentFeat.*;
import com.ptc.pfc.pfcFeature.*;

import java.io.*;
import java.util.*;
import java.text.*;

public class Make_BOM_list {

	static Session curSession;
	static final String MSGFILE = "bom.txt";

	public static void start() {

		try {
			curSession = pfcGlobal.GetProESession();
			UICommand inputCommand = curSession.UICreateCommand("Make BOM", new BOM_list_Listener(curSession));
			curSession.UIAddButton(inputCommand,
									"Applications",
									"Applications.psh_util_pproc",
									"Make_BOM",
									"Create_BOM",
									MSGFILE);
		}
		catch(jxthrowable x) {
			System.out.println("Exception:"+x);
		}
	}

	public static void stop() {}
}

class BOM_list_Listener extends DefaultUICommandActionListener {

	static Session session;
	static Model model;
	static BOMExportInstructions bom_instrs;
	static Assembly assy;

	public BOM_list_Listener(Session sess) {
		session = sess;
	}

	public void OnCommand () {

		try {
			String DateStg = getDatestgsh();

			session=pfcGlobal.GetProESession();
			model=session.GetCurrentModel();
			String full_name = model.GetFullName();
			String bom_name = full_name + "." + DateStg + ".bom";
			bom_instrs = pfcModel.BOMExportInstructions_Create();
			model.Export(bom_name, bom_instrs);

			String get_RL = model.GetReleaseLevel();
			String get_REV = model.GetRevision();
			String get_VER = model.GetVersion();
			String inf_name = full_name + "." + DateStg + ".inf";

			try {
				PrintStream ff = new PrintStream(new FileOutputStream(inf_name));
				ff.println( "Model Name: " + full_name );
				ff.println( "Release Level: " + get_RL );
				ff.println( "Revision: " + get_REV );
				ff.println( "Version: " + get_VER );
				ff.flush();
				ff.close();
			}
			catch(java.io.IOException IOEx) {
				System.out.println("Cannot create " + inf_name + " file.");
			}

			//Assembly assy;
			Features comps;
			ComponentFeat comp;
			ModelDescriptor desc;
			String inf2_name = full_name + "." + DateStg + "_2.inf";

			try {
				PrintStream cf = new PrintStream(new FileOutputStream(inf2_name));
				//comps = assy.ListFeaturesByType (Boolean.FALSE, FeatureType.FEATTYPE_COMPONENT);
				comps = assy.ListFeaturesByType(new Boolean(true), FeatureType.FEATTYPE_COMPONENT);

				for (int ii = 0; ii < comps.getarraysize(); ii++)
				{
					//comp = (ComponentFeat)comps.get(ii);
					//desc = comp.GetModelDescr();
					//String feat_stg = comp.GetModelDescr().GetFullName();
					//String get_cRL	= comp.GetReleaseLevel();
					//String get_cREV	= comp.GetRevision();
					//String get_cVER	= comp.GetVersion();
					//cf.println( "Component Release Level: " + get_cRL );
					//cf.println( "Component Revision: " + get_cREV );
					//cf.println( "Component Version: " + get_cVER );
					cf.println( "Component No: " + ii );
					//cf.println( "Component Description: " + desc );
					//cf.println( "Component Description2: " + feat_stg );
				}

				cf.flush();
				cf.close();
			}
			catch(java.io.IOException IOEx) {
				System.out.println("Cannot create " + inf2_name + " file.");
			}

			public static Models models = session.ListModels();

			int modelCount = models.getarraysize();

			//Vector [] modelData;
			//modelData = new Vector [modelCount];
			String inf3_name = full_name + "." + DateStg + "_3.inf";

			try {
				PrintStream vf = new PrintStream(new FileOutputStream(inf3_name));

		    	for (int i = 0; i< modelCount; i++) {
					getModelInfo(i);
      			}

				vf.flush();
				vf.close();
			}
			catch(java.io.IOException IOEx) {
				System.out.println("Cannot create " + inf3_name + " file.");
			}
		}
		catch (jxthrowable x) {
			System.out.println ("Caught exception: "+x);
			x.printStackTrace ();
		}
	}

	public void getModelInfo(int index) throws jxthrowable {

		Model model_i;

		/* Model data */
		String name, typeString, path, creationString, accessString,
		String fname, ftypeString, fstatusString;
		ModelType type;
		ModelDescriptor desc;
		Integer versionStamp, fid;
		DateFormat defaultDateFormat;
		Parameter creation, access;
		ParamValue creationValue, accessValue;
		FeatureType ftype;
		FeatureStatus fstatus;

		model_i = models.get(index);

		name = model_i.GetFullName();
		vf.println( "Model Name: " + name );

		type = model_i.GetType();
		typeString = Labeler2.pfcModelType[type.getValue()];
		vf.println( "Model Type: " + typeString );

		versionStamp = new Integer(model_i.GetVersionStamp());
		vf.println( "Version Stamp: " + versionStamp );

		defaultDateFormat = DateFormat.getDateTimeInstance();
		creation = model_i.GetParam(CREATION_PARAM);

		if (creation == null) {
			creationValue = pfcModelItem.CreateStringParamValue(defaultDateFormat.format(new Date()));
			creation = model_i.CreateParam(CREATION_PARAM, creationValue);
		}

		creationValue = creation.GetValue();
		creationString = creationValue.GetStringValue();
		vf.println( "Creation Date: " + creationString );

		access = model_i.GetParam(ACCESS_PARAM);

		if (access == null) {
			accessValue = pfcModelItem.CreateStringParamValue("NEW");
			access = model_i.CreateParam(ACCESS_PARAM, accessValue);
		}

		accessValue = access.GetValue();
		accessString = accessValue.GetStringValue();
		vf.println( "Access Date: " + accessString );

		accessValue = pfcModelItem.CreateStringParamValue(defaultDateFormat.format(new Date()));
		access.SetValue(accessValue);

		return;
	}

	public String getDatestgsh() {
 		SimpleDateFormat df = new SimpleDateFormat ("MMddyy", Locale.getDefault());
		Date tm = new Date();
		String dateinst = df.format(tm);

		return dateinst;
	}

	public static String getModelName (Model model)
	{
		try {
			return (model.GetFullName ());
		}
		catch (jxthrowable x) {
			System.out.println ("Caught exception: "+x);
			x.printStackTrace ();
		}
		return ("");
	}

	public static Model getModelFromComp (Session session, String parentName, ComponentFeat comp) {
		Model compModel = null;
		String compId = "";
		try {
			compId = Integer.toString (comp.GetId ());
			if (comp.GetStatus () == FeatureStatus.FEAT_ACTIVE) {
				ModelDescriptor		compDescr = comp.GetModelDescr ();
				compModel = session.GetModelFromDescr (compDescr);
				if (compModel == null)
					System.out.println ("Warning - model " + compDescr.GetFullName () +
							  " in component with Id " + compId +
							  " in model " + parentName +
							  " is not in session ");
			}
		}
		catch (jxthrowable x) {
			System.out.println ("Caught exception: "+x);
			x.printStackTrace ();
		}
		return (compModel);
	}
}
