
import com.ptc.cipjava.*;

import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.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.pfcModelItem.*;
import com.ptc.pfc.pfcAssembly.*;
import com.ptc.pfc.pfcComponentFeat.*;
import com.ptc.pfc.pfcFeature.*;
import com.ptc.pfc.pfcBase.*;
import com.ptc.pfc.pfcDimension.*;
import com.ptc.pfc.pfcGeometry.*;
import com.ptc.pfc.pfcLayer.*;
import com.ptc.pfc.pfcExceptions.*;

import java.io.*;
import java.util.*;
import java.text.*;

public class Test_BOM {

	static Session curSession;
	static final String MSGFILE = "bom.txt";

	public static void start() {

		try {
			curSession = pfcGlobal.GetProESession();
			UICommand inputCommand = curSession.UICreateCommand("Test BOM", new Test_BOM_Listener(curSession));
			curSession.UIAddButton(inputCommand,
									"Applications",
									"Applications.psh_util_pproc",
									"Test_BOM",
									"Test_BOM_2",
									MSGFILE);
		}
		catch(jxthrowable x) {
			System.out.println("Exception:"+x);
		}
	}

	public static void stop() {}
}

class Test_BOM_Listener extends DefaultUICommandActionListener {

	static Session session;
	static Model curmodel;
	static Assembly assy;
	static Models models;
	static Vector [] modelData;
	static String CREATION_PARAM = "JLINK_TRACKER_CREATION_PARAM";
	static String ACCESS_PARAM = "JLINK_TRACKER_ACCESS_PARAM";

	public Test_BOM_Listener(Session sess) {
		session = sess;
	}

	public void OnCommand () {

		try {
	 		SimpleDateFormat df = new SimpleDateFormat ("MMddyy", Locale.getDefault());
			Date tm = new Date();
			String DateStg = df.format(tm);

			session=pfcGlobal.GetProESession();
			curmodel=session.GetCurrentModel();
			String full_name = curmodel.GetFullName();

			models = session.ListModels();

			int modelCount = models.getarraysize();

			modelData = new Vector [modelCount];

			for (int i = 0; i< modelCount; i++) {
				getModelInfo(i);
			}

			String inf_name = full_name + "_list." + DateStg + ".inf";

			try {
				PrintStream vf = new PrintStream(new FileOutputStream(inf_name));

				for (int i =0; i<modelCount; i++) {
					vf.println( modelData[i] );
				}
				vf.flush();
				vf.close();
			}
			catch(java.io.IOException IOEx) {
				System.out.println("Cannot create " + inf_name + " file.");
			}
		}
		catch (jxthrowable x) {
			System.out.println ("Caught exception: "+x);
			x.printStackTrace ();
		}
	}

	public void getModelInfo(int index) throws jxthrowable {
		Vector data_i;
		Model model_i;

		String name, typeString, path, creationString, accessString;
		String fname, ftypeString, fstatusString, versionStampstg;
		ModelType type;
		ModelDescriptor desc;
		Integer versionStamp;
		DateFormat defaultDateFormat;
		Parameter creation, access;
		ParamValue creationValue, accessValue;

		model_i = models.get(index);
		data_i = new Vector (5);

		name = model_i.GetFullName();
		name = "Model Name: " + name;
		data_i.addElement(name);

		type = model_i.GetType();
		typeString = Labeler2.pfcModelType[type.getValue()];
		typeString = "Model Type: " + typeString;
		data_i.addElement(typeString);

		versionStamp = new Integer(model_i.GetVersionStamp());
		versionStampstg = "Version Stamp: " + versionStamp.toString();

		data_i.addElement(versionStampstg);

		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();
		creationString = "Creation Date: " + creationString;
		data_i.addElement(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();
		accessString = "Access Date: " + accessString;
		data_i.addElement(accessString);

		accessValue = pfcModelItem.CreateStringParamValue(defaultDateFormat.format(new Date()));
		access.SetValue(accessValue);

		modelData[index] = data_i;

		return;
	}
}
